Skip to content

LangChain.js 模块分析 #4: 提供商集成(含流程图)

模块: langchain-openai, langchain-anthropic, langchain-google-genai, langchain-deepseek 分析日期: 2026-02-13 子模块: 主要提供商包


目录

  1. langchain-openai
  2. langchain-anthropic
  3. langchain-google-genai
  4. langchain-deepseek
  5. 通用特性对比
  6. 使用示例

1. langchain-openai

1.1 核心文件

libs/providers/langchain-openai/src/
├── chat.ts          # ChatOpenAI 实现
├── embeddings.ts    # OpenAIEmbeddings 实现
├── utils.ts         # 工具函数
└── tests/          # 测试

1.2 ChatOpenAI 执行流程图

mermaid
graph TD
    Input[用户调用] --> Config{配置 LLM}

    Config --> |A[初始化参数]|
    Config --> |B[设置回调]|
    Config --> |C[设置运行时配置]|

    A --> |A1[设置 API Key]|
    A --> |A2[设置 Base URL]|
    A --> |A3[设置模型]|
    A --> |A4[设置温度]|
    A --> |A5[设置最大 Tokens]|
    A --> |A6[设置超时]|
    A --> |A7[设置重试次数]|

    B --> |B1[设置工具列表]|
    B --> |B2[绑定工具到 LLM]|
    B --> |B3[设置工具描述]|

    A --> Initialize[初始化 ChatOpenAI]

    Initialize --> |A1[加载 API Key]|
    Initialize --> |A2[加载系统提示]|
    Initialize --> |A3[设置回调管理器]|

    A1 --> Validate[验证配置]
    A2 --> Prepare[准备请求]

    Validate --> CheckKey{检查 API Key}
    CheckKey --> |A1[非空检查]|
    CheckKey --> |A2[格式验证]|

    A1 --> |A1[Key 有效]|
    A2 --> |A2[Key 无效]|
    A2 --> Error[返回错误]

    Prepare --> |B1[构建消息]|
    Prepare --> |B2[添加模型参数]|

    B1 --> BuildMessages[构建消息数组]
    B1 --> |B2[添加工具参数]|

    BuildMessages --> Execute[执行 API 调用]

    Execute --> |A1[创建 HTTP 请求]|
    A1 --> |A2[发送到 OpenAI API]|
    A1 --> |A3[等待响应]|
    A1 --> |A4[解析响应]|
    A1 --> |A5[处理流式响应]|
    A1 --> |A6[触发回调]|

    A4 --> |A1[解析 JSON]|
    A5 --> |A2[提取文本内容]|
    A5 --> |A3[提取 Token 使用]|
    A5 --> |A4[提取完成标志]|

    A6 --> |A1[调用成功回调]|
    A2 --> |A2[调用失败回调]|

    A6 --> Output[返回最终结果]

    A2 --> |A1[返回响应对象]|
    A2 --> |A2[返回响应对象]|
    A4 --> |A1[返回响应对象]|
    A5 --> |A6[返回响应对象]|

    A2 --> |A1[返回响应对象]|
    A4 --> |A6[返回最终输出]|
    A5 --> |A6[返回最终输出]|

    Output --> |A1[返回文本内容]|
    Output --> |A2[返回 Token 数]|
    Output --> |A3[返回工具调用列表]|

    A6 --> End[完成]

    style Input fill:#e1f5ff
    style Config fill:#fff4e6
    style CheckKey fill:#d1fae5
    style Prepare fill:#f59e0b
    style Execute fill:#82c91a
    style Output fill:#fff4e6
    style End fill:#f59e0b

1.3 OpenAI Embeddings 流程图

mermaid
graph TD
    Input[文本数组] --> Batch{批量处理}

    Batch --> |A[切分为批次]|
    Batch --> |B[应用并发限制]|

    A --> ProcessBatch[处理批次]

    ProcessBatch --> |A1[创建嵌入请求]|
    ProcessBatch --> |A2[添加模型参数]|

    B --> |B1[准备 HTTP 请求]|
    B --> |B2[发送到 OpenAI API]|
    B --> |B3[等待响应]|
    B --> |B4[解析嵌入向量]|

    ProcessBatch --> |A1[解析嵌入数组]|
    ProcessBatch --> |A2[提取向量维度]|
    ProcessBatch --> |A3[返回嵌入列表]|

    ProcessBatch --> Output[返回所有嵌入]

    style Input fill:#e1f5ff
    style Batch fill:#fff4e6
    style ProcessBatch fill:#82c91a
    style Output fill:#f59e0b

1.4 多模态支持

