49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
import os
|
|
|
|
import httpx
|
|
|
|
from app import audit
|
|
from app.constants import PROMPT_ALERT_SYSTEM
|
|
from app.crypto import SecretBox
|
|
from app.db import Database
|
|
from app.ollama import OllamaClient
|
|
from app.prompts import PromptRepository, PromptService, default_registry
|
|
from app.settings import SettingsStore
|
|
|
|
|
|
class Clock:
|
|
def __init__(self, now=1_800_000_000.0):
|
|
self.now = now
|
|
|
|
def __call__(self):
|
|
return self.now
|
|
|
|
|
|
def make_env(legacy=None):
|
|
db = Database(":memory:")
|
|
store = SettingsStore(db, SecretBox(os.urandom(32)))
|
|
if legacy is not None:
|
|
store.set(PROMPT_ALERT_SYSTEM, legacy, "t")
|
|
clock = Clock()
|
|
service = PromptService(PromptRepository(db), default_registry(), audit.recorder(db), store.get, clock)
|
|
return db, store, service, clock
|
|
|
|
|
|
def patch_ollama(monkeypatch, handler):
|
|
original = OllamaClient.__init__
|
|
|
|
def patched(self, *args, **kwargs):
|
|
kwargs["transport"] = httpx.MockTransport(handler)
|
|
kwargs["retries"] = 0
|
|
original(self, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(OllamaClient, "__init__", patched)
|
|
|
|
|
|
def chat_reply(content="Ergebnis"):
|
|
return httpx.Response(200, json={"message": {"role": "assistant", "content": content}})
|
|
|
|
|
|
def audit_dump(db):
|
|
return " ".join(str(tuple(row)) for row in db.query("SELECT * FROM audit"))
|