-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eff9d95
commit 234cde8
Showing
39 changed files
with
1,226 additions
and
187 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
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
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,22 @@ | ||
# `pydantic_ai.Agent` | ||
|
||
::: pydantic_ai.Agent | ||
options: | ||
members: | ||
- __init__ | ||
- run | ||
- run_sync | ||
- run_stream | ||
- model | ||
- last_run_messages | ||
- system_prompt | ||
- retriever_plain | ||
- retriever_context | ||
- result_validator | ||
|
||
::: pydantic_ai.agent | ||
options: | ||
members: | ||
- KnownModelName | ||
- ResultValidatorFunc | ||
- SystemPromptFunc |
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,3 @@ | ||
# `pydantic_ai.dependencies` | ||
|
||
::: pydantic_ai.dependencies |
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,3 @@ | ||
# `pydantic_ai.exceptions` | ||
|
||
::: pydantic_ai.exceptions |
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,3 @@ | ||
# `pydantic_ai.messages` | ||
|
||
::: pydantic_ai.messages |
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,3 @@ | ||
# `pydantic_ai.models.function` | ||
|
||
::: pydantic_ai.models.function |
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,3 @@ | ||
# `pydantic_ai.models.gemini` | ||
|
||
::: pydantic_ai.models.gemini |
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,3 @@ | ||
# `pydantic_ai.models` | ||
|
||
::: pydantic_ai.models |
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,3 @@ | ||
# `pydantic_ai.models.openai` | ||
|
||
::: pydantic_ai.models.openai |
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,3 @@ | ||
# `pydantic_ai.models.test` | ||
|
||
::: pydantic_ai.models.test |
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,10 @@ | ||
# `pydantic_ai.result` | ||
|
||
::: pydantic_ai.result | ||
options: | ||
members: | ||
- ResultData | ||
- RunResult | ||
- StreamedRunResult | ||
- _BaseRunResult | ||
- Cost |
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,53 @@ | ||
# PydanticAI | ||
|
||
You can think of PydanticAI as an Agent Framework or a shim to use Pydantic with LLMs — they're the same thing. | ||
|
||
PydanticAI ties to make working with LLMs feel similar to building a web application. | ||
|
||
## Example — Retrievers | ||
|
||
Partial example of using retrievers to help an LLM respond to a user's query about the weather: | ||
|
||
```py title="weather_agent.py" | ||
import httpx | ||
from pydantic_ai import Agent, CallContext | ||
|
||
weather_agent = Agent( # (1)! | ||
'openai:gpt-4o', # (2)! | ||
deps=httpx.AsyncClient, # (3)! | ||
system_prompt='Be concise, reply with one sentence.', # (4)! | ||
) | ||
|
||
|
||
@weather_agent.retriever_context # (5)! | ||
async def get_location(ctx: CallContext[httpx.AsyncClient], location_description: str) -> dict[str, float]: | ||
"""Get the latitude and longitude of a location by its description.""" # (6)! | ||
... | ||
|
||
|
||
@weather_agent.retriever_context # (7)! | ||
async def get_weather(ctx: CallContext[httpx.AsyncClient], lat: float, lng: float) -> dict[str, str]: | ||
"""Get the weather at a location by its latitude and longitude.""" | ||
... | ||
|
||
|
||
result = weather_agent.run_sync('What is the weather like in West London and in Wiltshire?') # (8)! | ||
print(result.data) # (9)! | ||
# > 'The weather in West London is raining, while in Wiltshire it is sunny.' | ||
|
||
print(result.all_messages()) # (10)! | ||
``` | ||
|
||
1. An agent that can tell users about the weather in a particular location. Agents combine a system prompt, a response type (here `str`) and "retrievers" (aka tools). | ||
2. Here we configure the agent to use OpenAI's GPT-4o model, you can also customise the model when running the agent. | ||
3. We specify a dependency for the agent, in this case an HTTP client, which retrievers will use to make requests to external services. PydanticAI's system of dependency injection provides a powerful, type safe way to customise the behaviour of your agents, including for unit tests and evals. | ||
4. Static system prompts can be registered as key word arguments to the agent, dynamic system prompts can be registered with the `@agent.system_prompot` decorator and benefit from dependency injection. | ||
5. Retrievers let you register "tools" which the LLM can call while trying to respond to a user. You inject dependencies into the retriever with `CallContext`, any other arguments become the tool schema passed to the LLM, Pydantic is used to validate these arguments, errors are passed back to the LLM so it can retry. | ||
6. This docstring is also passed to the LLM as a description of the tool. | ||
7. Multiple retrievers can be registered with the same agent, the LLM can choose which (if any) retrievers to call in order to respond to a user. | ||
8. Run the agent synchronously, conducting a conversation with the LLM until a final response is reached. (internally agents are all async, `run_sync` is a helper using `asyncio.run` to call `run()`) | ||
9. The response from the LLM, in this case a `str`, Agents are generic in both the type of `deps` and `result_type`, so calls are typed end-to-end. | ||
10. `result.all_messages()` includes details of messages exchanged, this is useful both to understand the conversation that took place and useful if you want to continue the conversation later — messages can be passed back to later `run/sync_run` calls. | ||
|
||
!!! tip "Complete `weather_agent.py` example" | ||
The above `weather_agent.py` example is complete for the sake of brevity, but you can find a complete example [here](#TODO). |
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,3 @@ | ||
# Installation | ||
|
||
TODO |
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,138 @@ | ||
site_name: PydanticAI | ||
site_description: PydanticAI | ||
strict: true | ||
site_url: https://ai.pydantic.dev | ||
|
||
repo_name: pydantic/pydantic-ai | ||
repo_url: https://github.com/pydantic/pydantic-ai | ||
edit_uri: edit/main/docs/ | ||
|
||
copyright: © Pydantic Services Inc. 2024 to present | ||
|
||
nav: | ||
- Getting Started: | ||
- index.md | ||
- install.md | ||
- API Reference: | ||
- api/agent.md | ||
- api/result.md | ||
- api/messages.md | ||
- api/dependencies.md | ||
- api/exceptions.md | ||
- "pydantic_ai.models": | ||
- api/models/index.md | ||
- api/models/openai.md | ||
- api/models/gemini.md | ||
- api/models/test.md | ||
- api/models/function.md | ||
|
||
extra: | ||
# hide the "Made with Material for MkDocs" message | ||
generator: false | ||
|
||
theme: | ||
name: "material" | ||
palette: | ||
- media: "(prefers-color-scheme)" | ||
scheme: default | ||
primary: pink | ||
accent: pink | ||
toggle: | ||
icon: material/lightbulb | ||
name: "Switch to light mode" | ||
- media: "(prefers-color-scheme: light)" | ||
scheme: default | ||
primary: pink | ||
accent: pink | ||
toggle: | ||
icon: material/lightbulb-outline | ||
name: "Switch to dark mode" | ||
- media: "(prefers-color-scheme: dark)" | ||
scheme: slate | ||
primary: pink | ||
accent: pink | ||
toggle: | ||
icon: material/lightbulb-auto-outline | ||
name: "Switch to system preference" | ||
features: | ||
- search.suggest | ||
- search.highlight | ||
- content.tabs.link | ||
- content.code.annotate | ||
- content.code.copy | ||
- content.code.select | ||
- navigation.expand | ||
- navigation.indexes | ||
- navigation.path | ||
- navigation.tabs | ||
- navigation.sections | ||
- navigation.tracking | ||
- navigation.top # alternatively, we could do navigation.tabs.sticky | ||
- toc.follow | ||
# logo: "logo-white.svg" | ||
# favicon: "favicon.png" | ||
|
||
# https://www.mkdocs.org/user-guide/configuration/#validation | ||
validation: | ||
omitted_files: warn | ||
absolute_links: warn | ||
unrecognized_links: warn | ||
|
||
# used for analytics | ||
extra_javascript: | ||
- "/flarelytics/client.js" | ||
|
||
markdown_extensions: | ||
- tables | ||
- admonition | ||
- attr_list | ||
- md_in_html | ||
- pymdownx.details | ||
- pymdownx.caret | ||
- pymdownx.critic | ||
- pymdownx.mark | ||
- pymdownx.superfences | ||
- pymdownx.snippets | ||
- pymdownx.tilde | ||
- pymdownx.highlight: | ||
pygments_lang_class: true | ||
- pymdownx.extra: | ||
pymdownx.superfences: | ||
custom_fences: | ||
- name: mermaid | ||
class: mermaid | ||
format: !!python/name:pymdownx.superfences.fence_code_format | ||
- pymdownx.emoji: | ||
emoji_index: !!python/name:material.extensions.emoji.twemoji | ||
emoji_generator: !!python/name:material.extensions.emoji.to_svg | ||
- pymdownx.tabbed: | ||
alternate_style: true | ||
- pymdownx.tasklist: | ||
custom_checkbox: true | ||
- sane_lists # this means you can start a list from any number | ||
|
||
watch: | ||
- pydantic_ai | ||
- pydantic_ai_examples | ||
|
||
plugins: | ||
- search | ||
- mkdocstrings: | ||
handlers: | ||
python: | ||
paths: [src/packages/pydantic_ai/pydantic_ai] | ||
options: | ||
members_order: source | ||
separate_signature: true | ||
show_signature_annotations: true | ||
signature_crossrefs: true | ||
group_by_category: false | ||
show_source: false | ||
heading_level: 2 | ||
import: | ||
- url: https://docs.python.org/3/objects.inv | ||
- url: https://docs.pydantic.dev/latest/objects.inv | ||
- url: https://fastapi.tiangolo.com/objects.inv | ||
- url: https://typing-extensions.readthedocs.io/en/latest/objects.inv | ||
- url: https://rich.readthedocs.io/en/stable/objects.inv | ||
# waiting for https://github.com/encode/httpx/discussions/3091#discussioncomment-11205594 |
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
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.