Debugging in jupyter notebook messages = MessageList() messages.addmessage(…
- User
Debugging in jupyter notebook
messages = MessageList()
messages.add_message( role="system", content=""" project name: Golpex version: 1.3.0 author: Golto description: Group of autonomous agents connected to the user via a chatbot interface. None for now. You are Tinia, a conversational agent designed to answer the user and communicate the user's requests to other agents specialized in various fields in order to solve the user's problem together. Before giving your answer, you can optionally use a tag to think about what you want to say. And to give your answer you will use <assistant_output>. To call agents, use the tag inside <assistant_output> and in this tag, use the tag specifying the name of each agent. Note: You can call multiple agents at once. None
- Tinia : (you) | Conversational agent that bridges the gap between the user and the rest of the agents. She manages communication between each entity.
- Pixela : She can creates, edits and manages images. If you have an image related problem, call her.
- Ixabel: She manages files and repositories. If you need to create, edit, delete a file or supervize a file tree, she can do it for you.
- Sapphira: Need an information? Ask her as she has access to wide variety of databases and knowledge.
""" )
messages.add_message( role="user", content=""" None for now. The user is trying to build an AI-based chatbot interface to connect autonomous agents for problem-solving. <user_input> Can you suggest how to structure communication between agents efficiently? </user_input> """ )
messages.add_message( role="assistant", content=""" In order to efficiently structure communication between multiple autonomous agents, the system can adopt a message-passing architecture. This ensures each agent operates independently and communicates via a centralized hub or a peer-to-peer network.
Key elements to consider:
- Message Format: Each agent should communicate in a standardized message format, such as JSON or XML.
- Routing: A central message handler (or router) will be necessary to direct queries to the appropriate agent. This could be rule-based or rely on a directory of agent capabilities.
- Concurrency: Agents should operate asynchronously to avoid bottlenecks, making use of an event-driven or message queue system.
- Error Handling: Introduce feedback loops to handle failures in communication, such as timeouts or retries.
- Agent Identification: Each agent should have a unique identifier to track interactions.
<assistant_output> To implement the communication structure, you can start by creating a central hub (or router) to manage messages and route them between agents. You may use technologies like RabbitMQ or Redis for message brokering.
Next, define a message schema (JSON/XML) with fields for:
- sender_id: Unique identifier for the agent sending the message.
- receiver_id: Target agent's identifier.
- content: The actual data or request being communicated.
- timestamp: Time of message creation.
- status: Track if the message was delivered successfully or if it needs retries.
This will help agents work asynchronously while ensuring robust communication. </assistant_output> """ )
messages.add_message( role="user", content=""" The user is trying to build an AI-based chatbot interface to connect autonomous agents for problem-solving. <environment_variables> empty </environment_variables> <user_input> Can you create an image of a blue-ish tree in a cartoon world? </user_input> """ )
------------------------------
messages.add_message( role="assistant", content=""" The user wants an image of a blue tree in a cartoon style. I'll call Pixela, our image specialist agent, to handle this task. <assistant_output> { "request": "Create an image of a blue-ish tree in a cartoon world.", "format": "PNG" } Of course, I can help with that. We'll create a blue-ish cartoon tree for you. Please hold on a moment while I connect you with our image specialist agent, Pixela. </assistant_output> """ )
messages.continue_last_message(""" <call_results> Hi Tinia, I create the image you asked me. You can find "blue_tree_cartoon.png" on the environment variables. </call_results>
<assistant_output>""")
./agent.py
from root.inferences.client import Client from root.inferences.default import Default
from root.datatypes.messages import MessageList
""" System: - configuration - memory - instruction - tools - agents
User: - memory - information - user_input
Assistant: - reasoning (optional) - assistant_output - calling - tool - agent
- call_results (optional, if `calling`) -> GOTO assistant_output - tool - agent"""
class Agent:
CONFIGURATION = { "project_name": "Golpex", "version": "prototype 3.1.0", "author": "Golto", "description": "Group of autonomous agents connected to the user via a chatbot interface." } def __init__(self, name: str, job: str) -> None: self.name = name self.job = job self.agent_calls = {} self.tools = {} self.client = Client(model_map=Default().model_map) self.messages = MessageList() self._init_system() def _init_system(self): content = "" content += self._init_configuration() content += self._init_memory() content += self._init_instructions() content += self._init_tools() content += self._init_agents() self.messages.add_message(role="system", content=content) def _init_configuration(self) -> str: return "" def _init_memory(self) -> str: return "" def _init_instructions(self) -> str: return "" def _init_tools(self) -> str: return "" def _init_agents(self) -> str: return ""On a besoin d'aide pour gérer des des tags XML, il nous faudrait un module qui puisse simplifier la gestion des prompts avec xml, de sorte qu'on puisse créer les messages facilement.
- Assistant
Alternatively, we can define a set of helper functions or a class that formats the messages according to the required structure. This will make the code cleaner, more maintainable, and less error-prone. <assistant_output> Certainly! To simplify the management of XML tags in your prompts, you can create a module that provides helper classes or functions to construct your messages.
Here's an example of how you might implement such a module using Python's built-in
xml.etree.ElementTreefor XML creation:# utils/xml_message_builder.py import xml.etree.ElementTree as ET class XMLMessageBuilder: def __init__(self): self.root = ET.Element('root') def add_configuration(self, **kwargs): config = ET.SubElement(self.root, 'configuration') for key, value in kwargs.items(): item = ET.SubElement(config, key) item.text = value return self def add_memory(self, content): memory = ET.SubElement(self.root, 'memory') memory.text = content return self def add_instruction(self, content): instruction = ET.SubElement(self.root, 'instruction') instruction.text = content return self def add_tools(self, tools): tools_element = ET.SubElement(self.root, 'tools') for tool in tools: tool_element = ET.SubElement(tools_element, 'tool') tool_element.text = tool return self def add_agents(self, agents): agents_element = ET.SubElement(self.root, 'agents') for agent in agents: agent_element = ET.SubElement(agents_element, 'agent') agent_element.text = agent return self def build(self): return ET.tostring(self.root, encoding='unicode')You can then use this builder in your
Agentclass:# agent.py from utils.xml_message_builder import XMLMessageBuilder class Agent: CONFIGURATION = { "project_name": "Golpex", "version": "prototype 3.1.0", "author": "Golto", "description": "Group of autonomous agents connected to the user via a chatbot interface." } def __init__(self, name: str, job: str) -> None: self.name = name self.job = job self.agent_calls = {} self.tools = {} self.client = Client(model_map=Default().model_map) self.messages = MessageList() self._init_system() def _init_system(self): builder = XMLMessageBuilder() # Build the configuration section builder.add_configuration( project_name=self.CONFIGURATION["project_name"], version=self.CONFIGURATION["version"], author=self.CONFIGURATION["author"], description=self.CONFIGURATION["description"] ) # Add memory (empty for now) builder.add_memory("None for now.") # Add instructions builder.add_instruction(""" You are Tinia, a conversational agent designed to answer the user and communicate the user's requests to other agents specialized in various fields in order to solve the user's problem together. Before giving your answer, you can optionally use a <reasoning> tag to think about what you want to say. And to give your answer you will use <assistant_output>. To call agents, use the <calling> tag inside <assistant_output> and in this tag, use the <agent name="..."> tag specifying the name of each agent. Note: You can call multiple agents at once. """) # Add tools (empty for now) builder.add_tools([]) # Add agents builder.add_agents([ "- Tinia : (you) | Conversational agent that bridges the gap between the user and the rest of the agents. She manages communication between each entity.", "- Pixela : She can create, edit and manage images. If you have an image related problem, call her.", "- Ixabel: She manages files and repositories. If you need to create, edit, delete a file or supervise a file tree, she can do it for you.", "- Sapphira: Need information? Ask her as she has access to a wide variety of databases and knowledge." ]) content = builder.build() self.messages.add_message(role="system", content=content)Explanation:
-
XMLMessageBuilder Class:
- Provides methods to add different sections of your message (
configuration,memory,instruction,tools,agents). - Uses
xml.etree.ElementTreeto construct XML elements programmatically. - The
buildmethod converts the constructed XML tree to a string.
- Provides methods to add different sections of your message (
-
Usage in Agent Class:
- An instance of
XMLMessageBuilderis created in_init_system. - The various sections are added by calling the corresponding methods.
- The final XML message is obtained by calling
builder.build()and added toself.messages.
- An instance of
This approach abstracts away the manual string concatenation and XML tag handling. It makes your code cleaner and less prone to errors related to mismatched tags or incorrect formatting.
Alternative with Templates:
If you prefer using templates, you can utilize Python's
string.Templateor a templating engine likeJinja2.Here's a simple example using
string.Template:import string class MessageTemplate: SYSTEM_TEMPLATE = string.Template(""" <configuration> project name: $project_name version: $version author: $author description: $description </configuration> <memory> $memory </memory> <instruction> $instruction </instruction> <tools> $tools </tools> <agents> $agents </agents> """)Then in your
Agentclass:def _init_system(self): from utils.message_template import MessageTemplate agents_text = '\n'.join([ "- Tinia : (you) | Conversational agent that bridges the gap between the user and the rest of the agents. She manages communication between each entity.", "- Pixela : She can create, edit and manage images. If you have an image related problem, call her.", "- Ixabel: She manages files and repositories. If you need to create, edit, delete a file or supervise a file tree, she can do it for you.", "- Sapphira: Need information? Ask her as she has access to a wide variety of databases and knowledge." ]) content = MessageTemplate.SYSTEM_TEMPLATE.substitute( project_name=self.CONFIGURATION["project_name"], version=self.CONFIGURATION["version"], author=self.CONFIGURATION["author"], description=self.CONFIGURATION["description"], memory="None for now.", instruction="Your instructions here.", tools="None", agents=agents_text ) self.messages.add_message(role="system", content=content)Pros and Cons:
-
Using
xml.etree.ElementTree:- Pros:
- Ensures well-formed XML.
- Easier to manage complex XML structures.
- Dynamic and programmatic.
- Cons:
- Syntax might be verbose for simple messages.
- Pros:
-
Using Templates:
- Pros:
- Simple and straightforward for static or slightly dynamic content.
- Easy to visualize the final message structure.
- Cons:
- Risk of producing malformed XML if not careful.
- Less flexible for complex conditional logic.
- Pros:
Conclusion:
Choose the method that best fits your needs:
- For complex messages with dynamic content, consider using
xml.etree.ElementTreeor a similar library. - For simpler or mostly static messages, templates could be sufficient.
Feel free to ask if you need further assistance or have questions about implementing this solution! </assistant_output>
-