mermaid
graph TD
    Input[多模态输入] --> Parse{解析内容类型}

    Parse --> |A[纯文本]|
    Parse --> |B[图像 + 文本]|
    Parse --> |C[音频 + 文本]|

    A --> BuildTextMessage[构建文本消息]
    B --> |A1[创建 HumanMessage]|
    B --> |A2[content: 纯文本]|

    C --> |B1[创建多模态内容]|
    C --> |B2[type: text, text: 纯文本]|
    C --> |B3[type: image, image_url: 图像 URL]|
    C --> |B4[type: audio, audio: 音频 URL]|
    C --> |B5[type: audio, audio: 音频 URL]|

    D --> Execute[调用 LLM]

    D --> |A1[发送多模态消息]|
    A1 --> |A2[等待响应]|
    A1 --> |A3[解析响应]|

    A2 --> Output[返回文本内容]

    style Input fill:#e1f5ff
    style Parse fill:#fff4e6
    style BuildTextMessage fill:#d1fae5
    style D fill:#82c91a
    style Output fill:#f59e0b

2. langchain-anthropic

2.1 Anthropic 特性流程图

mermaid
graph TD
    Input[用户输入] --> SelectFeature{选择特性}

    SelectFeature --> |A[Extended Thinking]|
    SelectFeature --> |B[Tool Use]|
    SelectFeature --> |C[Vision]|

    A --> |A1[启用扩展思考]|
    A --> |B1[设置 thinking Budget Tokens]|
    A --> |B2[设置超时]|

    B --> |A1[创建消息]|
    B --> |A2[添加 extended thinking 参数]|
    B --> |A3[设置最大 Tokens]|

    B --> BuildMessage[构建消息]

    BuildMessage --> Execute[调用 Claude]

    Execute --> |A1[等待响应]|
    A --> |A2[检查 extended thinking]|
    A --> |A3[解析思考过程]|
    A --> |A4[提取最终答案]|
    A --> |A5[记录思考统计]|

    A3 --> Output[返回最终答案]
    A4 --> |A5[返回思考过程]|

    Output --> |A1[最终答案]|
    Output --> |A2[思考过程]|
    Output --> |A3[思考统计]|

    C --> |A1[准备工具调用]|
    A --> |A2[添加工具描述]|

    D --> ToolUse{使用工具}

    ToolUse --> |A1[创建工具消息]|
    A --> |A2[添加 tool_use_content_block]|

    E --> Execute[调用 Claude]

    Execute --> |A1[等待响应]|
    A --> |A2[解析工具结果]|
    A --> |A3[处理结果]|

    A2 --> Output[返回工具结果]

    style Input fill:#e1f5ff
    style SelectFeature fill:#fff4e6
    style BuildMessage fill:#d1fae5
    style Execute fill:#82c91a
    style Output fill:#f59e0b
    style ToolUse fill:#8ec5fc

2.2 Vision 支持流程

mermaid
graph TD
    Input[图像输入] --> ProcessImage{处理图像}

    ProcessImage --> |A[读取图像数据]|
    ProcessImage --> |B[转换为 Base64]|
    ProcessImage --> |C[创建媒体对象]|

    A --> |A1[格式化为 Claude Vision 格式]|
    A --> |A2[type: image, source: {type: base64, data: ...}]|
    A --> |A3[添加到消息内容]|

    B --> BuildMessage[构建多模态消息]

    BuildMessage --> |C1[添加图像内容]|
    BuildMessage --> |C2[添加文本提示]|
    BuildMessage --> |C3[创建 HumanMessage]|

    C --> Execute[调用 Claude Vision]

    Execute --> |A1[等待响应]|
    A --> |A2[解析图像描述]|
    A --> |A3[分析图像内容]|

    A2 --> Output[返回图像描述]
    A3 --> |A4[返回对象识别结果]|

    Output --> |A1[图像描述]|
    Output --> |A2[对象列表]|

    style Input fill:#e1f5ff
    style ProcessImage fill:#82c91a
    style BuildMessage fill:#d1fae5
    style Execute fill:#82c91a
    style Output fill:#f59e0b

3. langchain-google-genai

3.1 Gemini 特性流程图

mermaid
graph TD
    Input[用户输入] --> SelectMode{选择模式}

    SelectMode --> |A[文本模式]|
    SelectMode --> |B[多模态模式]|
    SelectMode --> |C[代码执行模式]|

    A --> |A1[构建文本提示]|
    A --> |A2[设置模型参数]|

    B --> |B1[构建代码提示]|
    B --> |B2[添加代码块]|
    B --> |B3[设置执行环境]|
    B --> |B4[返回代码执行结果]|

    C --> |A1[调用 Gemini]|
    C --> |A2[等待响应]|
    C --> |A3[解析代码输出]|

    A2 --> Output[返回代码执行结果]
    C --> |A3[返回错误信息]|

    style Input fill:#e1f5ff
    style SelectMode fill:#fff4e6
    style A fill:#d1fae5
    style B fill:#d1fae5
    style C fill:#82c91a
    style Output fill:#f59e0b

