You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/index.md
+46-31Lines changed: 46 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,35 +14,43 @@ PydanticAI is a Python Agent Framework designed to make it less painful to build
14
14
* Multi-model — currently with OpenAI and Gemini are support, Anthropic [coming soon](https://github.com/pydantic/pydantic-ai/issues/63), simply interface to implement other models or adapt existing ones
15
15
* Type-safe
16
16
* Built on tried and tested best practices in Python
17
-
* Structured response validation with Pydantic
18
-
* Streamed responses, including validation of streamed structured responses with Pydantic
19
-
* Novel, type-safe dependency injection system
20
-
* Logfire integration
17
+
*[Structured response](results.md#structured-result-validation) validation with Pydantic
18
+
*[Streamed responses](results.md#streamed-results), including validation of streamed structured responses with Pydantic
*[Logfire integration](logfire.md) for debugging and performance monitoring
21
21
22
22
!!! example "In Beta"
23
23
PydanticAI is in early beta, the API is subject to change and there's a lot more to do.
24
24
[Feedback](https://github.com/pydantic/pydantic-ai/issues) is very welcome!
25
25
26
-
## Example — Hello World
26
+
## Hello World Example
27
27
28
28
Here's a very minimal example of PydanticAI.
29
29
30
30
```py title="hello_world.py"
31
31
from pydantic_ai import Agent
32
32
33
-
agent = Agent('gemini-1.5-flash', system_prompt='Be concise, reply with one sentence.')
33
+
agent = Agent( # (1)!
34
+
'gemini-1.5-flash',
35
+
system_prompt='Be concise, reply with one sentence.', # (2)!
36
+
)
34
37
35
-
result = agent.run_sync('Where does "hello world" come from?')
38
+
result = agent.run_sync('Where does "hello world" come from?')# (3)!
36
39
print(result.data)
37
40
"""
38
41
The first known use of "hello, world" was in a 1974 textbook about the C programming language.
39
42
"""
40
43
```
41
-
_(This example is complete, it can be run "as is")_
44
+
45
+
1. Define a very simple agent, here we configure the agent to use [Gemini 1.5's Flash](api/models/gemini.md) model, you can also set the model when running the agent.
46
+
2. Static [system prompts](agents.md#system-prompts) can be registered as keyword arguments to the agent. For more complex system prompts, see the example below.
47
+
3.[Run the agent](agents.md#running-agents) synchronously, conducting a conversation with the LLM, here the exchange should be very short: PydanticAI will send the system prompt and the user query to the LLM, the model will return a text response.
48
+
49
+
4._(This example is complete, it can be run "as is")_
42
50
43
51
Not very interesting yet, but we can easily add retrievers, dynamic system prompts and structured responses to build more powerful agents.
44
52
45
-
## Example — Retrievers and Dependency Injection
53
+
## Retrievers & Dependency Injection Example
46
54
47
55
Small but complete example of using PydanticAI to build a support agent for a bank.
48
56
@@ -59,10 +67,10 @@ from bank_database import DatabaseConn
59
67
@dataclass
60
68
classSupportDependencies: # (3)!
61
69
customer_id: int
62
-
db: DatabaseConn
70
+
db: DatabaseConn# (12)!
63
71
64
72
65
-
classSupportResult(BaseModel):
73
+
classSupportResult(BaseModel):# (13)!
66
74
support_advice: str= Field(description='Advice returned to the customer')
67
75
block_card: bool= Field(description='Whether to block their')
68
76
risk: int= Field(description='Risk level of query', ge=0, le=10)
result =await support_agent.run('What is my balance?', deps=deps) # (8)!
115
+
print(result.data) # (10)!
116
+
"""
117
+
support_advice='Hello John, your current account balance, including pending transactions, is $123.45.' block_card=False risk=1
118
+
"""
110
119
111
-
result = support_agent.run_sync('I just lost my card!', deps=deps)
112
-
print(result.data)
113
-
"""
114
-
support_advice="I'm sorry to hear that, John. We are temporarily blocking your card to prevent unauthorized transactions." block_card=True risk=8
115
-
"""
120
+
result =awaitsupport_agent.run('I just lost my card!', deps=deps)
121
+
print(result.data)
122
+
"""
123
+
support_advice="I'm sorry to hear that, John. We are temporarily blocking your card to prevent unauthorized transactions." block_card=True risk=8
124
+
"""
116
125
```
117
126
118
-
1. An [agent](agents.md) that acts as first-tier support in a bank, agents are generic in the type of dependencies they take and the type of result they return, in this case `Deps` and `SupportResult`.
119
-
2. Here we configure the agent to use [OpenAI's GPT-4o model](api/models/openai.md), you can also customise the model when running the agent.
120
-
3. The `SupportDependencies` dataclass is used to pass dataand connections into the model that will be needed when running [system prompts](agents.md#system-prompts) and [retrievers](agents.md#retrievers). 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.
121
-
4. Static [system prompts](agents.md#system-prompts) can be registered as keyword arguments to the agent
122
-
5. dynamic [system prompts](agents.md#system-prompts) can be registered with the `@agent.system_prompot` decorator and benefit from dependency injection.
123
-
6.[Retrievers](agents.md#retrievers) let you register "tools" which the LLM may call while responding to a user. You inject dependencies into the retriever with[`CallContext`][pydantic_ai.dependencies.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.
124
-
7. The docstring is also passed to the LLM as a description of the tool.
125
-
8.[Run the agent](agents.md#running-agents)synchronously, conducting a conversation with the LLM until a final response is reached.
127
+
1. An [agent](agents.md) that acts as first-tier support in a bank, agents are generic in the type of dependencies they take and the type of result they return, in this case support agent has type `#!python Agent[SupportDependencies, SupportResult]`.
128
+
2. Here we configure the agent to use [OpenAI's GPT-4o model](api/models/openai.md), you can also set the model when running the agent.
129
+
3. The `SupportDependencies` dataclass is used to pass data, connections and logic into the model that will be needed when running [system prompts](agents.md#system-prompts) and [retrievers](agents.md#retrievers). PydanticAI's system of dependency injection provides a powerful, type safe way to customise the behaviour of your agents, including when unit tests and evals.
130
+
4. Static [system prompts](agents.md#system-prompts) can be registered as [keyword arguments][pydantic_ai.Agent.__init__] to the agent.
131
+
5. dynamic [system prompts](agents.md#system-prompts) can be registered with the [`@agent.system_prompt`][pydantic_ai.Agent.system_prompt] decorator and benefit from dependency injection. Dependencies are carried via the [`CallContext`][pydantic_ai.dependencies.CallContext] argument, this is parameterised with the `deps_type` from above, if the type annotation here is wrong, static type checkers will catch it.
132
+
6.[Retrievers](agents.md#retrievers) let you register "tools" which the LLM may call while responding to a user. Again dependencies are carried via[`CallContext`][pydantic_ai.dependencies.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.
133
+
7. The docstring is also passed to the LLM as a description of the tool. Parameter descriptions are [extracted](agents.md#retrievers-tools-and-schema) from the docstring and added to the tool schema sent to the LLM.
134
+
8.[Run the agent](agents.md#running-agents)asynchronously, conducting a conversation with the LLM until a final response is reached. Even in this fair simply case, the agent will exchange multiple messages with the LLM as retrievers are called to each a result.
126
135
9. The response from the agent will, be guaranteed to be a `SupportResult`, if validation fails [reflection](agents.md#reflection-and-self-correction) will mean the agent is prompted to try again.
127
136
10. The result will be validated with Pydantic to guarantee it is a `SupportResult`, since the agent is generic, it'll also be typed as a `SupportResult` to aid with static type checking.
128
-
11. In real use case, you'd add many more retrievers to the agent to extend the context it's equipped with and support it can provide.
137
+
11. In a real use case, you'd add many more retrievers and a longer system prompt to the agent to extend the context it's equipped with and support it can provide.
138
+
12. This is a simple sketch of a database connection, used to keep the example short and readable. In reality, you'd be connecting to an external database (e.g. PostgreSQL) to get information about customers.
139
+
13. This [Pydantic](https://docs.pydantic.dev) model is used to constrain the structured data returned by the agent. From this simple definition, Pydantic builds teh JSON Schema that tells the LLM how to return the data, and performs validation to guarantee the data is correct at the end of the conversation.
129
140
130
141
!!! tip "Complete `bank_support.py` example"
131
142
This example is incomplete for the sake of brevity (the definition of `DatabaseConn` is missing); you can find a complete `bank_support.py` example [here](examples/bank-support.md).
132
143
133
144
## Next Steps
134
145
135
146
To try PydanticAI yourself, follow instructions [in examples](examples/index.md).
147
+
148
+
Read the conceptual [documentation](agents.md) to learn more about building applications with PydanticAI.
149
+
150
+
Read the [API Reference](api/agent.md) to understand PydanticAI's interface.
Applications that use LLMs have some challenges that are well known and understood: LLMs are **slow**, **unreliable** and **expensive**.
4
-
These applications also have some challenges that most developers have encountered much less often: they're**fickle** and **non-deterministic**. Subtle changes in a prompt can completely change a model's performance, and there's no `EXPLAIN` query you can run to understand why.
4
+
These applications also have some challenges that most developers have encountered much less often: LLMs are**fickle** and **non-deterministic**. Subtle changes in a prompt can completely change a model's performance, and there's no `EXPLAIN` query you can run to understand why.
5
5
6
-
From a software engineers point of view, you can think of LLMs as the worst database you've ever heard of, but worse.
6
+
!!! danger
7
+
From a software engineers point of view, you can think of LLMs as the worst database you've ever heard of, but worse.
8
+
9
+
If LLMs weren't so bloody useful, we'd never touch them.
7
10
8
11
To build successful applications with LLMs, we need new tools to understand both model performance, and the behavior of applications that rely on them.
9
12
@@ -13,14 +16,26 @@ LLM Observability tools that just let you understand how your model is performin
13
16
14
17
[Pydantic Logfire](https://pydantic.dev/logfire) is an observability platform from the developers of Pydantic and PydanticAI, that aims to let you understand your entire application: Gen AI, classic predictive AI, HTTP traffic, database queries and everything else a modern application needs.
15
18
16
-
!!! note "Pydantic Logfire is a commercial product"
19
+
!!! tip "Pydantic Logfire is a commercial product"
17
20
Logfire is a commercially supported, hosted platform with an extremely generous and perpetual free tier.
18
21
You can sign up and start using Logfire in a couple of minutes.
19
22
20
23
PydanticAI has built-in (but optional) support for Logfire via the [`logfire-api`](https://github.com/pydantic/logfire/tree/main/logfire-api) no-op package.
21
24
22
-
That means if the `logfire` package is installed, detailed information about agent runs is sent to Logfire. But if the `logfire` package is not installed, there's no overhead and nothing is sent.
25
+
That means if the `logfire` package is installed and configured, detailed information about agent runs is sent to Logfire. But if the `logfire` package is not installed, there's virtually no overhead and nothing is sent.
23
26
24
27
Here's an example showing details of running the [Weather Agent](examples/weather-agent.md) in Logfire:
0 commit comments