Spaces:
Sleeping
Sleeping
Download src/db.py from renuka010/llm_comparisons: direct link, hf CLI and curl.
- Browser
- Download file 3.33 kB
-
https://huggingface.co/spaces/renuka010/llm_comparisons/resolve/main/src/db.py
- Command line
-
hf download hf://spaces/renuka010/llm_comparisons/src/db.py
-
curl -L -o db.py https://huggingface.co/spaces/renuka010/llm_comparisons/resolve/main/src/db.py
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 | |
| 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) | |
| ); | |
| """) | |