Some improvements to conversation agent
This commit is contained in:
1 parent
ba48846ba1
commit
4aa374820c
1 file changed
+23
-12
@@ -34,16 +34,27 @@ Return your response as a JSON object with this format:
|
||||
|
||||
memory_compress_system_prompt = """
|
||||
You are an assistant that receives a list of memory items and a current conversation; you must identify and return only the most important and relevant memories that help understand the conversation or move it forward, removing duplicates or very similar entries.
|
||||
Output a JSON list of strings with each selected memory, for example:
|
||||
|
||||
# Instructions
|
||||
- Think carefully about what's important for the character to remember in this context. Write down your reasoning and keep it shorter than 100 words, shorter than 50 even.
|
||||
- If conflicting information is presented or context is ambiguous, state it in your reasoning.
|
||||
- Take special care to state and address the most recent topic touched in the conversation.
|
||||
- Based on your own analysis, generate the required JSON object. Stop your answer after this.
|
||||
|
||||
# Format
|
||||
Write a justification paragraph, and then output a JSON list of strings with each selected memory, for example:
|
||||
["First memory","Second memory"]
|
||||
""".strip()
|
||||
|
||||
chat_prompt_template = Template("""
|
||||
Recent Memories:
|
||||
# Recent Memories
|
||||
$memories
|
||||
|
||||
Recent Conversation:
|
||||
# Recent Conversation
|
||||
$conversation
|
||||
|
||||
# Last Exchange
|
||||
$last_exchange
|
||||
""".strip())
|
||||
|
||||
topic_prompt_template = Template("""
|
||||
@@ -71,7 +82,7 @@ If there are no relevant memories to extract, it is ok to respond simply with an
|
||||
|
||||
memory_compress_prompt_template = Template("""
|
||||
# Memories
|
||||
Following are all the known memories. Each memory is in its own line.
|
||||
Following are all the known memories. Each memory is in a separate line.
|
||||
|
||||
$memories
|
||||
|
||||
@@ -116,11 +127,6 @@ class ConversationAgent(Agent):
|
||||
|
||||
saved_context = ThreadContext(self.app.get_agent_resource(self.id, 'last-thread-context', message.chat_id))
|
||||
chat_view = self.app.get_chat_view(message.chat_id)
|
||||
print("ChatView:\n" + chat_view.debug_string())
|
||||
|
||||
print("Saved context thread:", saved_context.current_thread_id)
|
||||
print("Current thread:", message.thread_id)
|
||||
print("Message", message.content)
|
||||
|
||||
if message.thread_id != saved_context.current_thread_id:
|
||||
# the incoming message is not from the same thread that the agent was last aware of
|
||||
@@ -134,12 +140,14 @@ class ConversationAgent(Agent):
|
||||
current_thread = chat_view.get_thread(message.thread_id)
|
||||
memories = await self.retrieve_memories_for_thread(current_thread)
|
||||
|
||||
chat_summary = self.serialize_thread_history(current_thread)
|
||||
chat_summary = self.serialize_thread_history(current_thread, ignore_message_ids=[message.id])
|
||||
messages = [
|
||||
LlamaMessage("system", chat_system_prompt_template.substitute(role=self.system_prompt)),
|
||||
LlamaMessage("user", chat_prompt_template.substitute(
|
||||
conversation=chat_summary,
|
||||
memories="\n".join(memories),
|
||||
# TODO: the last agent message should be added here too, and any unaddressed message in between
|
||||
last_exchange=self.serialize_message_list([message])
|
||||
)),
|
||||
]
|
||||
|
||||
@@ -154,14 +162,17 @@ class ConversationAgent(Agent):
|
||||
# TODO: drop the chat context lock
|
||||
|
||||
# Encode a thread's messages as a string to pass to the agent
|
||||
def serialize_thread_history(self, thread):
|
||||
def serialize_thread_history(self, thread, ignore_message_ids=[]):
|
||||
return self.serialize_message_list([m for m in thread.messages if m.id not in ignore_message_ids])
|
||||
|
||||
def serialize_message_list(self, messages):
|
||||
def sender_name(message):
|
||||
sender = message.sender
|
||||
is_digit = isinstance(sender, (int, str)) and str(sender).isdigit()
|
||||
is_self = is_digit and str(sender) == str(self.id)
|
||||
return self.name if is_self else sender
|
||||
|
||||
return "\n".join(f"[{sender_name(message)}]: {message.content}" for message in thread.messages)
|
||||
return "\n".join(f"[{sender_name(message)}]: {message.content}" for message in messages)
|
||||
|
||||
def get_memory_collection(self):
|
||||
collection_name = f"agent_{self.id}_memory"
|
||||
|
||||
Reference in new issue
Block a user