agentic_huge_data_base / wiki
页面 Mem0 · 6.1 大模型提供方·DeepWiki 中文全文译文

6.1 · 大模型提供方(LLM Providers)

长期记忆与上下文管理 · 聚焦本章的模块关系、源码依据与实现要点。

项目Mem0 章节6.1 状态全文译文 模块模型调用与提供方适配、检索、召回与索引、系统架构、接口与服务契约
源码线索
  • LLM.md
  • cookbooks/customer-support-chatbot.ipynb
  • docs/components/embedders/models/aws_bedrock.mdx
  • docs/components/embedders/models/azure_openai.mdx
  • docs/components/embedders/models/langchain.mdx
  • docs/components/llms/models/anthropic.mdx
  • docs/components/llms/models/aws_bedrock.mdx
  • docs/components/llms/models/azure_openai.mdx
  • docs/components/llms/models/deepseek.mdx
  • docs/components/llms/models/google_AI.mdx
模块标签
  • 模型调用与提供方适配
  • 检索、召回与索引
  • 系统架构
  • 接口与服务契约
  • 界面与交互

章节正文

大模型提供方

大语言模型(LLM)提供者

相关源文件

本章引用的主要源码文件:

  • LLM.md
  • cookbooks/customer-support-chatbot.ipynb
  • docs/components/embedders/models/aws_bedrock.mdx
  • docs/components/embedders/models/azure_openai.mdx
  • docs/components/embedders/models/langchain.mdx
  • docs/components/llms/models/anthropic.mdx
  • docs/components/llms/models/aws_bedrock.mdx
  • docs/components/llms/models/azure_openai.mdx
  • docs/components/llms/models/deepseek.mdx
  • docs/components/llms/models/google_AI.mdx
  • docs/components/llms/models/groq.mdx
  • docs/components/llms/models/langchain.mdx
  • docs/components/llms/models/litellm.mdx
  • docs/components/llms/models/minimax.mdx
  • docs/components/llms/models/mistral_AI.mdx
  • docs/components/llms/models/openai.mdx
  • docs/components/llms/models/together.mdx
  • docs/components/llms/models/vllm.mdx
  • docs/components/vectordbs/dbs/langchain.mdx
  • docs/components/vectordbs/dbs/opensearch.mdx
  • evaluation/metrics/llm_judge.py
  • evaluation/prompts.py
  • examples/misc/vllm_example.py
  • examples/multiagents/llamaindex_learning_system.py
  • mem0-ts/src/oss/src/embeddings/langchain.ts
  • mem0/client/project.py
  • mem0/client/utils.py
  • mem0/configs/llms/anthropic.py
  • mem0/configs/llms/aws_bedrock.py
  • mem0/configs/llms/azure.py
  • mem0/configs/llms/deepseek.py
  • mem0/configs/llms/lmstudio.py
  • mem0/configs/llms/minimax.py
  • mem0/configs/llms/ollama.py
  • mem0/configs/llms/openai.py
  • mem0/configs/llms/vllm.py
  • mem0/embeddings/aws_bedrock.py
  • mem0/embeddings/azure_openai.py
  • mem0/embeddings/langchain.py
  • mem0/exceptions.py
  • mem0/llms/anthropic.py
  • mem0/llms/aws_bedrock.py
  • mem0/llms/azure_openai.py
  • mem0/llms/azure_openai_structured.py
  • mem0/llms/base.py
  • mem0/llms/deepseek.py
  • mem0/llms/groq.py
  • mem0/llms/langchain.py
  • mem0/llms/litellm.py
  • mem0/llms/minimax.py
  • mem0/llms/ollama.py
  • mem0/llms/openai.py
  • mem0/llms/openai_structured.py
  • mem0/llms/together.py
  • mem0/llms/vllm.py
  • tests/embeddings/test_azure_openai_embeddings.py
  • tests/embeddings/test_gemini_emeddings.py
  • tests/llms/test_anthropic.py
  • tests/llms/test_aws_bedrock.py
  • tests/llms/test_azure_openai.py
  • tests/llms/test_azure_openai_structured.py
  • tests/llms/test_deepseek.py
  • tests/llms/test_gemini.py
  • tests/llms/test_groq.py
  • tests/llms/test_langchain.py
  • tests/llms/test_litellm.py
  • tests/llms/test_minimax.py
  • tests/llms/test_ollama.py
  • tests/llms/test_openai.py
  • tests/llms/test_together.py
  • tests/llms/test_vllm.py
  • tests/test_memory_integration.py
  • tests/test_project.py
  • tests/test_telemetry.py

本文档介绍 Mem0 开源部署中大语言模型(LLM)提供者的集成方式。Memory 类使用大语言模型提供者从消息中提取事实、确定内存更新操作(添加、更新、删除)并执行智能内存整合。Mem0 通过基于工厂模式的统一抽象层支持 18 种以上大语言模型提供者。

有关嵌入向量模型提供者的信息,请参见嵌入向量提供者。有关通用大语言模型配置指南,请参见大语言模型配置

架构总览

Mem0 中的大语言模型提供者采用基于工厂的架构,其中 LlmFactory 根据配置动态实例化特定提供者的实现。所有提供者都继承自 LLMBase 并实现标准的 generate_response() 方法。

图示:大语言模型提供者工厂架构

Mem0 · 架构总览 · 图 1
Mem0 · 架构总览 · 图 1