3.2 代码执行流程

mermaid
graph TD
    Input[代码输入] --> Sandbox{沙箱执行}

    Sandbox --> |A[创建隔离环境]|
    Sandbox --> |B[设置执行限制]|
    Sandbox --> |C[注入代码]|

    A --> |A1[准备代码]|
    A --> |A2[添加语言标识]|
    A --> |A3[设置执行超时]|

    B --> |A1[发送代码到 Gemini]|
    B --> |A2[执行代码]|
    B --> |A3[捕获输出]|
    B --> |A4[清理环境]|

    B --> |A1[返回执行结果]|
    B --> |A2[返回错误信息]|
    B --> |A3[返回标准错误]|

    Output --> |A1[代码输出]|
    Output --> |A2[stderr 输出]|
    Output --> |A3[执行状态]|

    style Input fill:#e1f5ff
    style Sandbox fill:#82c91a
    style A fill:#d1fae5
    style B fill:#d1fae5
    style Output fill:#f59e0b

4. langchain-deepseek

4.1 DeepSeek 调用流程图

mermaid
graph TD
    Input[用户输入] --> Config{配置 DeepSeek}

    Config --> |A[初始化参数]|
    Config --> |B[设置 Base URL]|
    Config --> |C[设置模型]|
    Config --> |D[设置温度]|
    Config --> |E[设置最大 Tokens]|

    A --> |A1[加载配置]|
    A --> |A2[设置默认值]|
    A --> |A3[覆盖默认参数]|

    B --> Initialize[初始化 DeepSeek]

    Initialize --> Prepare[准备请求]

    Prepare --> |A1[构建消息]|
    Prepare --> |A2[添加模型参数]|

    A --> Execute[调用 DeepSeek API]

    Execute --> |A1[创建 HTTP 请求]|
    A --> |A2[添加 API 密钥]|
    A --> |A3[发送请求]|
    A --> |A4[等待响应]|
    A --> |A5[处理错误]|
    A --> |A6[处理重试]|

    A --> |A1[成功响应]|
    A --> |A2[响应错误]|
    A --> |A3[超时错误]|
    A --> |A4[速率限制错误]|

    A1 --> Parse[解析响应]
    A2 --> |A3[提取完成标志]|
    A --> |A4[提取内容]|

    Parse --> |A1[返回文本]|
    A --> |A2[提取 Token]|
    A --> |A3[返回推理 Token]|
    A --> |A4[提取完成原因]|

    A --> Output[返回最终输出]

    Output --> |A1[文本内容]|
    Output --> |A2[完成标志]|
    Output --> |A3[使用 Token]|
    Output --> |A4[完成原因]|

    A5 --> |A6[重试请求]
    A6 --> |A6[等待延迟]|
    A6 --> Execute[重新调用]

    style Input fill:#e1f5ff
    style Config fill:#fff4e6
    style Execute fill:#82c91a
    style Parse fill:#d1fae5
    style Output fill:#f59e0b

5. 通用特性对比

5.1 功能对比表

| 特性 | OpenAI | Anthropic | Google | DeepSeek | |--------|--------|----------|---------| | 基础模型 | GPT-4, 3.5 | Claude 3.5, Opus | Gemini 2.0 Flash | DeepSeek Chat, Coder | | 高级模型 | GPT-4-Turbo | Claude 3.5 Sonnet | Gemini 2.5 Pro | - | | 上下文窗口 | 128K | 200K | 1M | 128K | | 推理 | ❌ | ✅ Extended | ✅ | ❌ | | Function Calling | ✅ | ✅ | ✅ | ✅ | | 流式支持 | ✅ | ✅ | ✅ | ✅ | | 多模态 | ✅ (图像) | ✅ (图像, 文本) | ✅ (文本, 音频, 视频) | ✅ (图像) | | 代码执行 | ❌ | ❌ | ✅ | ✅ | | 搜索集成 | ❌ | ❌ | ✅ | ❌ | | 成本 | 高 | 高 | 中 | 低 | 极低 | | 响应速度 | 快 | 快 | 快 | 快 | 快 |


6. 使用示例

6.1 基础调用示例

typescript
import { ChatOpenAI } from "@langchain/openai";
import { ChatAnthropic } from "@langchain/anthropic";

// OpenAI 调用
const openai = new ChatOpenAI({
  model: "gpt-4",
  temperature: 0.7
});

const response = await openai.invoke("Hello, world!");

