|
1 |
| -# Coming soon |
| 1 | +<div align="center"> |
| 2 | + <a href="https://ai.pydantic.dev/"> |
| 3 | + <picture> |
| 4 | + <source media="(prefers-color-scheme: dark)" srcset="https://ai.pydantic.dev/img/pydantic-ai-dark.svg"> |
| 5 | + <img src="https://ai.pydantic.dev/img/pydantic-ai-light.svg" alt="PydanticAI"> |
| 6 | + </picture> |
| 7 | + </a> |
| 8 | +</div> |
| 9 | +<div align="center"> |
| 10 | + <em>Agent Framework / shim to use Pydantic with LLMs</em> |
| 11 | +</div> |
| 12 | +<div align="center"> |
| 13 | + <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> |
| 14 | + <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> |
| 15 | + <a href="https://pypi.python.org/pypi/pydantic-ai"><img src="https://img.shields.io/pypi/v/pydantic-ai.svg" alt="PyPI"></a> |
| 16 | + <a href="https://github.com/pydantic/pydantic-ai"><img src="https://img.shields.io/pypi/pyversions/pydantic-ai.svg" alt="versions"></a> |
| 17 | + <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> |
| 18 | +</div> |
2 | 19 |
|
3 |
| -[](https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml?query=branch%3Amain) |
4 |
| -[](https://coverage-badge.samuelcolvin.workers.dev/redirect/pydantic/pydantic-ai) |
5 |
| -[](https://pypi.python.org/pypi/pydantic-ai) |
6 |
| -[](https://github.com/pydantic/pydantic-ai) |
7 |
| -[](https://github.com/pydantic/pydantic-ai/blob/main/LICENSE) |
| 20 | +--- |
| 21 | + |
| 22 | +**Documentation**: [ai.pydantic.dev](https://ai.pydantic.dev/) |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +When I first found FastAPI, I got it immediately. I was excited to find something so innovative and ergonomic built on Pydantic. |
| 27 | + |
| 28 | +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. |
| 29 | + |
| 30 | +PydanticAI is a Python Agent Framework designed to make it less painful to build production grade applications with Generative AI. |
| 31 | + |
| 32 | +## Why use PydanticAI |
| 33 | + |
| 34 | +* 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) |
| 35 | +* Model-agnostic — currently OpenAI, Gemini, and Groq are supported. And there is a simple interface to implement support for other models. |
| 36 | +* [Type-safe](https://ai.pydantic.dev/agents/#static-type-checking) |
| 37 | +* 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 |
| 38 | +* [Structured response](https://ai.pydantic.dev/results/#structured-result-validation) validation with Pydantic |
| 39 | +* [Streamed responses](https://ai.pydantic.dev/results/#streamed-results), including validation of streamed _structured_ responses with Pydantic |
| 40 | +* Novel, type-safe [dependency injection system](https://ai.pydantic.dev/dependencies/), useful for testing and eval-driven iterative development |
| 41 | +* [Logfire integration](https://ai.pydantic.dev/logfire/) for debugging and monitoring the performance and general behavior of your LLM-powered application |
| 42 | + |
| 43 | +## example "In Beta" |
| 44 | + |
| 45 | +PydanticAI is in early beta, the API is still subject to change and there's a lot more to do. |
| 46 | +[Feedback](https://github.com/pydantic/pydantic-ai/issues) is very welcome! |
| 47 | + |
| 48 | +## Hello World Example |
| 49 | + |
| 50 | +Here's a minimal example of PydanticAI: |
| 51 | + |
| 52 | +```py |
| 53 | +from pydantic_ai import Agent |
| 54 | + |
| 55 | +agent = Agent( # (1)! |
| 56 | + 'gemini-1.5-flash', |
| 57 | + system_prompt='Be concise, reply with one sentence.', |
| 58 | +) |
| 59 | + |
| 60 | +result = agent.run_sync('Where does "hello world" come from?') |
| 61 | +print(result.data) |
| 62 | +""" |
| 63 | +The first known use of "hello, world" was in a 1974 textbook about the C programming language. |
| 64 | +""" |
| 65 | +``` |
| 66 | + |
| 67 | +_(This example is complete, it can be run "as is")_ |
| 68 | + |
| 69 | +Not very interesting yet, but we can easily add "tools", dynamic system prompts, and structured responses to build more powerful agents. |
| 70 | + |
| 71 | +## Tools & Dependency Injection Example |
| 72 | + |
| 73 | +Here is a concise example using PydanticAI to build a support agent for a bank: |
| 74 | + |
| 75 | +**(Better documented example [in the docs](https://ai.pydantic.dev/#tools-dependency-injection-example))** |
| 76 | + |
| 77 | +```py |
| 78 | +from dataclasses import dataclass |
| 79 | + |
| 80 | +from pydantic import BaseModel, Field |
| 81 | +from pydantic_ai import Agent, RunContext |
| 82 | + |
| 83 | +from bank_database import DatabaseConn |
| 84 | + |
| 85 | + |
| 86 | +@dataclass |
| 87 | +class SupportDependencies: |
| 88 | + customer_id: int |
| 89 | + db: DatabaseConn |
| 90 | + |
| 91 | + |
| 92 | +class SupportResult(BaseModel): |
| 93 | + support_advice: str = Field(description='Advice returned to the customer') |
| 94 | + block_card: bool = Field(description="Whether to block the customer's card") |
| 95 | + risk: int = Field(description='Risk level of query', ge=0, le=10) |
| 96 | + |
| 97 | + |
| 98 | +support_agent = Agent( |
| 99 | + 'openai:gpt-4o', |
| 100 | + deps_type=SupportDependencies, |
| 101 | + result_type=SupportResult, |
| 102 | + system_prompt=( |
| 103 | + 'You are a support agent in our bank, give the ' |
| 104 | + 'customer support and judge the risk level of their query.' |
| 105 | + ), |
| 106 | +) |
| 107 | + |
| 108 | + |
| 109 | +@support_agent.system_prompt |
| 110 | +async def add_customer_name(ctx: RunContext[SupportDependencies]) -> str: |
| 111 | + customer_name = await ctx.deps.db.customer_name(id=ctx.deps.customer_id) |
| 112 | + return f"The customer's name is {customer_name!r}" |
| 113 | + |
| 114 | + |
| 115 | +@support_agent.tool |
| 116 | +async def customer_balance( |
| 117 | + ctx: RunContext[SupportDependencies], include_pending: bool |
| 118 | +) -> str: |
| 119 | + """Returns the customer's current account balance.""" |
| 120 | + balance = await ctx.deps.db.customer_balance( |
| 121 | + id=ctx.deps.customer_id, |
| 122 | + include_pending=include_pending, |
| 123 | + ) |
| 124 | + return f'${balance:.2f}' |
| 125 | + |
| 126 | + |
| 127 | +... |
| 128 | + |
| 129 | + |
| 130 | +async def main(): |
| 131 | + deps = SupportDependencies(customer_id=123, db=DatabaseConn()) |
| 132 | + result = await support_agent.run('What is my balance?', deps=deps) |
| 133 | + print(result.data) |
| 134 | + """ |
| 135 | + support_advice='Hello John, your current account balance, including pending transactions, is $123.45.' block_card=False risk=1 |
| 136 | + """ |
| 137 | + |
| 138 | + result = await support_agent.run('I just lost my card!', deps=deps) |
| 139 | + print(result.data) |
| 140 | + """ |
| 141 | + support_advice="I'm sorry to hear that, John. We are temporarily blocking your card to prevent unauthorized transactions." block_card=True risk=8 |
| 142 | + """ |
| 143 | +``` |
| 144 | + |
| 145 | +## Next Steps |
| 146 | + |
| 147 | +To try PydanticAI yourself, follow the instructions [in the examples](https://ai.pydantic.dev/examples/). |
| 148 | + |
| 149 | +Read the [docs](https://ai.pydantic.dev/agents/) to learn more about building applications with PydanticAI. |
| 150 | + |
| 151 | +Read the [API Reference](https://ai.pydantic.dev/api/agent/) to understand PydanticAI's interface. |
0 commit comments