来源: mem0/llms/base.py:11-18, mem0/llms/openai.py:14-36, mem0/llms/aws_bedrock.py:34-64, mem0/llms/ollama.py:15-36

基础抽象

LLMBase 类

所有大语言模型提供者都继承自 LLMBase mem0/llms/base.py:11-14。它定义了核心接口:

  • generate_response():用于文本生成和工具调用的主要方法 mem0/llms/base.py:36-47
  • _get_supported_params():通过检查提供者的 generate_response 签名来过滤特定模型的有效参数,以防止 API 错误 mem0/llms/base.py:49-65
BaseLlmConfig 类

BaseLlmConfig 提供通用参数,如 modeltemperatureapi_keymax_tokenstop_ptop_k mem0/configs/llms/base.py:7-34

关键提供者实现

OpenAI 与 OpenRouter

OpenAILLM 类支持原生 OpenAI、Azure OpenAI(通过单独类)和 OpenRouter mem0/llms/openai.py:14-15

  • OpenRouter:如果环境中存在 OPENROUTER_API_KEY,则使用 OpenRouter 基础 URL 初始化客户端 mem0/llms/openai.py:41-47
  • 推理模型:支持 reasoning_effort 参数,适用于 o3-mini 等模型 mem0/llms/openai.py:32, tests/llms/test_openai.py:173-175
  • 工具解析:实现 _parse_response 方法,从 OpenAI 选择结构中提取内容和工具调用 mem0/llms/openai.py:54-82

图示:OpenAI 请求流程

Mem0 · OpenAI 与 OpenRouter · 图 2
Mem0 · OpenAI 与 OpenRouter · 图 2

来源: mem0/llms/openai.py:84-149, mem0/llms/openai.py:54-82

AWS Bedrock

AWSBedrockLLM 支持多种提供者,包括 Anthropic、Meta、Mistral 和 Cohere mem0/llms/aws_bedrock.py:19-23

  • 初始化:使用 boto3 初始化 bedrock-runtime 客户端 mem0/llms/aws_bedrock.py:77-83
  • 提供者检测:根据模型 ID 字符串自动检测底层提供者 mem0/llms/aws_bedrock.py:26-31
  • 能力映射:根据提供者确定模型是否支持工具、视觉或流式传输 mem0/llms/aws_bedrock.py:120-129

来源: mem0/llms/aws_bedrock.py:34-75, mem0/llms/aws_bedrock.py:120-144

Azure OpenAI

AzureOpenAILLM 处理与 Azure OpenAI 服务的集成 mem0/llms/azure_openai.py:16

  • 认证:支持 API 密钥和 DefaultAzureCredential 两种方式,用于 Entra ID(原 Azure AD)认证 mem0/llms/azure_openai.py:51-59
  • 结构化输出AzureOpenAIStructuredLLM 使用 .parse() 方法确保 JSON 模式正确 mem0/llms/azure_openai_structured.py:15-57
  • 推理支持:支持推理模型的 reasoning_effort 参数,适用于 Azure 部署 mem0/llms/azure_openai.py:34, tests/llms/test_azure_openai.py:138-141

来源: mem0/llms/azure_openai.py:16-69, mem0/llms/azure_openai_structured.py:15-57

Ollama(本地)

OllamaLLM 提供本地托管模型的集成 mem0/llms/ollama.py:15

  • 配置:将 max_tokens 等标准参数映射到 Ollama 的 num_predict mem0/llms/ollama.py:129-133
  • JSON 格式:当 response_format 设置为 json_object 时,会向 Ollama 客户端传递 format="json",并在用户提示词后追加回退指令 mem0/llms/ollama.py:120-126

来源: mem0/llms/ollama.py:15-41, mem0/llms/ollama.py:113-143

vLLM

VllmLLM 使用 vLLM 提供的兼容 OpenAI 的服务器 mem0/llms/vllm.py:13

  • 客户端:使用指向 vLLM 基础 URL 的标准 openai.OpenAI 客户端 mem0/llms/vllm.py:41
  • 默认模型:默认使用 Qwen/Qwen2.5-32B-Instruct mem0/llms/vllm.py:37

来源: mem0/llms/vllm.py:13-41, mem0/llms/vllm.py:94-109

支持的提供者列表

提供者类名默认模型来源
OpenAIOpenAILLMgpt-5-minimem0/llms/openai.py:39
AnthropicAnthropicLLMclaude-3-5-sonnet-20240620mem0/llms/anthropic.py
AWS BedrockAWSBedrockLLM*取决于配置*mem0/llms/aws_bedrock.py:34
OllamaOllamaLLMllama3.1:70bmem0/llms/ollama.py:39
GroqGroqLLMllama-3-3-70b-versatilemem0/llms/groq.py:20
Azure OpenAIAzureOpenAILLMgpt-5-minimem0/llms/azure_openai.py:42
TogetherTogetherLLMmistralai/Mixtral-8x7B-Instruct-v0.1mem0/llms/together.py:20
vLLMVllmLLMQwen/Qwen2.5-32B-Instructmem0/llms/vllm.py:37

响应回调

OpenAI 提供者(以及使用其基础的其他提供者)支持 response_callback mem0/llms/openai.py:142。这允许开发者在生成过程中挂接钩子,以捕获原始响应、Token 使用量或延迟指标 mem0/llms/openai.py:144, tests/llms/test_openai.py:109-124

来源: mem0/llms/openai.py:142-149, tests/llms/test_openai.py:109-131