|
| 1 | +""" |
| 2 | +Copied and modified for eval-protocol from https://ai.pydantic.dev/multi-agent-applications/#agent-delegation |
| 3 | +
|
| 4 | +To test your Pydantic AI multi-agent application, you can pass a function that |
| 5 | +sets up the agents and their tools. The function should accept parameters that |
| 6 | +map a model to each agent. In completion_params, you can provide mappings of |
| 7 | +model to agent based on key. |
| 8 | +""" |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from eval_protocol.models import EvaluationRow, Message |
| 13 | +from eval_protocol.pytest import evaluation_test |
| 14 | +from pydantic_ai import Agent |
| 15 | + |
| 16 | +from eval_protocol.pytest.default_pydantic_ai_rollout_processor import PydanticAgentRolloutProcessor |
| 17 | +from pydantic_ai import RunContext |
| 18 | +from pydantic_ai.models import Model |
| 19 | +from pydantic_ai.usage import UsageLimits |
| 20 | + |
| 21 | + |
| 22 | +def setup_agent(joke_generation_model: Model, joke_selection_model: Model) -> Agent: |
| 23 | + """ |
| 24 | + This is an extra step that most applications will probably need to do to |
| 25 | + parameterize the model that their agents use. But we believe that this is a |
| 26 | + necessary step for multi-agent applications if developers want to solve the |
| 27 | + model selection problem. |
| 28 | + """ |
| 29 | + joke_selection_agent = Agent( |
| 30 | + model=joke_selection_model, |
| 31 | + system_prompt=( |
| 32 | + "Use the `joke_factory` to generate some jokes, then choose the best. You must return just a single joke." |
| 33 | + ), |
| 34 | + ) |
| 35 | + joke_generation_agent = Agent(joke_generation_model, output_type=list[str]) |
| 36 | + |
| 37 | + @joke_selection_agent.tool |
| 38 | + async def joke_factory(ctx: RunContext[None], count: int) -> list[str]: |
| 39 | + r = await joke_generation_agent.run( |
| 40 | + f"Please generate {count} jokes.", |
| 41 | + usage=ctx.usage, |
| 42 | + ) |
| 43 | + return r.output |
| 44 | + |
| 45 | + return joke_selection_agent |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.asyncio |
| 49 | +@evaluation_test( |
| 50 | + input_messages=[Message(role="user", content="Hello, how are you?")], |
| 51 | + completion_params=[ |
| 52 | + { |
| 53 | + "model": { |
| 54 | + "joke_generation_model": {"model": "accounts/fireworks/models/gpt-oss-120b", "provider": "fireworks"}, |
| 55 | + "joke_selection_model": {"model": "accounts/fireworks/models/gpt-oss-20b", "provider": "fireworks"}, |
| 56 | + } |
| 57 | + }, |
| 58 | + ], |
| 59 | + rollout_processor=PydanticAgentRolloutProcessor(), |
| 60 | + rollout_processor_kwargs={ |
| 61 | + "agent": setup_agent, |
| 62 | + # PydanticAgentRolloutProcessor will pass usage_limits into the "run" call |
| 63 | + "usage_limits": UsageLimits(request_limit=5, total_tokens_limit=30), |
| 64 | + }, |
| 65 | + mode="pointwise", |
| 66 | +) |
| 67 | +async def test_pydantic_multi_agent(row: EvaluationRow) -> EvaluationRow: |
| 68 | + """ |
| 69 | + Super simple hello world test for Pydantic AI. |
| 70 | + """ |
| 71 | + return row |
0 commit comments