Files
scrapyard-cogworks/wiki/Assistant.md
T
kingston 26a0728e14 docs: move cog documentation into the wiki and stub the README
The root README carried the full ten cog manual while the wiki held the
authoritative pages. Point the repository at the wiki, keep only install and
catalog basics in the README, and remove the per cog documents that are now
folded into wiki source under wiki/.
2026-11-03 12:54:42 -06:00

7.1 KiB

Assistant

A chat assistant that answers in a dedicated channel or when the bot is mentioned. It talks to any OpenAI-compatible chat completions API and defaults to a local Ollama server.

Requirements

  • An OpenAI-compatible backend. The default is http://127.0.0.1:11434/v1, which is Ollama's OpenAI-compatible endpoint.
  • For any non-local backend, an API key.
  • No Python package beyond aiohttp, which Red provides.

Backend configuration

Global config:

Key Default Set by
base_url http://127.0.0.1:11434/v1 [p]assistant baseurl [url]
api_key null [p]assistant setkey

setkey posts a button. Pressing it opens a modal, and the submitted value is written to global config. The key is never echoed back and is stored in plain text.

baseurl with no argument resets the URL to the local default. A trailing slash is stripped. A URL containing 127.0.0.1 or localhost is reported as local and needs no key; any other URL is reported as remote and requires one.

The local check is a substring test, so a URL such as http://127.0.0.1.example.com/v1 counts as local even though it is remote.

Per-guild config:

Key Default
enabled true
chat_channel null
model llama3.2:3b-instruct-q4_K_M
vision_model llama3.2:3b-instruct-q4_K_M
temperature 0.7
system_prompt A short instruction to be witty, playful and brief
question_mode false
max_tokens null (unlimited)
reasoning_effort null (model default)

Commands

The group is assistant, guild only. Running it bare sends command help.

Command Arguments Permission Behavior
[p]assistant setkey none Bot owner Button and modal to store the API key
[p]assistant baseurl [url] Bot owner Show or set the backend, no argument resets to local
[p]assistant toggle none Any member Enables or disables the assistant for the guild
[p]assistant channel [channel] Any member Sets or clears the dedicated chat channel
[p]assistant model <model> Any member Sets the model id
[p]assistant visionmodel [model] Any member Shows or sets the vision model id
[p]assistant models none Any member Lists models from the backend with reference prices
[p]assistant temp <value> Any member Sets temperature, 0.0 to 2.0
[p]assistant maxlength [tokens] Any member Sets or clears the response token cap
[p]assistant reasoning [level] Any member Sets or clears reasoning effort
[p]assistant system <prompt> Any member Replaces the system prompt
[p]assistant showprompt none Manage Server Prints the system prompt in a code block
[p]assistant question none Any member Toggles question mode
[p]assistant clear none Any member Deletes the in-memory history for the current channel
[p]assistant settings none Any member Posts an embed of the current settings

The permission split is deliberate in only two places. setkey and baseurl are owner only because they control credentials and cost. showprompt is admin only because it may contain internal instructions. Everything else, including the model, temperature, token cap, system prompt and chat channel, can be changed by any member who can run commands.

reasoning accepts none, minimal, low, medium, high, case-insensitively. An invalid value lists the options. No argument clears the setting.

maxlength rejects values below 1. No argument clears the cap.

models requires a key for remote backends and reports "API key not set" otherwise. It calls GET <base_url>/models, filters out known embedding and reranker ids, attaches a price table in EUR per million tokens for known ids, sorts by output price and then id, marks the current model, and sends an embed. Unknown ids are listed last with "pricing unknown". The price table is hardcoded for one provider and does not reflect the local Ollama catalogue.

Message flow

The listener ignores bots and direct messages, then requires the guild to be enabled and a key to be present when the backend is remote. It responds only when the message is in the configured chat channel or mentions the bot. Mention tokens are stripped from the content.

The listener then collects image URLs from the invoking message's attachments and from the replied-to message's attachments and embeds. If there are any images, it replies "Images not supported at this time.", empties the list, and continues with the remaining text. If there is no text, it stops.

Question mode requires the text to end with a question mark. A mention that fails this test gets a reply explaining the rule. A message in the chat channel that fails is ignored silently.

The bot shows a typing indicator, calls the backend, and replies to the original message. A None reply produces "No response from the assistant." An empty string produces a note that the response was empty. Replies longer than 2000 characters are split into consecutive replies, each under the limit, in order.

Request construction

The request body is:

{
  "model": "<guild model>",
  "messages": [{"role": "system", "content": "<prompt>"}, ...history],
  "temperature": 0.7
}

max_tokens is added when configured. reasoning_effort is added when configured. An Authorization: Bearer header is added whenever a key exists, including for local backends.

History is keyed by guild and channel and held in a dictionary. Each request sends the system prompt plus the last 20 entries. After a successful reply the history is trimmed to the last 20 entries once it exceeds 40, so the window grows to 40 and drops back to 20. History is lost on reload and is never written to disk.

The HTTP timeout is 120 seconds total. A non-200 response returns "API error : <first 300 characters of the body>". Any exception returns "Request failed: ". A response without choices[0].message returns "Unexpected API response."

Vision

Vision is not available in the current code. The image collection code and the multimodal request branch exist, but the handler clears the image list before calling the API, so the branch for image content is unreachable. The visionmodel command and setting have no effect.

Limits and failure modes

  • No cooldown and no concurrency limit. Every qualifying message triggers an API call, and a busy channel produces one call per message.
  • Replies are posted as separate messages when chunked, which can trigger Discord rate limits during long outputs.
  • A local backend that is down produces a request failure reply for each message.
  • [p]assistant clear affects only the channel where it runs.
  • The guild enabled flag defaults to true, so a freshly loaded cog starts answering in any channel where the bot is mentioned.

Stored data

Global config under identifier 987654321 holds the API key and base URL. Guild config holds the keys listed above. Chat history exists only in memory. red_delete_data_for_user is not implemented for this cog, so deleting a user's Red data does not remove their prompts from a running history.