大模型提供方
大语言模型(LLM)提供者
相关源文件
本章引用的主要源码文件:
LLM.mdcookbooks/customer-support-chatbot.ipynbdocs/components/embedders/models/aws_bedrock.mdxdocs/components/embedders/models/azure_openai.mdxdocs/components/embedders/models/langchain.mdxdocs/components/llms/models/anthropic.mdxdocs/components/llms/models/aws_bedrock.mdxdocs/components/llms/models/azure_openai.mdxdocs/components/llms/models/deepseek.mdxdocs/components/llms/models/google_AI.mdxdocs/components/llms/models/groq.mdxdocs/components/llms/models/langchain.mdxdocs/components/llms/models/litellm.mdxdocs/components/llms/models/minimax.mdxdocs/components/llms/models/mistral_AI.mdxdocs/components/llms/models/openai.mdxdocs/components/llms/models/together.mdxdocs/components/llms/models/vllm.mdxdocs/components/vectordbs/dbs/langchain.mdxdocs/components/vectordbs/dbs/opensearch.mdxevaluation/metrics/llm_judge.pyevaluation/prompts.pyexamples/misc/vllm_example.pyexamples/multiagents/llamaindex_learning_system.pymem0-ts/src/oss/src/embeddings/langchain.tsmem0/client/project.pymem0/client/utils.pymem0/configs/llms/anthropic.pymem0/configs/llms/aws_bedrock.pymem0/configs/llms/azure.pymem0/configs/llms/deepseek.pymem0/configs/llms/lmstudio.pymem0/configs/llms/minimax.pymem0/configs/llms/ollama.pymem0/configs/llms/openai.pymem0/configs/llms/vllm.pymem0/embeddings/aws_bedrock.pymem0/embeddings/azure_openai.pymem0/embeddings/langchain.pymem0/exceptions.pymem0/llms/anthropic.pymem0/llms/aws_bedrock.pymem0/llms/azure_openai.pymem0/llms/azure_openai_structured.pymem0/llms/base.pymem0/llms/deepseek.pymem0/llms/groq.pymem0/llms/langchain.pymem0/llms/litellm.pymem0/llms/minimax.pymem0/llms/ollama.pymem0/llms/openai.pymem0/llms/openai_structured.pymem0/llms/together.pymem0/llms/vllm.pytests/embeddings/test_azure_openai_embeddings.pytests/embeddings/test_gemini_emeddings.pytests/llms/test_anthropic.pytests/llms/test_aws_bedrock.pytests/llms/test_azure_openai.pytests/llms/test_azure_openai_structured.pytests/llms/test_deepseek.pytests/llms/test_gemini.pytests/llms/test_groq.pytests/llms/test_langchain.pytests/llms/test_litellm.pytests/llms/test_minimax.pytests/llms/test_ollama.pytests/llms/test_openai.pytests/llms/test_together.pytests/llms/test_vllm.pytests/test_memory_integration.pytests/test_project.pytests/test_telemetry.py
本文档介绍 Mem0 开源部署中大语言模型(LLM)提供者的集成方式。Memory 类使用大语言模型提供者从消息中提取事实、确定内存更新操作(添加、更新、删除)并执行智能内存整合。Mem0 通过基于工厂模式的统一抽象层支持 18 种以上大语言模型提供者。
有关嵌入向量模型提供者的信息,请参见嵌入向量提供者。有关通用大语言模型配置指南,请参见大语言模型配置。
架构总览
Mem0 中的大语言模型提供者采用基于工厂的架构,其中 LlmFactory 根据配置动态实例化特定提供者的实现。所有提供者都继承自 LLMBase 并实现标准的 generate_response() 方法。
图示:大语言模型提供者工厂架构
来源: 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 提供通用参数,如 model、temperature、api_key、max_tokens、top_p 和 top_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/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_predictmem0/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-Instructmem0/llms/vllm.py:37。
来源: mem0/llms/vllm.py:13-41, mem0/llms/vllm.py:94-109
支持的提供者列表
| 提供者 | 类名 | 默认模型 | 来源 |
|---|---|---|---|
| OpenAI | OpenAILLM | gpt-5-mini | mem0/llms/openai.py:39 |
| Anthropic | AnthropicLLM | claude-3-5-sonnet-20240620 | mem0/llms/anthropic.py |
| AWS Bedrock | AWSBedrockLLM | *取决于配置* | mem0/llms/aws_bedrock.py:34 |
| Ollama | OllamaLLM | llama3.1:70b | mem0/llms/ollama.py:39 |
| Groq | GroqLLM | llama-3-3-70b-versatile | mem0/llms/groq.py:20 |
| Azure OpenAI | AzureOpenAILLM | gpt-5-mini | mem0/llms/azure_openai.py:42 |
| Together | TogetherLLM | mistralai/Mixtral-8x7B-Instruct-v0.1 | mem0/llms/together.py:20 |
| vLLM | VllmLLM | Qwen/Qwen2.5-32B-Instruct | mem0/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