This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
from app.config import LLM_API_KEY
|
||||
from app.config import VLLM_BASE_URL
|
||||
import time
|
||||
"""
|
||||
Mem0 记忆层客户端封装模块
|
||||
负责 Mem0 的初始化、检索和存储
|
||||
@@ -7,7 +10,11 @@ import asyncio
|
||||
from typing import Optional, List, Dict
|
||||
from mem0 import AsyncMemory
|
||||
|
||||
from app.config import QDRANT_URL, QDRANT_COLLECTION_NAME, QDRANT_API_KEY, LLAMACPP_EMBEDDING_URL, LLAMACPP_API_KEY
|
||||
from app.config import (
|
||||
QDRANT_URL,QDRANT_COLLECTION_NAME,QDRANT_API_KEY,
|
||||
VLLM_BASE_URL, LLM_API_KEY,
|
||||
LLAMACPP_EMBEDDING_URL, LLAMACPP_API_KEY
|
||||
)
|
||||
from app.logger import info, warning, error
|
||||
|
||||
class Mem0Client:
|
||||
@@ -42,9 +49,13 @@ class Mem0Client:
|
||||
}
|
||||
},
|
||||
"llm": {
|
||||
"provider": "langchain",
|
||||
"provider": "openai",
|
||||
"config": {
|
||||
"model": self.llm
|
||||
"model": "LLM_MODEL",
|
||||
"api_key": LLM_API_KEY,
|
||||
"openai_base_url": VLLM_BASE_URL,
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000,
|
||||
}
|
||||
},
|
||||
"embedder": {
|
||||
@@ -118,36 +129,18 @@ class Mem0Client:
|
||||
warning(f"⚠️ Mem0 检索失败: {e}")
|
||||
return []
|
||||
|
||||
async def add_memories(self, messages: List[Dict[str, str]], user_id: str) -> bool:
|
||||
"""
|
||||
添加记忆(自动提取事实并存储)
|
||||
|
||||
Args:
|
||||
messages: 消息列表,格式为 [{"role": "user/assistant/system", "content": "..."}]
|
||||
user_id: 用户 ID
|
||||
|
||||
Returns:
|
||||
bool: 是否成功
|
||||
"""
|
||||
if not self.mem0:
|
||||
warning("⚠️ Mem0 未初始化,跳过记忆添加")
|
||||
return False
|
||||
|
||||
try:
|
||||
await asyncio.wait_for(
|
||||
self.mem0.add(
|
||||
messages,
|
||||
user_id=user_id,
|
||||
metadata={"type": "conversation"}
|
||||
),
|
||||
timeout=60.0
|
||||
)
|
||||
info("📝 [记忆添加] 已提交给 Mem0 进行事实提取")
|
||||
return True
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
error("❌ Mem0 记忆添加超时 (60s)")
|
||||
return False
|
||||
except Exception as e:
|
||||
error(f"❌ Mem0 记忆添加失败: {e}")
|
||||
return False
|
||||
async def add_memories(self, messages, user_id):
|
||||
if not self.mem0:
|
||||
return False
|
||||
try:
|
||||
start = time.time()
|
||||
info(f"📝 开始 Mem0 add,消息数: {len(messages)}")
|
||||
await asyncio.wait_for(
|
||||
self.mem0.add(messages, user_id=user_id, metadata={"type": "conversation"}),
|
||||
timeout=60.0
|
||||
)
|
||||
info(f"✅ Mem0 add 完成,耗时: {time.time() - start:.2f}s")
|
||||
return True
|
||||
except asyncio.TimeoutError:
|
||||
error(f"❌ Mem0 记忆添加超时 (60s),已等待 {time.time() - start:.2f}s")
|
||||
return False
|
||||
Reference in New Issue
Block a user