mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-25 16:43:55 +08:00
增加智控台管理【本地记忆】功能
This commit is contained in:
@@ -144,6 +144,21 @@ def get_agent_models(
|
||||
},
|
||||
)
|
||||
|
||||
def save_mem_local_short(
|
||||
mac_address: str, short_momery: str
|
||||
) -> Optional[Dict]:
|
||||
try:
|
||||
return ManageApiClient._instance._execute_request(
|
||||
"PUT",
|
||||
f"/agent/device/" + mac_address,
|
||||
json={
|
||||
"summaryMemory": short_momery,
|
||||
},
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"存储短期记忆到服务器失败: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def report(
|
||||
mac_address: str, session_id: str, chat_type: int, content: str, audio
|
||||
|
||||
@@ -388,6 +388,8 @@ class ConnectionHandler:
|
||||
]["Intent"]
|
||||
if private_config.get("prompt", None) is not None:
|
||||
self.config["prompt"] = private_config["prompt"]
|
||||
if private_config.get("summaryMemory", None) is not None:
|
||||
self.config["summaryMemory"] = private_config["summaryMemory"]
|
||||
if private_config.get("device_max_output_size", None) is not None:
|
||||
self.max_output_size = int(private_config["device_max_output_size"])
|
||||
if private_config.get("chat_history_conf", None) is not None:
|
||||
@@ -421,7 +423,11 @@ class ConnectionHandler:
|
||||
|
||||
def _initialize_memory(self):
|
||||
"""初始化记忆模块"""
|
||||
self.memory.init_memory(self.device_id, self.llm)
|
||||
self.memory.init_memory(
|
||||
self.device_id,
|
||||
self.llm,
|
||||
self.config["summaryMemory"]
|
||||
)
|
||||
|
||||
def _initialize_intent(self):
|
||||
self.intent_type = self.config["Intent"][
|
||||
|
||||
@@ -4,6 +4,7 @@ import json
|
||||
import os
|
||||
import yaml
|
||||
from config.config_loader import get_project_dir
|
||||
from config.manage_api_client import save_mem_local_short
|
||||
|
||||
|
||||
short_term_memory_prompt = """
|
||||
@@ -93,17 +94,22 @@ TAG = __name__
|
||||
|
||||
|
||||
class MemoryProvider(MemoryProviderBase):
|
||||
def __init__(self, config):
|
||||
def __init__(self, config, summary_memory):
|
||||
super().__init__(config)
|
||||
self.short_momery = ""
|
||||
self.memory_path = get_project_dir() + "data/.memory.yaml"
|
||||
self.load_memory()
|
||||
self.load_memory(summary_memory)
|
||||
|
||||
def init_memory(self, role_id, llm):
|
||||
def init_memory(self, role_id, llm, summary_memory=None):
|
||||
super().init_memory(role_id, llm)
|
||||
self.load_memory()
|
||||
self.load_memory(summary_memory)
|
||||
|
||||
def load_memory(self, summary_memory):
|
||||
# api获取到总结记忆后直接返回
|
||||
if summary_memory:
|
||||
self.short_momery = summary_memory
|
||||
return
|
||||
|
||||
def load_memory(self):
|
||||
all_memory = {}
|
||||
if os.path.exists(self.memory_path):
|
||||
with open(self.memory_path, "r", encoding="utf-8") as f:
|
||||
@@ -152,6 +158,7 @@ class MemoryProvider(MemoryProviderBase):
|
||||
print("Error:", e)
|
||||
|
||||
self.save_memory_to_file()
|
||||
save_mem_local_short(self.role_id, self.short_momery)
|
||||
logger.bind(tag=TAG).info(f"Save memory successful - Role: {self.role_id}")
|
||||
|
||||
return self.short_momery
|
||||
|
||||
@@ -319,6 +319,7 @@ def initialize_modules(
|
||||
modules["memory"] = memory.create_instance(
|
||||
memory_type,
|
||||
config["Memory"][select_memory_module],
|
||||
config['summaryMemory'],
|
||||
)
|
||||
logger.bind(tag=TAG).info(f"初始化组件: memory成功 {select_memory_module}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user