-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_benchmark.py
56 lines (47 loc) · 1.71 KB
/
test_benchmark.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
from deepeval import assert_test
from deepeval.test_case import LLMTestCase, LLMTestCaseParams
from deepeval.metrics import AnswerRelevancyMetric, GEval
from openai import OpenAI
def test_answer_relevancy():
client = OpenAI()
# Define the input question once
input_question = "How do contract terms influence negotiations in software and SaaS?"
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant. Keep your answers concise and to the point."},
{"role": "user", "content": input_question}
],
temperature=0.9,
max_tokens=600
)
# Store the response content
response_content = response.choices[0].message.content
# Create the test case with the actual response
test_case = LLMTestCase(
input=input_question,
expected_output="paris is good",
actual_output=response_content
)
# Create evaluation metrics with lower thresholds to make failures more visible
relevancy_metric = AnswerRelevancyMetric(
threshold=0.5,
include_reason=True
)
correctness_metric = GEval(
name="Correctness",
evaluation_params=[
LLMTestCaseParams.EXPECTED_OUTPUT,
LLMTestCaseParams.ACTUAL_OUTPUT,
LLMTestCaseParams.INPUT
],
criteria="The actual output should be relevant to the input and match the expected output as closely as possible.",
threshold=0.5
)
# Add debug printing
print(f"Actual output: {response_content}")
# Run the evaluation and catch any exceptions
try:
assert_test(test_case, [relevancy_metric, correctness_metric])
except AssertionError as e:
print(f"Test failed as expected: {e}")