4 of 102 min

The logic by which the bot answers people

The bot doesn’t work on the principle of “saw a message → generated text right away”.

First it decides whether to answer at all. Then it gathers context, pulls in the profile, and only after that builds a reply.

One of the key nodes looks like this:

ctx = ann_build_context_from_db(chat_id, limit=ANN_CONTEXT_MSG_LIMIT, max_chars=ANN_CONTEXT_MAX_CHARS)
profile_block = _build_profile_prompt_block(last_user_text, base_max_tokens, mention_max_tokens)

messages = [
    {"role": "system", "content": system_prompt},
    {"role": "system", "content": profile_block},
    {"role": "user", "content": ctx},
]

What matters here:

• ann_build_context_from_db(…) The function gathers context from the database. So before replying the bot takes not only the last message but the history of the conversation.

• chat_id Context is gathered for a specific chat. This protects against mixing different dialogues.

• limit=ANN_CONTEXT_MSG_LIMIT Limits how many messages go into the context. Otherwise the prompt bloats and gets noisy.

• max_chars=ANN_CONTEXT_MAX_CHARS Additionally limits the size of the context in characters. This is needed so as not to overload the model.

• _build_profile_prompt_block(…) Separately builds the user profile block. So the bot mixes in not only the chat history but also accumulated observations about the person.

• last_user_text The user’s last message is passed to the profile block. This helps choose a more relevant tone and fact from the profile.

• messages = […] This is the final assembly of the prompt package before generation.

• {“role”: “system”, “content”: system_prompt} The main system layer. This holds the bot’s general character, its frame and behaviour.

• {“role”: “system”, “content”: profile_block} The second system layer. This holds personalisation: tone, facts, the particulars of a specific person.

• {“role”: “user”, “content”: ctx} And this holds the assembled context of the conversation itself.

So the logic is:

  1. the bot doesn’t answer “blindly”
  2. first it gathers the dialogue history
  3. then it adds the person’s profile
  4. then it connects this with the general philosophy and system rules
  5. and only after that generates a reply

That’s why its reply rests not on one last line but on several layers at once: context, profile, memory and system logic. The bot answers not “a message” but the situation of a conversation.

Series contents
  1. How the idea of a book gave rise to a space for memory, conversation and recirculation
  2. What this project is really made of
  3. Memory, profile and context: what the bot rests on
  4. The logic by which the bot answers people
  5. How I made a system that doesn't let content vanish
  6. How the project's philosophy is sewn into the code
  7. Why this project lives not only in text
  8. How the bot gathers and updates people's profiles
  9. How I protect the project from chaos, duplicates and chance
  10. Why I wrote this code not for the sake of automation