AI: personal assistants without the dead API, and keep Ollama warm
CI / compile (pull_request) Successful in 5s
CI / unit (pull_request) Successful in 22s
CI / integration (pull_request) Successful in 26s
build / build (push) Successful in 12s
CI / compile (push) Successful in 5s
CI / unit (push) Successful in 22s
CI / integration (push) Successful in 26s
CI / compile (pull_request) Successful in 5s
CI / unit (pull_request) Successful in 22s
CI / integration (pull_request) Successful in 26s
build / build (push) Successful in 12s
CI / compile (push) Successful in 5s
CI / unit (push) Successful in 22s
CI / integration (push) Successful in 26s
Two things the field report asked for. 1) PERSONAL ASSISTANTS (replacing the sunset OpenAI Assistants API) The old implementation gave three capabilities. Two are reimplemented here, the third was confirmed unused and is deliberately not replaced: * per-user persona - it already lived in system_gpt_settings.json; it was only ever being shipped to OpenAI. It is now the system prompt. * per-user conversation thread - OpenAI held this server-side. It now lives in assistant_memory.json, keyed by discord user id, trimmed to the most recent turns (CONJURER_ASSISTANT_MEMORY_TURNS) and written atomically so a torn write cannot lose someone's history. Deliberately a plain trim, not the AI summarisation used for the bar's shared memory: these are private DMs and must not end up in a public "legend". * file_search - not replaced. Confirmed not in use. The conversation goes through handle_response with request_type="NONE" and an explicit message list, which keeps it out of the bar's shared memory. The big win: create_chat_assistant hardcoded model="gpt-4o", so assistants were locked to OpenAI. They now run on whatever $gadaj_teraz selects - Claude and Ollama included. create_chat_assistant / chat_with_assistant are gone, and with them the last call to beta.threads in the startup path - so the cog cannot be killed by that API again. (add_files_to_vector_store / delete_files_from_vector_store still reference beta.assistants but are dead code - nothing calls them - so they cannot crash anything; left alone rather than widening this change.) 2) KEEPING A SELF-HOSTED MODEL WARM Loading is the slow part - the GPU is shared with other users - so we preload via Ollama's documented mechanism: /api/generate with a model, a keep_alive and NO prompt. It loads the model and generates nothing. * on switching to ollama, $gadaj_teraz fires a preload in the BACKGROUND (not awaited: loading can take minutes and the command must answer at once), so the wait lands on the operator rather than the first user; * a warm loop re-asserts keep_alive every CONJURER_OLLAMA_WARM_MINUTES. Both are hard-guarded on the ACTIVE provider being ollama. Warming a metered API would burn tokens and money for nothing, so that guard is pinned by a test asserting the preload is never called for gpt/claude, and another asserting the preload body carries no prompt (a prompt would make every warm-up generate). Tests: 82 unit + 71 integration green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit was merged in pull request #28.
This commit is contained in:
@@ -245,6 +245,16 @@ DELIVERED_DIR = os.getenv(
|
||||
)
|
||||
DELIVERED_MAX = int(os.getenv("CONJURER_DELIVERED_MAX", "10000"))
|
||||
|
||||
# Personal DM assistants. Replaces the OpenAI Assistants API (threads/runs),
|
||||
# which was sunset and answers 404: the persona now rides handle_response, so it
|
||||
# works on EVERY backend, and the conversation lives here instead of on OpenAI's
|
||||
# server. Kept per user so private DMs never bleed into the bar's shared memory,
|
||||
# and trimmed to the most recent turns so it cannot grow without bound.
|
||||
ASSISTANT_MEMORY_FILE = os.getenv(
|
||||
"CONJURER_ASSISTANT_MEMORY", os.path.join(_STATE_ROOT, "assistant_memory.json")
|
||||
)
|
||||
ASSISTANT_MEMORY_TURNS = int(os.getenv("CONJURER_ASSISTANT_MEMORY_TURNS", "40"))
|
||||
|
||||
FILE_SERVICE_ADDRESS = os.getenv("CONJURER_FILE_SERVICE", "http://192.168.1.15:5000")
|
||||
RADIO_HARBOR_ADDRESS = os.getenv("CONJURER_RADIO_HARBOR", "http://192.168.1.15:54321")
|
||||
# Betoniarka (radio-operator service colocated with Liquidsoap). Falls back to
|
||||
@@ -411,6 +421,19 @@ else:
|
||||
AI_TIMEOUT_SECONDS = int(os.getenv("CONJURER_AI_TIMEOUT_SECONDS", "120"))
|
||||
|
||||
OLLAMA_URL = os.getenv("CONJURER_OLLAMA_URL", "").rstrip("/")
|
||||
# Keeping a self-hosted model resident. Loading it is the slow part (it is
|
||||
# offloaded to a GPU shared with other users), so we preload it - Ollama's
|
||||
# /api/generate with a model and NO prompt loads it and generates nothing, which
|
||||
# costs no tokens and no money. KEEP_ALIVE is how long Ollama should then hold
|
||||
# it; the warm loop re-asserts that well inside the window.
|
||||
# STRICTLY Ollama-only: doing this against a paid API would burn tokens for
|
||||
# nothing, so every caller checks the active provider first.
|
||||
OLLAMA_KEEP_ALIVE = os.getenv("CONJURER_OLLAMA_KEEP_ALIVE", "30m")
|
||||
OLLAMA_WARM_MINUTES = float(os.getenv("CONJURER_OLLAMA_WARM_MINUTES", "10"))
|
||||
# A preload waits for the model to finish loading, which on a shared GPU is the
|
||||
# slow path we are trying to move off the user's first message.
|
||||
OLLAMA_PRELOAD_TIMEOUT = int(os.getenv("CONJURER_OLLAMA_PRELOAD_TIMEOUT", "600"))
|
||||
|
||||
OLLAMA_LATEST_MODEL = os.getenv("CONJURER_OLLAMA_MODEL", "llama3.1:8b")
|
||||
OLLAMA_CHEAP_MODEL = os.getenv("CONJURER_OLLAMA_CHEAP_MODEL", OLLAMA_LATEST_MODEL)
|
||||
if openai and OLLAMA_URL:
|
||||
|
||||
Reference in New Issue
Block a user