separate and improve conversation agent
This commit is contained in:
1 parent
951ceaa05b
commit
3c3febb92d
6 files changed
+161
-109
No files matched your search
@@ -75,92 +75,6 @@ class DebugAgent(Agent):
|
||||
thread_id=chat_view.current_thread_id,
|
||||
)
|
||||
|
||||
class ConversationAgent(Agent):
|
||||
identifier = "conversation"
|
||||
schema_fields = []
|
||||
tools = []
|
||||
|
||||
def __init__(self, app, data):
|
||||
Agent.__init__(self, app, data)
|
||||
|
||||
# Constant system prompt for developing purposes
|
||||
self.system_prompt = "You are a friendly AI having a casual conversation with the user."
|
||||
|
||||
async def prompt(self, content):
|
||||
# TODO: Advanced features
|
||||
messages = [
|
||||
LlamaMessage("system", self.system_prompt),
|
||||
LlamaMessage("user", content)
|
||||
]
|
||||
response = await replicate_llama_completion(self.app, messages)
|
||||
return response
|
||||
|
||||
async def received_message(self, message):
|
||||
|
||||
# TODO: before retrieving the chat context, acquire a lock for it
|
||||
|
||||
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())
|
||||
|
||||
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
|
||||
# store details about the old thread in the agent's long term memory
|
||||
old_thread = chat_view.get_thread(saved_context.current_thread_id)
|
||||
await self.process_thread(old_thread)
|
||||
|
||||
# TODO: This is a good place to store the new context, its fine that the agent hasnt responded
|
||||
|
||||
current_thread = chat_view.get_thread(message.thread_id)
|
||||
|
||||
# REMOVE THIS. Only for testing, while the function only prints...
|
||||
await self.process_thread(current_thread)
|
||||
|
||||
chat_summary = self.serialize_thread_history(current_thread)
|
||||
messages = [
|
||||
LlamaMessage("system", f"{self.system_prompt}\nYou are given the context of the conversation, respond with your next message in the conversation."),
|
||||
LlamaMessage("user", f"Conversation History:\n{chat_summary}")
|
||||
]
|
||||
|
||||
response = await replicate_llama_completion(self.app, messages)
|
||||
self.app.send_chat_message(
|
||||
chat_id=message.chat_id,
|
||||
sender=self.id,
|
||||
content=response,
|
||||
thread_id=current_thread.id,
|
||||
)
|
||||
|
||||
# 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 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)
|
||||
|
||||
# Compute thread summnary and concrete memories, then save the progress done
|
||||
async def process_thread(self, thread):
|
||||
# TODO: extract memories from the thread messages, then store them in the agent's vector memory db
|
||||
# TODO: somewhere here it must be known if the thread was seen before: the same memory should not be stored multiple times
|
||||
print(f"PROCESSING THREAD: {thread.id}")
|
||||
|
||||
instruction = "Your task is to identify facts from a conversation that its participants would remember in the long term. Each line in your response is a different memory, if there's nothing relevant to remember, simply give an empty response."
|
||||
intro = f"Identify the facts that {self.name} will remember long term after the conversation. Following the line break is the personality prompt for {self.name}."
|
||||
|
||||
chat_history = self.serialize_thread_history(thread)
|
||||
messages = [
|
||||
LlamaMessage("system", f"{instruction}\n{intro}\n\n{self.system_prompt}"),
|
||||
LlamaMessage("user", f"Conversation History:\n{chat_history}")
|
||||
]
|
||||
response = await replicate_llama_completion(self.app, messages)
|
||||
memories = [line for line in response.split('\n') if line.strip()]
|
||||
|
||||
print("New agent memories:", memories)
|
||||
|
||||
class ToolUsingAgent:
|
||||
identifier = "tool-using"
|
||||
schema_fields = [
|
||||
@@ -353,14 +267,6 @@ class CoderV1Agent(Llama8bAgent):
|
||||
async def gral_update_file(self, message_history: list, filepath: str, file_content: str):
|
||||
return await gral.gral_update_file(self.app, self.config['system_prompt'], message_history, filepath, file_content)
|
||||
|
||||
# Used by ConversationAgent to keep track of the last chat thread it participated in
|
||||
class ThreadContext:
|
||||
def __init__(self, data):
|
||||
if data:
|
||||
self.current_thread_id = data.get('current_thread_id')
|
||||
else:
|
||||
self.current_thread_id = None
|
||||
|
||||
class ParsedResponse:
|
||||
def __init__(self):
|
||||
self.response = None
|
||||
|
||||
Reference in new issue
Block a user