SuperAPI AI 工具集

langchain

智能体与模型

查看项目

更新于 10-15 23:48

技能介绍

用途:LangChain 是开源框架,提供预置智能体(agent)架构,并为任意模型或工具提供集成,可用不到 10 行代码构建智能体与 LLM 应用,已集成 OpenAI、Anthropic、Google 等数百种服务。

主要能力:使用 create_agent() 与预置智能体循环构建工具调用智能体;通过 init_chat_model() 在不改应用代码的情况下切换模型提供商;为 LLM 响应添加结构化输出,解析为带类型对象;借助各提供商包集成任意模型或工具;使用中间件处理限流、缓存等横切关注点。

使用方式:Python 安装 pip install -U langchain;JavaScript/TypeScript 安装 npm install langchain @langchain/core。安装提供商集成:Python 如 pip install -U langchain-openai(或 langchain-anthropic、langchain-google-genai);JavaScript/TypeScript 如 npm install @langchain/openai(或 @langchain/anthropic、@langchain/google-genai)。用 create_agent(model=..., tools=[...], system_prompt=...) 创建智能体并调用 agent.invoke(...);用 init_chat_model("openai:gpt-5.5") 等字符串切换提供商,格式为 "provider:model-name";用 @tool 装饰器定义工具。

适用场景:需要工具调用智能体、切换模型提供商、结构化输出、集成任意模型或工具时使用。

限制与注意事项:工具函数名必须是合法 Python 标识符,用 snake_case,如 get_weather 而非 get-weather;工具参数不得命名为 type、name、description,以免与工具 schema 冲突;模型位于独立提供商包中,基础 langchain 包不含提供商;init_chat_model() 使用 "provider:model-name" 格式。复杂多步工作流与自定义控制流应改用 LangGraph;需要规划、子智能体和上下文管理的开箱即用智能体应改用 Deep Agents。LangChain 提供核心构建块,LangGraph 增加编排,Deep Agents 在其上提供高层能力。相关技能:langgraph、deep-agents、langsmith。API 参考可浏览 https://reference.langchain.com/python/langchain-core,MCP 服务器见 https://reference.langchain.com/mcp。