-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathgenerate_traces.py
More file actions
209 lines (178 loc) · 5.98 KB
/
Copy pathgenerate_traces.py
File metadata and controls
209 lines (178 loc) · 5.98 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import pytest
import os
from eval_protocol.models import EvaluationRow, Message
from eval_protocol.pytest import evaluation_test
from eval_protocol.pytest.default_pydantic_ai_rollout_processor import PydanticAgentRolloutProcessor
from eval_protocol.pytest.types import RolloutProcessorConfig
from tests.chinook.pydantic.agent import setup_agent
from tests.chinook.dataset import collect_dataset
try:
from langfuse import Langfuse, observe
from pydantic_ai.agent import Agent
from pydantic_ai.models.openai import OpenAIChatModel
LANGFUSE_AVAILABLE = True
langfuse_client = Langfuse()
Agent.instrument_all()
except ImportError:
LANGFUSE_AVAILABLE = False
langfuse_client = None
def observe(*args, **kwargs):
def decorator(func):
return func
return decorator if args and callable(args[0]) else decorator
LLM_JUDGE_PROMPT = (
"Your job is to compare the response to the expected answer.\n"
"The response will be a narrative report of the query results.\n"
"If the response contains the same or well summarized information as the expected answer, return 1.0.\n"
"If the response does not contain the same information or is missing information, return 0.0."
)
def agent_factory(config: RolloutProcessorConfig) -> Agent:
model_name = config.completion_params["model"]
provider = config.completion_params["provider"]
model = OpenAIChatModel(model_name, provider=provider)
return setup_agent(model)
@pytest.mark.skipif(
os.environ.get("CI") == "true",
reason="Only run this test locally (skipped in CI)",
)
@pytest.mark.asyncio
@observe()
@evaluation_test(
input_rows=[collect_dataset()[0:1]],
completion_params=[
{
"model": "accounts/fireworks/models/kimi-k2-instruct",
"provider": "fireworks",
},
],
rollout_processor=PydanticAgentRolloutProcessor(agent_factory),
mode="pointwise",
)
async def test_complex_query_0(row: EvaluationRow) -> EvaluationRow:
"""
Complex queries - PydanticAI automatically creates rich Langfuse traces.
"""
# Have to postprocess tools because row.tools isn't set until during rollout
if langfuse_client:
langfuse_client.update_current_trace(tags=["chinook_sql"], metadata={"tools": row.tools})
return row
@pytest.mark.skipif(
os.environ.get("CI") == "true",
reason="Only run this test locally (skipped in CI)",
)
@pytest.mark.asyncio
@observe()
@evaluation_test(
input_rows=[collect_dataset()[1:2]],
completion_params=[
{
"model": "accounts/fireworks/models/kimi-k2-instruct",
"provider": "fireworks",
},
],
rollout_processor=PydanticAgentRolloutProcessor(agent_factory),
mode="pointwise",
)
async def test_complex_query_1(row: EvaluationRow) -> EvaluationRow:
"""
Complex queries - PydanticAI automatically creates rich Langfuse traces.
"""
if langfuse_client:
langfuse_client.update_current_trace(tags=["chinook_sql"], metadata={"tools": row.tools})
return row
@pytest.mark.skipif(
os.environ.get("CI") == "true",
reason="Only run this test locally (skipped in CI)",
)
@pytest.mark.asyncio
@observe()
@evaluation_test(
input_rows=[collect_dataset()[2:3]],
completion_params=[
{
"model": "accounts/fireworks/models/kimi-k2-instruct",
"provider": "fireworks",
},
],
rollout_processor=PydanticAgentRolloutProcessor(agent_factory),
mode="pointwise",
)
async def test_complex_query_2(row: EvaluationRow) -> EvaluationRow:
"""
Complex queries - PydanticAI automatically creates rich Langfuse traces.
"""
if langfuse_client:
langfuse_client.update_current_trace(tags=["chinook_sql"], metadata={"tools": row.tools})
return row
@pytest.mark.skipif(
os.environ.get("CI") == "true",
reason="Only run this test locally (skipped in CI)",
)
@pytest.mark.asyncio
@observe()
@evaluation_test(
input_rows=[collect_dataset()[3:4]],
completion_params=[
{
"model": "accounts/fireworks/models/kimi-k2-instruct",
"provider": "fireworks",
},
],
rollout_processor=PydanticAgentRolloutProcessor(agent_factory),
mode="pointwise",
)
async def test_complex_query_3(row: EvaluationRow) -> EvaluationRow:
"""
Complex queries - PydanticAI automatically creates rich Langfuse traces.
"""
if langfuse_client:
langfuse_client.update_current_trace(tags=["chinook_sql"], metadata={"tools": row.tools})
return row
@pytest.mark.skipif(
os.environ.get("CI") == "true",
reason="Only run this test locally (skipped in CI)",
)
@pytest.mark.asyncio
@observe()
@evaluation_test(
input_rows=[collect_dataset()[4:5]],
completion_params=[
{
"model": "accounts/fireworks/models/kimi-k2-instruct",
"provider": "fireworks",
},
],
rollout_processor=PydanticAgentRolloutProcessor(agent_factory),
mode="pointwise",
)
async def test_complex_query_4(row: EvaluationRow) -> EvaluationRow:
"""
Complex queries - PydanticAI automatically creates rich Langfuse traces.
"""
if langfuse_client:
langfuse_client.update_current_trace(tags=["chinook_sql"], metadata={"tools": row.tools})
return row
@pytest.mark.skipif(
os.environ.get("CI") == "true",
reason="Only run this test locally (skipped in CI)",
)
@pytest.mark.asyncio
@observe()
@evaluation_test(
input_rows=[collect_dataset()[5:6]],
completion_params=[
{
"model": "accounts/fireworks/models/kimi-k2-instruct",
"provider": "fireworks",
},
],
rollout_processor=PydanticAgentRolloutProcessor(agent_factory),
mode="pointwise",
)
async def test_complex_query_5(row: EvaluationRow) -> EvaluationRow:
"""
Complex queries - PydanticAI automatically creates rich Langfuse traces.
"""
if langfuse_client:
langfuse_client.update_current_trace(tags=["chinook_sql"], metadata={"tools": row.tools})
return row