工具包与结构化工具输入
将相关工具打包成可复用的工具包,并使用模式定义结构化且带类型的参数,让代理能够以正确参数可靠地调用工具。
工具包与结构化工具输入 是 CoddyKit 上的免费 AI Agents with LangChain & Autonomous Workflows 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Agents with LangChain & Autonomous Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Agents with LangChain & Autonomous Workflows 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Beyond Single Tools
Real integrations rarely need just one function. A database integration needs list-tables, run-query, and describe-schema together.
A toolkit groups related tools so an agent gets a coherent capability in one bundle.
The Single-String Limitation
A basic tool takes one string input. But many actions need multiple typed arguments — a date range, a user id, a limit.
Structured tools let the agent pass several validated parameters instead of cramming them into one string.
Defining an Input Schema
Use a Pydantic model to describe the arguments. The field descriptions guide the LLM on how to fill them.
from pydantic import BaseModel, Field
class SearchArgs(BaseModel):
query: str = Field(description='Search keywords')
limit: int = Field(description='Max results', default=5)Attaching the Schema to a Tool
Pass args_schema so the agent knows the exact shape it must produce.
from langchain.tools import StructuredTool
def search(query: str, limit: int = 5):
return run_search(query, limit)
tool = StructuredTool.from_function(
func=search,
name='search',
description='Search the catalog',
args_schema=SearchArgs
)The @tool Decorator Shortcut
The @tool decorator infers a schema from your type hints and docstring, the quickest way to make a structured tool.
from langchain.tools import tool
@tool
def get_weather(city: str, units: str = 'metric') -> str:
'Get current weather for a city.'
return fetch_weather(city, units)Why Descriptions Matter
The LLM chooses tools and fills arguments from your name and descriptions. Vague text causes wrong tool choice or bad parameters.
- Say what the tool does and when to use it
- Describe each field clearly
- Mention units and formats
Building a Toolkit
A toolkit subclass exposes a get_tools() method returning the grouped tools. Shared config (like a client) lives on the toolkit.
from langchain.tools import BaseToolkit
class CrmToolkit(BaseToolkit):
client: object
def get_tools(self):
return [list_contacts, create_contact, add_note]Giving Tools to an Agent
Expand a toolkit into the agent's tool list. The agent now has the whole capability set at once.
toolkit = CrmToolkit(client=crm)
agent = create_agent(llm, toolkit.get_tools())Validation Protects You
Because the schema is enforced, malformed agent output is rejected before your function runs. This stops invalid types or missing required fields from reaching your integration.
Handling Tool Errors
Tools can fail (network, bad input). Catch exceptions and return a helpful message so the agent can recover or ask the user, instead of crashing the run.
@tool
def fetch_order(order_id: str) -> str:
'Look up an order by id.'
try:
return api.get(order_id)
except NotFound:
return 'No order found with that id.'Reusability
Toolkits make integrations portable: build a GitHubToolkit once and drop it into any agent. Combine multiple toolkits to give an agent broad, well-defined powers.
Quick Check
Test your tools knowledge.
Recap
You learned to build robust integrations:
- Structured tools accept multiple typed arguments via an
args_schema - The
@tooldecorator infers schemas from hints - Clear descriptions drive correct tool use
- Toolkits bundle related tools for reuse
- Validation and error handling keep agents stable
Well-structured tools are the backbone of dependable agent integrations.
常见问题解答
「工具包与结构化工具输入」课时是免费的吗?
是的 — 「工具包与结构化工具输入」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Agents with LangChain & Autonomous Workflows 课程的其余内容,请升级到 CoddyKit PRO。 AI Agents with LangChain & Autonomous Workflows 课程共包含 4 节课。
「工具包与结构化工具输入」这节课中我会学到什么?
将相关工具打包成可复用的工具包,并使用模式定义结构化且带类型的参数,让代理能够以正确参数可靠地调用工具。 你通过在浏览器中直接运行的动手代码来练习 AI Agents with LangChain & Autonomous Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Agents with LangChain & Autonomous Workflows 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Agents with LangChain & Autonomous Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「工具包与结构化工具输入」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Agents with LangChain & Autonomous Workflows 课中编写并运行代码吗?
能。每节 AI Agents with LangChain & Autonomous Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 创建自定义 LangChain 工具
- 与外部 API 集成
- 网页抓取与数据增强
- 工具包与结构化工具输入