Skip to content

Commit deb48c7

Browse files
authored
tweaking index and other docs (#66)
1 parent 599c088 commit deb48c7

File tree

4 files changed

+67
-37
lines changed

4 files changed

+67
-37
lines changed

docs/index.md

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,43 @@ PydanticAI is a Python Agent Framework designed to make it less painful to build
1414
* 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
1515
* Type-safe
1616
* 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
19+
* Novel, type-safe [dependency injection system](dependencies.md)
20+
* [Logfire integration](logfire.md) for debugging and performance monitoring
2121

2222
!!! example "In Beta"
2323
PydanticAI is in early beta, the API is subject to change and there's a lot more to do.
2424
[Feedback](https://github.com/pydantic/pydantic-ai/issues) is very welcome!
2525

26-
## Example — Hello World
26+
## Hello World Example
2727

2828
Here's a very minimal example of PydanticAI.
2929

3030
```py title="hello_world.py"
3131
from pydantic_ai import Agent
3232

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+
)
3437

35-
result = agent.run_sync('Where does "hello world" come from?')
38+
result = agent.run_sync('Where does "hello world" come from?') # (3)!
3639
print(result.data)
3740
"""
3841
The first known use of "hello, world" was in a 1974 textbook about the C programming language.
3942
"""
4043
```
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")_
4250

4351
Not very interesting yet, but we can easily add retrievers, dynamic system prompts and structured responses to build more powerful agents.
4452

45-
## Example — Retrievers and Dependency Injection
53+
## Retrievers & Dependency Injection Example
4654

4755
Small but complete example of using PydanticAI to build a support agent for a bank.
4856

@@ -59,10 +67,10 @@ from bank_database import DatabaseConn
5967
@dataclass
6068
class SupportDependencies: # (3)!
6169
customer_id: int
62-
db: DatabaseConn
70+
db: DatabaseConn # (12)!
6371

6472

65-
class SupportResult(BaseModel):
73+
class SupportResult(BaseModel): # (13)!
6674
support_advice: str = Field(description='Advice returned to the customer')
6775
block_card: bool = Field(description='Whether to block their')
6876
risk: int = Field(description='Risk level of query', ge=0, le=10)
@@ -101,35 +109,42 @@ async def customer_balance(
101109
... # (11)!
102110

103111

104-
deps = SupportDependencies(customer_id=123, db=DatabaseConn())
105-
result = support_agent.run_sync('What is my balance?', deps=deps) # (8)!
106-
print(result.data) # (10)!
107-
"""
108-
support_advice='Hello John, your current account balance, including pending transactions, is $123.45.' block_card=False risk=1
109-
"""
112+
async def main():
113+
deps = SupportDependencies(customer_id=123, db=DatabaseConn())
114+
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+
"""
110119

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 = await support_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+
"""
116125
```
117126

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 data and 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.
126135
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.
127136
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.
129140

130141
!!! tip "Complete `bank_support.py` example"
131142
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).
132143

133144
## Next Steps
134145

135146
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.

docs/install.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ To use Logfire with PydanticAI, install PydanticAI with the `logfire` optional g
3434
uv add 'pydantic-ai[logfire]'
3535
```
3636

37-
From there, follow the [Logfire documentation](https://logfire.pydantic.dev/docs/) to configure Logfire.
37+
From there, follow the [Logfire setup cods](logfire.md#integrating-logfire) to configure Logfire.
3838

3939
## Next Steps
4040

docs/logfire.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Monitoring and Performance
22

33
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.
55

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.
710

811
To build successful applications with LLMs, we need new tools to understand both model performance, and the behavior of applications that rely on them.
912

@@ -13,14 +16,26 @@ LLM Observability tools that just let you understand how your model is performin
1316

1417
[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.
1518

16-
!!! note "Pydantic Logfire is a commercial product"
19+
!!! tip "Pydantic Logfire is a commercial product"
1720
Logfire is a commercially supported, hosted platform with an extremely generous and perpetual free tier.
1821
You can sign up and start using Logfire in a couple of minutes.
1922

2023
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.
2124

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.
2326

2427
Here's an example showing details of running the [Weather Agent](examples/weather-agent.md) in Logfire:
2528

2629
![Weather Agent Logfire](img/logfire-weather-agent.png)
30+
31+
## Integrating Logfire
32+
33+
TODO
34+
35+
## Debugging
36+
37+
TODO
38+
39+
## Monitoring Performance
40+
41+
TODO

pydantic_ai_examples/bank_support.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class DatabaseConn:
1616
"""This is a fake database for example purposes.
1717
1818
In reality, you'd be connecting to an external database
19-
to get information about customers.
19+
(e.g. PostgreSQL) to get information about customers.
2020
"""
2121

2222
@classmethod

0 commit comments

Comments
 (0)