mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
添加 长期记忆功能
This commit is contained in:
parent
0bdc205497
commit
5507287415
8 changed files with 591 additions and 9 deletions
|
|
@ -60,6 +60,7 @@ selected_module:
|
|||
LLM: ChatGLMLLM
|
||||
# TTS将根据配置名称对应的type调用实际的TTS适配器
|
||||
TTS: EdgeTTS
|
||||
EMBD: SentenceTransformer
|
||||
|
||||
ASR:
|
||||
FunASR:
|
||||
|
|
@ -72,6 +73,11 @@ VAD:
|
|||
model_dir: models/snakers4_silero-vad
|
||||
min_silence_duration_ms: 700 # 如果说话停顿比较长,可以把这个值设置大一些
|
||||
|
||||
EMBD:
|
||||
SentenceTransformer:
|
||||
model_dir: models/jina-embeddings-v2-base-zh
|
||||
device: "cpu"
|
||||
|
||||
LLM:
|
||||
# 当前支持的type为openai、dify、ollama,可自行适配
|
||||
AliLLM:
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import logging
|
|||
from typing import Dict, Any
|
||||
from copy import deepcopy
|
||||
from core.utils.util import get_project_dir
|
||||
from core.utils import asr, vad, llm, tts
|
||||
from core.utils import asr, vad, llm, tts, embedding
|
||||
|
||||
class PrivateConfig:
|
||||
def __init__(self, device_id: str, default_config: Dict[str, Any]):
|
||||
|
|
@ -173,6 +173,10 @@ class PrivateConfig:
|
|||
config["TTS"][selected_modules["TTS"]]["type"],
|
||||
config["TTS"][selected_modules["TTS"]],
|
||||
self.default_config.get("delete_audio", True) # Using default_config for global settings
|
||||
),
|
||||
embedding.create_instance(
|
||||
selected_modules["EMBD"],
|
||||
config["EMBD"][selected_modules["EMBD"]]
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -17,9 +17,12 @@ from concurrent.futures import ThreadPoolExecutor, TimeoutError
|
|||
from core.handle.audioHandle import handleAudioMessage, sendAudioMessage
|
||||
from config.private_config import PrivateConfig
|
||||
from core.auth import AuthMiddleware, AuthenticationError
|
||||
from core.utils.llm_memory import MemoryManager
|
||||
|
||||
|
||||
|
||||
class ConnectionHandler:
|
||||
def __init__(self, config: Dict[str, Any], _vad, _asr, _llm, _tts):
|
||||
def __init__(self, config: Dict[str, Any], _vad, _asr, _llm, _tts, _embd):
|
||||
self.config = config
|
||||
self.logger = logging.getLogger(__name__)
|
||||
self.auth = AuthMiddleware(config)
|
||||
|
|
@ -46,6 +49,7 @@ class ConnectionHandler:
|
|||
self.asr = _asr
|
||||
self.llm = _llm
|
||||
self.tts = _tts
|
||||
self.embd = _embd
|
||||
self.dialogue = None
|
||||
|
||||
# vad相关变量
|
||||
|
|
@ -76,6 +80,9 @@ class ConnectionHandler:
|
|||
|
||||
self.private_config = None
|
||||
|
||||
self.llm_memory = MemoryManager(llm=_llm, embd=_embd)
|
||||
|
||||
|
||||
async def handle_connection(self, ws):
|
||||
try:
|
||||
# 获取并验证headers
|
||||
|
|
@ -86,6 +93,7 @@ class ConnectionHandler:
|
|||
await self.auth.authenticate(self.headers)
|
||||
|
||||
device_id = self.headers.get("device-id", None)
|
||||
self.device_id = device_id
|
||||
|
||||
# Load private configuration if device_id is provided
|
||||
bUsePrivateConfig = self.config.get("use_private_config", False)
|
||||
|
|
@ -94,12 +102,13 @@ class ConnectionHandler:
|
|||
self.private_config = PrivateConfig(device_id, self.config)
|
||||
await self.private_config.load_or_create()
|
||||
# Create private instances using private config
|
||||
vad, asr, llm, tts = self.private_config.create_private_instances()
|
||||
vad, asr, llm, tts, embd = self.private_config.create_private_instances()
|
||||
if vad is not None and asr is not None and llm is not None and tts is not None:
|
||||
self.vad = vad
|
||||
self.asr = asr
|
||||
self.llm = llm
|
||||
self.tts = tts
|
||||
self.embd = embd
|
||||
|
||||
self.logger.info(f"Loaded private config and instances for device {device_id}")
|
||||
self.private_config.update_last_chat_time()
|
||||
|
|
@ -125,6 +134,7 @@ class ConnectionHandler:
|
|||
await self._route_message(message)
|
||||
except websockets.exceptions.ConnectionClosed:
|
||||
self.logger.info("客户端断开连接")
|
||||
await self.llm_memory.add_chat_paragraph(device_id,self.dialogue.get_llm_dialogue()[1:])
|
||||
await self.close()
|
||||
|
||||
except AuthenticationError as e:
|
||||
|
|
@ -133,6 +143,7 @@ class ConnectionHandler:
|
|||
return
|
||||
except Exception as e:
|
||||
self.logger.error(f"Connection error: {str(e)}")
|
||||
await self.llm_memory.add_chat_paragraph(device_id,self.dialogue.get_llm_dialogue()[1:])
|
||||
await ws.close()
|
||||
return
|
||||
|
||||
|
|
@ -153,14 +164,19 @@ class ConnectionHandler:
|
|||
self.prompt = self.prompt.replace("{date_time}", date_time)
|
||||
self.dialogue.put(Message(role="system", content=self.prompt))
|
||||
|
||||
def chat(self, query):
|
||||
def chat(self, query:str):
|
||||
query = self.llm_memory.handle_user_scene(self.device_id, query)
|
||||
self.dialogue.put(Message(role="user", content=query))
|
||||
response_message = []
|
||||
start = 0
|
||||
# 提交 LLM 任务
|
||||
try:
|
||||
start_time = time.time() # 记录开始时间
|
||||
llm_responses = self.llm.response(self.session_id, self.dialogue.get_llm_dialogue())
|
||||
memory_summary = self.llm_memory.get_memory_summary(self.device_id)
|
||||
llm_responses = self.llm.response(
|
||||
self.session_id,
|
||||
self.dialogue.get_llm_dialogue_with_memory(memory_summary)
|
||||
)
|
||||
except Exception as e:
|
||||
self.logger.error(f"LLM 处理出错 {query}: {e}")
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -19,6 +19,18 @@ class Dialogue:
|
|||
def put(self, message: Message):
|
||||
self.dialogue.append(message)
|
||||
|
||||
def get_llm_dialogue_with_memory(self, memory_data:str) -> List[Dict[str, str]]:
|
||||
dialogue = []
|
||||
|
||||
for m in self.dialogue:
|
||||
if m.role == "system" and len(memory_data.strip()) > 0:
|
||||
content = f"{m.content}\n\n你还保留着一些长期的记忆,这有助于让你的对话更加丰富和连贯:\n<start>{memory_data}</end>"
|
||||
else:
|
||||
content = m.content
|
||||
dialogue.append({"role": m.role, "content": content})
|
||||
|
||||
return dialogue
|
||||
|
||||
def get_llm_dialogue(self) -> List[Dict[str, str]]:
|
||||
dialogue = []
|
||||
for m in self.dialogue:
|
||||
|
|
|
|||
48
core/utils/embedding.py
Normal file
48
core/utils/embedding.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
from abc import ABC, abstractmethod
|
||||
import os
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class EMBD(ABC):
|
||||
@abstractmethod
|
||||
def encode(self, conn, data):
|
||||
pass
|
||||
|
||||
class SentenceEMBD(EMBD):
|
||||
def __init__(self, config: dict,):
|
||||
from sentence_transformers import SentenceTransformer,export_optimized_onnx_model
|
||||
self.model_dir = config.get("model_dir")
|
||||
self.device = config.get("device","cpu")
|
||||
# model_dir = "./models/jina-embeddings-v2-base-zh"
|
||||
if not os.path.exists(os.path.join(self.model_dir,"onnx","model_O3.onnx")):
|
||||
self.model = SentenceTransformer(
|
||||
self.model_dir,
|
||||
backend="onnx",
|
||||
device=self.device,
|
||||
model_kwargs={"file_name": "model.onnx"}
|
||||
)
|
||||
export_optimized_onnx_model(self.model, "O3", self.model_dir)
|
||||
logging.info(f"优化模型成功: {self.model_dir}")
|
||||
|
||||
self.model = SentenceTransformer(
|
||||
self.model_dir,
|
||||
backend="onnx",
|
||||
model_kwargs={"file_name": "onnx/model_O3.onnx"},
|
||||
device=self.device,
|
||||
)
|
||||
logging.info(f"加载模型成功: {self.model_dir}")
|
||||
|
||||
def encode(self, data):
|
||||
return self.model.encode(data)
|
||||
|
||||
def create_instance(class_name: str, *args, **kwargs) -> EMBD:
|
||||
"""工厂方法创建embedding实例"""
|
||||
|
||||
model_map = {
|
||||
"SentenceTransformer": SentenceEMBD,
|
||||
}
|
||||
|
||||
if cls := model_map.get(class_name):
|
||||
return cls(*args, **kwargs)
|
||||
raise ValueError(f"不支持的embedding类型: {class_name}")
|
||||
491
core/utils/llm_memory.py
Normal file
491
core/utils/llm_memory.py
Normal file
|
|
@ -0,0 +1,491 @@
|
|||
import hashlib
|
||||
import threading
|
||||
import json
|
||||
import os
|
||||
from collections import deque
|
||||
import uuid
|
||||
|
||||
from numpy.linalg import norm
|
||||
import asyncio
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import numpy as np
|
||||
from datetime import datetime
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
# Cosine similarity function
|
||||
cos_sim = lambda a, b: (a @ b.T) / (norm(a) * norm(b))
|
||||
|
||||
|
||||
import numpy as np
|
||||
|
||||
class IntentRecognizer:
|
||||
intent_embeddings_cache = {}
|
||||
cache_lock = threading.Lock()
|
||||
def __init__(self, embedding_model):
|
||||
self.embedding_model = embedding_model
|
||||
# 存储注册的意图和相似度阈值以及对应的回调函数
|
||||
self.intent_callbacks = []
|
||||
|
||||
def register_intent(self, intent_phrases, callback_function, similarity_threshold=0.7):
|
||||
"""
|
||||
注册一个意图
|
||||
:param intent_phrases: 意图相关的短语列表
|
||||
:param callback_function: 对应的回调函数
|
||||
:param similarity_threshold: 意图与查询的相似度阈值,默认为0.7
|
||||
"""
|
||||
with self.cache_lock:
|
||||
for phrase in intent_phrases:
|
||||
# 如果该短语的嵌入已经缓存,则直接使用缓存
|
||||
if phrase not in IntentRecognizer.intent_embeddings_cache:
|
||||
# 计算嵌入并缓存
|
||||
intent_embedding = self.embedding_model.encode([phrase])[0]
|
||||
IntentRecognizer.intent_embeddings_cache[phrase] = intent_embedding
|
||||
else:
|
||||
# 使用缓存的嵌入
|
||||
intent_embedding = IntentRecognizer.intent_embeddings_cache[phrase]
|
||||
|
||||
# 存储每个意图短语及其对应的回调函数和相似度阈值
|
||||
self.intent_callbacks.append({
|
||||
"phrase": phrase,
|
||||
"embedding": intent_embedding,
|
||||
"callback": callback_function,
|
||||
"threshold": similarity_threshold
|
||||
})
|
||||
|
||||
def handle_query(self, query: str, *args, **kwargs):
|
||||
"""
|
||||
处理用户查询并进行意图识别,调用匹配的回调函数
|
||||
:param query: 用户查询
|
||||
:param args: 任意位置参数传递给回调函数
|
||||
:param kwargs: 任意关键字参数传递给回调函数
|
||||
:return: 回调函数的返回值
|
||||
"""
|
||||
query = query.strip()
|
||||
# 计算用户查询的嵌入表示
|
||||
query_embedding = self.embedding_model.encode([query])[0]
|
||||
|
||||
best_match = None
|
||||
highest_similarity = -1
|
||||
matched_callback = None
|
||||
|
||||
# 比较查询与注册的意图短语的相似度
|
||||
for intent in self.intent_callbacks:
|
||||
similarity = np.dot(query_embedding, intent["embedding"]) / (np.linalg.norm(query_embedding) * np.linalg.norm(intent["embedding"]))
|
||||
if similarity > highest_similarity and similarity >= intent["threshold"]:
|
||||
highest_similarity = similarity
|
||||
best_match = intent["phrase"]
|
||||
matched_callback = intent["callback"]
|
||||
|
||||
if best_match and matched_callback:
|
||||
# 调用与匹配意图对应的回调函数,传递位置参数和关键字参数
|
||||
logger.info(f"{query}\t意图识别结果:{best_match}\tfunc:{matched_callback}\t (相似度: {highest_similarity:.2f})")
|
||||
return matched_callback(query,*args, **kwargs)
|
||||
logger.info(f"{query}\t未识别到意图")
|
||||
return None
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class MemoryManager:
|
||||
_instance = None
|
||||
_lock = threading.Lock()
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
with cls._lock:
|
||||
if cls._instance is None:
|
||||
cls._instance = super(MemoryManager, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
llm,
|
||||
embd,
|
||||
summary_prompt: str = "请总结下面的聊天内容,必须在{content_len}字数以内,必须包含聊天的完整细节:\n\n{content}",
|
||||
max_memory_length: int = 1000,
|
||||
summary_length: int = 1000,
|
||||
max_summary_length: int = 2000,
|
||||
|
||||
memory_dir='memory_data'
|
||||
):
|
||||
"""
|
||||
初始化记忆管理类
|
||||
:param embedding_model: 用于生成文本嵌入的模型
|
||||
:param max_memory_length: 最大记忆总字符长度
|
||||
:param summary_length: 每次生成的总结的长度
|
||||
:param memory_file: 保存记忆数据的文件路径
|
||||
"""
|
||||
self.max_memory_length = max_memory_length
|
||||
self.summary_length = summary_length
|
||||
self.max_summary_length = max_summary_length
|
||||
|
||||
self.memory_data = {} # 用字典管理每个用户的记忆
|
||||
self.lock = threading.Lock() # 锁对象,用于线程同步
|
||||
self.summary_prompt = summary_prompt
|
||||
self.llm = llm
|
||||
self.embedding_model = embd # 用于生成文本的嵌入
|
||||
|
||||
self.memory_dir = memory_dir
|
||||
if not os.path.exists(self.memory_dir):
|
||||
os.makedirs(self.memory_dir,exist_ok=True) # 创建目录
|
||||
|
||||
# 确保目录具有读写权限
|
||||
if not os.access(self.memory_dir, os.W_OK):
|
||||
try:
|
||||
os.chmod(self.memory_dir, 0o700) # 设置读写权限
|
||||
logger.info(f"已为目录 {self.memory_dir} 设置读写权限")
|
||||
except Exception as e:
|
||||
logger.warning(f"无法修改 {self.memory_dir} 的权限: {e}")
|
||||
raise PermissionError(f"没有足够权限访问目录: {self.memory_dir}")
|
||||
# 加载已有记忆或初始化新的记忆
|
||||
self.load_memory()
|
||||
|
||||
# ThreadPoolExecutor for async tasks
|
||||
self.executor = ThreadPoolExecutor()
|
||||
|
||||
|
||||
self.intent_recognizer = IntentRecognizer(self.embedding_model)
|
||||
self.intent_recognizer.register_intent(
|
||||
["回忆对话", "回忆上次讲的", "请回忆","有什么记忆","刚才讲了什么","上次讲了什么"],
|
||||
self._recall_last_conversation,
|
||||
similarity_threshold=0.7
|
||||
)
|
||||
self.intent_recognizer.register_intent(
|
||||
["删除我的记忆","删除所有记忆"],
|
||||
self._del_all,
|
||||
similarity_threshold=0.9
|
||||
)
|
||||
|
||||
def _recall_last_conversation(self,query: str, token_name: str)->str:
|
||||
"""
|
||||
回忆上次的对话
|
||||
:param token_name: 用户唯一标识符
|
||||
:param query: 用户查询
|
||||
:return: 上次的对话内容
|
||||
"""
|
||||
user_memory = self.search_memory(token_name, query, top_k=5)
|
||||
|
||||
|
||||
if len(user_memory) == 0:
|
||||
|
||||
user_memory = self.memory_data.get(token_name, {'memory':[]})
|
||||
|
||||
user_memory = list(user_memory['memory'])
|
||||
user_memory = user_memory[-10:]
|
||||
|
||||
|
||||
|
||||
output = "\n".join([msg['content'] for msg in user_memory])
|
||||
|
||||
|
||||
return f"{query}\n\n我们上次聊到了:\n\n{output}"
|
||||
|
||||
def _generate_summary(self, chat_paragraph: list):
|
||||
"""
|
||||
生成聊天内容的总结(可以根据具体要求定制总结逻辑)
|
||||
:param chat_content: 聊天内容
|
||||
:return: 总结的字符串
|
||||
"""
|
||||
if self.llm is None:
|
||||
return "".join([msg['content'] for msg in chat_paragraph])[:self.summary_length]
|
||||
|
||||
response_message = []
|
||||
try:
|
||||
llm_responses = self.llm.response(
|
||||
str(uuid.uuid4()),
|
||||
[
|
||||
{
|
||||
"role": "user",
|
||||
"content": self.summary_prompt.format(content=json.dumps(chat_paragraph),content_len=self.summary_length)
|
||||
}
|
||||
]
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"LLM 处理出错 {query}: {e}")
|
||||
return None
|
||||
|
||||
for content in llm_responses:
|
||||
response_message.append(content)
|
||||
return "".join(response_message)
|
||||
|
||||
def _forget_old_memory(self, token_name: str):
|
||||
"""
|
||||
超过最大记忆总长度时,删除不重要的记忆
|
||||
:param token_name: 用户唯一标识符
|
||||
"""
|
||||
user_memory = self.memory_data.get(token_name, {'memory': deque(), 'total_length': 0})
|
||||
|
||||
while user_memory['total_length'] > self.max_memory_length and user_memory['memory']:
|
||||
# 删除最旧的聊天内容和对应的嵌入
|
||||
oldest_message = user_memory['memory'].popleft()
|
||||
oldest_embedding = user_memory['embeddings'].popleft() # Remove corresponding embedding
|
||||
user_memory['total_length'] -= len(oldest_message['content'])
|
||||
|
||||
|
||||
|
||||
def _update_summary(self, token_name: str, chat_paragraph: list[dict]):
|
||||
"""
|
||||
更新记忆总结,合并每个聊天段落的内容并生成摘要
|
||||
:param token_name: 用户唯一标识符
|
||||
"""
|
||||
|
||||
if len(chat_paragraph) == 0:
|
||||
return
|
||||
|
||||
summary = self._generate_summary(chat_paragraph)
|
||||
|
||||
if summary is None or len(summary.strip()) == 0:
|
||||
return
|
||||
|
||||
self.memory_data[token_name].setdefault('memory_summary', []).append(summary)
|
||||
|
||||
# 生成并保存摘要的嵌入
|
||||
summary_embedding = self.embedding_model.encode([summary])[0]
|
||||
self.memory_data[token_name].setdefault('summary_embeddings', []).append(summary_embedding)
|
||||
|
||||
def _combine_summary(self, token_name: str):
|
||||
"""
|
||||
将所有的记忆总结合并为一个
|
||||
:param token_name: 用户唯一标识符
|
||||
"""
|
||||
|
||||
user_memory = self.memory_data.get(token_name, {'memory_summary': []})
|
||||
|
||||
# 如果没有超过最大总结长度,直接返回
|
||||
old_summary = "".join(user_memory['memory_summary'])
|
||||
if len(user_memory) == 0 or len(old_summary) < self.max_memory_length:
|
||||
logger.debug("不需要合并,总结长度:", len(old_summary))
|
||||
return
|
||||
logger.debug("合并前的总结长度:", len(old_summary))
|
||||
summary = self._generate_summary(user_memory['memory_summary'])
|
||||
logger.debug("合并后的总结长度:", len(summary))
|
||||
self.memory_data[token_name]['memory_summary'] = [summary]
|
||||
|
||||
# 生成并保存摘要的嵌入
|
||||
summary_embedding = self.embedding_model.encode([summary])[0]
|
||||
self.memory_data[token_name]['summary_embeddings'] = [summary_embedding]
|
||||
|
||||
def _del_all(self, query:str, token_name: str)->str:
|
||||
"""
|
||||
删除所有记忆
|
||||
:param token_name: 用户唯一标识符
|
||||
"""
|
||||
self.memory_data[token_name] = {'memory': deque(), 'total_length': 0, 'memory_summary': [], 'summary_embeddings': [], 'embeddings': deque()}
|
||||
self.save_memory()
|
||||
return f"你必须{query}\n\n你必须回答同意删除所有记忆"
|
||||
|
||||
async def add_chat_paragraph(self, token_name: str, chat_paragraph: list[dict]):
|
||||
"""
|
||||
添加一段聊天内容到记忆中
|
||||
:param token_name: 用户唯一标识符
|
||||
:param chat_paragraph: 聊天段落,包含多条聊天记录
|
||||
"""
|
||||
loop = asyncio.get_event_loop()
|
||||
await loop.run_in_executor(self.executor, self._add_chat_paragraph, token_name, chat_paragraph)
|
||||
|
||||
def _add_chat_paragraph(self, token_name: str, chat_paragraph: list[dict]):
|
||||
with self.lock: # 使用锁保证线程安全
|
||||
if token_name not in self.memory_data:
|
||||
self.memory_data[token_name] = {'memory': deque(), 'total_length': 0, 'memory_summary': [], 'summary_embeddings': [], 'embeddings': deque()}
|
||||
|
||||
user_memory = self.memory_data[token_name]
|
||||
new_embeddings = []
|
||||
|
||||
for message in chat_paragraph:
|
||||
role = message['role']
|
||||
content = message['content']
|
||||
|
||||
chat_hash = hashlib.md5(content.encode()).hexdigest()
|
||||
|
||||
user_memory['memory'].append({"role": role, "content": content, "hash": chat_hash, "datatime": "{:%Y-%m-%d %H:%M:%S}".format(datetime.now())})
|
||||
user_memory['total_length'] += len(content)
|
||||
|
||||
# 生成嵌入
|
||||
embedding = self.embedding_model.encode([content])[0] # 使用嵌入模型生成嵌入
|
||||
user_memory['embeddings'].append(embedding)
|
||||
|
||||
# 更新记忆总结
|
||||
self._update_summary(token_name, chat_paragraph)
|
||||
|
||||
# 如果记忆超出了限制,删除不重要的内容
|
||||
self._forget_old_memory(token_name)
|
||||
|
||||
# 合并压缩记忆总结
|
||||
self._combine_summary(token_name)
|
||||
|
||||
# 保存记忆到文件
|
||||
self.save_memory()
|
||||
|
||||
def handle_user_scene(self,token_name:str, query: str) -> str:
|
||||
"""
|
||||
解析用户查询
|
||||
:param query: 用户查询
|
||||
:return: 解析后的查询
|
||||
"""
|
||||
query = query.strip()
|
||||
if len(query) == 0:
|
||||
return query
|
||||
|
||||
result = self.intent_recognizer.handle_query(query, token_name)
|
||||
if result is None:
|
||||
return query
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def get_memory_summary(self, token_name: str) -> str:
|
||||
"""
|
||||
获取当前用户的记忆总结
|
||||
:param token_name: 用户唯一标识符
|
||||
:return: 当前的总结
|
||||
"""
|
||||
user_memory = self.memory_data.get(token_name, {'memory_summary': []})
|
||||
return "\n\n".join(user_memory['memory_summary'])
|
||||
|
||||
def get_full_memory(self, token_name: str) -> list[str]:
|
||||
"""
|
||||
获取当前用户的完整记忆内容
|
||||
:param token_name: 用户唯一标识符
|
||||
:return: 当前的所有记忆内容
|
||||
"""
|
||||
user_memory = self.memory_data.get(token_name, {'memory': []})
|
||||
return [msg['content'] for msg in user_memory['memory']]
|
||||
|
||||
def search_memory(self, token_name: str, query: str, top_k: int = 5, threshold: float = 0.5):
|
||||
"""
|
||||
使用嵌入进行记忆查询
|
||||
:param token_name: 用户唯一标识符
|
||||
:param query: 查询内容
|
||||
:param top_k: 返回最相似的记忆条数
|
||||
:param threshold: 相似度阈值,低于此阈值的结果将被忽略
|
||||
:return: 最相似的记忆条目
|
||||
"""
|
||||
user_memory = self.memory_data.get(token_name, {'embeddings': []})
|
||||
if not user_memory['embeddings']:
|
||||
return []
|
||||
|
||||
# 生成查询的嵌入
|
||||
query_embedding = self.embedding_model.encode([query])[0]
|
||||
|
||||
# 计算查询与记忆中每条消息的相似度
|
||||
similarities = [cos_sim(query_embedding, embedding) for embedding in user_memory['embeddings']]
|
||||
|
||||
# 获取最相似的记忆条目,过滤相似度低于阈值的结果
|
||||
filtered_indices = [i for i, sim in enumerate(similarities) if sim >= threshold]
|
||||
top_indices = sorted(filtered_indices, key=lambda i: similarities[i], reverse=True)[:top_k]
|
||||
similar_memory = [user_memory['memory'][i] for i in top_indices]
|
||||
|
||||
return similar_memory
|
||||
|
||||
def search_memory_summary(self, token_name: str, query: str, top_k: int = 5, threshold: float = 0.5):
|
||||
"""
|
||||
使用嵌入进行记忆总结查询
|
||||
:param token_name: 用户唯一标识符
|
||||
:param query: 查询内容
|
||||
:param top_k: 返回最相似的记忆条数
|
||||
:param threshold: 相似度阈值,低于此阈值的结果将被忽略
|
||||
:return: 最相似的记忆总结条目
|
||||
"""
|
||||
user_memory = self.memory_data.get(token_name, {'memory_summary': [], 'summary_embeddings': []})
|
||||
if not user_memory['summary_embeddings']:
|
||||
return []
|
||||
|
||||
# 生成查询的嵌入
|
||||
query_embedding = self.embedding_model.encode([query])[0]
|
||||
|
||||
# 计算查询与记忆总结的相似度
|
||||
similarities = [cos_sim(query_embedding, embedding) for embedding in user_memory['summary_embeddings']]
|
||||
|
||||
# 获取最相似的记忆总结条目,过滤相似度低于阈值的结果
|
||||
filtered_indices = [i for i, sim in enumerate(similarities) if sim >= threshold]
|
||||
top_indices = sorted(filtered_indices, key=lambda i: similarities[i], reverse=True)[:top_k]
|
||||
similar_summary = [user_memory['memory_summary'][i] for i in top_indices]
|
||||
|
||||
return similar_summary
|
||||
def save_memory(self):
|
||||
"""
|
||||
将记忆保存到本地文件,每个文件以token_name作为文件名
|
||||
"""
|
||||
try:
|
||||
for token_name, user_data in self.memory_data.items():
|
||||
memory_data_serializable = {
|
||||
'memory': list(user_data['memory']),
|
||||
'total_length': user_data['total_length'],
|
||||
'memory_summary': user_data['memory_summary'],
|
||||
'summary_embeddings': [embedding.tolist() for embedding in user_data['summary_embeddings']],
|
||||
'embeddings': [embedding.tolist() for embedding in user_data['embeddings']]
|
||||
}
|
||||
|
||||
file_path = os.path.join(self.memory_dir, f"{token_name}.json")
|
||||
with open(file_path, 'w', encoding='utf-8') as file:
|
||||
json.dump(memory_data_serializable, file, ensure_ascii=False, indent=4)
|
||||
logger.info(f"记忆已保存至 {file_path}")
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"保存记忆时出错: {e}")
|
||||
|
||||
def load_memory(self):
|
||||
"""
|
||||
从本地文件加载记忆数据
|
||||
"""
|
||||
try:
|
||||
self.memory_data = {}
|
||||
|
||||
for file_name in os.listdir(self.memory_dir):
|
||||
if file_name.endswith('.json'):
|
||||
token_name = file_name[:-5] # 去掉 .json 后缀
|
||||
file_path = os.path.join(self.memory_dir, file_name)
|
||||
|
||||
with open(file_path, 'r', encoding='utf-8') as file:
|
||||
try:
|
||||
memory_data = json.load(file)
|
||||
except Exception as e:
|
||||
logger.warning(f"加载记忆{file_path}时出错: {e}")
|
||||
continue
|
||||
|
||||
self.memory_data[token_name] = {
|
||||
'memory': deque(memory_data['memory']),
|
||||
'total_length': memory_data['total_length'],
|
||||
'memory_summary': memory_data['memory_summary'],
|
||||
'summary_embeddings': [np.array(embedding) for embedding in memory_data['summary_embeddings']],
|
||||
'embeddings': deque([np.array(embedding) for embedding in memory_data['embeddings']])
|
||||
}
|
||||
logger.info(f"记忆已从 {file_path} 加载")
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"加载记忆时出错: {e}")
|
||||
self.memory_data = {}
|
||||
|
||||
if __name__ == '__main__':
|
||||
from sentence_transformers import SentenceTransformer
|
||||
# 示例使用
|
||||
embedding_model = SentenceTransformer('jinaai/jina-embeddings-v2-base-zh', trust_remote_code=True, device="cpu")
|
||||
memory_manager = MemoryManager(llm=None, embedding_model=embedding_model, max_memory_length=100)
|
||||
|
||||
# 模拟两段对话
|
||||
chat_paragraph1 = [
|
||||
{"role": "user", "content": "吹到了你的发。😔"},
|
||||
{"role": "assistant", "content": "发丝轻拂,独怜此际,梦断谁家?😔"}
|
||||
]
|
||||
|
||||
chat_paragraph2 = [
|
||||
{"role": "user", "content": "今天天气。😔"},
|
||||
{"role": "assistant", "content": "晴天?😔"}
|
||||
]
|
||||
|
||||
# 添加第一段聊天
|
||||
asyncio.run(memory_manager.add_chat_paragraph("user1", chat_paragraph1))
|
||||
|
||||
# 添加第二段聊天
|
||||
asyncio.run(memory_manager.add_chat_paragraph("user1", chat_paragraph2))
|
||||
|
||||
# 使用嵌入进行记忆查询
|
||||
query = "今天天气?"
|
||||
similar_memory = memory_manager.search_memory("user1", query, threshold=0.5)
|
||||
print("最相似的记忆内容:", [msg['content'] for msg in similar_memory])
|
||||
|
||||
# 使用嵌入进行记忆总结查询
|
||||
similar_summary = memory_manager.search_memory_summary("user1", query, threshold=0.5)
|
||||
print("最相似的记忆总结:", similar_summary)
|
||||
|
|
@ -3,14 +3,14 @@ import websockets
|
|||
import logging
|
||||
from core.connection import ConnectionHandler
|
||||
from core.utils.util import get_local_ip
|
||||
from core.utils import asr, vad, llm, tts
|
||||
from core.utils import asr, vad, llm, tts, embedding
|
||||
|
||||
|
||||
class WebSocketServer:
|
||||
def __init__(self, config: dict):
|
||||
self.config = config
|
||||
self.logger = logging.getLogger(__name__)
|
||||
self._vad, self._asr, self._llm, self._tts = self._create_processing_instances()
|
||||
self._vad, self._asr, self._llm, self._tts, self._embd = self._create_processing_instances()
|
||||
|
||||
def _create_processing_instances(self):
|
||||
"""创建处理模块实例"""
|
||||
|
|
@ -38,6 +38,10 @@ class WebSocketServer:
|
|||
self.config["TTS"][self.config["selected_module"]["TTS"]]["type"],
|
||||
self.config["TTS"][self.config["selected_module"]["TTS"]],
|
||||
self.config["delete_audio"]
|
||||
),
|
||||
embedding.create_instance(
|
||||
self.config["selected_module"]["EMBD"],
|
||||
self.config["EMBD"][self.config["selected_module"]["EMBD"]]
|
||||
)
|
||||
)
|
||||
|
||||
|
|
@ -56,5 +60,5 @@ class WebSocketServer:
|
|||
|
||||
async def _handle_connection(self, websocket):
|
||||
"""处理新连接,每次创建独立的ConnectionHandler"""
|
||||
handler = ConnectionHandler(self.config, self._vad, self._asr, self._llm, self._tts)
|
||||
handler = ConnectionHandler(self.config, self._vad, self._asr, self._llm, self._tts, self._embd)
|
||||
await handler.handle_connection(websocket)
|
||||
|
|
|
|||
|
|
@ -15,3 +15,4 @@ aiohttp==3.9.3
|
|||
aiohttp_cors==0.7.0
|
||||
ormsgpack==1.7.0
|
||||
ruamel.yaml==0.18.10
|
||||
sentence-transformers[onnx]==3.4.1
|
||||
|
|
|
|||
Loading…
Reference in a new issue