Skip to content

Build an Advisor

An Advisor is an Assistant that only talks with the participant. It answers their messages and can speak up on its own, with text, pictures, charts, questions and notifications. It never changes the screen and never sends, accepts or ends: the participant does all of that. The most it does to the negotiation is fill in an offer for the participant to check and send. This tutorial builds one in five short steps.

Advisor or Copilot?

A study runs one of two types of Assistant (see Assistant types):

  • an Advisor, the subject of this page: an AI assistant to talk to;
  • a Copilot, a full negotiation support system that can also fill and send offers, highlight the screen, change the layout and use tools. Build a Copilot covers it.

If you are not sure which to build, start here. Everything an Advisor does, a Copilot can do too.

The Advisor's screen

In an Advisor study every participant sees the same screen:

  • on the left, the negotiation history with the offer area under it;
  • on the right, the scenario information (Preferences, Scenario Info and, when the study offers it, My Results) above your Advisor's chat.

The Advisor's screen: the history and the offer area on the left, the scenario information above the chat on the right

The participant cannot rearrange it, and there are no other tools. So the only thing that differs from one Advisor to the next is what it says, and when.

What you write

An Advisor is a Python class. Everything is optional:

You override It is called Return
reply(text) when the participant sends a message the answer, or None
on_start(state) when the negotiation starts a message, or None
on_turn(state) when it is the participant's turn (the opponent just answered) a message, a TurnReply (a message, the starting offer and the message to the opponent, all at once), or None
on_end(state) when the negotiation ends a message, or None

state is the live negotiation, the same as self.context(); leave it out (def on_turn(self)) if you do not need it. What you return is posted in the chat. To do more, call these:

You call It does
self.context() returns the live negotiation (the offers, what they are worth to the participant, the time left)
self.say(text, image=..., chart=...) posts a chat message, optionally with a picture or a chart
self.notify(text) pops up a notification
self.ask(question, options) asks in the chat with answer buttons and waits for the answer
self.ask_llm(prompt) sends one prompt to the study's language model and returns its answer
self.set_offer(values), self.set_message(text) fill in the participant's offer ({issue: value}) or message to the opponent, when they ask; never sent

If you override nothing, the Advisor chats through a language model and sees the live negotiation with every message.

Level 0: set up

Install HANI2 and create the settings folder:

pip install hani2
hani2 setup

Get the example files:

git clone https://github.com/autoneg/hani2
cd hani2/examples/advisor_tutorial

To run one level, pass its file and class to --assistant:

hani2 --page play --assistant advisor1_echo.py:EchoAdvisor

Your browser opens the playground. Press Start negotiation and type in the chat on the right. An Advisor class starts the server in Advisor mode by itself (--assistant-type advisor does the same for any Advisor). Restart the server after every change to your code.

Level 1: answer every message

"""Advisor level 1 - the smallest Advisor: answer every message (no language model)."""

from hani2.assistant import Advisor


class EchoAdvisor(Advisor):
    def reply(self, text):
        return f"You said: {text}"

Type "hi" and the Advisor answers "You said: hi".

The level 1 Advisor answering

reply runs on a worker thread, so it may take as long as it needs without freezing the page.

Try this: add name = "Echo" to the class and watch the chat's title change.

Level 2: read the negotiation

self.context() returns the live negotiation as a dictionary. This Advisor judges the opponent's latest offer.

"""Advisor level 2 - read the live negotiation and judge the opponent's offer (no language model)."""

from hani2.assistant import Advisor


class RulesAdvisor(Advisor):
    def reply(self, text):
        ctx = self.context()
        offer = ctx["partner_offer"]
        if offer is None:
            return "Your opponent has not made an offer yet."
        worth, reserved = ctx["partner_offer_utility"], ctx["reserved_value"]
        verdict = "worse than no deal: do not accept it" if worth < reserved else "better than no deal"
        offer_text = ", ".join(f"{issue}: {value}" for issue, value in offer.items())
        return f"Their offer ({offer_text}) is worth **{worth:.0%}** to you, {verdict}."

Start a negotiation, wait for the opponent's offer and ask anything.

The level 2 Advisor judging the opponent's offer

The keys you will use most:

Key What it holds
partner_offer the opponent's latest offer, {issue: value} (None before their first offer)
partner_offer_utility what that offer is worth to the participant, from 0 to 1
reserved_value what no deal is worth to the participant
history the recent offers: {"by": "you" or "partner", "offer", "message", "utility"} (old name)
relative_time how much of the negotiation has passed, from 0 to 1
issues the issues and their possible values
result the outcome once the negotiation has ended

(partner is the stored name of the opponent; old name.) Numbers the participant may not know in this study are left out, as None.

Try this: also say how many offers the opponent has made so far, from ctx["history"].

Level 3: a language model with your persona

Drop reply and the Advisor asks the study's language model. Every message reaches the model with the live negotiation attached, so you only write the persona.

"""Advisor level 3 - a language model with your own persona.

Without ``reply`` the Advisor asks the study's language model, with the live negotiation
attached to every message. All you write is the persona.
"""

from hani2.assistant import Advisor

COACH_PROMPT = """You are a calm, concise negotiation coach. You can only talk with the negotiator.
- Answer in at most three short sentences.
- Compare the opponent's offer with the negotiator's reserved value before advising.
- Never advise accepting an offer worth less than the reserved value.
- Say "your opponent" for the other side ("partner" in the state) and give values as percentages."""


class CoachAdvisor(Advisor):
    name = "Coach"

    def build_system_prompt(self):
        return COACH_PROMPT

