多提供方插件架构
多提供商插件架构
相关源文件
本章引用的主要源码文件:
graphiti_core/cross_encoder/__init__.pygraphiti_core/cross_encoder/bge_reranker_client.pygraphiti_core/cross_encoder/client.pygraphiti_core/cross_encoder/openai_reranker_client.pygraphiti_core/driver/__init__.pygraphiti_core/driver/driver.pygraphiti_core/driver/falkordb_driver.pygraphiti_core/driver/neo4j_driver.pygraphiti_core/embedder/client.pygraphiti_core/embedder/openai.pygraphiti_core/embedder/voyage.pygraphiti_core/helpers.pygraphiti_core/llm_client/anthropic_client.pygraphiti_core/llm_client/client.pygraphiti_core/llm_client/config.pygraphiti_core/llm_client/groq_client.pygraphiti_core/llm_client/openai_base_client.pygraphiti_core/llm_client/openai_client.pygraphiti_core/llm_client/openai_generic_client.pygraphiti_core/llm_client/utils.py
本文描述了 Graphiti 中的四个可插拔提供商子系统:大语言模型(LLM)客户端、嵌入向量(Embedder)客户端、交叉编码器/重排序器(Cross-Encoder/Reranker)客户端和图数据库驱动(Graph Database Driver)。每个子系统都由一个抽象基类(ABC)定义,具体实现可以在构造时进行替换,而无需修改任何核心管线逻辑。
关于图数据库驱动实现的详细文档,请参见数据库抽象层。关于完整的 AI 服务实现列表,请参见大语言模型客户端架构和嵌入向量与重排序服务。
设计总览
Graphiti 将*做什么*(知识图谱操作)与*怎么做*(由哪个服务执行每个步骤)分离开来。Graphiti 类通过构造函数参数接受四个可插拔组件:
| 参数 | 抽象基类 | 默认实现 |
|---|---|---|
llm_client | LLMClient | OpenAIClient |
embedder | EmbedderClient | OpenAIEmbedder |
cross_encoder | CrossEncoderClient | OpenAIRerankerClient |
graph_driver | GraphDriver | Neo4jDriver |
每个抽象基类定义了一个最小接口。所有内部管线函数都通过这些接口进行调用,不直接依赖任何提供商 SDK。
整体组合图——Graphiti 及其四个可插拔组件:
来源:graphiti_core/llm_client/client.py:71-84, graphiti_core/embedder/client.py:30-38, graphiti_core/cross_encoder/client.py:20-40, graphiti_core/driver/driver.py:90-115
大语言模型客户端
抽象基类:LLMClient
定义在 graphiti_core/llm_client/client.py 中。每个大语言模型提供商实现都必须继承 LLMClient 并实现 _generate_response 方法 graphiti_core/llm_client/client.py:139-147。
公共接口是 generate_response,它负责处理以下事项:
- 通过
get_extraction_language_instruction追加多语言提取指令graphiti_core/llm_client/client.py:176-176。 - 通过可插拔的
Tracer发出追踪跨度graphiti_core/llm_client/client.py:182-192。 - 检查可选的
LLMCachegraphiti_core/llm_client/client.py:194-203。 - 在
TokenUsageTracker中记录 Token 使用量graphiti_core/llm_client/client.py:214-214。 - 调用内部抽象方法
_generate_response_with_retrygraphiti_core/llm_client/client.py:127-137。
LLMConfig
所有大语言模型客户端都接受一个 LLMConfig 实例,该实例定义在 graphiti_core/llm_client/config.py:28-68:
| 字段 | 类型 | 用途 |
|---|---|---|
api_key | str | None | 提供商 API 密钥 |
model | str | None | 主模型名称 |
small_model | str | None | 轻量级提示使用的模型 |
base_url | str | None | 覆盖端点 URL |
temperature | float | 采样温度(默认 1) |
max_tokens | int | 最大输出 Token 数(默认 16384) |
ModelSize 枚举(small、medium)控制运行时选择哪个模型名称——small 映射到 small_model,medium 映射到 model graphiti_core/llm_client/config.py:23-25。
具体的大语言模型实现
大语言模型客户端类层次结构:
来源:graphiti_core/llm_client/client.py:71-248, graphiti_core/llm_client/openai_base_client.py:40-296, graphiti_core/llm_client/openai_client.py:27-126, graphiti_core/llm_client/openai_generic_client.py:37-214, graphiti_core/llm_client/anthropic_client.py:103-448, graphiti_core/llm_client/groq_client.py:48-86
提供商对比
| 类 | 结构化输出策略 | 可选依赖 |
|---|---|---|
OpenAIClient | 使用 response_model 的 Responses API parse graphiti_core/llm_client/openai_client.py:65-101 | openai(默认) |
OpenAIGenericClient | json_schema 响应格式 graphiti_core/llm_client/openai_generic_client.py:112-121 | openai(默认) |
AnthropicClient | 使用 Pydantic 模型模式的工具调用 graphiti_core/llm_client/anthropic_client.py:189-232 | graphiti-core[anthropic] |
GroqClient | json_object 响应格式 graphiti_core/llm_client/groq_client.py:77-77 | graphiti-core[groq] |
OpenAIGenericClient 是推荐用于 Ollama 和其他兼容 OpenAI 的本地端点的客户端。它默认使用 16K Token 限制,而不是标准的 8K,以支持复杂的本地模型提示 graphiti_core/llm_client/openai_generic_client.py:75-75。
嵌入向量客户端
抽象基类:EmbedderClient
定义在 graphiti_core/embedder/client.py:30-38 中。包含两个相关方法:
create(input_data)——生成单个嵌入向量。create_batch(input_data_list)——在单个 API 调用中生成多个嵌入向量。
EmbedderConfig Pydantic 模型 graphiti_core/embedder/client.py:26-27 携带一个字段 embedding_dim。所有嵌入向量配置都继承自 EmbedderConfig。
具体的嵌入向量实现
嵌入向量客户端类层次结构:
来源:graphiti_core/embedder/client.py:26-38, graphiti_core/embedder/openai.py:27-66, graphiti_core/embedder/voyage.py:38-76
| 类 | 默认模型 | 可选依赖 |
|---|---|---|
OpenAIEmbedder | text-embedding-3-small graphiti_core/embedder/openai.py:24-24 | openai(默认) |
VoyageAIEmbedder | voyage-3 graphiti_core/embedder/voyage.py:35-35 | graphiti-core[voyageai] |
交叉编码器/重排序器客户端
抽象基类:CrossEncoderClient
定义在 graphiti_core/cross_encoder/client.py:20-40 中。包含一个抽象方法:
rank(query: str, passages: list[str]) -> list[tuple[str, float]]
返回按相关性分数降序排列的段落。搜索系统使用此方法来优化向量搜索结果。
具体的重排序器实现
交叉编码器客户端类层次结构:
来源:graphiti_core/cross_encoder/client.py:20-40, graphiti_core/cross_encoder/openai_reranker_client.py:34-123, graphiti_core/cross_encoder/bge_reranker_client.py:34-54
| 类 | 机制 | 可选依赖 |
|---|---|---|
OpenAIRerankerClient | 使用对数概率的布尔分类器 graphiti_core/cross_encoder/openai_reranker_client.py:91-114 | openai(默认) |
BGERerankerClient | 通过 sentence-transformers 进行本地推理 graphiti_core/cross_encoder/bge_reranker_client.py:44-46 | graphiti-core[sentence-transformers] |
图数据库驱动
GraphDriver 抽象基类是 Graphiti 数据库可移植性的基础。它提供了对专用操作集(例如 entity_node_ops、episode_node_ops)的抽象访问,这些操作集按提供商实现。
高层驱动层次结构:
来源:graphiti_core/driver/driver.py:59-63, graphiti_core/driver/driver.py:90-210
具体的驱动实现
具体的驱动实现了 GraphDriver 接口,并提供面向不同提供方的操作。
- Neo4jDriver:使用
AsyncGraphDatabase驱动实现与 Neo4j 的连接graphiti_core/driver/neo4j_driver.py:60-75。它通过session.begin_transaction()支持原生事务graphiti_core/driver/neo4j_driver.py:151-161。 - FalkorDriver:使用
FalkorDB异步客户端实现与 FalkorDB 的连接graphiti_core/driver/falkordb_driver.py:110-146。它使用自定义的FalkorDriverSessiongraphiti_core/driver/falkordb_driver.py:75-108来包装查询执行。
可选依赖模式
依赖第三方 SDK 的提供商实现使用 TYPE_CHECKING 守卫配合惰性导入,如果未安装相应的额外依赖,则在运行时抛出描述性的 ImportError。
if TYPE_CHECKING:
import anthropic
else:
try:
import anthropic
except ImportError:
raise ImportError(
'anthropic is required for AnthropicClient. '
'Install it with: pip install graphiti-core[anthropic]'
) from None
| pip 额外依赖 | 解锁的提供商 |
|---|---|
| *(无,默认)* | OpenAIClient、OpenAIGenericClient、OpenAIEmbedder、OpenAIRerankerClient |
anthropic | AnthropicClient graphiti_core/llm_client/anthropic_client.py:31-44 |
groq | GroqClient graphiti_core/llm_client/groq_client.py:22-34 |
voyageai | VoyageAIEmbedder graphiti_core/embedder/voyage.py:20-29 |
falkordb | FalkorDriver graphiti_core/driver/falkordb_driver.py:22-34 |
来源:graphiti_core/llm_client/anthropic_client.py:31-44, graphiti_core/llm_client/groq_client.py:22-34, graphiti_core/embedder/voyage.py:20-29, graphiti_core/driver/falkordb_driver.py:22-34