Skip to content

Commit 5ce859f

Browse files
authored
readme improvements (#109)
1 parent a745f78 commit 5ce859f

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ PydanticAI is a Python Agent Framework designed to make it less painful to build
4040
* Novel, type-safe [dependency injection system](https://ai.pydantic.dev/dependencies/), useful for testing and eval-driven iterative development
4141
* [Logfire integration](https://ai.pydantic.dev/logfire/) for debugging and monitoring the performance and general behavior of your LLM-powered application
4242

43-
## example "In Beta"
43+
## In Beta!
4444

4545
PydanticAI is in early beta, the API is still subject to change and there's a lot more to do.
4646
[Feedback](https://github.com/pydantic/pydantic-ai/issues) is very welcome!
@@ -52,11 +52,16 @@ Here's a minimal example of PydanticAI:
5252
```py
5353
from pydantic_ai import Agent
5454

55-
agent = Agent( # (1)!
55+
# Define a very simple agent including the model to use, you can also set the model when running the agent.
56+
agent = Agent(
5657
'gemini-1.5-flash',
58+
# Register a static system prompt using a keyword argument to the agent.
59+
# For more complex dynamically-generated system prompts, see the example below.
5760
system_prompt='Be concise, reply with one sentence.',
5861
)
5962

63+
# Run the agent synchronously, conducting a conversation with the LLM.
64+
# 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. See below for a more complex run.
6065
result = agent.run_sync('Where does "hello world" come from?')
6166
print(result.data)
6267
"""
@@ -83,21 +88,29 @@ from pydantic_ai import Agent, RunContext
8388
from bank_database import DatabaseConn
8489

8590

91+
# SupportDependencies is used to pass data, connections, and logic into the model that will be needed when running
92+
# system prompt and tool functions. Dependency injection provides a type-safe way to customise the behavior of your agents.
8693
@dataclass
8794
class SupportDependencies:
8895
customer_id: int
8996
db: DatabaseConn
9097

9198

99+
# This pydantic model defines the structure of the result returned by the agent.
92100
class SupportResult(BaseModel):
93101
support_advice: str = Field(description='Advice returned to the customer')
94102
block_card: bool = Field(description="Whether to block the customer's card")
95103
risk: int = Field(description='Risk level of query', ge=0, le=10)
96104

97105

106+
# This agent will act as first-tier support in a bank.
107+
# Agents are generic in the type of dependencies they accept and the type of result they return.
108+
# In this case, the support agent has type `Agent[SupportDependencies, SupportResult]`.
98109
support_agent = Agent(
99110
'openai:gpt-4o',
100111
deps_type=SupportDependencies,
112+
# The response from the agent will, be guaranteed to be a SupportResult,
113+
# if validation fails the agent is prompted to try again.
101114
result_type=SupportResult,
102115
system_prompt=(
103116
'You are a support agent in our bank, give the '
@@ -106,30 +119,42 @@ support_agent = Agent(
106119
)
107120

108121

122+
# Dynamic system prompts can can make use of dependency injection.
123+
# Dependencies are carried via the `RunContext` argument, which is parameterized with the `deps_type` from above.
124+
# If the type annotation here is wrong, static type checkers will catch it.
109125
@support_agent.system_prompt
110126
async def add_customer_name(ctx: RunContext[SupportDependencies]) -> str:
111127
customer_name = await ctx.deps.db.customer_name(id=ctx.deps.customer_id)
112128
return f"The customer's name is {customer_name!r}"
113129

114130

131+
# `tool` let you register functions which the LLM may call while responding to a user.
132+
# Again, dependencies are carried via `RunContext`, any other arguments become the tool schema passed to the LLM.
133+
# Pydantic is used to validate these arguments, and errors are passed back to the LLM so it can retry.
115134
@support_agent.tool
116135
async def customer_balance(
117136
ctx: RunContext[SupportDependencies], include_pending: bool
118-
) -> str:
137+
) -> float:
119138
"""Returns the customer's current account balance."""
139+
# The docstring of a tool is also passed to the LLM as the description of the tool.
140+
# Parameter descriptions are extracted from the docstring and added to the parameter schema sent to the LLM.
120141
balance = await ctx.deps.db.customer_balance(
121142
id=ctx.deps.customer_id,
122143
include_pending=include_pending,
123144
)
124-
return f'${balance:.2f}'
145+
return balance
125146

126147

127-
...
148+
... # In a real use case, you'd add more tools and a longer system prompt
128149

129150

130151
async def main():
131152
deps = SupportDependencies(customer_id=123, db=DatabaseConn())
153+
# Run the agent asynchronously, conducting a conversation with the LLM until a final response is reached.
154+
# Even in this fairly simple case, the agent will exchange multiple messages with the LLM as tools are called to retrieve a result.
132155
result = await support_agent.run('What is my balance?', deps=deps)
156+
# The result will be validated with Pydantic to guarantee it is a `SupportResult`, since the agent is generic,
157+
# it'll also be typed as a `SupportResult` to aid with static type checking.
133158
print(result.data)
134159
"""
135160
support_advice='Hello John, your current account balance, including pending transactions, is $123.45.' block_card=False risk=1

docs/index.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,12 @@ async def add_customer_name(ctx: RunContext[SupportDependencies]) -> str:
9595
@support_agent.tool # (6)!
9696
async def customer_balance(
9797
ctx: RunContext[SupportDependencies], include_pending: bool
98-
) -> str:
98+
) -> float:
9999
"""Returns the customer's current account balance.""" # (7)!
100-
balance = await ctx.deps.db.customer_balance(
100+
return await ctx.deps.db.customer_balance(
101101
id=ctx.deps.customer_id,
102102
include_pending=include_pending,
103103
)
104-
return f'${balance:.2f}'
105104

106105

107106
... # (11)!
@@ -127,8 +126,8 @@ async def main():
127126
3. The `SupportDependencies` dataclass is used to pass data, connections, and logic into the model that will be needed when running [system prompt](agents.md#system-prompts) and [tool](agents.md#function-tools) functions. PydanticAI's system of dependency injection provides a [type-safe](agents.md#static-type-checking) way to customise the behavior of your agents, and can be especially useful when running [unit tests](testing-evals.md) and evals.
128127
4. Static [system prompts](agents.md#system-prompts) can be registered with the [`system_prompt` keyword argument][pydantic_ai.Agent.__init__] to the agent.
129128
5. Dynamic [system prompts](agents.md#system-prompts) can be registered with the [`@agent.system_prompt`][pydantic_ai.Agent.system_prompt] decorator, and can make use of dependency injection. Dependencies are carried via the [`RunContext`][pydantic_ai.dependencies.RunContext] argument, which is parameterized with the `deps_type` from above. If the type annotation here is wrong, static type checkers will catch it.
130-
6. [`tool`](agents.md#function-tools) let you register functions which the LLM may call while responding to a user. Again, dependencies are carried via [`RunContext`][pydantic_ai.dependencies.RunContext], and any other arguments become the tool schema passed to the LLM. Pydantic is used to validate these arguments, and errors are passed back to the LLM so it can retry.
131-
7. The docstring of a tool is also passed to the LLM as the description of the tool. Parameter descriptions are [extracted](agents.md#function-tools-and-schema) from the docstring and added to the tool schema sent to the LLM.
129+
6. [`tool`](agents.md#function-tools) let you register functions which the LLM may call while responding to a user. Again, dependencies are carried via [`RunContext`][pydantic_ai.dependencies.RunContext], any other arguments become the tool schema passed to the LLM. Pydantic is used to validate these arguments, and errors are passed back to the LLM so it can retry.
130+
7. The docstring of a tool is also passed to the LLM as the description of the tool. Parameter descriptions are [extracted](agents.md#function-tools-and-schema) from the docstring and added to the parameter schema sent to the LLM.
132131
8. [Run the agent](agents.md#running-agents) asynchronously, conducting a conversation with the LLM until a final response is reached. Even in this fairly simple case, the agent will exchange multiple messages with the LLM as tools are called to retrieve a result.
133132
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.
134133
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.

0 commit comments

Comments
 (0)