Refactor the Action/Function components, and refactor the basic architecture of Agent Task/Todo.
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
from ast import Dict
|
||||
from datetime import timedelta
|
||||
from typing import List
|
||||
# pylint:disable=E0402
|
||||
from datetime import datetime,timedelta
|
||||
from typing import Dict
|
||||
|
||||
from ..frame.compute_kernel import ComputeKernel
|
||||
from ..proto.ai_function import SimpleAIOperation
|
||||
from ..proto.ai_function import SimpleAIAction
|
||||
from ..proto.agent_msg import AgentMsg, AgentMsgType
|
||||
|
||||
from .chatsession import *
|
||||
from .llm_context import GlobaToolsLibrary
|
||||
from .chatsession import AIChatSession
|
||||
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class AgentMemory:
|
||||
def __init__(self,agent_id:str,db_path:str) -> None:
|
||||
@@ -14,12 +20,16 @@ class AgentMemory:
|
||||
self.model_name:str = "gp4-1106-preview"
|
||||
self.threshold_hours = 72
|
||||
|
||||
self.actions = {}
|
||||
self.init_actions()
|
||||
|
||||
def init_actions(self) -> Dict:
|
||||
chatlog_append_op = SimpleAIOperation("chatlog_append","Append request & reply message to chatlog. No params",self.action_chatlog_append)
|
||||
self.actions[chatlog_append_op.get_name()] = chatlog_append_op
|
||||
@classmethod
|
||||
def register_actions(cls):
|
||||
async def action_chatlog_append(parms:Dict):
|
||||
memory = parms.get("_memory")
|
||||
if memory:
|
||||
return await memory.action_chatlog_append(parms)
|
||||
|
||||
chatlog_append_action = SimpleAIAction("chatlog_append","Append request & reply message to chatlog. No params",action_chatlog_append)
|
||||
GlobaToolsLibrary.get_instance().register_tool_function(chatlog_append_action,"agent.memory.chatlog.append")
|
||||
|
||||
|
||||
def get_session_from_msg(self,msg:AgentMsg) -> AIChatSession:
|
||||
if msg.msg_type == AgentMsgType.TYPE_GROUPMSG:
|
||||
@@ -32,8 +42,8 @@ class AgentMemory:
|
||||
|
||||
async def load_chatlogs(self,msg:AgentMsg,n:int=6,m:int=64,token_limit=800)->str:
|
||||
chatsession = self.get_session_from_msg(msg)
|
||||
# 必定加载n条(n>=2),期望加载m条
|
||||
# m条里的信息逐步添加,知道距离现在的时间未72小时以上,且消耗了足够的Token
|
||||
# Must load n (n> = 2), and hope to load the M
|
||||
# The information in the # M is gradually added, knowing that it is less than 72 hours from the current time, and consumes enough tokens
|
||||
|
||||
messages_n = chatsession.read_history(n) # read
|
||||
if len(messages_n) >= n:
|
||||
@@ -44,7 +54,7 @@ class AgentMemory:
|
||||
histroy_str = ""
|
||||
read_count = 0
|
||||
for msg in messages_n:
|
||||
dt = datetime.datetime.fromtimestamp(float(msg.create_time))
|
||||
dt = datetime.fromtimestamp(float(msg.create_time))
|
||||
formatted_time = dt.strftime('%y-%m-%d %H:%M:%S')
|
||||
record_str = f"{msg.sender},[{formatted_time}]\n{msg.body}\n"
|
||||
token_limit -= ComputeKernel.llm_num_tokens_from_text(record_str,self.model_name)
|
||||
@@ -57,9 +67,9 @@ class AgentMemory:
|
||||
if read_count < 3:
|
||||
logging.warning(f"read history {read_count} < 3, will not load more")
|
||||
|
||||
now = datetime.datetime.now()
|
||||
now = datetime.now()
|
||||
for msg in messages_m:
|
||||
dt = datetime.datetime.fromtimestamp(float(msg.create_time))
|
||||
dt = datetime.fromtimestamp(float(msg.create_time))
|
||||
time_diff = now - dt
|
||||
if time_diff > timedelta(hours=self.threshold_hours):
|
||||
break
|
||||
@@ -95,10 +105,18 @@ class AgentMemory:
|
||||
return "lzc is your master. Male, 40 years old, Mother tongue is Chinese, senior software engineer."
|
||||
return None
|
||||
|
||||
def get_actions(self) -> Dict:
|
||||
return self.actions
|
||||
async def update_contact_summary(self,contact_id:str,summary:str) -> str:
|
||||
return "OK"
|
||||
|
||||
async def get_sth_summary(self,sth_id:str) -> str:
|
||||
return None
|
||||
|
||||
async def update_sth_summary(self,sth_id:str,summary:str) -> str:
|
||||
return None
|
||||
|
||||
async def get_log_summary(self,msg:AgentMsg) -> str:
|
||||
return None
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user