用途:介绍使用 LangChain 构建智能体的基础方法,核心是 create_agent(),它负责智能体循环、工具执行与状态管理。
主要能力:create_agent 支持配置 model(如 "anthropic:claude-sonnet-4-5" 或模型实例)、tools 工具列表、system_prompt/systemPrompt 指令、checkpointer 状态持久化与 middleware 处理钩子。工具用 Python 的 @tool 装饰器或 TypeScript 的 tool() 函数定义,需提供清晰描述与参数说明。
记忆与状态:加入 MemorySaver 检查点并传入 thread_id 配置,可在多次调用间保持对话记忆,例如 config = {"configurable": {"thread_id": "user-123"}}。
中间件:用于人工审批、错误处理与日志。HumanInTheLoopMiddleware(Python)/ humanInTheLoopMiddleware(TypeScript)实现审批流程,需配合 checkpointer 与 thread_id;中断后可用 agent.invoke(Command(resume={"decisions": [{"type": "approve"}]}), config=config) 恢复。自定义钩子用 @wrap_tool_call(Python)或 createMiddleware(TypeScript)。
结构化输出:可用 response_format 或 with_structured_output() 获取带类型校验的响应,例如定义 ContactInfo 模型后调用。
限制与注意:需设置 recursion_limit/recursionLimit 防止循环失控;结果内容应从 result["messages"][-1].content 或 result.messages 末项读取,而非直接访问 result.content。资料未提供安装命令、收费与平台支持等细节。