renuka010
initial commit
ad8e354
Raw History Blame Contribute Delete
3.33 kB
import sqlite3
from contextlib import contextmanager
from pathlib import Path
from src.config import cfg
def _connect() -> sqlite3.Connection:
Path(cfg.DB_PATH).parent.mkdir(parents=True, exist_ok=True)
conn = sqlite3.connect(cfg.DB_PATH, check_same_thread=False)
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA journal_mode=WAL")
return conn
@contextmanager
def get_db():
conn = _connect()
try:
yield conn
conn.commit()
except Exception:
conn.rollback()
raise
finally:
conn.close()
def init_db() -> None:
with get_db() as conn:
conn.executescript("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS conversations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL DEFAULT 'New Chat',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
conversation_id INTEGER NOT NULL,
model_type TEXT NOT NULL,
role TEXT NOT NULL,
content TEXT NOT NULL,
latency_ms INTEGER,
token_estimate INTEGER,
error TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (conversation_id) REFERENCES conversations(id)
);
CREATE TABLE IF NOT EXISTS conversation_summaries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
conversation_id INTEGER NOT NULL,
model_type TEXT NOT NULL,
summary TEXT NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (conversation_id) REFERENCES conversations(id),
UNIQUE(conversation_id, model_type)
);
CREATE TABLE IF NOT EXISTS memory_facts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
conversation_id INTEGER NOT NULL,
fact TEXT NOT NULL,
keywords TEXT,
importance INTEGER DEFAULT 1,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (conversation_id) REFERENCES conversations(id)
);
CREATE TABLE IF NOT EXISTS eval_prompts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
category TEXT NOT NULL,
prompt TEXT NOT NULL,
expected_behavior TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS eval_results (
id INTEGER PRIMARY KEY AUTOINCREMENT,
prompt_id INTEGER NOT NULL,
model_type TEXT NOT NULL,
response TEXT NOT NULL,
score INTEGER NOT NULL,
notes TEXT,
latency_ms INTEGER,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (prompt_id) REFERENCES eval_prompts(id)
);
""")