[docs]defbuild_system_message()->str:""" Builds the system message by replacing placeholders in the base system message with corresponding values from the configuration. :return: The formatted system message. :rtype: str """pattern=r"\{([^}]*)\}"base_system_message=config_toml["openai"]["agent"]["system_message"]matches=re.finditer(pattern,config_toml["openai"]["agent"]["system_message"])system_message_format=""end_index=0config_format_json=workdir.config_format(config_toml)formatchinmatches:start_index=match.start()system_message_format+=base_system_message[end_index:start_index]end_index=match.end()system_message_format+=config_format_json.get(base_system_message[start_index+1:end_index-1],"")system_message_format+=base_system_message[end_index:]returnsystem_message_format
[docs]@dataclassclassUserMemory:""" Represents the memory for a user, including the system message and a conversation buffer. :param system_message: The initial system message. :type system_message: SystemMessage :param k: The size of the conversation buffer. :type k: int :param memory: The conversation buffer memory. :type memory: ConversationBufferWindowMemory """system_message:SystemMessagek:intmemory:ConversationBufferWindowMemory
[docs]@classmethoddefcreate_ai_instance(cls,k:int)->"UserMemory":""" Creates an instance of UserMemory with the specified buffer size. :param k: The size of the conversation buffer. :type k: int :return: A new instance of UserMemory. :rtype: UserMemory """system_message=SystemMessage(content=build_system_message())returncls(system_message=system_message,k=k,memory=ConversationBufferWindowMemory(memory_key="chat_history",k=k,return_messages=True,chat_memory=ChatMessageHistory(messages=[system_message]),),)
[docs]defclear_history(self):""" Clears the chat history while retaining the initial system message. """self.memory.chat_memory.messages.clear()self.memory.chat_memory.messages.append(self.system_message)
[docs]defget_memory(self)->ConversationBufferWindowMemory:""" Retrieves the current conversation buffer memory, ensuring its size does not exceed 2*k messages. :return: The conversation buffer memory. :rtype: ConversationBufferWindowMemory """ifself.memory.chat_memory.messages.__len__()>self.k*2-1:self.memory.chat_memory.messages=[self.system_message,*self.memory.chat_memory.messages[:-self.k*2],]returnself.memory
[docs]classAIMemory:""" Manages the memory for multiple users, each with their own conversation buffer. :param k: The default size of the conversation buffer. :type k: int, optional, default=13 """def__init__(self,k:int=13):self.k=kself.memory:Dict[str,UserMemory]={}
[docs]defget_memory(self,id:str)->ConversationBufferWindowMemory:""" Retrieves the memory for a given user. If the user does not have memory initialized, it creates a new instance. :param id: The identifier for the user. :type id: str :return: The user's conversation buffer memory. :rtype: ConversationBufferWindowMemory """memoize=self.memory.get(id)ifnotmemoize:memoize=UserMemory.create_ai_instance(config_toml["openai"]["agent"]["memory_size"])self.memory[id]=memoizereturnmemoize.memory
[docs]defdelete_memory(self,id:str):""" Deletes the memory for a given user. :param id: The identifier for the user. :type id: str """ifself.memory.get(id):delself.memory[id]
[docs]defclear_history(self,id:str):""" Clears the chat history for a given user while retaining their initial system message. :param id: The identifier for the user. :type id: str """ifself.memory.get(id):self.memory[id].clear_history()