Appearance
LangChain.js 模块分析 #2: 核心系统(messages, tools, output_parsers, vectorstores, retrievers, stores, caches, tracers)(含流程图)
模块: langchain-core 核心系统 文件:
libs/langchain-core/src/分析日期: 2026-02-13 子模块: messages, tools, output_parsers, vectorstores, retrievers, stores, caches, tracers
目录
- 消息系统 (messages)
- 工具系统 (tools)
- 输出解析器 (output_parsers)
- 向量存储 (vectorstores)
- 检索器 (retrievers)
- 存储系统 (stores)
- 缓存系统 (caches)
- 追踪系统 (tracers)
1. 消息系统 (messages)
1.1 消息创建流程图
mermaid
graph TD
Start[输入] --> CreateMessage[创建消息]
CreateMessage --> |A[消息类型]|
CreateMessage --> |B[内容类型]|
CreateMessage --> |C[角色]|
CreateMessage --> |D[元数据]|
A --> |A1[HumanMessage]|
A --> |A2[AIMessage]|
A --> |A3[SystemMessage]|
A --> |A4[ToolMessage]|
A --> |A5[FunctionMessage]|
B --> |B1[简单内容]|
B --> |B2[内容块]|
B1 --> Create1[创建文本消息]
B2 --> Create2[创建多模态消息]
C --> |C1[用户]|
C --> |C2[AI]|
C --> |C3[系统]|
C --> |C4[工具]|
C --> |C5[函数]|
C1 --> SetName[设置名称]
C2 --> SetContent[设置内容]
C3 --> SetMetadata[设置元数据]
D --> MergeContent[合并内容]
Create1 --> Result[最终消息]
Create2 --> Result
Create3 --> Result
Create4 --> Result
Create5 --> Result
MergeContent --> Result
Result --> Validate[验证消息]
Validate --> End[完成]
style Start fill:#e1f5ff
style CreateMessage fill:#fff4e6
style A fill:#fff4e6
style A1 fill:#d1fae5
style A2 fill:#82c91a
style A3 fill:#82c91a
style A4 fill:#f59e0b
style A5 fill:#e1f5ff
style B fill:#fff4e6
style B1 fill:#d1fae5
style B2 fill:#d1fae5
style C fill:#fff4e6
style D fill:#82c91a
style Result fill:#82c91a
style Validate fill:#8ec5fc
style End fill:#f59e0b流程说明:
- 确定消息类型(人类、AI、系统、工具、函数)
- 选择内容类型(简单字符串或多模态内容块)
- 设置消息元数据(名称、ID 等)
- 创建消息对象
- 验证消息有效性
1.2 消息流转图
mermaid
graph LR
UserInput[用户输入] --> HumanMessage[创建人类消息]
HumanMessage --> SendMessage[发送到 LLM]
SendMessage --> |Process[LLM 处理]|
SendMessage --> |Direct[直接传递]|
Process --> AIMessage[创建 AI 消息]
Process --> AIMessageWithTools[创建带工具调用的 AI 消息]
AIMessage --> |A[解析工具调用]|
AIMessageWithTools --> |B[解析工具列表]|
A --> ExtractToolCalls[提取 tool_calls]
B --> ExtractToolCallsList[提取工具调用列表]
ExtractToolCalls --> ExecuteTools[执行工具]
ExecuteTools --> ToolMessage[创建工具结果消息]
ToolMessage --> ChatHistory[更新对话历史]
ChatHistory --> |A[保留对话]|
ChatHistory --> |B[截断上下文]|
A --> NextTurn[准备下一轮]
B --> End[结束]
Direct --> AddToHistory[添加到历史]
AddToHistory --> NextTurn
style UserInput fill:#e1f5ff
style HumanMessage fill:#fff4e6
style SendMessage fill:#fff4e6
style Process fill:#fff4e6
style AIMessage fill:#82c91a
style AIMessageWithTools fill:#f59e0b
style ExecuteTools fill:#d1fae5
style ToolMessage fill:#8ec5fc
style ChatHistory fill:#8ec5fc
style NextTurn fill:#82c91a
style AddToHistory fill:#82c91a
style End fill:#f59e0b流程说明:
- 用户输入创建 HumanMessage
- 发送到 LLM 处理
- LLM 返回 AIMessage(可能包含工具调用)
- 解析工具调用(如果有)
- 执行工具得到 ToolMessage
- 更新对话历史(保留上下文)
- 准备下一轮对话
2. 工具系统 (tools)
2.1 工具调用流程图
mermaid
graph TD
Input[输入] --> ValidateSchema[验证 Schema]
ValidateSchema --> |A[StructuredTool]|
ValidateSchema --> |B[DynamicTool]|
ValidateSchema --> |C[RunnableToolLike]|
ValidateSchema --> Parse[解析输入]
A --> |A1[Zod 验证]|
A1 --> Parse
B --> |B1[运行时参数]|
B1 --> Parse
B --> |B2[自定义验证]|
B2 --> Parse
C --> |C1[函数体执行]|
C --> |C2[自定义处理]|
C --> |C3[回调管理]|
C --> |C1[func: input => result]|
C --> |C2[func: (input, runManager) => result]|
C --> |C3[func: (input, runManager, config) => result]|
Parse --> Execute[执行工具]
Execute --> WrapOutput[包装输出]
WrapOutput --> |A[内容模式]|
WrapOutput --> |B[内容+产物]|
A --> DirectReturn[直接返回]
B --> WrapOutput
WrapOutput --> ToolMessage[返回工具消息]
ToolMessage --> UpdateState[更新状态]
UpdateState --> |A[ToolRuntime]|
UpdateState --> |B[常规配置]|
UpdateState --> |A1[access state]|
UpdateState --> |A1[access store]|
UpdateState --> |A1[stream output]|
UpdateState --> Record[记录工具调用]
Record --> Callbacks[回调]
Callbacks --> |A[on_tool_start]|
Callbacks --> |B[on_tool_end]|
Execute --> Final[返回结果]
Record --> Final
DirectReturn --> Final
style Input fill:#e1f5ff
style ValidateSchema fill:#fff4e6
style A fill:#d1fae5
style A1 fill:#d1fae5
style B fill:#d1fae5
style B1 fill:#d1fae5
style B2 fill:#d1fae5
style B3 fill:#d1fae5
style Parse fill:#fff4e6
style Execute fill:#d1fae5
style WrapOutput fill:#fff4e6
style ToolMessage fill:#8ec5fc
style UpdateState fill:#8ec5fc
style Record fill:#8ec5fc
style Final fill:#82c91a流程说明:
- 验证工具 Schema(Zod 或 JSON)
- 解析输入参数
- 执行工具函数
- 包装输出(内容或内容+产物)
- 返回工具消息
- 更新状态和记录回调
3. 输出解析器 (output_parsers)
3.1 解析流程图
mermaid
graph TD
Input[LLM 输出] --> Parser[解析器]
Parser --> |A[StringOutputParser]|
Parser --> |B[StructuredOutputParser]|
Parser --> |C[JsonOutputParser]|
Parser --> |D[ListOutputParser]|
A --> |A1[提取纯文本]|
A1 --> Result[返回字符串]
B --> |B1[解析 JSON 到对象]|
B1 --> |B2[Zod 验证]|
B1 --> |B3[类型推断]|
B1 --> Result[返回类型化对象]
C --> |C1[解析 JSON 到 Record]|
C1 --> Result[返回键值对]
D --> |D1[解析 JSON 列表]|
D --> |D2[处理每个元素]|
D --> |D3[Zod 验证列表]|
D --> Result[返回对象数组]
A --> Validate[验证]
B --> Validate
B1 --> Validate
C1 --> Validate
C2 --> Validate
D2 --> Validate
Validate --> Transform[转换]
Transform --> |A[清理输出]|
Transform --> |B[格式化输出]|
Transform --> |C[额外处理]|
A --> Result[最终结果]
B --> Result
C --> Result
D --> Result
Transform --> Result
style Input fill:#e1f5ff
style Parser fill:#fff4e6
style A fill:#d1fae5
style B fill:#d1fae5
style C fill:#d1fae5
style D fill:#d1fae5
style Validate fill:#fff4e6
style Transform fill:#fff4e6
style Result fill:#82c91a流程说明:
- 选择合适的解析器类型
- 解析 LLM 输出文本
- 验证和转换输出类型
- 处理解析错误
- 返回结构化数据
4. 向量存储 (vectorstores)
4.1 添加文档流程图
mermaid
graph TD
Input[文档数组] --> Split[分割文档]
Split --> |A[RecursiveCharacterTextSplitter]|
Split --> |B[RecursiveTokenTextSplitter]|
Split --> |C[MarkdownTextSplitter]|
A --> |A1[分割为字符块]|
A1 --> Chunks1[获取文档块]
B --> |B1[分割为 Token 块]|
B1 --> Chunks2[获取 Token 块]
C --> |C1[按 Markdown 标题分割]|
C --> Chunks3[获取 Markdown 块]
Chunks1 --> Embed[生成嵌入向量]
Embed --> |A[OpenAIEmbeddings]|
Embed --> |B[AnthropicEmbeddings]|
Embed --> |C[GoogleEmbeddings]|
A --> BatchEmbed[批量生成]
BatchEmbed --> Batch1[创建 API 请求]
BatchEmbed --> Vectors[获取嵌入向量]
A --> |A1[单次调用]|
A --> Vectors
B --> |B1[并发调用]|
B --> Vectors
C --> |C1[生成嵌入]|
C --> Vectors
Vectors --> Add[添加到向量存储]
Add --> |A[PineconeStore]|
Add --> |B[ChromaDB]|
Add --> |C[Qdrant]|
Add --> |D[InMemoryVectorStore]|
A --> |A1[调用远程 API]|
A --> ID1[获取文档 ID]
B --> |B1[本地数据库操作]|
B --> ID2[获取文档 ID]
C --> |C1[内存存储]|
C --> IDs[生成临时 ID]
ID1 --> AddIDs[添加 ID]
ID2 --> AddIDs
IDs --> AddIDs
AddIDs --> BatchAdd[批量添加]
BatchAdd --> |A[单个添加]|
BatchAdd --> |B[批量操作]|
BatchAdd --> Add
Add --> |A[设置 ID]|
Add --> |A[设置元数据]|
Add --> Add[设置嵌入向量]
Add --> Record[记录成功]
Add --> Success[完成]
B --> |B1[批量插入]|
B --> |B2[事务性操作]|
B --> Record
C --> |C1[键值对存储]|
C --> IDs[生成随机键]
C --> |C1[添加到内存]|
C --> Record
Add --> Success
Add --> Success
BatchAdd --> Success
Success --> Index[建立索引]
Index --> |A[IVF 索引]|
Index --> |B[HNSW 索引]|
Index --> |C[自定义索引]|
A --> |A1[向量 + ID]|
A --> Index1[建立索引]
B --> |B1[向量 + 随机键]|
B --> Index2[建立索引]
C --> |C1[自定义索引算法]|
C --> Index3[建立索引]
Index1 --> Indexed[已索引]
Index2 --> Indexed
Index3 --> Indexed
Indexed --> Search[支持搜索]
Search --> |A[相似度搜索]|
Search --> |B[MMR 搜索]|
A --> |A1[计算余弦相似度]|
A --> |A2[返回 top-k 结果]|
A --> Results[相似文档列表]
B --> |B1[MMR 算法]|
B --> |B2[平衡相关性和多样性]|
B --> |B3[重新排序结果]|
B --> Results[重新排序]
Search --> Results
Results --> Final[完成]
style Input fill:#e1f5ff
style Split fill:#fff4e6
style A fill:#d1fae5
style B fill:#d1fae5
style C fill:#d1fae5
style Embed fill:#fff4e6
style Add fill:#8ec5fc
style BatchAdd fill:#d1fae5
style Success fill:#82c91a
style Index fill:#8ec5fc
style Search fill:#fff4e6
style Results fill:#82c91a
style Final fill:#f59e0b流程说明:
- 加载文档数组
- 选择合适的文本分割器
- 分割文档为合适大小的块
- 为每个块生成嵌入向量
- 批量添加到向量存储
- 建立索引以支持快速搜索
- 支持相似度搜索和 MMR 搜索
5. 检索器 (retrievers)
5.1 检索流程图
mermaid
graph TD
Input[查询文本] --> Embed[生成查询向量]
Embed --> |A[OpenAIEmbeddings]|
Embed --> |B[CohereEmbeddings]|
Embed --> |C[CustomEmbeddings]|
A --> QueryVector[获取查询向量]
QueryVector --> Search[向量存储搜索]
Search --> |A[VectorStoreRetriever]|
Search --> |B[MultiVectorStoreRetriever]|
A --> |A1[单向量存储搜索]|
A1 --> |A2[指定 k 值]|
A1 --> |A3[可选分数阈值]|
A1 --> |A4[可选搜索类型]|
A --> Results[获取相关文档]
B --> |B1[多向量存储搜索]|
B --> |B2[加权合并]|
B --> |B3[重排序结果]|
B --> Results
C --> |C[HybridRetriever]|
C --> |C1[稠密检索 + 稀疏]|
C --> |C2[交叉编码检索]|
C --> Results
A --> |A1[简单返回]|
B --> |A1[后处理]|
B --> |C1[应用过滤]|
B --> Results
A1 --> |A1[应用元数据过滤]|
A2 --> Apply[应用评分模型]
A2 --> |Rerank[重排序]|
A2 --> Results
Results --> PostProcess[后处理]
PostProcess --> |A[格式化输出]|
PostProcess --> |B[转换 Document 对象]|
PostProcess --> |C[返回 Document[]]|
PostProcess --> |D[自定义后处理]|
A --> Result
B --> Result
C --> Result
D --> FinalDocuments[最终文档列表]
FinalDocuments --> End[完成]
style Input fill:#e1f5ff
style Embed fill:#fff4e6
style Search fill:#fff4e6
style A fill:#d1fae5
style A1 fill:#d1fae5
style A2 fill:#d1fae5
style A3 fill:#d1fae5
style Results fill:#82c91a
style PostProcess fill:#fff4e6
style A fill:#d1fae5
style B fill:#d1fae5
style C fill:#d1fae5
style D fill:#82c91a
style End fill:#f59e0b流程说明:
- 将查询文本转换为嵌入向量
- 在向量存储中搜索相关文档
- 支持单向量存储、多向量存储、混合检索
- 应用后处理(过滤、重排序、格式化)
- 返回文档列表
6. 存储系统 (stores)
6.1 存储操作流程图
mermaid
graph TD
Input[操作请求] --> OpType[操作类型]
OpType --> |A[mget]|
OpType --> |B[mset]|
OpType --> |C[mdelete]|
OpType --> |D[yield*]|
A --> |A1[传入键数组]|
A1 --> |A2[并发读取]|
A --> |A3[返回 (V | undefined)[]]|
A1 --> Return[返回值]
B --> |B1[传入键值对]|
B --> |B2[并发写入]|
B --> |B3[返回 void]|
B --> Return
C --> |C1[传入键数组]|
C --> |C2[并发删除]|
C --> |C3[返回 void]|
C --> Return
D --> |D1[创建异步生成器]|
D --> |D2[yield 每个键]|
D --> |D3[结束生成器]|
Return --> |A[返回值]|
Return --> |B[返回值]|
Return --> |C[返回值]|
Return --> |D[返回值]
A --> |A1[从内存获取]|
A --> Validate[验证键存在]|
A --> |A2[返回值或 undefined]|
A --> Return
B --> |B1[设置值]|
B --> |B2[覆盖或新增]|
B --> Return
C --> |C1[删除值]|
C --> |C2[验证键存在]|
C --> |C3[删除键]|
C --> Return
A --> |A1[验证键存在]|
A --> |A2[返回 true/false]|
A --> Return
A --> |A1[返回值]|
A --> |B1[删除值]|
B --> |C[删除值]|
C --> Return
D --> |D2[结束迭代]|
D --> End[完成]
style Input fill:#e1f5ff
style OpType fill:#fff4e6
style A fill:#d1fae5
style B fill:#d1fae5
style C fill:#d1fae5
style D fill:#d1fae5
style Return fill:#fff4e6
style End fill:#f59e0b流程说明:
- 确定操作类型(mget/mset/mdelete/yield*)
- 执行存储操作
- 并发处理以提高性能
- 返回操作结果
7. 缓存系统 (caches)
7.1 缓存操作流程图
mermaid
graph TD
Input[缓存请求] --> CacheType[缓存类型]
CacheType --> |A[InMemoryCache]|
CacheType --> |B[RedisCache]|
CacheType --> |C[SemanticCache]|
A --> |A1[检查键是否存在]|
A --> |A2[返回缓存值]|
A --> |A3[更新缓存]|
A --> |A4[失效缓存]|
A1 --> |A1[简单字符串匹配]|
A --> |A2[返回缓存命中]|
A --> |Return[返回缓存值]
A --> |A2[检查键是否存在]|
A --> |A3[获取嵌入]|
A --> |A4[计算相似度]|
A --> |A5[返回缓存值]|
A --> Return
B --> |B1[Redis 客户端连接]|
B --> |B2[执行 Redis 命令]|
B --> |B3[返回缓存值]|
B --> Return
A --> |A3[设置 TTL]|
A --> |A4[自动过期]|
A --> Return
A2 --> |A4[手动失效]|
A --> Return
A --> |A4[验证键存在]|
A --> |A5[缓存未命中]|
A --> |A6[返回 undefined]|
A --> Return
A --> |A4[更新缓存]|
A --> |A5[存储 LLM 输出]|
A --> |A6[更新元数据]|
A --> Return
A --> |A5[缓存命中]|
A --> |A6[直接返回缓存值]|
A --> Return
Return --> |A[返回缓存值]|
Return --> |B[返回缓存值]|
Return --> |A2[返回缓存值]|
Return --> |A3[返回 undefined]|
Return --> |A4[返回缓存值]|
Return --> |A5[返回缓存值]|
Return --> |A6[返回 undefined]|
Return --> |A6[返回缓存值]|
Return --> |A6[返回缓存值]|
Return --> |A6[返回缓存值]|
Return --> |A6[返回缓存值]|
Return --> Final[返回]
A --> |A1[缓存命中]|
A --> |A2[缓存未命中]|
A --> |A3[缓存命中]|
A --> |A4[缓存未命中]|
A --> |A5[缓存命中]|
A --> |A6[缓存未命中]
A1 --> Check1[检查 LLM 输出]
A2 --> |B[检查嵌入向量]|
A3 --> |C[检查缓存状态]
A1 --> |A1[缓存命中 - 直接返回]|
A1 --> |A1[缓存未命中 - 需要计算]|
A2 --> |B1[缓存命中 - 直接返回]|
A2 --> |B1[缓存未命中 - 需要计算]|
A1[缓存未命中] --> Compute[计算相似度]
Compute --> |A1[计算余弦相似度]|
Compute --> |A2[返回相似度]|
Compute --> |A3[是否达到阈值]|
Compute --> |A4[返回缓存值或 undefined]|
Compute --> Return
Compute --> Return
A --> Return
A --> Return
A --> UpdateCache[更新缓存]
UpdateCache --> |A1[更新值]|
UpdateCache --> |A2[设置 TTL]|
UpdateCache --> Return
UpdateCache --> Return
A --> |A4[缓存命中]|
A --> |A4[缓存未命中]|
A --> Return
B --> |B1[缓存命中]|
B --> |B2[缓存未命中]|
B --> Return
A --> |A5[缓存命中]|
A --> |A6[缓存未命中]|
A --> Return
Check1 --> Check2
Check2 --> Check3
Check2 --> |B1[缓存命中]|
Check2 --> |B2[缓存未命中]|
Check2 --> Return
Check3 --> |C1[缓存命中]|
Check3 --> |C2[缓存未命中]|
Check3 --> Return
A --> UpdateCache
A --> UpdateCache
B --> UpdateCache
B --> UpdateCache
C --> UpdateCache
C --> UpdateCache
A --> |A[命中]|
A --> |A[未命中]
B --> |B[命中]|
B --> |B[未命中]
C --> |C[命中]|
C --> |C[未命中]
Return --> End
style Input fill:#e1f5ff
style CacheType fill:#fff4e6
style A fill:#d1fae5
style B fill:#d1fae5
style C fill:#d1fae5
style Compute fill:#d1fae5
style UpdateCache fill:#d1fae5
style Return fill:#fff4e6
style End fill:#f59e0b流程说明:
- 检查缓存是否存在
- 如果存在,返回缓存值
- 如果不存在,可能需要计算(语义缓存)
- 更新缓存内容
- 支持过期策略
8. 追踪系统 (tracers)
8.1 追踪流程图
mermaid
graph TD
Input[执行请求] --> TraceType[追踪类型]
TraceType --> |A[LangSmithTracer]|
TraceType --> |B[ConsoleTracer]|
TraceType --> |C[CustomTracer]|
A --> |A1[初始化 Tracer]|
A --> |A2[开始追踪]|
A --> |A3[记录输入]|
A --> |A4[记录输出]|
A --> |A5[记录错误]|
A --> |A6[记录嵌套]|
A --> |A7[结束追踪]|
A1 --> |A1[创建运行 ID]|
A1 --> |A2[发送到 LangSmith]|
A1 --> |A1[发送到 LangSmith]|
A --> |A2[创建 Run 对象]|
A --> Run
A --> |A2[调用开始回调]|
A --> |A3[获取回调管理器]|
A --> |A4[包装回调]|
A --> Invoke[执行 Runnable]|
A4 --> Wrap[包装回调]|
A --> Invoke
A4 --> |A1[获取 Runnable 名称]|
A4 --> |A2[获取标签]|
A4 --> |A3[获取元数据]|
A4 --> Invoke
A2 --> |A1[执行开始回调]|
A2 --> |A2[记录运行类型]|
A2 --> |A3[记录开始时间]|
A2 --> |A4[获取上下文]|
A4 --> Trace
B --> |B1[记录到控制台]|
B --> |B2[格式化输出]|
B --> |B3[记录元数据]|
B --> |B1[console.log inputs]|
B --> |B2[console.log outputs]|
B --> |B3[console.log metadata]|
B --> Return
B1 --> |A2[格式化日志]|
B2 --> Return
B3 --> Return
C --> |C1[自定义追踪逻辑]|
C --> |C2[记录到文件]|
C --> |C3[发送到外部服务]|
C --> Return
A --> |A3[记录中间步骤]|
A3 --> |A4[记录工具调用]|
A4 --> |A5[记录模型调用]|
A5 --> |A6[完成追踪]
A3 --> |A6[记录输出]|
A --> |A7[记录最终状态]|
A --> |A7[结束时间]|
A --> |A8[记录错误]|
A --> |A9[发送到 LangSmith]|
A --> |A3[发送]|
A --> |A4[发送]|
A --> |A5[发送]|
A --> |A6[发送]|
A --> |A7[发送]|
A --> |A8[发送]|
A --> |A9[Send]
A4 --> Wrap
A5 --> Wrap
A6 --> Wrap
Wrap --> |A1[记录错误]|
Wrap --> |A2[抛出异常]|
Wrap --> |A3[记录结束]|
Wrap --> Return
A4 --> |A3[记录结束]|
A5 --> |A6[记录结束]|
A6 --> |A7[记录结束]|
A7 --> Return
A8 --> Return
A --> |A4[抛出错误]|
A --> Error
A --> Return
B --> |B1[返回格式化日志]|
B --> Return
C --> |C1[返回自定义状态]|
C --> Return
Return --> End[完成]
style Input fill:#e1f5ff
style TraceType fill:#fff4e6
style A fill:#d1fae5
style B fill:#d1fae5
style C fill:#d1fae5
style Invoke fill:#fff4e6
style Return fill:#fff4e6
style End fill:#f59e0b流程说明:
- 初始化追踪器(LangSmith 或自定义)
- 包装回调以记录执行过程
- 记录输入、输出、错误
- 发送追踪数据到外部服务(如 LangSmith)
- 完成追踪并清理资源
总结
核心系统提供了 LangChain.js 的基础能力:
- 消息系统 - 类型安全的多模态消息支持
- 工具系统 - 灵活的工具定义和调用机制
- 输出解析 - 结构化输出解析
- 向量存储 - 高效的相似度搜索
- 检索器 - 统一的数据检索接口
- 存储系统 - 键值存储抽象
- 缓存系统 - LLM 响应缓存
- 追踪系统 - LangSmith 集成和调试
关键特性:
- 类型安全的泛型支持
- 完整的错误处理
- 高性能的批量操作
- 灵活的配置系统
- 强大的追踪能力
✅ 模块 2 分析完成: 核心系统(含完整流程图)📊 总进度: 2/6 (33.3%)