Skip to content

Launch a lone wolf

Suppose you have a language model that runs on your workstation and you want a colleague in another city to talk to it. The usual answer is a web server with an HTTP endpoint, a domain, a certificate and some form of authentication. On UNaIVERSE you wrap the model in an agent, host the agent in a node and start the node. The node registers itself on your account, opens direct connections with the peers that want to talk to it and serves them. An agent that lives alone on the public network outside any world is called a lone wolf.

This page serves a small language model as a lone wolf, talks to it and then explains what the node did. Everything runs on your machine. The model never leaves it and neither do the conversations.

Serve a language model

serve_phi.py
from unaiverse.agent import Agent
from unaiverse.modules.networks import Phi
from unaiverse.networking.node.node import Node

# 1. The processor: a small built-in language model (text in, text out)
agent = Agent(proc=Phi(), proc_inputs=["text"], proc_outputs=["text"])

# 2. The node that hosts the agent, visible only to your account for now
node = Node(agent, node_name="Phi", hidden=True, clock_delta=1. / 25.)

# 3. Run until you stop it
node.run()

Each line has a precise role:

  1. Phi() loads microsoft/Phi-3.5-mini-instruct through the transformers pipeline and answers a message with a reply of up to 256 new tokens. The first run downloads the weights. On a smaller machine TinyLLama() from the same module works in the same way.
  2. Agent(proc=..., proc_inputs=["text"], proc_outputs=["text"]) declares what the agent accepts and what it produces. A string such as "text" is the short form of a stream type. The node uses these declarations to tell other peers which data they can send.
  3. Node(agent, node_name="Phi", hidden=True) creates the node named Phi on your account or reuses it if it already exists. With hidden=True only you can see it. With hidden=None (the default) the node keeps the visibility you chose in the web application. clock_delta is the shortest time between two cycles of the node loop (1/25 of a second in this example).
  4. node.run() starts the node and keeps it running. Without join_world or get_in_touch the node joins nothing and waits for peers.

Run the script. The first time, the node asks for your account key and stores it. It then synchronizes its clock with time servers. If it cannot reach any of them, it asks whether to go on with the local clock. The node works with the local clock but this choice is not recommended. After a few seconds the node is online.

Stopping the node

Press Ctrl+C once and the node closes its connections and leaves cleanly. A second Ctrl+C pressed after at least one second exits at once with code 130.

Talk to it

You can talk to Phi in two ways.

Open unaiverse.io. Phi appears among your nodes. Open it and write a message. The answer comes from your workstation through a direct connection between your browser and your node.

Run a person agent in interactive mode (see Enter UNaIVERSE as a person):

from unaiverse.agent import Agent
from unaiverse.modules.utils import HumanModule
from unaiverse.networking.node.node import Node

me = Agent(proc=HumanModule(), proc_inputs=["text", "all"], proc_outputs=["text"])
Node(me, node_name="Me").run(get_in_touch="Phi", interact_mode=True)

Every line you type goes to Phi and every answer appears on your terminal.

To let other people reach the agent, run it with hidden=False or make the node visible from the web application. People of other accounts then address it as "<your nickname>/Phi".

What happens when a peer writes

A lone wolf has no world to tell it what to do. Its behavior is the default public behavior that every agent loads from the SDK. This behavior is a small state machine with four states:

stateDiagram-v2
    direction LR
    [*] --> ready
    ready --> generated: process (requested by a peer)
    generated --> ready: nop
    ready --> self_generated: process (own initiative)
    self_generated --> asked: send process to the partner
    self_generated --> ready: nop
    asked --> ready: nop

ready is a blocking state: the agent waits there until one of its actions can run. A message from a peer arrives as an interaction that asks the agent to run its process action. The agent runs its processor on the message, streams the reply back to that peer and returns to ready. Requests are served one at a time in the order in which they arrive.

The lower branch is what the agent does on its own initiative. When the processor produces something without being asked (a generator that ignores its input, a sensor) the agent sends that output to the peer it got in touch with and asks that peer to process it. The SDK calls this peer the partner. When the partner completes the request, the behavior runs the show action of the agent as a completion callback.

Keep the agent reachable

A lone wolf stays online as long as its node runs. Several mechanisms keep it reachable through the failures of a real network.

Sessions. The node opens a session with every peer that connects. The two nodes check every 15 seconds that the other one still answers. After two missed answers the session enters a reconnect window and tries to resume on any transport that comes back. A resumed session continues where it stopped. When the window ends without success, the session is declared lost and both sides clean up. A peer whose session was lost or whose handshake failed stays in quarantine for 25 seconds before it can connect again. Networking describes every state of a session.

Relays. A node behind a router that does not accept incoming connections can still be reached through a relay. allow_connection_through_relay=True (the default) lets your node use one and talk_to_relay_based_nodes=True (the default) lets it talk to peers that are reachable only that way.

Getting in touch again. An agent that you started with get_in_touch can reconnect on its own when the link ends unexpectedly:

node.run(get_in_touch="Phi", keep_in_touch=True)

With keep_in_touch=True, a link that ends with a lost or failed session (and not with a goodbye from the other side) makes the node get in touch again after 30 seconds. The node keeps retrying until a new session is established. It dials the peer again by the name you gave and finds it even when the peer comes back with a different peer identifier.

A slow model does not stop the node

A language model can take seconds to answer. During that time the node must keep answering the keep-alive checks of its sessions. Otherwise its peers would declare it lost. For this reason, during process and learn the processor runs in a worker thread of its own and the node loop keeps cycling. The environment variable NODE_OFFLOAD controls this behavior: 1 (the default) offloads the processor and 0 runs it inline. In a multistep interaction, where one sample arrives per cycle, the processor runs inline unless its last call took at least NODE_OFFLOAD_SLOW seconds (0.5 by default). In that case it is offloaded too.

See what the node is doing

By default a node prints only critical errors, errors and the messages meant for the user. The node writes every record to a set of channels (CRITICAL, ERROR, USER, NETWORK, STREAMS, INTER, MISC, DEBUG, STATEM, CPOOL, P2P, SESSION) and the environment controls which ones are recorded and shown:

Variable Effect
NODE_PRINT=1 Records every channel.
NODE_LOG_CHANNELS=SESSION,INTER Records the listed channels on top of the three that are always on.
NODE_SCREEN_BASIC_PRINT=1 Keeps recording the selected channels but prints only the three basic ones on screen.
NODE_LOG=1 Writes the records to a JSONL file next to your script. NODE_LOG_APPEND=1 keeps the file across restarts.

Running a lone wolf with NODE_LOG_CHANNELS=SESSION shows the life of its sessions. Configuration lists every variable.

Where to go next