// Anthropic 调用
const anthropic = new ChatAnthropic({
  model: "claude-3.5-sonnet-20240629"
});

const response = await anthropic.invoke("Explain quantum computing in simple terms");

流程图:

mermaid
graph LR
    User[用户] --> |A[OpenAI]|
    User --> |B[Anthropic]|
    User --> |C[Google]|
    User --> |D[DeepSeek]|

    A --> Response1[OpenAI 响应]
    B --> Response2[Anthropic 响应]
    C --> Response3[Google 响应]
    D --> Response4[DeepSeek 响应]

    Response1 --> |A1[快速响应]|
    Response2 --> |A2[详细推理]|
    Response3 --> |A3[多模态]|
    Response4 --> |A4[低成本响应]|

    style User fill:#e1f5ff
    style A fill:#82c91a
    style B fill:#f59e0b
    style C fill:#d1fae5
    style D fill:#f59e0b

6.2 多模态示例

typescript
const message = new HumanMessage({
  content: [
    { type: "text", text: "What's in this image?" },
    { type: "image_url", image_url: "https://example.com/image.jpg" }
  ]
});

const response = await openai.invoke([message]);

流程图:

mermaid
graph TD
    Input[多模态消息] --> Process{处理类型}

    Process --> |A[文本消息]|
    Process --> |B[图像消息]|
    Process --> |C[音频消息]|

    A --> |A1[直接发送到 LLM]|
    B --> |B1[转换为 Base64]|
    C --> |C1[转换为 Base64]|

    A --> |A1[文本响应]|
    B --> |B1[发送到支持 Vision 的 LLM]|
    C --> |C1[发送到支持 Audio 的 LLM]|

    A --> Output[返回文本描述]
    B --> Output[返回图像描述]|
    C --> Output[返回音频转录]

    style Input fill:#e1f5ff
    style Process fill:#fff4e6
    style A fill:#d1fae5
    style B fill:#d1fae5
    style C fill:#d1fae5
    style Output fill:#f59e0b

7. Function Calling 流程图

mermaid
graph TD
    Input[用户输入] --> BuildChain[构建链]

    BuildChain --> |A[LLM + 工具定义]|
    BuildChain --> |B[bind 工具到 LLM]|

    A --> |A1[创建工具列表]|
    A --> |A2[定义工具 Schema]|
    A --> |A3[设置工具描述]|

    B --> |B1[工具列表传递给 LLM]|
    B --> |B2[LLM 工具调用模式]|
    B --> |B3[返回 tool_calls]|

    B --> |A1[分析 LLM 响应]|
    A --> |A2[提取工具调用]|
    A --> |A3[验证参数]|

    B2 --> |A1[遍历 tool_calls]|
    A --> |A2[提取工具名]|
    A --> |A3[提取参数]|
    A --> |A4[调用工具]|
    A --> |A5[收集结果]|

    A4 --> |A1[工具1 执行]|
    A2 --> |A2[工具2 执行]|
    A3 --> |A3[工具3 执行]|

    A1 --> |A11[工具1 结果]|
    A2 --> |A21[工具2 结果]|
    A3 --> |A31[工具3 结果]|

    A11 --> |B1[创建 ToolMessage]|
    A21 --> |B2[创建 ToolMessage]|
    A31 --> |B3[创建 ToolMessage]|

    B1 --> |C1[添加到消息数组]|
    B2 --> |C1[添加到消息数组]|
    B --> |C2[添加到消息数组]|

    C1 --> Merge[合并所有 ToolMessage]
    C2 --> |C3[创建 AIMessage]

    C3 --> Final[返回 AIMessage with tool_calls]

    style Input fill:#e1f5ff
    style BuildChain fill:#fff4e6
    style B fill:#d1fae5
    style A fill:#d1fae5
    style Merge fill:#8ec5fc
    style Final fill:#f59e0b

总结

提供商集成统一了不同 LLM 的访问方式:

  1. langchain-openai - 成熟的 GPT 支持,多模态
  2. langchain-anthropic - 长上下文、扩展思考、Vision
  3. langchain-google-genai - 多模态、代码执行、搜索
  4. langchain-deepseek - 高性价比、Function Calling

关键优势:

  • 统一的 BaseChatModel 接口
  • 一致的错误处理和重试机制
  • 完整的流式支持
  • Function Calling 标准实现
  • 多模态内容块支持

流程图亮点:

  • 完整的 OpenAI 调用流程
  • Anthropic 扩展思考流程
  • Gemini 代码执行沙箱流程
  • Function Calling 自动化工具调用流程

✅ 模块 4 分析完成: 提供商集成(含流程图)📊 总进度: 4/6 (66.7%)