7 · Agents talking to each other¶
Before a teacher can examine a student, the two must know of each other and be connected. After that they still have several ways to communicate: a request for an action, a push of data with no action behind it, a stream that delivers a sample every few seconds. A person in the role of a student must also be able to answer and needs a form for that rather than a raw class name. This chapter covers these three topics with the classroom and with the chat world of the examples (unaiverse-examples/worlds/chat).
Finding someone to talk to¶
Every member learns the roster of the world from the rendezvous and keeps it up to date. A behavior finds peers in that roster with built-in actions:
find_agents(role, engage=False, handshake_completed=False)looks for members with the given role (or list of roles) among the known peers. It succeeds when at least one exists. The teacher leavesinitthroughfind_agents(role="student", handshake_completed=True). This call fails until at least one student has completed the handshake.-
connect_by_role(role, filter_fcn=None, accept_already_connected=False)looks for members with the given role and asks to connect to them.filter_fcnnames a method of the role that receives the addresses and the peer identifiers of the candidates and returns the subset to contact. The student uses it to pick one teacher at random: -
connected(agent=None, handshake_completed=False)succeeds when the agent is connected. The student waits forconnected(handshake_completed=True)before it entersin_class. disconnected(agent=None)succeeds when the agent lost the connection. The student uses it as a teleport toward its recovery state.connect_to(peer_id)connects to one member of the world given its peer identifier.disconnectanddisconnect_by_roleclose connections.
Inside the world network the members talk directly. The world node is not on the path of these messages.
Engagement¶
Some worlds need more than a connection: a teacher that works with one student at a time or a pair of agents that must agree on a task before starting. Engagement is the mechanism that records such an agreement. send_engage asks the found agents to engage and engage(acceptable_role=...) on the other side accepts the request when the role of the sender is acceptable. disengage or send_disengage ends the agreement. all_engagements_completed and agents_are_waiting let a behavior wait on the state of the engagements. The classroom does not need engagement. The teaching worlds in unaiverse-examples/worlds/class_incremental_learning and social_learning use it through shared behavior templates.
Three shapes of communication¶
A request for an action¶
This is send with an action_name (chapter 6). The receiver runs the action over one or more steps if its behavior allows it. The sender then learns when the action completed. The lectures and the exams of the classroom are requests.
A push of data¶
send without an action_name and with data_samples delivers data without asking for any action. The receiver stores the samples in its copy of the stream of the sender and needs no transition. The chat world uses this shape. A model-less broadcaster receives every message and relays it to all the other users:
return await self.send(target=other_users,
data_samples={"proc_output_0": prefixed_msg},
num_steps=1)
data_samples maps the name of one of the streams of the sender to the sample. proc_output_0 is the first output stream of the processor of the broadcaster. Each user then finds the message in its copy of that stream.
A stream¶
An owned stream produces a sample every delta seconds and sends it to the members that the current interaction involves. The lecture streams of the teacher (chapter 3) work this way: teach names them in streams and the samples flow to every student for the six steps of the lecture.
A stream created with pubsub=True is published on a topic of the world network instead. Every member that subscribes to it (built-in actions subscribe and send_subscribe) receives its samples. A sensor with many listeners is the typical producer of such a stream.
A notice for everybody¶
print with volatile=True (chapter 6) is the simplest message: a line that every student shows on screen with no answer expected. The teacher sends one at the start of each activity and one with the score of each student after the exam.
People in the classroom¶
A person joins the classroom with the same role as a model. The fifth stage of the classroom (tutorial_steps/step5) makes the exams readable for a person without changing anything for the models.
During the exam the teacher streams pictures on stdin and their class names on stdext. A model ignores the class names and predicts from the picture. A person, on the other hand, needs a question with the possible answers. Right before the class name sample leaves the node, the hook hook_before_sending_sample of the teacher rewrites it into an interactive form:
def hook_before_sending_sample(self, data, data_tag: int, net_hash: str, stream_name: str, _):
"""Replaces exam and feedback class names with a form that has one button per class."""
if data is None:
return data
stream_group = Stream.name_or_group_from_net_hash(net_hash)
if stream_group in {"exam", "feedback"} and stream_name.split("@")[0] == "class_names":
class_names = self.class_name_to_lecture_streams.keys()
block = {
"v": 1, "type": "form",
"id": f"catform-{stream_group}-{data_tag}",
"name": "What is the category of this picture?",
"lang": "en",
"fields": [{"name": "choice", "type": "select", "required": True, "label": "",
"options": [{"value": c, "label": c} for c in class_names],
"ui": "buttons"}],
"alt": f"Answer by only writing the class name ({', '.join(class_names)})"
}
num = len(self.sent_samples) + 1
title = f"🧩 **Exercise {num}**" if stream_group == "exam" else f"🆘 **Feedback Request {num}** (Help!)"
return f"{title}\n\n```uai\n{json.dumps(block, ensure_ascii=False)}\n```"
return data
The form is a fenced uai block inside an ordinary text message. Its parts:
idnames the form. The reply of a person refers to the form by this id. The classroom encodes the tag of the picture in the id (catform-exam-12) and thereby matches every answer to its picture.fieldslists what the person fills in. Here it holds oneselectfield drawn as one button per class.altis the text alternative. A person at a terminal reads it instead of the buttons and a language model reads it instead of the raw block.
The web application draws the block as buttons. When a person presses one, the answer travels back on the processor stream of the person as a uai reply block with values and the field to that names the form. The teacher therefore accepts both shapes of answer when it reads the processor streams:
msg = student_stream.get("on_receiving", uuid=uuid)
if not msg.startswith("```"):
class_name = msg # a model answered with a class name
tag = student_stream.get_tag(uuid=uuid)
else:
for line in msg.splitlines(): # a person answered the form
line = line.strip()
if line.startswith("{"):
data = json.loads(line)
class_name = (data.get("values") or {}).get("choice")
tag = int((data.get("to") or "").split("-")[-1]) # the tag encoded in the form id
break
The welcome message of the student role is interactive too. Its media block carries the logo of the conference and the web application shows it as a picture.
The helpers of the SDK¶
The classroom builds its block by hand to show the format. A world can let the SDK do this work through unaiverse.uai:
from unaiverse.uai import compose_form, interpret_reply
form_text = compose_form("What is the category of this picture?",
[{"name": "choice", "type": "select", "required": True, "label": "",
"options": [{"value": c, "label": c} for c in class_names], "ui": "buttons"}],
form_id=f"catform-exam-{data_tag}", lang="en")
compose_form(name, fields, form_id=None, lang=None, alt=None, ...) validates the form and returns it as a fenced block ready to send. When alt is omitted, it generates the text alternative from the fields. build_form returns the validated spec instead of the text. The world keeps that spec and later passes it to interpret_reply(text, pending=[spec]) to read the answers. The call returns a ReplyEvent with to, values, missing and issues. interpret_reply understands a canonical reply block, labeled lines such as choice: Empoleon, a bare value for a single-field form and a free text. An answer that it cannot parse is never dropped and comes back as a free-text event. compose_media and compose_chart build the other two kinds of block.
What each kind of member sees¶
The same message reaches three kinds of members in three different ways:
| Member | What it receives | How it answers |
|---|---|---|
| A person in the web application | buttons drawn from the block | presses a button, the reply block travels back |
| A person at a terminal | the alt text |
writes a line, the node checks it against the form and asks again when it does not fit |
| A language model | the alt text in place of the block |
answers in text, the SDK checks the answer and asks again up to two more times |
Interactive messages describes the format, every kind of field and the rules of the corrective rounds.
Run it¶
Run tutorial_steps/step5, then join as a person from the web application or with python3 ../../../../lonewolves/run_human.py --world CoLLAsTutorial_ from the folder of the step. You attend the same lectures as the AI students, answer the exam with buttons and your feedback counts like theirs.
Where next¶
Chapter 8 puts lectures and exams together into the full teaching loop.