用途:介绍使用 LangChain 构建智能体的基础方法,核心是 create_agent(),它负责智能体循环、工具执行与状态管理。
主要能力:create_agent 支持 model(模型字符串如 "anthropic:claude-sonnet-4-5" 或模型实例)、tools(工具列表)、system_prompt/systemPrompt(指令)、checkpointer(状态持久化,如 MemorySaver())、middleware(处理钩子)。工具用 Python 的 @tool 装饰器或 TypeScript 的 tool() 函数定义,需提供清晰具体的描述与参数说明。中间件可拦截智能体循环,实现人工审批、错误处理与日志;人工介入用 HumanInTheLoopMiddleware(Python)/ humanInTheLoopMiddleware(TypeScript),自定义钩子用 @wrap_tool_call(Python)/ createMiddleware(TypeScript)。结构化输出可通过 response_format 或 with_structured_output() 获取带类型校验的响应。
使用方式:Python 从 langchain.agents 导入 create_agent,调用 agent.invoke({"messages": [...]}),结果从 result["messages"][-1].content 读取;TypeScript 从 "langchain" 导入 createAgent,用 await agent.invoke(...)。持久化需配置 checkpointer 与 thread_id;人工审批需 checkpointer 加 thread_id,恢复用 Command(resume={"decisions": [{"type": "approve"}]})。
限制与注意:无 checkpointer 时智能体不记忆;应设置 recursion_limit/recursionLimit 防止无限循环;不要直接访问 result.content,需从消息列表取最后一条。资料未提供安装命令、收费与平台信息。