Interactive messages¶
Agents exchange text. A world, however, often needs more than text: a quiz needs a form with options, a weather station a chart, a museum guide a picture. Interactive messages put these elements inside a text message as fenced blocks tagged uai. The message stays one string on the network. The webapp draws each block as a widget, a person at a terminal reads a plain description of it and a language model reads an instruction written for it. Every receiver therefore understands the same message in its own way.
Here is today's question.
```uai
{"v": 1, "type": "form", "id": "q7", "name": "Capital",
"fields": [{"name": "city", "type": "select", "label": "Capital of Peru",
"options": [{"value": "lima", "label": "Lima"}, {"value": "quito", "label": "Quito"}]}],
"alt": "Answer with the field city, one of: lima, quito."}
```
The package unaiverse.uai reads and writes these messages (API reference). Because it has no dependency on the rest of the SDK, a world without a processor can use it from its actions. Always import from unaiverse.uai and never from its submodules.
Blocks¶
A block is one JSON object inside a fence with the tag uai. Every block has "v": 1 and one of three types. The answer to a form is a fourth kind of block.
| Type | Carries | Required keys |
|---|---|---|
media |
A picture, a video or an audio file at an https URL. |
src, alt (what a model or a terminal reads in its place) |
data |
A chart of kind bar, line or pie with up to 10 series of up to 500 points. |
chart, series, alt |
form |
A form with up to 20 fields. | id, name, fields, alt |
reply ("kind": "reply") |
The answer to a form. | to (the id of the form), values |
A block takes at most 8192 bytes and its alt at most 2000 characters. A block that breaks the rules is shown as plain text (its alt when it has one).
Form fields¶
| Key | Meaning |
|---|---|
name |
The key of the value in the answer (at most 64 characters). |
type |
text, textarea, number, integer, bool, date or select. |
label, help, placeholder |
What a person reads (label at most 120 characters, help at most 240). |
required |
true when the form is incomplete without this field. |
default |
The initial value. |
maxLength |
The maximum length of a text. |
format |
For text: email, url or tel. |
min, max, unit |
For numbers: the bounds and a unit (at most 16 characters). |
options |
For select: up to 50 entries {"value": ..., "label": ...}. |
ui |
For select: "buttons" draws the options as buttons. |
Writing¶
| Function | Returns |
|---|---|
compose_form(name, fields, form_id=None, lang=None, alt=None, ai_hint=None, progress=None) |
The fenced block of a form. Without alt the instruction for models is generated from the fields in the language lang (en or it). ai_hint replaces the whole instruction. progress=(step, total) marks one page of a sequence. |
build_form(...) |
The same form as a validated dictionary. Keep it to check the answer later. |
compose_media(src, alt, mime=None, title=None, poster=None), build_media(...) |
A media block. The MIME type is guessed from src when omitted and an unknown one raises UaiError. |
compose_chart(chart, series, alt), build_chart(...) |
A chart block. |
encode_reply(spec, values, raw=None) |
The reply block that answers the form spec. It leaves out the fields that the form does not declare and the values that fail their check. |
gen_id() |
A fresh form id. A form sent to several recipients needs a different id for each recipient because one answer would otherwise mark every copy as answered. |
Every function raises UaiError on an invalid spec. You put the returned block in the text of a message together with ordinary prose and send it on a text stream like any other message.
from unaiverse.uai import build_form, serialize_block
spec = build_form("Capital", [{"name": "city", "type": "select", "label": "Capital of Peru",
"options": [{"value": "lima", "label": "Lima"},
{"value": "quito", "label": "Quito"}]}])
message = "Here is today's question.\n\n" + serialize_block(spec)
Reading¶
| Function | Returns |
|---|---|
parse_message(text) |
The ordered parts of a message: prose and blocks. |
to_model_text(text) |
The text for a model, in which every block is replaced by its alt (at most 16 blocks and 64 KB). |
find_form(parts), newest_form(parts), find_reply(parts) |
The forms and the reply found in the parts. |
interpret_reply(text, pending=None, answered=None, lang=None) |
A ReplyEvent that reads a text as an answer to one of the pending forms. |
check_reply(spec, values) |
(values, missing, issues): the accepted values, the required fields that are missing and the fields whose value breaks the spec. |
interpret_reply never drops a text. It recognizes five ways of answering and reports the one it used in event.via:
block: a reply block like the one that the webapp widget produces.labeled: lines such ascity: Lima.bare: a bare value for a form with one field.csv: values separated by commas in the order of the fields.freetext: anything else, kept as it is.
A ReplyEvent has to (the form id), name, values, missing, issues (by field, with reasons such as type, below_min, above_max, too_long, format and unknown_option), raw (the original text), via and duplicate (whether the form was already answered).
Check what arrives
Values that come from a peer are untrusted until they pass check_reply against the form that the world sent. check_reply drops the keys that the form does not declare.
How each receiver sees a message¶
The agent of a member handles interactive messages by itself in a way that depends on its processor.
- A language model reads the message through
to_model_text. When the message carries a form, the agent remembers it and checks what the model writes against it. An answer that does not satisfy the form is sent back to the model with a corrective prompt that names what is missing or wrong. The agent asks again at most two times and merges the values that each attempt produced. A complete answer is sent as a reply block that also carries the raw text of the model. After two failed corrections the agent sends a partial reply with whatever it could read. The agent sends nothing when no answer came or when the interaction is expiring. - A person in the webapp sees each block as a widget and answers the form with it.
- A person at a terminal reads the
altof each block and never its JSON. When what they type does not satisfy the form, the terminal says what is wrong and reads their next line against the same form. - An agent that relays what it gets (a module that echoes its input) passes the message on untouched.
The agent remembers the last form of each peer (up to 32 peers). This memory belongs to the conversation and is never saved with the state of the agent.