Fix ai-service Dockerfile: bake in drug_entities.json, override its path
This commit is contained in:
@@ -93,3 +93,59 @@ class PostgresTraceRepository:
|
||||
decision=row[4], reason=row[5], resolved_drug_id=row[6],
|
||||
citations=tuple(row[7]), created_at=row[8],
|
||||
)
|
||||
|
||||
|
||||
class PostgresConversationStore:
|
||||
"""Durable, cross-worker replacement for `RagAgent`'s in-process
|
||||
`dict[str, list[str]]` history — the F-08 gap named in ADR 0008: history
|
||||
was lost on restart and not shared across workers.
|
||||
|
||||
Append-only, one connection per call — the same tradeoffs as
|
||||
`PostgresTraceRepository` (F-09: real pooling is a further improvement
|
||||
not made here) and the same `connect_timeout=5` for the same reason
|
||||
(an unreachable-but-not-refusing host hangs on the OS TCP timeout
|
||||
otherwise, defeating a caller's fail-open try/except just as completely
|
||||
as no try/except at all).
|
||||
|
||||
Raises on any error rather than swallowing it — `RagAgent` is the
|
||||
caller that decides fail-open (lose this turn's memory, not the
|
||||
response), matching how `routers/rag.py` already wraps
|
||||
`PostgresTraceRepository.save()` rather than the repository hiding its
|
||||
own failures.
|
||||
"""
|
||||
|
||||
def __init__(self, dsn: str) -> None:
|
||||
self._dsn = dsn
|
||||
|
||||
def migrate(self, migration_path: Path) -> None:
|
||||
import psycopg
|
||||
|
||||
statement = migration_path.read_text(encoding="utf-8")
|
||||
with psycopg.connect(self._dsn, connect_timeout=5) as connection:
|
||||
connection.execute(statement)
|
||||
|
||||
def recent(self, conversation_id: str, limit: int) -> list[str]:
|
||||
"""The last `limit` lines, oldest first — matches the ordering the
|
||||
understanding LLM prompt already expects ("LỊCH SỬ HỘI THOẠI (cũ ->
|
||||
mới)")."""
|
||||
import psycopg
|
||||
|
||||
with psycopg.connect(self._dsn, connect_timeout=5) as connection:
|
||||
rows = connection.execute(
|
||||
"""
|
||||
SELECT line FROM rag_conversation_turn
|
||||
WHERE conversation_id = %s ORDER BY id DESC LIMIT %s
|
||||
""",
|
||||
(conversation_id, limit),
|
||||
).fetchall()
|
||||
return [row[0] for row in reversed(rows)]
|
||||
|
||||
def append(self, conversation_id: str, line: str) -> None:
|
||||
import psycopg
|
||||
|
||||
with psycopg.connect(self._dsn, connect_timeout=5) as connection:
|
||||
connection.execute(
|
||||
"INSERT INTO rag_conversation_turn (conversation_id, line) "
|
||||
"VALUES (%s, %s)",
|
||||
(conversation_id, line),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user