The default model is qwen3:4b-instruct on a local Ollama server (ollama pull qwen3:4b-instruct). Language models explains how to use another one.

The level 3 Advisor answering in its own persona

The model can also ask the participant a question with answer buttons, pop up a notification and draw a chart in the chat; it decides when.

Try this: ask "Draw me a chart of what their offers were worth to me".

Level 4: speak up on your own

An Advisor does not have to wait to be asked. Return a message from on_start, on_turn or on_end, call notify for a notification, and ask for a question with buttons.

"""Advisor level 4 - speak up on its own: a question at the start, a nudge on each turn, a debrief.

Return text from ``on_start``, ``on_turn`` or ``on_end`` to post it in the chat; ``notify`` pops
up a notification; ``ask`` asks with answer buttons and waits for the answer.
"""

from hani2.assistant import Advisor


class SpeakUpAdvisor(Advisor):
    target = 0.7  # the share of the best deal to aim for

    def on_start(self):
        answer = self.ask("How hard do you want to push?", ["Gently", "Firmly"])
        self.target = 0.4 if answer == "Gently" else 0.7
        return f"Got it. I will tell you when an offer is worth at least {self.target:.0%} to you."

    def on_turn(self):
        ctx = self.context()
        worth = ctx["partner_offer_utility"]
        if worth is not None and worth >= self.target:
            self.notify(f"This offer is worth {worth:.0%} to you: consider accepting it.", "success")
        return None  # nothing for the chat this time

    def on_end(self):
        result = self.context()["result"] or {}
        return "A deal! Well done." if result.get("agreement") else "No deal this time."

When you press Start negotiation, the Advisor asks how hard to push:

The level 4 Advisor asking a question with answer buttons

Then, on each of your turns, it pops up a notification when the opponent's offer is good enough. When the negotiation ends it says how it went.

A notification from the level 4 Advisor

ask waits for the answer (the participant may also type one) and returns None when nobody answers within three minutes. Always handle None.

Try this: in on_end, ask "Was my advice useful?" with "Yes" and "No".

Start the participant's turn with an offer

on_turn can also choose the offer the participant's action panel starts with, and the message to the opponent, together with what it says in the chat:

from hani2.assistant import Advisor, TurnReply


class StartingAdvisor(Advisor):
    def on_turn(self, state):
        theirs = state["partner_offer"]  # the opponent's offer (None when the participant opens)
        if theirs is None:
            return None
        return TurnReply(say="I started your offer from theirs; adjust it before sending.", offer=theirs)

The admin must grant the starting_offer ability (off by default). Without it, or when on_turn returns no offer or answers after starting_offer_wait seconds (default 5), the panel starts as usual: the participant's last offer, else their best one. The participant still sends the offer themselves.

Level 5: answer with a chart

say takes a chart: a Plotly figure, or the same as a plain dictionary, or a Matplotlib figure. image= takes a picture file instead (pictures are embedded in the message, never loaded from another site).

"""Advisor level 5 - answer with a chart: what each of the opponent's offers was worth to you."""

from hani2.assistant import Advisor


class ChartAdvisor(Advisor):
    def reply(self, text):
        theirs = [h for h in self.context()["history"] if h["by"] == "partner" and h["utility"] is not None]
        if not theirs:
            return "No offers from your opponent yet: ask me again after their first one."
        chart = {
            "data": [{"type": "scatter", "mode": "lines+markers", "name": "Their offers",
                      "x": list(range(1, len(theirs) + 1)), "y": [h["utility"] for h in theirs]}],
            "layout": {"yaxis": {"title": {"text": "Worth to you"}, "tickformat": ".0%", "range": [0, 1]},
                       "xaxis": {"title": {"text": "Their offer"}, "dtick": 1}},
        }
        self.say("Here is what each of their offers was worth to you:", chart=chart,
                 caption="Rising means they are conceding.")
        return None  # the chart is the answer

Play a few rounds, then ask "How are their offers moving?".

The level 5 Advisor answering with a chart

say can also show tables and other display components from hani2.tools.ui, through components=[ui.table(rows)]. Buttons and inputs are left out: for a choice, use ask.

Try this: add a second series with the participant's own offers (h["by"] == "you").

Testing offline

hani2.testing.make_session builds a whole negotiation without a browser. Pass assistant_type="advisor":

from hani2.testing import make_session


def test_my_advisor():
    s = make_session(agent_class="advisor2_rules:RulesAdvisor", assistant_type="advisor")
    s.start()
    s.wait_for_turn()
    s.user_chat("what do you think?")
    s.wait_agent_idle()
    assert "worth" in s.chat[-1]["text"]

Run it from the folder that holds your Advisor, so that advisor2_rules can be imported.

What you built

An Advisor answers (reply), reads the negotiation (context), uses a language model with your persona, speaks up on its own (on_start, on_turn, on_end, notify, ask), shows charts (say) and fills in offers for the participant to send (set_offer, TurnReply). That is all an Advisor can do, and all it needs.

  • Assistant types explains how a study runs Advisors.
  • The Assistant guide lists every setting, including the language model, unprompted turns and who knows what.
  • Build a Copilot goes further: an Assistant that also acts and shapes the screen.

Troubleshooting

  • "... is a Copilot, but this study runs Advisors": your class does not subclass Advisor. Import it with from hani2.assistant import Advisor.
  • "... is an Advisor, but this study runs Copilots": start the server with --assistant-type advisor, or set assistant_type: advisor for the study.
  • "Assistant error" in the chat at level 3: no language model answers. Start Ollama, or see Language models.
  • Nothing changes after an edit: restart the server. Each session creates its Advisor once.