用途:本技能介绍如何用 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 中 agent = create_agent(model=..., tools=[...], system_prompt=...) 后调用 agent.invoke({"messages": [...]}),从 result["messages"][-1].content 取结果;TypeScript 用 createAgent 与 await agent.invoke。记忆需加 checkpointer 与 thread_id;人工介入需 checkpointer 加 thread_id,恢复用 agent.invoke(Command(resume={"decisions": [{"type": "approve"}]}), config=config)。
限制与注意:无持久化时智能体不记忆;应设置 recursion_limit/recursionLimit 防止循环失控;结果需从 messages 数组读取而非 result.content。具体收费、平台与效果资料未提供。