Introduction
Agent communication protocols help different AI agents talk and work together smoothly, like friends sharing ideas to solve a problem.
Jump into concepts and practice - no test required
protocol = {
'message_type': ['inform', 'request', 'confirm', 'query'],
'sender': 'agent_name',
'receiver': 'agent_name',
'content': 'information or command',
'timestamp': 'time_sent'
}message = {
'message_type': 'inform',
'sender': 'AgentA',
'receiver': 'AgentB',
'content': 'The door is open',
'timestamp': '2024-06-01T10:00:00Z'
}message = {
'message_type': 'request',
'sender': 'AgentB',
'receiver': 'AgentA',
'content': 'Please close the door',
'timestamp': '2024-06-01T10:01:00Z'
}message = {
'message_type': 'confirm',
'sender': 'AgentA',
'receiver': 'AgentB',
'content': 'Door is now closed',
'timestamp': '2024-06-01T10:02:00Z'
}import time class Agent: def __init__(self, name): self.name = name def send_message(self, receiver, message_type, content): message = { 'message_type': message_type, 'sender': self.name, 'receiver': receiver.name, 'content': content, 'timestamp': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()) } print(f"{self.name} sends to {receiver.name}: {message}") receiver.receive_message(message) def receive_message(self, message): print(f"{self.name} received message: {message}") # Create two agents agent_a = Agent('AgentA') agent_b = Agent('AgentB') # AgentA informs AgentB agent_a.send_message(agent_b, 'inform', 'The door is open') # AgentB requests AgentA agent_b.send_message(agent_a, 'request', 'Please close the door') # AgentA confirms to AgentB agent_a.send_message(agent_b, 'confirm', 'Door is now closed')
message = {"sender": "AgentA", "receiver": "AgentB", "type": "request", "content": "status update", "time": "10:00"}message["type"] return?def send_message(sender, receiver, content):
message = {
"sender": sender,
"receiver": receiver,
"content": content,
"time": time.now(),
"type": "info"
}
return messagetime.now(), but the time module does not have a now() function.datetime.now() from the datetime module.request message asking for data, and Agent B replies with a response message containing the data. Which protocol design best supports this interaction?