feat: Konversationsgedaechtnis - Kurzzeit-Gespraechsverlauf pro Session
- Store: messages-Tabelle + append_message/get_recent_messages (mit Mandanten-Schutz) - LLMProvider.complete um history erweitert (openrouter + local bauen system+history+user) - Orchestrator.chat_text reicht den Verlauf ans LLM weiter - chat.py: bei session_id letzte HISTORY_MAX_MESSAGES laden, danach User-/Assistant-Turn speichern - ohne session_id weiterhin zustandslos; ?debug zeigt history_len - Config HISTORY_MAX_MESSAGES (Default 10) - Tests: 32 gruen (3 neue Gedaechtnis-Tests) - Doku aktualisiert (README, BEDIENUNGSANLEITUNG, Architektur) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
e0e69fdf15
commit
16a964032e
12 changed files with 225 additions and 40 deletions
|
|
@ -11,6 +11,7 @@ from app.dependencies import (
|
|||
resolve_route,
|
||||
build_orchestrator,
|
||||
resolve_output_endpoint,
|
||||
get_store,
|
||||
)
|
||||
from app.schemas import ChatRequest
|
||||
|
||||
|
|
@ -50,10 +51,18 @@ async def chat(
|
|||
}
|
||||
voice = payload.voice or settings.openrouter_tts_voice
|
||||
|
||||
store = get_store()
|
||||
|
||||
try:
|
||||
route = resolve_route(user, session_id, overrides)
|
||||
orchestrator = build_orchestrator(route)
|
||||
output = await resolve_output_endpoint(route)
|
||||
# Gespraechsverlauf laden (nur bei gesetzter session_id -> sonst zustandslos).
|
||||
history = (
|
||||
store.get_recent_messages(session_id, settings.history_max_messages)
|
||||
if session_id
|
||||
else []
|
||||
)
|
||||
except SessionOwnershipError as exc:
|
||||
raise HTTPException(status_code=403, detail=str(exc))
|
||||
except RoutingError as exc:
|
||||
|
|
@ -65,33 +74,39 @@ async def chat(
|
|||
language=route.language,
|
||||
voice=voice,
|
||||
output=output,
|
||||
history=history,
|
||||
)
|
||||
|
||||
if debug:
|
||||
return JSONResponse(
|
||||
content={
|
||||
"ok": True,
|
||||
"voice": voice,
|
||||
"route": route.as_dict(),
|
||||
"trace": {
|
||||
"raw_transcript": trace.raw_transcript,
|
||||
"cleaned_transcript": trace.cleaned_transcript,
|
||||
"semantic_response": trace.semantic_response,
|
||||
"spoken_response": trace.spoken_response,
|
||||
"tts_ready_text": trace.tts_ready_text,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
headers = {
|
||||
"Content-Language": route.language,
|
||||
"X-Audio-Format": "pcm",
|
||||
"X-Audio-Sample-Rate": "24000",
|
||||
"X-Audio-Channels": "1",
|
||||
"X-Audio-Sample-Width": "16",
|
||||
**_route_headers(route),
|
||||
}
|
||||
return StreamingResponse(BytesIO(audio), media_type="audio/pcm", headers=headers)
|
||||
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=502, detail=str(exc))
|
||||
|
||||
# Turn persistieren (User-Eingabe + semantische Antwort) fuer das Gedaechtnis.
|
||||
if session_id:
|
||||
store.append_message(session_id, user.id, "user", payload.text)
|
||||
store.append_message(session_id, user.id, "assistant", trace.semantic_response)
|
||||
|
||||
if debug:
|
||||
return JSONResponse(
|
||||
content={
|
||||
"ok": True,
|
||||
"voice": voice,
|
||||
"route": route.as_dict(),
|
||||
"history_len": len(history),
|
||||
"trace": {
|
||||
"raw_transcript": trace.raw_transcript,
|
||||
"cleaned_transcript": trace.cleaned_transcript,
|
||||
"semantic_response": trace.semantic_response,
|
||||
"spoken_response": trace.spoken_response,
|
||||
"tts_ready_text": trace.tts_ready_text,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
headers = {
|
||||
"Content-Language": route.language,
|
||||
"X-Audio-Format": "pcm",
|
||||
"X-Audio-Sample-Rate": "24000",
|
||||
"X-Audio-Channels": "1",
|
||||
"X-Audio-Sample-Width": "16",
|
||||
**_route_headers(route),
|
||||
}
|
||||
return StreamingResponse(BytesIO(audio), media_type="audio/pcm", headers=headers)
|
||||
|
|
|
|||
|
|
@ -123,6 +123,7 @@ class Settings(BaseSettings):
|
|||
db_path: str = str(BASE_DIR / "data" / "voice-assistant.db")
|
||||
admin_api_key: str = ""
|
||||
auth_enabled: bool = True
|
||||
history_max_messages: int = 10
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=ENV_FILE, case_sensitive=False, extra="ignore"
|
||||
)
|
||||
|
|
|
|||
|
|
@ -76,6 +76,7 @@ class Orchestrator:
|
|||
language: str | None = None,
|
||||
voice: str | None = None,
|
||||
output=None,
|
||||
history: list[dict] | None = None,
|
||||
):
|
||||
trace = PipelineTrace()
|
||||
|
||||
|
|
@ -83,7 +84,8 @@ class Orchestrator:
|
|||
trace.cleaned_transcript = await self.input_cleaner.run(text or "")
|
||||
|
||||
trace.semantic_response = await self.llm.complete(
|
||||
trace.cleaned_transcript or ""
|
||||
trace.cleaned_transcript or "",
|
||||
history=history,
|
||||
)
|
||||
if not trace.semantic_response:
|
||||
raise RuntimeError("LLM returned an empty response")
|
||||
|
|
|
|||
|
|
@ -2,4 +2,9 @@ from abc import ABC, abstractmethod
|
|||
|
||||
class LLMProvider(ABC):
|
||||
@abstractmethod
|
||||
async def complete(self, text: str, session_id: str | None = None) -> str: ...
|
||||
async def complete(
|
||||
self,
|
||||
text: str,
|
||||
history: list[dict] | None = None,
|
||||
session_id: str | None = None,
|
||||
) -> str: ...
|
||||
|
|
|
|||
|
|
@ -7,10 +7,17 @@ class LocalOpenAICompatibleLLM(LLMProvider):
|
|||
self.api_key = api_key
|
||||
self.model = model
|
||||
|
||||
async def complete(self, text: str, session_id: str | None = None) -> str:
|
||||
async def complete(
|
||||
self,
|
||||
text: str,
|
||||
history: list[dict] | None = None,
|
||||
session_id: str | None = None,
|
||||
) -> str:
|
||||
messages = list(history) if history else []
|
||||
messages.append({"role": "user", "content": text})
|
||||
payload = {
|
||||
"model": self.model,
|
||||
"messages": [{"role": "user", "content": text}],
|
||||
"messages": messages,
|
||||
"temperature": 0.3,
|
||||
}
|
||||
async with httpx.AsyncClient(timeout=120) as client:
|
||||
|
|
|
|||
|
|
@ -49,7 +49,12 @@ class OpenRouterLLMProvider(LLMProvider):
|
|||
self.api_key = (api_key or "").strip()
|
||||
self.model = (model or "").strip()
|
||||
|
||||
async def complete(self, text: str, session_id: str | None = None) -> str:
|
||||
async def complete(
|
||||
self,
|
||||
text: str,
|
||||
history: list[dict] | None = None,
|
||||
session_id: str | None = None,
|
||||
) -> str:
|
||||
if not self.api_key:
|
||||
raise ValueError("OPENROUTER_API_KEY is empty")
|
||||
if not self.model:
|
||||
|
|
@ -57,12 +62,14 @@ class OpenRouterLLMProvider(LLMProvider):
|
|||
if not text or not text.strip():
|
||||
raise ValueError("LLM input text is empty")
|
||||
|
||||
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
||||
if history:
|
||||
messages.extend(history)
|
||||
messages.append({"role": "user", "content": text.strip()})
|
||||
|
||||
payload = {
|
||||
"model": self.model,
|
||||
"messages": [
|
||||
{"role": "system", "content": SYSTEM_PROMPT},
|
||||
{"role": "user", "content": text.strip()},
|
||||
],
|
||||
"messages": messages,
|
||||
}
|
||||
|
||||
timeout = httpx.Timeout(connect=10.0, read=120.0, write=30.0, pool=10.0)
|
||||
|
|
|
|||
49
app/store.py
49
app/store.py
|
|
@ -70,6 +70,14 @@ class Store(ABC):
|
|||
def update_session(self, session_id: str, user_id: str, values: dict) -> Session:
|
||||
"""Erstellt/aktualisiert eine Session des Nutzers. Fremde Session -> SessionOwnershipError."""
|
||||
|
||||
@abstractmethod
|
||||
def append_message(self, session_id: str, user_id: str, role: str, content: str) -> None:
|
||||
"""Haengt eine Nachricht an die Session an. Fremde Session -> SessionOwnershipError."""
|
||||
|
||||
@abstractmethod
|
||||
def get_recent_messages(self, session_id: str, limit: int) -> list[dict]:
|
||||
"""Liefert die letzten `limit` Nachrichten chronologisch ([{'role','content'}, ...])."""
|
||||
|
||||
|
||||
class SQLiteStore(Store):
|
||||
def __init__(self, db_path: str):
|
||||
|
|
@ -102,6 +110,15 @@ class SQLiteStore(Store):
|
|||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
session_id TEXT NOT NULL,
|
||||
role TEXT NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_session
|
||||
ON messages(session_id, id);
|
||||
"""
|
||||
)
|
||||
|
||||
|
|
@ -195,3 +212,35 @@ class SQLiteStore(Store):
|
|||
(session_id, user_id, payload, now, now),
|
||||
)
|
||||
return Session(id=session_id, user_id=user_id, data=data)
|
||||
|
||||
# ----- Nachrichten / Gespraechsverlauf ----------------------------------
|
||||
def append_message(self, session_id: str, user_id: str, role: str, content: str) -> None:
|
||||
existing = self.get_session(session_id)
|
||||
if existing and existing.user_id != user_id:
|
||||
raise SessionOwnershipError(
|
||||
f"Session {session_id!r} gehoert einem anderen Nutzer"
|
||||
)
|
||||
now = _now()
|
||||
with self._connect() as conn:
|
||||
if not existing:
|
||||
conn.execute(
|
||||
"INSERT INTO sessions (id, user_id, data_json, created_at, updated_at)"
|
||||
" VALUES (?, ?, ?, ?, ?)",
|
||||
(session_id, user_id, "{}", now, now),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO messages (session_id, role, content, created_at)"
|
||||
" VALUES (?, ?, ?, ?)",
|
||||
(session_id, role, content, now),
|
||||
)
|
||||
|
||||
def get_recent_messages(self, session_id: str, limit: int) -> list[dict]:
|
||||
if limit <= 0:
|
||||
return []
|
||||
with self._connect() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT role, content FROM messages WHERE session_id = ?"
|
||||
" ORDER BY id DESC LIMIT ?",
|
||||
(session_id, limit),
|
||||
).fetchall()
|
||||
return [{"role": row["role"], "content": row["content"]} for row in reversed(rows)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue