AI概念 一图流

RAG graph LR subgraph "Query Process 💬 问答流程" direction TB Q1((用户问题)) --> Q2{{"Embedding模型🧮<br>向量化"}} Q2 --> Q3{"检索Retrieval🔍<br>计算相似度(topK)"} Q3 -.查询.-> VectorDB VectorDB -.topk个结果.-> Q3 Q3 --> Q4[["相关片段📄"]] Q4 -.注入上下文.-> Q5("增强/Augmented🔀<br>Prompt=问题+相关片段") Q1 -.原始问题.-> Q5 Q5 -.输入.-> Q6{{"大语言模型🤖"}} Q6 --> Q7[["最终回答💬"]] end subgraph "Index Process 🔧 离线数据准备" direction TB KB["(私有知识库📚<br>PDF/Word/Wiki)"] --> Chunking("切片/Chunking🔪<br>长文本切分成小段") Chunking --> Embedding{{"Embedding模型🧮<br>向量化"}} Embedding --> VectorDB["(向量数据库🗄️<br> VectorDB ..)"] end style KB fill:#e1f5ff,stroke:#007acc,stroke-width:2px style VectorDB fill:#e1f5ff,stroke:#007acc,stroke-width:2px style Q1 fill:#fff2cc,stroke:#d6b656,stroke-width:2px style Q7 fill:#d5e8d4,stroke:#82b366,stroke-width:2px style Embedding fill:#f0e1ff,stroke:#9673a6,stroke-width:2px style Q6 fill:#f0e1ff,stroke:#9673a6,stroke-width:2px style Q4 fill:#ffe6cc,stroke:#d79b00,stroke-width:2px MCP sequenceDiagram User->>MCP Client/IDE/Agent:User Query(今天天气怎么样?) MCP Client/IDE/Agent->>MCP Server:连接 MCP Server & Get Tools MCP Server->>MCP Client/IDE/Agent:Tools List(文件工具、天气工具...) MCP Client/IDE/Agent->>LLM:User Query(今天天气怎么样?) + Tools List(这些工具可以用) LLM->>MCP Client/IDE/Agent: 建议调用的Tool + 参数(建议使用天气工具 参数:date:'20260127',city:'武汉') MCP Client/IDE/Agent->>MCP Server:使用参数调用Tool(天气工具(date, city)) MCP Server->>MCP Server:内部触发逻辑 MCP Server->>MCP Client/IDE/Agent:返回值/Error(temp:30°, weather:sunny) MCP Client/IDE/Agent->>LLM: Tool的返回值(temp:30°, weather:sunny) note over LLM: 思考、推理 LLM->>MCP Client/IDE/Agent:总结归纳后的自然语言(明日(20260127)天气为晴天,温度30°,建议穿凉爽的衣服) MCP Client/IDE/Agent->>User: 格式化结果 Agent React graph RL Input((Task/Input<br>任务输入)) --> Thought["Thought<br>思考: 分析任务,决定步骤"] subgraph ReAct_Loop ["ReAct Loop 循环"] direction TB Thought --> Action{Action<br>行动: 选择工具/动作} Action -- 调用工具 --> Execute["Environment/Tool<br>执行环境/工具"] Execute --> Observation["Observation<br>观察: 获取执行结果"] Observation --> Thought end Action -- 任务完成 --> Final["Final Answer<br>最终回答"] style Input fill:#fff2cc,stroke:#d6b656,stroke-width:2px style Final fill:#d5e8d4,stroke:#82b366,stroke-width:2px style Thought fill:#e1f5ff,stroke:#007acc,stroke-width:2px style Action fill:#f0e1ff,stroke:#9673a6,stroke-width:2px style Observation fill:#ffe6cc,stroke:#d79b00,stroke-width:2px

2025/07/02 · Aris

Elasticsearch一图流

