快速入门
本快速入门将带您从简单设置到构建一个功能完整的 AI 代理,仅需几分钟。
构建基本代理
首先创建一个简单的代理,它可以回答问题并调用工具。该代理将使用 Claude Sonnet 4.5 作为语言模型,一个基本的天气函数作为工具,以及一个简单的提示来指导其行为。
from langchain.agents import create_agent
def get_weather(city: str) -> str:
"""获取指定城市的天气。"""
return f"{city}总是阳光明媚!"
agent = create_agent(
model="anthropic:claude-sonnet-4-5",
tools=[get_weather],
system_prompt="你是一个乐于助人的助手",
)
# 运行代理
agent.invoke(
{"messages": [{"role": "user", "content": "旧金山的天气怎么样"}]}
)注意
对于此示例,您需要设置一个 Claude (Anthropic) 账户并获取 API 密钥。然后在终端中设置ANTHROPIC_API_KEY环境变量。
构建真实世界的代理
接下来,构建一个实用的天气预报代理,展示关键的生产概念:
- 详细的系统提示 以获得更好的代理行为
- 创建工具 与外部数据集成
- 模型配置 以确保响应一致性
- 结构化输出 以获得可预测的结果
- 对话记忆 以实现类似聊天的交互
- 创建并运行代理 构建一个功能完整的代理
让我们逐步完成每一步:
定义系统提示
系统提示定义了代理的角色和行为。保持其具体且可操作:
SYSTEM_PROMPT = """你是一位擅长用双关语表达的专家天气预报员。
你可以使用两个工具:
- get_weather_for_location:用于获取特定地点的天气
- get_user_location:用于获取用户的位置
如果用户询问天气,请确保你知道具体位置。如果从问题中可以判断他们指的是自己所在的位置,请使用 get_user_location 工具来查找他们的位置。"""创建工具
工具 允许模型通过调用您定义的函数与外部系统交互。
工具可以依赖于运行时上下文,也可以与代理记忆交互。
请注意下面的 get_user_location 工具如何使用运行时上下文:
from dataclasses import dataclass
from langchain.tools import tool, ToolRuntime
@tool
def get_weather_for_location(city: str) -> str:
"""获取指定城市的天气。"""
return f"{city}总是阳光明媚!"
@dataclass
class Context:
"""自定义运行时上下文模式。"""
user_id: str
@tool
def get_user_location(runtime: ToolRuntime[Context]) -> str:
"""根据用户 ID 获取用户信息。"""
user_id = runtime.context.user_id
return "Florida" if user_id == "1" else "SF"提示
工具应有良好的文档说明:其名称、描述和参数名称将成为模型提示的一部分。
LangChain 的@tool装饰器 会添加元数据,并通过ToolRuntime参数启用运行时注入。
配置模型
from langchain.chat_models import init_chat_model
model = init_chat_model(
"anthropic:claude-sonnet-4-5",
temperature=0.5,
timeout=10,
max_tokens=1000
)定义响应格式
(可选)如果需要代理响应符合特定模式,请定义结构化响应格式。
from dataclasses import dataclass
# 这里使用 dataclass,但也支持 Pydantic 模型。
@dataclass
class ResponseFormat:
"""代理的响应模式。"""
# 带双关语的回应(始终必需)
punny_response: str
# 天气的任何有趣信息(如果有)
weather_conditions: str | None = None添加记忆
向代理添加记忆,以在多次交互中保持状态。这允许代理记住之前的对话和上下文。
from langgraph.checkpoint.memory import InMemorySaver
checkpointer = InMemorySaver()注意
在生产环境中,请使用持久化的检查点保存到数据库。
详见 添加和管理记忆。
创建并运行代理
现在将所有组件组装成代理并运行它!
agent = create_agent(
model=model,
system_prompt=SYSTEM_PROMPT,
tools=[get_user_location, get_weather_for_location],
context_schema=Context,
response_format=ResponseFormat,
checkpointer=checkpointer
)
# `thread_id` 是给定对话的唯一标识符。
config = {"configurable": {"thread_id": "1"}}
response = agent.invoke(
{"messages": [{"role": "user", "content": "外面的天气怎么样?"}]},
config=config,
context=Context(user_id="1")
)
print(response['structured_response'])
# ResponseFormat(
# punny_response="佛罗里达今天依然是'阳光灿烂'的一天!阳光正在播放'rey-dio'热门歌曲!我得说,这是进行'solar-bration'的完美天气!如果你希望下雨,恐怕这个想法已经'被冲走'了——预报仍然'清晰地'灿烂!",
# weather_conditions="佛罗里达总是阳光明媚!"
# )
# 注意,我们可以使用相同的 `thread_id` 继续对话。
response = agent.invoke(
{"messages": [{"role": "user", "content": "谢谢!"}]},
config=config,
context=Context(user_id="1")
)
print(response['structured_response'])
# ResponseFormat(
# punny_response="你真是'雷'厉风行地欢迎!帮助你保持'当前'天气总是'轻而易举'。我只是'云'游四方,等待随时'淋浴'你更多预报。祝你在佛罗里达的阳光下度过'sun-sational'的一天!",
# weather_conditions=None
# )完整示例代码
from dataclasses import dataclass
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from langchain.tools import tool, ToolRuntime
from langgraph.checkpoint.memory import InMemorySaver
# 定义系统提示
SYSTEM_PROMPT = """你是一位擅长用双关语表达的专家天气预报员。
你可以使用两个工具:
- get_weather_for_location:用于获取特定地点的天气
- get_user_location:用于获取用户的位置
如果用户询问天气,请确保你知道具体位置。如果从问题中可以判断他们指的是自己所在的位置,请使用 get_user_location 工具来查找他们的位置。"""
# 定义上下文模式
@dataclass
class Context:
"""自定义运行时上下文模式。"""
user_id: str
# 定义工具
@tool
def get_weather_for_location(city: str) -> str:
"""获取指定城市的天气。"""
return f"{city}总是阳光明媚!"
@tool
def get_user_location(runtime: ToolRuntime[Context]) -> str:
"""根据用户 ID 获取用户信息。"""
user_id = runtime.context.user_id
return "Florida" if user_id == "1" else "SF"
# 配置模型
model = init_chat_model(
"anthropic:claude-sonnet-4-5",
temperature=0
)
# 定义响应格式
@dataclass
class ResponseFormat:
"""代理的响应模式。"""
# 带双关语的回应(始终必需)
punny_response: str
# 天气的任何有趣信息(如果有)
weather_conditions: str | None = None
# 设置记忆
checkpointer = InMemorySaver()
# 创建代理
agent = create_agent(
model=model,
system_prompt=SYSTEM_PROMPT,
tools=[get_user_location, get_weather_for_location],
context_schema=Context,
response_format=ResponseFormat,
checkpointer=checkpointer
)
# 运行代理
# `thread_id` 是给定对话的唯一标识符。
config = {"configurable": {"thread_id": "1"}}
response = agent.invoke(
{"messages": [{"role": "user", "content": "外面的天气怎么样?"}]},
config=config,
context=Context(user_id="1")
)
print(response['structured_response'])
# ResponseFormat(
# punny_response="佛罗里达今天依然是'阳光灿烂'的一天!阳光正在播放'rey-dio'热门歌曲!我得说,这是进行'solar-bration'的完美天气!如果你希望下雨,恐怕这个想法已经'被冲走'了——预报仍然'清晰地'灿烂!",
# weather_conditions="佛罗里达总是阳光明媚!"
# )
# 注意,我们可以使用相同的 `thread_id` 继续对话。
response = agent.invoke(
{"messages": [{"role": "user", "content": "谢谢!"}]},
config=config,
context=Context(user_id="1")
)
print(response['structured_response'])
# ResponseFormat(
# punny_response="你真是'雷'厉风行地欢迎!帮助你保持'当前'天气总是'轻而易举'。我只是'云'游四方,等待随时'淋浴'你更多预报。祝你在佛罗里达的阳光下度过'sun-sational'的一天!",
# weather_conditions=None
# )恭喜!您现在拥有一个 AI 代理,它可以:
- 理解上下文 并记住对话
- 智能使用多个工具
- 提供结构化响应,格式一致
- 通过上下文处理用户特定信息
- 跨交互维护对话状态