Skip to content

Tool Use

This tutorial will show you how to use Inference Grid with PydanticAI and build a simple agent that is able to use tools.

bash
pip install pydantic-ai

Next, we'll implement the roulette example from here but using Inference Grid as the model provider.

python
from pydantic_ai import Agent, RunContext
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider

model = OpenAIModel(
    'openai/o3-mini',
    provider=OpenAIProvider(
        base_url="https://relay.inferencegrid.ai/",
        api_key='{YOUR_API_KEY}'
    ),
)

roulette_agent = Agent(
    model,
    deps_type=int,
    output_type=bool,
    system_prompt=(
        'Use the `roulette_wheel` function to see if the '
        'customer has won based on the number they provide.'
    ),
)

@roulette_agent.tool
async def roulette_wheel(ctx: RunContext[int], square: int) -> str:
    """check if the square is a winner"""
    return 'winner' if square == ctx.deps else 'loser'


# Run the agent
success_number = 18
result = roulette_agent.run_sync('Put my money on square eighteen', deps=success_number)
print(result.output)
#> True

result = roulette_agent.run_sync('I bet five is the winner', deps=success_number)
print(result.output)
#> False