{"id":15302,"date":"2024-12-11T20:05:47","date_gmt":"2024-12-11T12:05:47","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=15302"},"modified":"2024-12-11T20:05:47","modified_gmt":"2024-12-11T12:05:47","slug":"pydanticai","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/de\/pydanticai\/","title":{"rendered":"PydanticAI\uff1a\u4f7f\u7528Pydantic\u6784\u5efa\u751f\u6210\u5f0fAI\u5e94\u7528\uff0c\u8ba9\u6784\u5efa\u751f\u4ea7\u7ea7AI\u5e94\u7528\u66f4\u52a0\u7b80\u5355"},"content":{"rendered":"<p>PydanticAI\u662f\u4e00\u4e2a\u57fa\u4e8ePydantic\u7684Python\u4ee3\u7406\u6846\u67b6\uff0c\u65e8\u5728\u7b80\u5316\u751f\u6210\u5f0fAI\u5e94\u7528\u7684\u5f00\u53d1\u3002\u5b83\u7531Pydantic\u56e2\u961f\u5f00\u53d1\uff0c\u652f\u6301\u591a\u79cd\u6a21\u578b\uff08\u5982OpenAI\u3001Gemini\u3001Groq\u7b49\uff09\uff0c\u5e76\u63d0\u4f9b\u7c7b\u578b\u5b89\u5168\u7684\u63a7\u5236\u6d41\u548c\u4ee3\u7406\u7ec4\u5408\u3002PydanticAI\u901a\u8fc7\u7ed3\u6784\u5316\u54cd\u5e94\u9a8c\u8bc1\u548c\u6d41\u5f0f\u54cd\u5e94\uff0c\u786e\u4fdd\u751f\u6210\u5f0fAI\u5e94\u7528\u7684\u9ad8\u6548\u6027\u548c\u53ef\u9760\u6027\u3002\u5176\u72ec\u7279\u7684\u4f9d\u8d56\u6ce8\u5165\u7cfb\u7edf\u6709\u52a9\u4e8e\u6d4b\u8bd5\u548c\u8fed\u4ee3\u5f00\u53d1\uff0c\u5e76\u96c6\u6210\u4e86Logfire\u7528\u4e8e\u8c03\u8bd5\u548c\u76d1\u63a7\u5e94\u7528\u6027\u80fd\u3002PydanticAI\u76ee\u524d\u5904\u4e8e\u65e9\u671f\u6d4b\u8bd5\u9636\u6bb5\uff0cAPI\u53ef\u80fd\u4f1a\u6709\u53d8\u52a8\uff0c\u6b22\u8fce\u7528\u6237\u53cd\u9988\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u529f\u80fd\u5217\u8868<\/h2>\n<ul>\n<li><strong>\u591a\u6a21\u578b\u652f\u6301<\/strong>\uff1a\u517c\u5bb9OpenAI\u3001Gemini\u3001Groq\u7b49\u591a\u79cd\u751f\u6210\u5f0fAI\u6a21\u578b\u3002<\/li>\n<li><strong>\u7c7b\u578b\u5b89\u5168<\/strong>\uff1a\u4f7f\u7528Pydantic\u8fdb\u884c\u7ed3\u6784\u5316\u54cd\u5e94\u9a8c\u8bc1\uff0c\u786e\u4fdd\u6570\u636e\u7c7b\u578b\u5b89\u5168\u3002<\/li>\n<li><strong>\u4f9d\u8d56\u6ce8\u5165\u7cfb\u7edf<\/strong>\uff1a\u63d0\u4f9b\u7c7b\u578b\u5b89\u5168\u7684\u4f9d\u8d56\u6ce8\u5165\uff0c\u4fbf\u4e8e\u6d4b\u8bd5\u548c\u8fed\u4ee3\u5f00\u53d1\u3002<\/li>\n<li><strong>\u6d41\u5f0f\u54cd\u5e94<\/strong>\uff1a\u652f\u6301\u6d41\u5f0f\u54cd\u5e94\u548c\u9a8c\u8bc1\uff0c\u63d0\u9ad8\u5e94\u7528\u54cd\u5e94\u901f\u5ea6\u548c\u53ef\u9760\u6027\u3002<\/li>\n<li><strong>Logfire\u96c6\u6210<\/strong>\uff1a\u7528\u4e8e\u8c03\u8bd5\u548c\u76d1\u63a7\u751f\u6210\u5f0fAI\u5e94\u7528\u7684\u6027\u80fd\u548c\u884c\u4e3a\u3002<\/li>\n<li><strong>\u7b80\u5355\u63a5\u53e3<\/strong>\uff1a\u63d0\u4f9b\u7b80\u6d01\u7684\u63a5\u53e3\uff0c\u4fbf\u4e8e\u6269\u5c55\u548c\u96c6\u6210\u5176\u4ed6\u6a21\u578b\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>\u4f7f\u7528\u5e2e\u52a9<\/h2>\n<h3>\u5b89\u88c5\u6d41\u7a0b<\/h3>\n<ol>\n<li><strong>\u5b89\u88c5PydanticAI<\/strong>\uff1a\u786e\u4fddPython\u7248\u672c\u4e3a3.9\u53ca\u4ee5\u4e0a\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5PydanticAI\uff1a<\/li>\n<\/ol>\n<pre><code>   pip install pydantic-ai\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>\u5b89\u88c5\u4f9d\u8d56<\/strong>\uff1aPydanticAI\u4f9d\u8d56\u4e8e\u4e00\u4e9b\u6838\u5fc3\u5e93\u548cLLM API\uff0c\u5b89\u88c5\u65f6\u4f1a\u81ea\u52a8\u5904\u7406\u8fd9\u4e9b\u4f9d\u8d56\u3002<\/li>\n<\/ol>\n<h3>\u4f7f\u7528\u6307\u5357<\/h3>\n<h4>\u521b\u5efa\u7b80\u5355\u4ee3\u7406<\/h4>\n<ol>\n<li><strong>\u5b9a\u4e49\u4ee3\u7406<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7406\u5e76\u6307\u5b9a\u4f7f\u7528\u7684\u6a21\u578b\u3002<\/li>\n<\/ol>\n<pre><code>   from pydantic_ai import Agent\r\nagent = Agent(\r\n'gemini-1.5-flash',\r\nsystem_prompt='Be concise, reply with one sentence.',\r\n)\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>\u8fd0\u884c\u4ee3\u7406<\/strong>\uff1a\u540c\u6b65\u8fd0\u884c\u4ee3\u7406\uff0c\u8fdb\u884c\u7b80\u5355\u7684\u5bf9\u8bdd\u3002<\/li>\n<\/ol>\n<pre><code>   result = agent.run_sync('Where does \"hello world\" come from?')\r\nprint(result.data)\r\n<\/code><\/pre>\n<h4>\u590d\u6742\u4ee3\u7406\u793a\u4f8b<\/h4>\n<ol>\n<li><strong>\u5b9a\u4e49\u4f9d\u8d56\u548c\u7ed3\u679c\u6a21\u578b<\/strong>\uff1a\u4f7f\u7528Pydantic\u5b9a\u4e49\u4f9d\u8d56\u548c\u7ed3\u679c\u6a21\u578b\u3002<\/li>\n<\/ol>\n<pre><code>   from dataclasses import dataclass\r\nfrom pydantic import BaseModel, Field\r\nfrom pydantic_ai import Agent, RunContext\r\nfrom bank_database import DatabaseConn\r\n@dataclass\r\nclass SupportDependencies:\r\ncustomer_id: int\r\ndb: DatabaseConn\r\nclass SupportResult(BaseModel):\r\nsupport_advice: str = Field(description='Advice returned to the customer')\r\nblock_card: bool = Field(description=\"Whether to block the customer's card\")\r\nrisk: int = Field(description='Risk level of query', ge=0, le=10)\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>\u521b\u5efa\u652f\u6301\u4ee3\u7406<\/strong>\uff1a\u5b9a\u4e49\u7cfb\u7edf\u63d0\u793a\u548c\u5de5\u5177\u51fd\u6570\u3002<\/li>\n<\/ol>\n<pre><code>   support_agent = Agent(\r\n'openai:gpt-4o',\r\ndeps_type=SupportDependencies,\r\nresult_type=SupportResult,\r\nsystem_prompt=(\r\n'You are a support agent in our bank, give the '\r\n'customer support and judge the risk level of their query.'\r\n),\r\n)\r\n@support_agent.system_prompt\r\nasync def add_customer_name(ctx: RunContext[SupportDependencies]) -&gt; str:\r\ncustomer_name = await ctx.deps.db.customer_name(id=ctx.deps.customer_id)\r\nreturn f\"The customer's name is {customer_name!r}\"\r\n@support_agent.tool\r\nasync def customer_balance(ctx: RunContext[SupportDependencies], include_pending: bool) -&gt; float:\r\n\"\"\"Returns the customer's current account balance.\"\"\"\r\nreturn await ctx.deps.db.customer_balance(id=ctx.deps.customer_id, include_pending=include_pending)\r\n<\/code><\/pre>\n<ol start=\"3\">\n<li><strong>\u8fd0\u884c\u652f\u6301\u4ee3\u7406<\/strong>\uff1a\u4f7f\u7528\u5b9a\u4e49\u7684\u4f9d\u8d56\u8fd0\u884c\u4ee3\u7406\u3002<\/li>\n<\/ol>\n<pre><code>   async def main():\r\ndeps = SupportDependencies(customer_id=123, db=DatabaseConn())\r\nresult = await support_agent.run('What is my balance?', deps=deps)\r\nprint(result.data)\r\n# \u8f93\u51fa\u793a\u4f8b\uff1asupport_advice='Hello John, your current account balance, including pending transactions, is $123.45.' block_card=False risk=1\r\nresult = await support_agent.run('I just lost my card!', deps=deps)\r\nprint(result.data)\r\n# \u8f93\u51fa\u793a\u4f8b\uff1asupport_advice=\"I'm sorry to hear that, John. Your card has been blocked for security reasons.\" block_card=True risk=7\r\n<\/code><\/pre>\n<h3>\u8c03\u8bd5\u548c\u76d1\u63a7<\/h3>\n<p>PydanticAI\u96c6\u6210\u4e86Logfire\uff0c\u7528\u4e8e\u8c03\u8bd5\u548c\u76d1\u63a7\u751f\u6210\u5f0fAI\u5e94\u7528\u7684\u6027\u80fd\u548c\u884c\u4e3a\u3002\u901a\u8fc7Logfire\uff0c\u53ef\u4ee5\u5b9e\u65f6\u67e5\u770b\u4ee3\u7406\u8fd0\u884c\u60c5\u51b5\uff0c\u5206\u6790\u54cd\u5e94\u6570\u636e\uff0c\u4f18\u5316\u5e94\u7528\u6027\u80fd\u3002<\/p>\n<h3>\u53cd\u9988\u548c\u6539\u8fdb<\/h3>\n<p>PydanticAI\u76ee\u524d\u5904\u4e8e\u65e9\u671f\u6d4b\u8bd5\u9636\u6bb5\uff0cAPI\u53ef\u80fd\u4f1a\u6709\u53d8\u52a8\u3002\u7528\u6237\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u9047\u5230\u4efb\u4f55\u95ee\u9898\u6216\u6709\u6539\u8fdb\u5efa\u8bae\uff0c\u6b22\u8fce\u901a\u8fc7GitHub\u63d0\u4ea4\u53cd\u9988\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PydanticAI\u662f\u4e00\u4e2a\u57fa\u4e8ePydantic\u7684Python\u4ee3\u7406\u6846\u67b6\uff0c\u65e8\u5728\u7b80\u5316\u751f\u6210\u5f0fAI\u5e94\u7528\u7684\u5f00\u53d1\u3002\u5b83\u7531Pydantic\u56e2\u961f\u5f00\u53d1\uff0c\u652f\u6301\u591a\u79cd\u6a21\u578b\uff08\u5982OpenAI\u3001Gemini\u3001Groq\u7b49\uff09\uff0c\u5e76\u63d0\u4f9b\u7c7b\u578b\u5b89\u5168\u7684\u63a7\u5236\u6d41\u548c\u4ee3\u7406\u7ec4\u5408\u3002PydanticA&#8230;<\/p>\n","protected":false},"author":1,"featured_media":61400,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[230],"class_list":["post-15302","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tool","tag-aikaiyuanxiangmu"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts\/15302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/comments?post=15302"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts\/15302\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/media\/61400"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/media?parent=15302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/categories?post=15302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/tags?post=15302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}