字数 910,阅读大约需 5 分钟
Python:Agent开发为什么使用Pydantic AI,工具篇(内置工具)
超级好用的AI Agent开发框架:PydanticAI。构建人工智能驱动的应用程序往往会导致非结构化输出、类型不匹配和生产可靠性问题。将大型语言模型集成到Python应用程序中的传统方法缺乏生产系统所需的结构和验证。本系列文章将对pydantic ai的官方文档,整理并献给大家。

🌟 关注我:下次更新,系统会自动弹窗提醒你,就像外卖到了那样准时!再也不怕错过我的脑洞和干货啦~
原文:内置工具 - Pydantic AI 框架[1]
LLM模型供应商的内置工具
内置工具是由 LLM 提供商提供的原生工具,可用于增强智能体的能力。因此需要在调用的模型供应商那里查看是否支持该功能!!!核心是模型供应商提供了这些能力,然后我们是用pydantic ai调用这些内置工具。推荐到pydantic ai文档页面或LLM官方文档查询是否支持这些内置工具。

Pydantic AI 支持以下内置工具:
• WebSearchTool:允许智能体搜索网络
• CodeExecutionTool:使智能体能够在安全的环境中执行代码
• UrlContextTool:使智能体能够将 URL 内容拉取到其上下文中
工具通过 builtin_tools 参数传递给智能体,并由模型提供商的基础设施执行
网络搜索工具
from pydantic_ai import Agent, WebSearchTool
agent = Agent('anthropic:claude-sonnet-4-0', builtin_tools=[WebSearchTool()])
result = agent.run_sync('Give me a sentence with the biggest news in AI this week.')
# > Scientists have developed a universal AI detector that can identify deepfake videos.支持多参数配置
from pydantic_ai import Agent, WebSearchTool, WebSearchUserLocation
agent = Agent(
'anthropic:claude-sonnet-4-0',
builtin_tools=[
WebSearchTool(
search_context_size='high',
user_location=WebSearchUserLocation(
city='San Francisco',
country='US',
region='CA',
timezone='America/Los_Angeles',
),
blocked_domains=['example.com', 'spam-site.net'],
allowed_domains=None, # Cannot use both blocked_domains and allowed_domains with Anthropic
max_uses=5, # Anthropic only: limit tool usage
)
],
)
result = agent.run_sync('Use the web to get the current time.')
# > In San Francisco, it's 8:21:41 pm PDT on Wednesday, August 6, 2025.代码执行工具
CodeExecutionTool 使智能体能够在安全的环境中执行代码,非常适合计算任务、数据分析和数学运算。
from pydantic_ai import Agent, CodeExecutionTool
agent = Agent('anthropic:claude-sonnet-4-0', builtin_tools=[CodeExecutionTool()])
result = agent.run_sync('Calculate the factorial of 15 and show your work')
# > The factorial of 15 is **1,307,674,368,000**.URL上下文工具
智能体能够将 URL 内容拉取到其上下文中,从而可以从网络上获取最新信息。
from pydantic_ai import Agent, UrlContextTool
agent = Agent('google-gla:gemini-2.5-flash', builtin_tools=[UrlContextTool()])
result = agent.run_sync('What is this? https://ai.pydantic.org.cn')
# > A Python agent framework for building Generative AI applications.彩蛋结尾
嘿,别滑了!手指停一停,听我说句悄悄话👇
🌟 关注我:下次更新,系统会自动弹窗提醒你,就像外卖到了那样准时!再也不怕错过我的脑洞和干货啦~
📌 收藏本文:这篇宝藏文章,现在不码住,以后想找只能捶胸顿足!点个收藏,让它成为你的私人知识库,随时回来挖宝~
❤️ 点赞在看:如果逗笑你了或者对你有用,麻烦高抬贵手点个赞!你的每个赞都是我熬夜写文的“鸡血”,让我更有动力产出更多有趣内容~
轻量云主机 VPS

专属优惠地址[2]:https://my.racknerd.com/aff.php?aff=14942

更多博客内容
访问:https://www.funkygod.vip/
博客:Tesla[3]
引用链接
[1] 内置工具 - Pydantic AI 框架: https://ai.pydantic.org.cn/builtin-tools/#overview[2] 专属优惠地址: https://my.racknerd.com/aff.php?aff=14942[3] Tesla: https://www.funkygod.vip/
评论区