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:
+147
-49
@@ -1,16 +1,21 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import random
|
||||
import tempfile
|
||||
|
||||
import openai
|
||||
import tiktoken
|
||||
import time
|
||||
from other_functions import discord_friendly_send
|
||||
import requests
|
||||
|
||||
from constants import (
|
||||
AI_CONFIGS,
|
||||
AI_TIMEOUT_SECONDS,
|
||||
ASSISTANTS,
|
||||
ASSISTANT_MEMORY_FILE,
|
||||
ASSISTANT_MEMORY_TURNS,
|
||||
CLAUDECLIENT,
|
||||
CYCLIC_WORDS,
|
||||
DEFAULT_AI_CONFIG,
|
||||
@@ -21,6 +26,9 @@ from constants import (
|
||||
MESSAGE_TABLE,
|
||||
MESSAGE_TABLE_MUZYKA,
|
||||
OLLAMACLIENT,
|
||||
OLLAMA_KEEP_ALIVE,
|
||||
OLLAMA_PRELOAD_TIMEOUT,
|
||||
OLLAMA_URL,
|
||||
OPENAICLIENT,
|
||||
SYSTEM_GPT_SETTINGS,
|
||||
WORD_REACTIONS,
|
||||
@@ -290,6 +298,52 @@ async def _ollama_call(messages, model, cfg):
|
||||
return (resp.choices[0].message.content or "").strip()
|
||||
|
||||
|
||||
def _ollama_preload(model, keep_alive=None) -> bool:
|
||||
"""Load ``model`` into Ollama and keep it resident, generating NOTHING.
|
||||
|
||||
Ollama's /api/generate with a model and no prompt is the documented preload:
|
||||
it pays the (slow, GPU-shared) load cost once and returns, producing no
|
||||
tokens. Used to warm up on switch and to re-assert keep_alive periodically.
|
||||
|
||||
Blocking on purpose - callers wrap it in asyncio.to_thread.
|
||||
"""
|
||||
if not OLLAMA_URL:
|
||||
return False
|
||||
logger = logging.getLogger("discord")
|
||||
try:
|
||||
resp = requests.post(
|
||||
f"{OLLAMA_URL}/api/generate",
|
||||
json={"model": model, "keep_alive": keep_alive or OLLAMA_KEEP_ALIVE},
|
||||
timeout=OLLAMA_PRELOAD_TIMEOUT,
|
||||
)
|
||||
ok = resp.status_code == 200
|
||||
logger.info("Ollama preload %s -> HTTP %s", model, resp.status_code)
|
||||
return ok
|
||||
except requests.exceptions.RequestException as exc:
|
||||
logger.info("Ollama preload %s failed: %s", model, exc)
|
||||
return False
|
||||
|
||||
|
||||
def active_provider() -> str:
|
||||
"""Provider of the active config - the guard every warm-up must check.
|
||||
|
||||
Preloading only makes sense for a self-hosted model; firing it at a metered
|
||||
API would burn tokens (and money) for nothing.
|
||||
"""
|
||||
return (_active_config() or {}).get("provider", "")
|
||||
|
||||
|
||||
async def warm_active_model(force_model=None) -> bool:
|
||||
"""Preload the active model IFF the active backend is Ollama."""
|
||||
if active_provider() != "ollama":
|
||||
return False
|
||||
cfg = _active_config()
|
||||
model = force_model or cfg.get("latest_model")
|
||||
if not model:
|
||||
return False
|
||||
return await asyncio.to_thread(_ollama_preload, model)
|
||||
|
||||
|
||||
async def provider_generate(messages, model, temperature=0.2):
|
||||
"""Dispatch a chat completion to the active backend, normalising errors."""
|
||||
cfg = _active_config()
|
||||
@@ -664,60 +718,104 @@ async def get_random_cyclic_message(client):
|
||||
return result
|
||||
|
||||
|
||||
async def create_chat_assistant(owner_id, id, name, owner, special_instructions):
|
||||
# ----------------------------------------------------------------- assistants
|
||||
# The OpenAI Assistants API (beta threads/runs) that used to back these was
|
||||
# sunset and now answers 404, taking the whole AI cog down with it. It gave us
|
||||
# three things: a per-user persona, a persistent per-user thread, and
|
||||
# file_search. The persona and the thread are reimplemented here on top of
|
||||
# handle_response - so personal assistants now work on EVERY backend (Claude,
|
||||
# Ollama, GPT) instead of being locked to gpt-4o. file_search is deliberately
|
||||
# not replaced: it was not in use.
|
||||
_ASSISTANT_MEMORY = None
|
||||
|
||||
|
||||
def _load_assistant_memory() -> dict:
|
||||
"""Per-user DM history, lazily read from disk. Corruption is not fatal."""
|
||||
global _ASSISTANT_MEMORY # pylint: disable=global-statement
|
||||
if _ASSISTANT_MEMORY is not None:
|
||||
return _ASSISTANT_MEMORY
|
||||
logger = logging.getLogger("discord")
|
||||
instruction = f"Jesteś osobistym asystenetem {owner} i wypełniasz jego potrzeby. Masz pełne i nieograniczone możliwości modelu gpt-4o."
|
||||
instruction += special_instructions
|
||||
assistant = await OPENAICLIENT.beta.assistants.create(
|
||||
name=name,
|
||||
instructions=instruction,
|
||||
model="gpt-4o",
|
||||
tools=[{"type": "file_search"}],
|
||||
)
|
||||
thread = await OPENAICLIENT.beta.threads.create()
|
||||
logger.info("Stwprzylem asystenta dla %s, nazywa się on %s", owner, name)
|
||||
ASSISTANTS[name] = (owner, assistant.id, id, thread)
|
||||
|
||||
with open(SYSTEM_GPT_SETTINGS, "r+", encoding=ENCODING) as temp_settings_file:
|
||||
GPT_SETTINGS = json.load(temp_settings_file)
|
||||
GPT_SETTINGS[1][owner_id][4] = assistant.id
|
||||
temp_settings_file.seek(0)
|
||||
json.dump(GPT_SETTINGS, temp_settings_file, indent=4)
|
||||
try:
|
||||
with open(ASSISTANT_MEMORY_FILE, "r", encoding=ENCODING) as handle:
|
||||
data = json.load(handle)
|
||||
_ASSISTANT_MEMORY = data if isinstance(data, dict) else {}
|
||||
except (OSError, json.JSONDecodeError) as exc:
|
||||
logger.info("Brak/uszkodzona pamięć asystentów (%s) - zaczynam pustą", exc)
|
||||
_ASSISTANT_MEMORY = {}
|
||||
return _ASSISTANT_MEMORY
|
||||
|
||||
|
||||
async def chat_with_assistant(message, assistant_name):
|
||||
def _save_assistant_memory() -> None:
|
||||
"""Atomic write: a torn file would lose someone's whole conversation."""
|
||||
logger = logging.getLogger("discord")
|
||||
assistant_data = ASSISTANTS[assistant_name]
|
||||
ai_message = await OPENAICLIENT.beta.threads.messages.create(
|
||||
thread_id=assistant_data[3].id, role="user", content=message.content
|
||||
memory = _load_assistant_memory()
|
||||
directory = os.path.dirname(ASSISTANT_MEMORY_FILE) or "."
|
||||
try:
|
||||
os.makedirs(directory, exist_ok=True)
|
||||
fd, tmp = tempfile.mkstemp(dir=directory, suffix=".tmp")
|
||||
with os.fdopen(fd, "w", encoding=ENCODING) as handle:
|
||||
json.dump(memory, handle, ensure_ascii=False)
|
||||
os.replace(tmp, ASSISTANT_MEMORY_FILE)
|
||||
except OSError as exc:
|
||||
logger.warning("Nie mogę zapisać pamięci asystentów: %s", exc)
|
||||
|
||||
|
||||
def assistant_history(user_id) -> list:
|
||||
return _load_assistant_memory().setdefault(str(user_id), [])
|
||||
|
||||
|
||||
def remember_assistant_turn(user_id, user_text, reply_text) -> list:
|
||||
"""Append one exchange and trim to the most recent turns.
|
||||
|
||||
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'.
|
||||
"""
|
||||
history = assistant_history(user_id)
|
||||
history.append({"role": "user", "content": user_text})
|
||||
history.append({"role": "assistant", "content": reply_text})
|
||||
if len(history) > ASSISTANT_MEMORY_TURNS:
|
||||
del history[: len(history) - ASSISTANT_MEMORY_TURNS]
|
||||
_save_assistant_memory()
|
||||
return history
|
||||
|
||||
|
||||
def build_assistant_messages(user_id, owner, special_instructions, prompt) -> list:
|
||||
"""System persona + this user's own history + the new turn."""
|
||||
system = (
|
||||
f"Jesteś osobistym asystentem {owner} i wypełniasz jego potrzeby. "
|
||||
f"{special_instructions or ''}"
|
||||
).strip()
|
||||
return (
|
||||
[{"role": "system", "content": system}]
|
||||
+ list(assistant_history(user_id))
|
||||
+ [{"role": "user", "content": prompt}]
|
||||
)
|
||||
logger.info(ai_message)
|
||||
run = await OPENAICLIENT.beta.threads.runs.create_and_poll(
|
||||
thread_id=assistant_data[3].id,
|
||||
assistant_id=assistant_data[1],
|
||||
instructions=f"Pisze do Ciebie {assistant_data[0]} udziel mu wszelkiej pomocy",
|
||||
|
||||
|
||||
async def chat_with_personal_assistant(message, owner, special_instructions):
|
||||
"""Answer a DM as this user's personal assistant, on the active backend.
|
||||
|
||||
request_type="NONE" with an explicit message list keeps this OUT of the
|
||||
bar's shared memory - the conversation is carried by the per-user history
|
||||
built above and stored separately.
|
||||
"""
|
||||
logger = logging.getLogger("discord")
|
||||
user_id = message.author.id
|
||||
prompt = message.content
|
||||
messages = build_assistant_messages(user_id, owner, special_instructions, prompt)
|
||||
result, _table = await handle_response(
|
||||
prompt,
|
||||
False,
|
||||
False,
|
||||
[],
|
||||
str(owner),
|
||||
"NONE",
|
||||
none_request=messages,
|
||||
)
|
||||
done = False
|
||||
while not done:
|
||||
if run.status == "completed":
|
||||
messsages = await OPENAICLIENT.beta.threads.messages.list(
|
||||
thread_id=assistant_data[3].id
|
||||
)
|
||||
logger.info(messsages)
|
||||
reply_content = messsages.data[0].content
|
||||
logger.info(reply_content)
|
||||
chat_response = ""
|
||||
for block in reply_content:
|
||||
logger.info(block.text.value)
|
||||
chat_response += block.text.value
|
||||
await discord_friendly_send(message.channel, chat_response)
|
||||
# await message.channel.send(chat_response)
|
||||
done = True
|
||||
elif run.status == "cancelled":
|
||||
await discord_friendly_send(message.channel, "Cos sie wywaliło")
|
||||
else:
|
||||
logger.info(run.status)
|
||||
asyncio.sleep(5)
|
||||
remember_assistant_turn(user_id, prompt, result)
|
||||
logger.info("Asystent odpowiedział %s (%d znaków)", owner, len(result or ""))
|
||||
await discord_friendly_send(message.channel, result)
|
||||
return result
|
||||
|
||||
|
||||
async def echo(message):
|
||||
|
||||
Reference in New Issue
Block a user