agentic_huge_data_base / wiki
页面 Mem0 · 3.6 Proxy 集成·DeepWiki 中文全文译文

3.6 · Proxy 集成(Proxy Integration)

长期记忆与上下文管理 · 本章是 Mem0 DeepWiki 中文译文的独立章节页,保留原始链接、源码锚点、模块标签和章节层级。

项目Mem0 章节3.6 状态全文译文 模块检索、召回与索引、接口与服务契约、界面与交互、系统架构
源码线索
  • mem0/__init__.py
  • mem0/configs/base.py
  • mem0/configs/prompts.py
  • mem0/configs/vector_stores/azure_ai_search.py
  • mem0/configs/vector_stores/vertex_ai_vector_search.py
  • mem0/configs/vector_stores/weaviate.py
  • mem0/memory/main.py
  • mem0/memory/storage.py
  • mem0/memory/utils.py
  • mem0/proxy/main.py
模块标签
  • 检索、召回与索引
  • 接口与服务契约
  • 界面与交互
  • 系统架构
  • 智能体运行时

中文译文

Proxy 集成(中文译文)

原始 DeepWiki 页面:https://deepwiki.com/mem0ai/mem0/3.6-proxy-integration
翻译时间:2026-05-27T08:44:53.486Z
翻译模型:deepseek-chat
原文字符数:8165
项目:Mem0 (mem0)

---

代理集成

相关源文件

以下文件被用作生成此 Wiki 页面的上下文:

  • mem0/__init__.py
  • mem0/configs/base.py
  • mem0/configs/prompts.py
  • mem0/configs/vector_stores/azure_ai_search.py
  • mem0/configs/vector_stores/vertex_ai_vector_search.py
  • mem0/configs/vector_stores/weaviate.py
  • mem0/memory/main.py
  • mem0/memory/storage.py
  • mem0/memory/utils.py
  • mem0/proxy/main.py
  • mem0/vector_stores/supabase.py
  • mem0/vector_stores/vertex_ai_vector_search.py
  • mem0/vector_stores/weaviate.py
  • tests/configs/test_prompts.py
  • tests/memory/test_main.py
  • tests/test_chatty_llm_parsing.py
  • tests/test_main.py
  • tests/test_memory.py
  • tests/test_proxy.py
  • tests/vector_stores/test_vertex_ai_vector_search.py

目的与范围

Mem0 代理通过 LiteLLM 为大语言模型(LLM)聊天补全提供透明的记忆集成。该代理会拦截聊天补全请求,自动搜索相关记忆,将其注入对话上下文,并存储新信息以供将来检索。该代理模拟 OpenAI 聊天补全 API,同时增加了记忆功能。

代理实现在 mem0/proxy/main.py mem0/proxy/main.py:1-191 中,同时支持开源的 Memory 类和托管的 MemoryClient

架构总览

代理架构由三个主要类组成,它们在 LiteLLM 补全调用周围封装了记忆操作:

graph TB
    subgraph "客户端应用"
        UserApp["应用代码"]
    end

    subgraph "Mem0 代理层"
        Mem0Class["Mem0<br/>mem0/proxy/main.py"]
        ChatClass["Chat<br/>mem0/proxy/main.py"]
        CompletionsClass["Completions<br/>mem0/proxy/main.py"]
    end

    subgraph "记忆后端"
        MemoryClass["Memory<br/>mem0/memory/main.py"]
        MemoryClientClass["MemoryClient<br/>mem0/client/main.py"]
    end

    subgraph "大语言模型集成"
        LiteLLM["litellm.completion()"]
    end

    UserApp --> Mem0Class
    Mem0Class --> ChatClass
    ChatClass --> CompletionsClass

    Mem0Class -.-> MemoryClass
    Mem0Class -.-> MemoryClientClass

    CompletionsClass --> MemoryClass
    CompletionsClass --> MemoryClientClass
    CompletionsClass --> LiteLLM

    LiteLLM --> ExternalLLM["OpenAI/Anthropic 等"]

来源: mem0/proxy/main.py:28-50, tests/test_proxy.py:1-101

核心组件

Mem0 类

Mem0mem0/proxy/main.py:28-40 作为代理初始化的入口点,支持两种初始化模式:

  1. 平台模式:如果提供了 api_key,则使用 MemoryClient 初始化 mem0/proxy/main.py:35-36
  2. 开源模式:使用本地 Memory 实例,通过配置字典或默认设置进行初始化 mem0/proxy/main.py:38

