-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pipe.py
165 lines (107 loc) · 4.5 KB
/
test_pipe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from app.pipe import Process, RAGStage, create_links
class Security(Process):
stage = RAGStage.SECURITY
def __init__(self, config: dict = {}) -> None: super().__init__(config)
def _process(self, text, data, errors, sentinel):
new_text = f"{text} > {self.stage.name!s}"
return new_text, data, errors, self.next_stage
class RCache(Process):
stage = RAGStage.RCACHE
def __init__(self, config: dict = {}) -> None: super().__init__(config)
def _process(self, text, data, errors, sentinel):
new_text = f"{text} > {self.stage.name!s}"
return new_text, data, errors, self.next_stage
class Rewrite(Process):
stage = RAGStage.REWRITE
def __init__(self, config: dict = {}) -> None: super().__init__(config)
def _process(self, text, data, errors, sentinel):
new_text = f"{text} > {self.stage.name!s}"
return new_text, data, errors, self.next_stage
class Retrieve(Process):
stage = RAGStage.RETRIEVE
def __init__(self, config: dict = {}) -> None: super().__init__(config)
def _process(self, text, data, errors, sentinel):
new_text = f"{text} > {self.stage.name!s}"
return new_text, data, errors, self.next_stage
class Rerank(Process):
stage = RAGStage.RERANK
def __init__(self, config: dict = {}) -> None: super().__init__(config)
def _process(self, text, data, errors, sentinel):
new_text = f"{text} > {self.stage.name!s}"
return new_text, data, errors, self.next_stage
class Consolidate(Process):
stage = RAGStage.CONSOLIDATE
def __init__(self, config: dict = {}) -> None: super().__init__(config)
def _process(self, text, data, errors, sentinel):
new_text = f"{text} > {self.stage.name!s}"
return new_text, data, errors, self.next_stage
class Generate(Process):
stage = RAGStage.GENERATE
def __init__(self, config: dict = {}) -> None: super().__init__(config)
def _process(self, text, data, errors, sentinel):
new_text = f"{text} > {self.stage.name!s}"
return new_text, data, errors, self.next_stage
class WCache(Process):
stage = RAGStage.WCACHE
def __init__(self, config: dict = {}) -> None: super().__init__(config)
def _process(self, text, data, errors, sentinel):
new_text = f"{text} > {self.stage.name!s}"
return new_text, data, errors, self.next_stage
# Test behaviours
def test_pipe_0():
security = Security()
rcache = RCache()
rewrite = Rewrite()
retrieve = Retrieve()
rerank = Rerank()
consolidate = Consolidate()
generate = Generate()
wcache = WCache()
pipeline = [security, rcache, rewrite, retrieve, rerank, consolidate, generate, wcache]
text = 'begin'
data, err, sentinel = {}, [], RAGStage.SECURITY
for process in pipeline:
text, data, err, sentinel = process(text, data, err, sentinel)
assert text == 'begin > SECURITY > RCACHE > REWRITE > RETRIEVE > RERANK > CONSOLIDATE > GENERATE > WCACHE'
assert err == []
assert data == {}
assert sentinel == RAGStage.END
# Test behaviours
def test_pipe_create_links():
"""Helper to set the next stage when some steps in RAG are not needed"""
security = Security()
generate = Generate()
wcache = WCache()
pipeline = [security, generate, wcache]
create_links(pipeline)
text = 'begin'
data, err, sentinel = {}, [], RAGStage.SECURITY
for process in pipeline:
text, data, err, sentinel = process(text, data, err, sentinel)
assert text == 'begin > SECURITY > GENERATE > WCACHE'
assert err == []
assert data == {}
assert sentinel == RAGStage.END
def test_pipe_cache_hit():
"""Dynamically set the next stage when cache hit"""
security = Security()
rcache = RCache()
rewrite = Rewrite()
retrieve = Retrieve()
rerank = Rerank()
consolidate = Consolidate()
generate = Generate()
wcache = WCache()
def _p(text, data, errors, sentinel):
if 'cache hit' in text:
return text, data, errors, RAGStage.GENERATE
return text, data, errors, sentinel.increment()
rcache._process = _p
text = 'begin with cache hit'
data, err, sentinel = {}, [], RAGStage.SECURITY
for process in [security, rcache, rewrite, retrieve, rerank, consolidate, generate, wcache]:
text, data, err, sentinel = process(text, data, err, sentinel)
assert text == 'begin with cache hit > SECURITY > GENERATE > WCACHE'
assert err == []
assert data == {}
assert sentinel == RAGStage.END