forked from microsoft/promptflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_test.py
186 lines (167 loc) · 6.14 KB
/
base_test.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import unittest
import os
import time
import traceback
class BaseTest(unittest.TestCase):
def setUp(self):
root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../")
self.flow_path = os.path.join(root, "chat-with-pdf")
self.data_path = os.path.join(
self.flow_path, "data/bert-paper-qna-3-line.jsonl"
)
self.eval_groundedness_flow_path = os.path.join(
root, "../evaluation/eval-groundedness"
)
self.eval_perceived_intelligence_flow_path = os.path.join(
root, "../evaluation/eval-perceived-intelligence"
)
self.all_runs_generated = []
self.config_3k_context = {
"EMBEDDING_MODEL_DEPLOYMENT_NAME": "text-embedding-ada-002",
"CHAT_MODEL_DEPLOYMENT_NAME": "gpt-35-turbo",
"PROMPT_TOKEN_LIMIT": 3000,
"MAX_COMPLETION_TOKENS": 256,
"VERBOSE": True,
"CHUNK_SIZE": 1024,
"CHUNK_OVERLAP": 32,
}
self.config_2k_context = {
"EMBEDDING_MODEL_DEPLOYMENT_NAME": "text-embedding-ada-002",
"CHAT_MODEL_DEPLOYMENT_NAME": "gpt-35-turbo",
"PROMPT_TOKEN_LIMIT": 2000,
"MAX_COMPLETION_TOKENS": 256,
"VERBOSE": True,
"CHUNK_SIZE": 1024,
"CHUNK_OVERLAP": 32,
}
# Switch current working directory to the folder of this file
self.cwd = os.getcwd()
os.chdir(os.path.dirname(os.path.abspath(__file__)))
def tearDown(self):
# Switch back to the original working directory
os.chdir(self.cwd)
for run in self.all_runs_generated:
try:
self.pf.runs.archive(run.name)
except Exception as e:
print(e)
traceback.print_exc()
def create_chat_run(
self,
data=None,
column_mapping=None,
connections=None,
runtime=None,
display_name="chat_run",
):
if column_mapping is None:
column_mapping = {
"chat_history": "${data.chat_history}",
"pdf_url": "${data.pdf_url}",
"question": "${data.question}",
"config": self.config_2k_context,
}
data = self.data_path if data is None else data
run = self.pf.run(
flow=self.flow_path,
data=data,
column_mapping=column_mapping,
connections=connections,
runtime=runtime,
display_name=display_name,
tags={"unittest": "true"},
stream=True,
)
self.all_runs_generated.append(run)
self.check_run_basics(run, display_name)
return run
def create_eval_run(
self,
eval_flow_path,
base_run,
column_mapping,
connections=None,
runtime=None,
display_name_postfix="",
):
display_name = eval_flow_path.split("/")[-1] + display_name_postfix
eval = self.pf.run(
flow=eval_flow_path,
run=base_run,
column_mapping=column_mapping,
connections=connections,
runtime=runtime,
display_name=display_name,
tags={"unittest": "true"},
stream=True,
)
self.all_runs_generated.append(eval)
self.check_run_basics(eval, display_name)
return eval
def check_run_basics(self, run, display_name=None):
self.assertTrue(run is not None)
if display_name is not None:
self.assertTrue(run.display_name.find(display_name) != -1)
self.assertEqual(run.tags["unittest"], "true")
def run_eval_with_config(
self, config: dict, runtime: str = None, display_name: str = None
):
run = self.create_chat_run(
column_mapping={
"question": "${data.question}",
"pdf_url": "${data.pdf_url}",
"chat_history": "${data.chat_history}",
"config": config,
},
runtime=runtime,
display_name=display_name,
)
self.pf.stream(run) # wait for completion
self.check_run_basics(run)
eval_groundedness = self.create_eval_run(
self.eval_groundedness_flow_path,
run,
{
"question": "${run.inputs.question}",
"answer": "${run.outputs.answer}",
"context": "${run.outputs.context}",
},
runtime=runtime,
display_name_postfix="_" + display_name,
)
self.pf.stream(eval_groundedness) # wait for completion
self.check_run_basics(eval_groundedness)
details = self.pf.get_details(eval_groundedness)
self.assertGreater(details.shape[0], 2)
metrics, elapsed = self.wait_for_metrics(eval_groundedness)
self.assertGreaterEqual(metrics["groundedness"], 0.0)
self.assertLessEqual(elapsed, 5) # metrics should be available within 5 seconds
eval_pi = self.create_eval_run(
self.eval_perceived_intelligence_flow_path,
run,
{
"question": "${run.inputs.question}",
"answer": "${run.outputs.answer}",
"context": "${run.outputs.context}",
},
runtime=runtime,
display_name_postfix="_" + display_name,
)
self.pf.stream(eval_pi) # wait for completion
self.check_run_basics(eval_pi)
details = self.pf.get_details(eval_pi)
self.assertGreater(details.shape[0], 2)
metrics, elapsed = self.wait_for_metrics(eval_pi)
self.assertGreaterEqual(metrics["perceived_intelligence_score"], 0.0)
self.assertLessEqual(elapsed, 5) # metrics should be available within 5 seconds
return run, eval_groundedness, eval_pi
def wait_for_metrics(self, run):
start = time.time()
metrics = self.pf.get_metrics(run)
cnt = 3
while len(metrics) == 0 and cnt > 0:
time.sleep(5)
metrics = self.pf.get_metrics(run)
cnt -= 1
end = time.time()
return metrics, end - start