-
Notifications
You must be signed in to change notification settings - Fork 317
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
0006c10
commit a745f78
Showing
5 changed files
with
168 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,151 @@ | ||
# Coming soon | ||
<div align="center"> | ||
<a href="https://ai.pydantic.dev/"> | ||
<picture> | ||
<source media="(prefers-color-scheme: dark)" srcset="https://ai.pydantic.dev/img/pydantic-ai-dark.svg"> | ||
<img src="https://ai.pydantic.dev/img/pydantic-ai-light.svg" alt="PydanticAI"> | ||
</picture> | ||
</a> | ||
</div> | ||
<div align="center"> | ||
<em>Agent Framework / shim to use Pydantic with LLMs</em> | ||
</div> | ||
<div align="center"> | ||
<a href="https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml?query=branch%3Amain"><img src="https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml/badge.svg?event=push" alt="CI"></a> | ||
<a href="https://coverage-badge.samuelcolvin.workers.dev/redirect/pydantic/pydantic-ai"><img src="https://coverage-badge.samuelcolvin.workers.dev/pydantic/pydantic-ai.svg" alt="Coverage"></a> | ||
<a href="https://pypi.python.org/pypi/pydantic-ai"><img src="https://img.shields.io/pypi/v/pydantic-ai.svg" alt="PyPI"></a> | ||
<a href="https://github.com/pydantic/pydantic-ai"><img src="https://img.shields.io/pypi/pyversions/pydantic-ai.svg" alt="versions"></a> | ||
<a href="https://github.com/pydantic/pydantic-ai/blob/main/LICENSE"><img src="https://img.shields.io/github/license/pydantic/pydantic-ai.svg?v" alt="license"></a> | ||
</div> | ||
|
||
[![CI](https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml/badge.svg?event=push)](https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml?query=branch%3Amain) | ||
[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/pydantic/pydantic-ai.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/pydantic/pydantic-ai) | ||
[![PyPI](https://img.shields.io/pypi/v/pydantic-ai.svg)](https://pypi.python.org/pypi/pydantic-ai) | ||
[![versions](https://img.shields.io/pypi/pyversions/pydantic-ai.svg)](https://github.com/pydantic/pydantic-ai) | ||
[![license](https://img.shields.io/github/license/pydantic/pydantic-ai.svg?v)](https://github.com/pydantic/pydantic-ai/blob/main/LICENSE) | ||
--- | ||
|
||
**Documentation**: [ai.pydantic.dev](https://ai.pydantic.dev/) | ||
|
||
--- | ||
|
||
When I first found FastAPI, I got it immediately. I was excited to find something so innovative and ergonomic built on Pydantic. | ||
|
||
Virtually every Agent Framework and LLM library in Python uses Pydantic, but when we began to use LLMs in [Pydantic Logfire](https://pydantic.dev/logfire), I couldn't find anything that gave me the same feeling. | ||
|
||
PydanticAI is a Python Agent Framework designed to make it less painful to build production grade applications with Generative AI. | ||
|
||
## Why use PydanticAI | ||
|
||
* Built by the team behind Pydantic (the validation layer of the OpenAI SDK, the Anthropic SDK, LangChain, LlamaIndex, AutoGPT, Transformers, CrewAI, Instructor and many more) | ||
* Model-agnostic — currently OpenAI, Gemini, and Groq are supported. And there is a simple interface to implement support for other models. | ||
* [Type-safe](https://ai.pydantic.dev/agents/#static-type-checking) | ||
* Control flow and agent composition is done with vanilla Python, allowing you to make use of the same Python development best practices you'd use in any other (non-AI) project | ||
* [Structured response](https://ai.pydantic.dev/results/#structured-result-validation) validation with Pydantic | ||
* [Streamed responses](https://ai.pydantic.dev/results/#streamed-results), including validation of streamed _structured_ responses with Pydantic | ||
* Novel, type-safe [dependency injection system](https://ai.pydantic.dev/dependencies/), useful for testing and eval-driven iterative development | ||
* [Logfire integration](https://ai.pydantic.dev/logfire/) for debugging and monitoring the performance and general behavior of your LLM-powered application | ||
|
||
## example "In Beta" | ||
|
||
PydanticAI is in early beta, the API is still subject to change and there's a lot more to do. | ||
[Feedback](https://github.com/pydantic/pydantic-ai/issues) is very welcome! | ||
|
||
## Hello World Example | ||
|
||
Here's a minimal example of PydanticAI: | ||
|
||
```py | ||
from pydantic_ai import Agent | ||
|
||
agent = Agent( # (1)! | ||
'gemini-1.5-flash', | ||
system_prompt='Be concise, reply with one sentence.', | ||
) | ||
|
||
result = agent.run_sync('Where does "hello world" come from?') | ||
print(result.data) | ||
""" | ||
The first known use of "hello, world" was in a 1974 textbook about the C programming language. | ||
""" | ||
``` | ||
|
||
_(This example is complete, it can be run "as is")_ | ||
|
||
Not very interesting yet, but we can easily add "tools", dynamic system prompts, and structured responses to build more powerful agents. | ||
|
||
## Tools & Dependency Injection Example | ||
|
||
Here is a concise example using PydanticAI to build a support agent for a bank: | ||
|
||
**(Better documented example [in the docs](https://ai.pydantic.dev/#tools-dependency-injection-example))** | ||
|
||
```py | ||
from dataclasses import dataclass | ||
|
||
from pydantic import BaseModel, Field | ||
from pydantic_ai import Agent, RunContext | ||
|
||
from bank_database import DatabaseConn | ||
|
||
|
||
@dataclass | ||
class SupportDependencies: | ||
customer_id: int | ||
db: DatabaseConn | ||
|
||
|
||
class SupportResult(BaseModel): | ||
support_advice: str = Field(description='Advice returned to the customer') | ||
block_card: bool = Field(description="Whether to block the customer's card") | ||
risk: int = Field(description='Risk level of query', ge=0, le=10) | ||
|
||
|
||
support_agent = Agent( | ||
'openai:gpt-4o', | ||
deps_type=SupportDependencies, | ||
result_type=SupportResult, | ||
system_prompt=( | ||
'You are a support agent in our bank, give the ' | ||
'customer support and judge the risk level of their query.' | ||
), | ||
) | ||
|
||
|
||
@support_agent.system_prompt | ||
async def add_customer_name(ctx: RunContext[SupportDependencies]) -> str: | ||
customer_name = await ctx.deps.db.customer_name(id=ctx.deps.customer_id) | ||
return f"The customer's name is {customer_name!r}" | ||
|
||
|
||
@support_agent.tool | ||
async def customer_balance( | ||
ctx: RunContext[SupportDependencies], include_pending: bool | ||
) -> str: | ||
"""Returns the customer's current account balance.""" | ||
balance = await ctx.deps.db.customer_balance( | ||
id=ctx.deps.customer_id, | ||
include_pending=include_pending, | ||
) | ||
return f'${balance:.2f}' | ||
|
||
|
||
... | ||
|
||
|
||
async def main(): | ||
deps = SupportDependencies(customer_id=123, db=DatabaseConn()) | ||
result = await support_agent.run('What is my balance?', deps=deps) | ||
print(result.data) | ||
""" | ||
support_advice='Hello John, your current account balance, including pending transactions, is $123.45.' block_card=False risk=1 | ||
""" | ||
|
||
result = await support_agent.run('I just lost my card!', deps=deps) | ||
print(result.data) | ||
""" | ||
support_advice="I'm sorry to hear that, John. We are temporarily blocking your card to prevent unauthorized transactions." block_card=True risk=8 | ||
""" | ||
``` | ||
|
||
## Next Steps | ||
|
||
To try PydanticAI yourself, follow the instructions [in the examples](https://ai.pydantic.dev/examples/). | ||
|
||
Read the [docs](https://ai.pydantic.dev/agents/) to learn more about building applications with PydanticAI. | ||
|
||
Read the [API Reference](https://ai.pydantic.dev/api/agent/) to understand PydanticAI's interface. |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.