|
| 1 | +# database/db_manager.py |
| 2 | +import sqlite3 |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +class DatabaseManager: |
| 6 | + def add_interaction_metrics(self, model_name, prompt, response, tokens_used, response_time, success_rate): |
| 7 | + with sqlite3.connect(self.db_path) as conn: |
| 8 | + cursor = conn.cursor() |
| 9 | + cursor.execute(''' |
| 10 | + INSERT INTO model_metrics |
| 11 | + (timestamp, model_name, prompt_length, response_length, tokens_used, |
| 12 | + response_time, success_rate) |
| 13 | + VALUES (?, ?, ?, ?, ?, ?, ?) |
| 14 | + ''', ( |
| 15 | + datetime.now(), model_name, len(prompt), len(response), |
| 16 | + tokens_used, response_time, success_rate |
| 17 | + )) |
| 18 | + |
| 19 | + def init_database(self): |
| 20 | + with sqlite3.connect(self.db_path) as conn: |
| 21 | + cursor = conn.cursor() |
| 22 | + |
| 23 | + # Create tables |
| 24 | + cursor.execute(''' |
| 25 | + CREATE TABLE IF NOT EXISTS chat_history ( |
| 26 | + id INTEGER PRIMARY KEY, |
| 27 | + timestamp DATETIME, |
| 28 | + model TEXT, |
| 29 | + message TEXT, |
| 30 | + response TEXT, |
| 31 | + tokens INTEGER, |
| 32 | + response_time FLOAT |
| 33 | + ) |
| 34 | + ''') |
| 35 | + |
| 36 | + cursor.execute(''' |
| 37 | + CREATE TABLE IF NOT EXISTS model_metrics ( |
| 38 | + id INTEGER PRIMARY KEY, |
| 39 | + model TEXT, |
| 40 | + date DATE, |
| 41 | + total_tokens INTEGER, |
| 42 | + avg_response_time FLOAT, |
| 43 | + total_conversations INTEGER |
| 44 | + ) |
| 45 | + ''') |
| 46 | + |
| 47 | + conn.commit() |
| 48 | + |
| 49 | + def add_chat_entry(self, model, message, response, tokens, response_time): |
| 50 | + with sqlite3.connect(self.db_path) as conn: |
| 51 | + cursor = conn.cursor() |
| 52 | + cursor.execute(''' |
| 53 | + INSERT INTO chat_history (timestamp, model, message, response, tokens, response_time) |
| 54 | + VALUES (?, ?, ?, ?, ?, ?) |
| 55 | + ''', (datetime.now(), model, message, response, tokens, response_time)) |
| 56 | + conn.commit() |
0 commit comments