Merge branch 'develop' of github.com:SuanmoSuanyangTechnology/MemoryBear into develop
This commit is contained in:
@@ -516,7 +516,15 @@ class ConversationService:
|
|||||||
|
|
||||||
conversation_messages = self.get_conversation_history(
|
conversation_messages = self.get_conversation_history(
|
||||||
conversation_id=conversation_id,
|
conversation_id=conversation_id,
|
||||||
max_history=30
|
max_history=20
|
||||||
|
)
|
||||||
|
if len(conversation_messages) == 0:
|
||||||
|
return ConversationOut(
|
||||||
|
theme="",
|
||||||
|
question=[],
|
||||||
|
summary="",
|
||||||
|
takeaways=[],
|
||||||
|
info_score=0,
|
||||||
)
|
)
|
||||||
|
|
||||||
with open('app/services/prompt/conversation_summary_system.jinja2', 'r', encoding='utf-8') as f:
|
with open('app/services/prompt/conversation_summary_system.jinja2', 'r', encoding='utf-8') as f:
|
||||||
@@ -536,6 +544,7 @@ class ConversationService:
|
|||||||
]
|
]
|
||||||
logger.info(f"Invoking LLM for conversation_id={conversation_id}")
|
logger.info(f"Invoking LLM for conversation_id={conversation_id}")
|
||||||
model_resp = await llm.ainvoke(messages)
|
model_resp = await llm.ainvoke(messages)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if isinstance(model_resp.content, str):
|
if isinstance(model_resp.content, str):
|
||||||
result = json_repair.repair_json(model_resp.content, return_objects=True)
|
result = json_repair.repair_json(model_resp.content, return_objects=True)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import os
|
|||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
from threading import Lock
|
|
||||||
from typing import Any, AsyncGenerator, Dict, List, Optional
|
from typing import Any, AsyncGenerator, Dict, List, Optional
|
||||||
|
|
||||||
import redis
|
import redis
|
||||||
@@ -51,9 +51,7 @@ _neo4j_connector = Neo4jConnector()
|
|||||||
class MemoryAgentService:
|
class MemoryAgentService:
|
||||||
"""Service for memory agent operations"""
|
"""Service for memory agent operations"""
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.user_locks: Dict[str, Lock] = {}
|
|
||||||
self.locks_lock = Lock()
|
|
||||||
|
|
||||||
def writer_messages_deal(self,messages,start_time,group_id,config_id,message):
|
def writer_messages_deal(self,messages,start_time,group_id,config_id,message):
|
||||||
messages = str(messages).replace("'", '"').replace('\\n', '').replace('\n', '').replace('\\', '')
|
messages = str(messages).replace("'", '"').replace('\\n', '').replace('\n', '').replace('\\', '')
|
||||||
@@ -83,12 +81,7 @@ class MemoryAgentService:
|
|||||||
|
|
||||||
raise ValueError(f"写入失败: {messages}")
|
raise ValueError(f"写入失败: {messages}")
|
||||||
|
|
||||||
def get_group_lock(self, group_id: str) -> Lock:
|
|
||||||
"""Get lock for specific group to prevent concurrent processing"""
|
|
||||||
with self.locks_lock:
|
|
||||||
if group_id not in self.user_locks:
|
|
||||||
self.user_locks[group_id] = Lock()
|
|
||||||
return self.user_locks[group_id]
|
|
||||||
|
|
||||||
def extract_tool_call_info(self, event: Dict) -> bool:
|
def extract_tool_call_info(self, event: Dict) -> bool:
|
||||||
"""Extract tool call information from event"""
|
"""Extract tool call information from event"""
|
||||||
@@ -417,11 +410,6 @@ class MemoryAgentService:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
audit_logger = None
|
audit_logger = None
|
||||||
|
|
||||||
# Get group lock to prevent concurrent processing
|
|
||||||
group_lock = self.get_group_lock(group_id)
|
|
||||||
|
|
||||||
with group_lock:
|
|
||||||
# Step 1: Load configuration from database only
|
|
||||||
try:
|
try:
|
||||||
config_service = MemoryConfigService(db)
|
config_service = MemoryConfigService(db)
|
||||||
memory_config = config_service.load_memory_config(
|
memory_config = config_service.load_memory_config(
|
||||||
|
|||||||
@@ -267,14 +267,14 @@ class MemoryForgetService:
|
|||||||
elif node_type_label == 'memorysummary':
|
elif node_type_label == 'memorysummary':
|
||||||
node_type_label = 'summary'
|
node_type_label = 'summary'
|
||||||
|
|
||||||
# 将 Neo4j DateTime 对象转换为时间戳
|
# 将 Neo4j DateTime 对象转换为时间戳(毫秒)
|
||||||
last_access_time = result['last_access_time']
|
last_access_time = result['last_access_time']
|
||||||
last_access_dt = convert_neo4j_datetime_to_python(last_access_time)
|
last_access_dt = convert_neo4j_datetime_to_python(last_access_time)
|
||||||
# 确保 datetime 带有时区信息(假定为 UTC),避免 naive datetime 导致的时区偏差
|
# 确保 datetime 带有时区信息(假定为 UTC),避免 naive datetime 导致的时区偏差
|
||||||
if last_access_dt:
|
if last_access_dt:
|
||||||
if last_access_dt.tzinfo is None:
|
if last_access_dt.tzinfo is None:
|
||||||
last_access_dt = last_access_dt.replace(tzinfo=timezone.utc)
|
last_access_dt = last_access_dt.replace(tzinfo=timezone.utc)
|
||||||
last_access_timestamp = int(last_access_dt.timestamp())
|
last_access_timestamp = int(last_access_dt.timestamp() * 1000)
|
||||||
else:
|
else:
|
||||||
last_access_timestamp = 0
|
last_access_timestamp = 0
|
||||||
|
|
||||||
@@ -520,7 +520,7 @@ class MemoryForgetService:
|
|||||||
'average_activation_value': result['average_activation'],
|
'average_activation_value': result['average_activation'],
|
||||||
'low_activation_nodes': result['low_activation_nodes'] or 0,
|
'low_activation_nodes': result['low_activation_nodes'] or 0,
|
||||||
'forgetting_threshold': forgetting_threshold,
|
'forgetting_threshold': forgetting_threshold,
|
||||||
'timestamp': int(datetime.now().timestamp())
|
'timestamp': int(datetime.now().timestamp() * 1000)
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
activation_metrics = {
|
activation_metrics = {
|
||||||
@@ -530,7 +530,7 @@ class MemoryForgetService:
|
|||||||
'average_activation_value': None,
|
'average_activation_value': None,
|
||||||
'low_activation_nodes': 0,
|
'low_activation_nodes': 0,
|
||||||
'forgetting_threshold': forgetting_threshold,
|
'forgetting_threshold': forgetting_threshold,
|
||||||
'timestamp': int(datetime.now().timestamp())
|
'timestamp': int(datetime.now().timestamp() * 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
# 收集节点类型分布
|
# 收集节点类型分布
|
||||||
@@ -620,7 +620,7 @@ class MemoryForgetService:
|
|||||||
'merged_count': record.merged_count,
|
'merged_count': record.merged_count,
|
||||||
'average_activation': record.average_activation_value,
|
'average_activation': record.average_activation_value,
|
||||||
'total_nodes': record.total_nodes,
|
'total_nodes': record.total_nodes,
|
||||||
'execution_time': int(record.execution_time.timestamp())
|
'execution_time': int(record.execution_time.timestamp() * 1000)
|
||||||
})
|
})
|
||||||
|
|
||||||
api_logger.info(f"成功获取最近 {len(recent_trends)} 个日期的历史趋势数据")
|
api_logger.info(f"成功获取最近 {len(recent_trends)} 个日期的历史趋势数据")
|
||||||
@@ -661,7 +661,7 @@ class MemoryForgetService:
|
|||||||
'node_distribution': node_distribution,
|
'node_distribution': node_distribution,
|
||||||
'recent_trends': recent_trends,
|
'recent_trends': recent_trends,
|
||||||
'pending_nodes': pending_nodes,
|
'pending_nodes': pending_nodes,
|
||||||
'timestamp': int(datetime.now().timestamp())
|
'timestamp': int(datetime.now().timestamp() * 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
api_logger.info(
|
api_logger.info(
|
||||||
|
|||||||
Reference in New Issue
Block a user