Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions docs/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ import random

from pydantic_ai import Agent, RunContext, Tool

system_prompt = """\
You're a dice game, you should roll the die and see if the number
you get back matches the user's guess. If so, tell them they're a winner.
Use the player's name in the response.
"""


def roll_die() -> str:
"""Roll a six-sided die and return the result."""
Expand All @@ -209,6 +215,7 @@ agent_a = Agent(
'google-gla:gemini-1.5-flash',
deps_type=str,
tools=[roll_die, get_player_name], # (1)!
system_prompt=system_prompt,
)
agent_b = Agent(
'google-gla:gemini-1.5-flash',
Expand All @@ -217,9 +224,15 @@ agent_b = Agent(
Tool(roll_die, takes_ctx=False),
Tool(get_player_name, takes_ctx=True),
],
system_prompt=system_prompt,
)
dice_result = agent_b.run_sync('My guess is 4', deps='Anne')
print(dice_result.data)

dice_result = {}
dice_result['a'] = agent_a.run_sync('My guess is 6', deps='Yashar')
dice_result['b'] = agent_b.run_sync('My guess is 4', deps='Anne')
print(dice_result['a'].data)
#> Tough luck, Yashar, you rolled a 4. Better luck next time.
print(dice_result['b'].data)
#> Congratulations Anne, you guessed correctly! You're a winner!
```

Expand Down
7 changes: 6 additions & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def test_docs_examples( # noqa: C901
eval_example: EvalExample,
mocker: MockerFixture,
client_with_handler: ClientWithHandler,
allow_model_requests: None,
env: TestEnv,
tmp_path: Path,
):
Expand Down Expand Up @@ -227,6 +228,7 @@ async def list_tools() -> list[None]:
'I bet five is the winner': ToolCallPart(
tool_name='roulette_wheel', args={'square': 5}, tool_call_id='pyd_ai_tool_call_id'
),
'My guess is 6': ToolCallPart(tool_name='roll_die', args={}, tool_call_id='pyd_ai_tool_call_id'),
'My guess is 4': ToolCallPart(tool_name='roll_die', args={}, tool_call_id='pyd_ai_tool_call_id'),
'Send a message to John Doe asking for coffee next week': ToolCallPart(
tool_name='get_user_by_name', args={'name': 'John'}
Expand Down Expand Up @@ -374,7 +376,10 @@ async def model_logic(messages: list[ModelMessage], info: AgentInfo) -> ModelRes
parts=[ToolCallPart(tool_name='get_player_name', args={}, tool_call_id='pyd_ai_tool_call_id')]
)
elif isinstance(m, ToolReturnPart) and m.tool_name == 'get_player_name':
return ModelResponse(parts=[TextPart("Congratulations Anne, you guessed correctly! You're a winner!")])
if 'Anne' in m.content:
return ModelResponse(parts=[TextPart("Congratulations Anne, you guessed correctly! You're a winner!")])
elif 'Yashar' in m.content:
return ModelResponse(parts=[TextPart('Tough luck, Yashar, you rolled a 4. Better luck next time.')])
if (
isinstance(m, RetryPromptPart)
and isinstance(m.content, str)
Expand Down