-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
351b249
commit c417c8d
Showing
20 changed files
with
531 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Pydantic AI Examples | ||
|
||
Examples of how to use Pydantic AI and what it can do. | ||
|
||
## Usage | ||
|
||
To run the examples, run: | ||
|
||
```bash | ||
uv run -m examples.<example_module_name> | ||
``` | ||
|
||
## Examples | ||
|
||
### `pydantic_model.py` | ||
|
||
(Demonstrates: custom `result_type`) | ||
|
||
Simple example of using Pydantic AI to construct a Pydantic model from a text input. | ||
|
||
```bash | ||
uv run --extra examples -m examples.pydantic_model | ||
``` | ||
|
||
This examples uses `openai:gpt-4o` by default but it works well with other models, e.g. you can run it | ||
with Gemini using: | ||
|
||
```bash | ||
PYDANTIC_AI_MODEL=gemini-1.5-pro uv run --extra examples -m examples.pydantic_model | ||
``` | ||
|
||
(or `PYDANTIC_AI_MODEL=gemini-1.5-flash...`) | ||
|
||
### `sql_gen.py` | ||
|
||
(Demonstrates: custom `result_type`, dynamic system prompt, result validation, agent deps) | ||
|
||
Example demonstrating how to use Pydantic AI to generate SQL queries based on user input. | ||
|
||
```bash | ||
uv run --extra examples -m examples.sql_gen | ||
``` | ||
|
||
or to use a custom prompt: | ||
|
||
```bash | ||
uv run --extra examples -m examples.sql_gen "find me whatever" | ||
``` | ||
|
||
This model uses `gemini-1.5-flash` by default since Gemini is good at single shot queries. | ||
|
||
### `weather.py` | ||
|
||
(Demonstrates: retrievers, multiple retrievers, agent deps) | ||
|
||
Example of Pydantic AI with multiple tools which the LLM needs to call in turn to answer a question. | ||
|
||
In this case the idea is a "weather" agent — the user can ask for the weather in multiple cities, | ||
the agent will use the `get_lat_lng` tool to get the latitude and longitude of the locations, then use | ||
the `get_weather` tool to get the weather. | ||
|
||
To run this example properly, you'll need two extra API keys: | ||
* A weather API key from [tomorrow.io](https://www.tomorrow.io/weather-api/) set via `WEATHER_API_KEY` | ||
* A geocoding API key from [geocode.maps.co](https://geocode.maps.co/) set via `GEO_API_KEY` | ||
|
||
**(Note if either key is missing, the code will fall back to dummy data.)** | ||
|
||
```bash | ||
uv run --extra examples -m examples.weather | ||
``` | ||
|
||
This example uses `openai:gpt-4o` by default. Gemini seems to be unable to handle the multiple tool | ||
calls. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Simple example of using Pydantic AI to construct a Pydantic model from a text input. | ||
Run with: | ||
uv run --extra examples -m examples.pydantic_model | ||
""" | ||
|
||
import os | ||
from typing import cast | ||
|
||
import logfire | ||
from pydantic import BaseModel | ||
|
||
from pydantic_ai import Agent | ||
from pydantic_ai.agent import KnownModelName | ||
|
||
# 'if-token-present' means nothing will be sent (and the example wil work) if you don't have logfire set up | ||
logfire.configure(send_to_logfire='if-token-present') | ||
|
||
|
||
class MyModel(BaseModel): | ||
city: str | ||
country: str | ||
|
||
|
||
model = cast(KnownModelName, os.getenv('PYDANTIC_AI_MODEL', 'openai:gpt-4o')) | ||
agent = Agent(model, result_type=MyModel, deps=None) | ||
|
||
if __name__ == '__main__': | ||
result = agent.run_sync('The windy city in the US of A.') | ||
print(result.response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.