该类通过 Chat 类暴露了一个 chat 属性,提供聊天补全接口 mem0/proxy/main.py:40

Chat 和 Completions 类

Chatmem0/proxy/main.py:43-45 提供了一个类似于 OpenAI 客户端结构的命名空间。它初始化一个 Completions 实例 mem0/proxy/main.py:48-191,该实例处理实际的补全逻辑。

Completions 中的关键方法:

方法参数返回值描述
create()model, messages, user_id, agent_id, run_id, **kwargslitellm.ModelResponse拦截请求,管理记忆,并调用 LiteLLM。
_prepare_messages()messagesList[dict]确保存在系统提示词,如果缺失则前置添加 MEMORY_ANSWER_PROMPT mem0/configs/prompts.py:4-13
_async_add_to_memory()messages, IDs, metadataNone生成一个守护线程,将对话存储到记忆中,不会阻塞。
_fetch_relevant_memories()messages, IDs, filtersList使用最后 6 条消息作为上下文查询记忆后端。

来源: mem0/proxy/main.py:48-177, mem0/configs/prompts.py:4-13

数据流与记忆注入

以下时序图展示了使用代理时的完整请求生命周期:

sequenceDiagram
    participant App as "应用"
    participant Comp as "Completions.create()"
    participant Mem as "Memory / MemoryClient"
    participant Lite as "litellm.completion()"

    App->>Comp: create(model, messages, user_id, ...)

    Note over Comp: 检查函数调用支持
    Comp->>Comp: litellm.supports_function_calling(model)

    Note over Comp: 异步记忆存储
    Comp->>Mem: _async_add_to_memory(messages)

    Note over Comp: 记忆检索
    Comp->>Mem: _fetch_relevant_memories(messages)
    Mem-->>Comp: relevant_memories

    Note over Comp: 上下文增强
    Comp->>Comp: _format_query_with_memories()

    Note over Comp: 大语言模型调用
    Comp->>Lite: completion(enhanced_messages, **kwargs)
    Lite-->>Comp: response

    Comp->>Comp: capture_event("mem0.chat.create")
    Comp-->>App: response

来源: mem0/proxy/main.py:95-145, mem0/proxy/main.py:152-177

记忆检索与格式化

当检测到用户消息是 messages 列表中的最后一项时,代理会执行记忆注入 mem0/proxy/main.py:104-108

  1. 搜索上下文:代理提取最后 6 条消息以形成搜索查询 mem0/proxy/main.py:168
  2. 搜索执行:代理使用生成的查询和提供的过滤器调用 self.mem0_client.search() mem0/proxy/main.py:170-177
  3. 上下文格式化_format_query_with_memories 方法 mem0/proxy/main.py:179-191 将检索到的事实连接成一个字符串。如果使用开源 Memorymem0/memory/main.py:7,它还会包含提取的实体/关系(如果可用)mem0/proxy/main.py:184-186
  4. 内容更新:检索到的事实会被前置添加到最终用户消息的内容中 mem0/proxy/main.py:108
异步存储

为了防止记忆存储增加最终用户的延迟,代理使用 threading.Thread 并设置 daemon=True 在后台调用 self.mem0_client.add() mem0/proxy/main.py:153-164

配置与使用

初始化

代理可以初始化为使用本地记忆引擎或 Mem0 平台。

from mem0.proxy.main import Mem0

# 模式 1:平台(MemoryClient)
mem0_platform = Mem0(api_key="m0-xxx")

# 模式 2:开源(Memory)
config = {
    "vector_store": {"provider": "chroma", "config": {"path": "./db"}},
    "llm": {"provider": "openai", "config": {"model": "gpt-4o"}}
}
mem0_oss = Mem0(config=config)

来源: mem0/proxy/main.py:29-40

LiteLLM 集成

代理利用 LiteLLM 的统一接口支持 100 多个大语言模型提供商。在继续之前,它会验证所选模型是否支持函数调用 mem0/proxy/main.py:98-101。所有标准的 OpenAI 兼容参数(例如 temperaturetoolsresponse_format)都会直接转发给 litellm.completion() mem0/proxy/main.py:110-140

遥测与跟踪

通过代理的每次成功补全都会通过 capture_event(开源模式)或 capture_client_event(平台模式)触发一个遥测事件,事件名称为 mem0.chat.create mem0/proxy/main.py:141-144

来源: mem0/proxy/main.py:141-144, mem0/memory/telemetry.py:23