Lucene 核心数据结构 (单机视角) Lucene 是如何通过倒排索引实现快速搜索,以及如何解决排序和存储问题的 graph TD subgraph Lucene_Segment ["Lucene Segment (最小索引单元/不可变)"] direction TB subgraph Inverted_Index ["倒排索引 (Inverted Index) - 用于搜索"] TI["Term Index (内存)"] TD["Term Dictionary (磁盘)"] PL["Posting List (磁盘)"] TI --"前缀索引/加速定位"--> TD TD --"关联文档ID"--> PL note1["> 举例: 查找 '小白'<br/>1. Term Index 定位大概位置<br/>2. Dictionary 找到 '小白'<br/>3. Posting List 得到 ID: [0, 1]"] style TI fill:#e1f5fe,stroke:#01579b style TD fill:#fff9c4,stroke:#fbc02d style PL fill:#fff9c4,stroke:#fbc02d end subgraph Storage ["数据存储"] SF["Stored Fields (行式存储)"] DV["Doc Values (列式存储)"] SF --"根据ID获取完整内容"--> Content["文档原始JSON"] DV --"用于排序/聚合"--> Sort["排序/聚合操作"] note2["> Stored Fields: 存完整数据<br/>> Doc Values: 空间换时间,优化排序"] style SF fill:#e8f5e9,stroke:#2e7d32 style DV fill:#f3e5f5,stroke:#7b1fa2 end end Query["搜索请求"] --> TI PL -.->|"得到文档ID"| Storage Elasticsearch 在Lucene基础上的分布式架构 架构图 graph Client["客户端应用"] --> Coord subgraph Cluster["Elasticsearch Cluster (集群)"] subgraph CoordLayer["协调节点层 (请求入口/分发/聚合)"] Coord["协调节点 (Coordinating Node)"] end subgraph MasterLayer["Master 节点层 (集群管理/选主/元数据)"] Master1["Master Node A"] Master2["Master Node B"] Master3["Master Node C"] end subgraph DataLayer["Data 节点层 (存储/搜索/写入)"] Data1["Data Node 1"] Data2["Data Node 2"] Data3["Data Node 3"] end subgraph IndexLayer["Index: news_index (逻辑索引)"] subgraph Shard0["Shard 0"] P0["Primary Shard 0"] R0["Replica Shard 0"] end subgraph Shard1["Shard 1"] P1["Primary Shard 1"] R1["Replica Shard 1"] end end subgraph LuceneLayer["Lucene 层 (每个分片内的底层引擎)"] subgraph SegA["Segment A (不可变)"] IIA["倒排索引"] SFA["Stored Fields"] DVA["Doc Values"] end subgraph SegB["Segment B (不可变)"] IIB["倒排索引"] SFB["Stored Fields"] DVB["Doc Values"] end end end %% === 核心链路简化 === %% 1. 请求路由: Coord -> Data Node (物理流向: 先找到持有分片的节点) Coord --"路由写入 / 分发查询"--> Data1 %% 2. 数据处理: Data Node -> Shard (节点调用内部主要分片) Data3 -- "执行请求" --> P0 %% 3. 数据同步: Primary -> Replica (跨节点同步) P1 -.-> R1 P0 -.-> R0 %% 4. 其它关系示意 Master3 -.-|"集群管理"| Data3 R0 -.-|"底层引擎"| SegA %% === 样式定义 === style Cluster fill:#f5f5f5,stroke:#333,stroke-width:2px style CoordLayer fill:#e3f2fd,stroke:#1565c0,stroke-dasharray: 5 5 style MasterLayer fill:#f3e5f5,stroke:#7b1fa2,stroke-dasharray: 5 5 style DataLayer fill:#e8f5e9,stroke:#2e7d32,stroke-dasharray: 5 5 style IndexLayer fill:#fff9c4,stroke:#fbc02d,stroke-dasharray: 5 5 style LuceneLayer fill:#fff3e0,stroke:#e65100 style SegA fill:#ffffff,stroke:#333 style SegB fill:#ffffff,stroke:#333 classDiagram class Cluster { +String cluster_name +List nodes } class Node { +String node_name +List roles +store_data() +coordinate_request() } class NodeRoles { Master Data Coordinating } note for NodeRoles "枚举类型 (Enumeration)<br/>Master: 集群管理<br/>Data: 数据存储<br/>Coordinating: 请求转发" class Index { +String index_name +List shards } class Shard { +int shard_id +LuceneInstance underlying_engine } class PrimaryShard { +sync_to_replica() } class ReplicaShard { +promote_to_primary() } class LuceneInstance { +List segments +merge_segments() } class Segment { +InvertedIndex +StoredFields +DocValues +is_immutable } Cluster *-- Node Node ..> NodeRoles : 扮演角色 Index *-- Shard Shard <|-- PrimaryShard Shard <|-- ReplicaShard Shard *-- LuceneInstance : 底层引擎 LuceneInstance "1" *-- "*" Segment : 包含多个 Node "1" o-- "*" Shard : 承载 note for PrimaryShard "负责写入,同步数据给副本" note for ReplicaShard "提供读能力,主分片挂掉后升级" note for Node "单节点可扩展为多节点集群<br/>通过 Raft 类机制选主" note for LuceneInstance "写入产生新Segment<br/>后台定期合并小Segment<br/>以优化文件句柄和查询性能" 从 Lucene 到 Elasticsearch 的演进 Lucene 作为一个单纯的搜索库,虽然功能强大,但在面对海量数据和高并发场景时存在明显的单机局限性: ...

2024/11/06 · Aris