Skip to content

Commit c4b9b3c

Browse files
author
Dylan Huang
committed
Refactor Agent class to accept EvaluationRow directly in the constructor, simplifying initialization. Update _call_model method to return a Message object with structured response data. Modify default_agent_rollout_processor to utilize the new constructor and streamline dataset population. Enhance evaluation_test to initialize eval_metadata for each row before running rollouts, ensuring consistent metadata handling.
1 parent 0409ef0 commit c4b9b3c

2 files changed

Lines changed: 40 additions & 35 deletions

File tree

eval_protocol/pytest/default_agent_rollout_processor.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class Agent:
2020
A really simple agent that calls the model until no more tool calls are needed.
2121
"""
2222

23-
def __init__(self, model: str, initial_messages: list[Message], config_path: str):
23+
def __init__(self, model: str, row: EvaluationRow, config_path: str):
2424
self.model = model
25-
self.evaluation_row: EvaluationRow = EvaluationRow(messages=initial_messages)
25+
self.evaluation_row: EvaluationRow = row
2626
self._policy = LiteLLMPolicy(model_id=model)
2727
self.mcp_client = MCPMultiClient(config_path=config_path) if config_path else None
2828
self.tools: Union[List[ChatCompletionToolParam], NotGiven] = NOT_GIVEN
@@ -82,13 +82,15 @@ async def call_agent(self) -> str:
8282
return await self.call_agent()
8383
return message["content"]
8484

85-
async def _call_model(
86-
self, messages: list[Message], tools: Optional[list[ChatCompletionToolParam]]
87-
) -> ChatCompletionMessage:
85+
async def _call_model(self, messages: list[Message], tools: Optional[list[ChatCompletionToolParam]]) -> Message:
8886
messages = [message.model_dump() if hasattr(message, "model_dump") else message for message in messages]
8987
tools = [{"function": tool["function"].model_dump(), "type": "function"} for tool in tools] if tools else []
9088
response = await self._policy._make_llm_call(messages=messages, tools=tools)
91-
return response["choices"][0]["message"]
89+
return Message(
90+
role=response["choices"][0]["message"]["role"],
91+
content=response["choices"][0]["message"]["content"],
92+
tool_calls=response["choices"][0]["message"]["tool_calls"],
93+
)
9294

9395
async def _execute_tool_call(
9496
self, tool_call_id: str, tool_name: str, tool_args_dict: dict
@@ -114,12 +116,10 @@ async def default_agent_rollout_processor(
114116
) -> List[EvaluationRow]:
115117
dataset: Dataset = []
116118
for row in rows:
117-
agent = Agent(model=config.model, initial_messages=row.messages, config_path=config.mcp_config_path)
119+
agent = Agent(model=config.model, row=row, config_path=config.mcp_config_path)
118120
await agent.setup()
119121
await agent.call_agent()
120-
dataset.append(
121-
EvaluationRow(ground_truth=row.ground_truth, **agent.evaluation_row.model_dump(exclude_none=True))
122-
)
122+
dataset.append(agent.evaluation_row)
123123
if agent.mcp_client:
124124
await agent.mcp_client.cleanup()
125125
return dataset

eval_protocol/pytest/evaluation_test.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -206,34 +206,49 @@ def wrapper_body(**kwargs):
206206
else:
207207
raise ValueError("No input dataset or input messages provided")
208208

209-
input_dataset: List[EvaluationRow] = []
210209
input_params = kwargs.get("input_params") or {}
211-
config = RolloutProcessorConfig(
212-
model=model_name,
213-
input_params=input_params,
214-
mcp_config_path=mcp_config_path or "",
215-
max_concurrent_rollouts=max_concurrent_rollouts,
216-
server_script_path=server_script_path,
217-
steps=steps,
210+
211+
# Create eval metadata with test function info and current commit hash
212+
eval_metadata = EvalMetadata(
213+
name=test_func.__name__,
214+
description=test_func.__doc__,
215+
version=versioneer.get_version(),
216+
status="running",
217+
num_runs=num_runs,
218+
aggregation_method=aggregation_method,
219+
threshold_of_success=threshold_of_success,
220+
passed=None,
218221
)
219-
input_dataset = execute_function(rollout_processor, rows=data, config=config)
220222

221-
# Populate completion_params in input_metadata for all rows
223+
# Populate completion_params in input_metadata for all rows and initialize eval_metadata BEFORE rollouts
222224
completion_params = CompletionParams(
223225
model=model_name,
224226
temperature=input_params.get("temperature"),
225227
max_tokens=input_params.get("max_tokens"),
226228
max_tool_calls=input_params.get("max_tool_calls"),
227229
)
228230

229-
for row in input_dataset:
231+
for row in data:
230232
if row.input_metadata is None:
231233
row.input_metadata = InputMetadata()
232234
row.input_metadata.completion_params = completion_params
233235
# Add mode to session_data
234236
if row.input_metadata.session_data is None:
235237
row.input_metadata.session_data = {}
236238
row.input_metadata.session_data["mode"] = mode
239+
# Initialize eval_metadata for each row
240+
row.eval_metadata = eval_metadata
241+
242+
# Now run the rollout processor with metadata-initialized data
243+
config = RolloutProcessorConfig(
244+
model=model_name,
245+
input_params=input_params,
246+
mcp_config_path=mcp_config_path or "",
247+
max_concurrent_rollouts=max_concurrent_rollouts,
248+
server_script_path=server_script_path,
249+
steps=steps,
250+
)
251+
input_dataset = execute_function(rollout_processor, rows=data, config=config)
237252

238253
all_results: List[EvaluationRow] = []
239254
for _ in range(num_runs):
@@ -283,21 +298,11 @@ def wrapper_body(**kwargs):
283298
if threshold_of_success is not None:
284299
passed = agg_score >= threshold_of_success
285300

286-
# Create eval metadata with test function info and current commit hash
287-
eval_metadata = EvalMetadata(
288-
name=test_func.__name__,
289-
description=test_func.__doc__,
290-
version=versioneer.get_version(),
291-
status="finished",
292-
num_runs=num_runs,
293-
aggregation_method=aggregation_method,
294-
threshold_of_success=threshold_of_success,
295-
passed=passed,
296-
)
297-
298-
# Add metadata to all results before logging
301+
# Update eval metadata status and passed field for all results
299302
for r in all_results:
300-
r.eval_metadata = eval_metadata
303+
if r.eval_metadata is not None:
304+
r.eval_metadata.status = "finished"
305+
r.eval_metadata.passed = passed
301306
default_logger.log(r)
302307

303308
# Check threshold after logging

0 commit comments

Comments
 (0)