|
| 1 | +import json |
| 2 | +import sqlite3 |
| 3 | +from dataclasses import asdict, dataclass, fields |
| 4 | + |
| 5 | + |
| 6 | +@dataclass |
1 | 7 | class Settings: |
2 | | - def __init__(self, model_path_or_name: str, db_path: str = "sqliterag.db"): |
3 | | - self.model_path_or_name = model_path_or_name |
4 | | - self.db_path = db_path |
| 8 | + model_path_or_name: str = "./models/Qwen3-Embedding-0.6B-Q8_0.gguf" |
| 9 | + model_config: str = "n_ctx=384" |
| 10 | + |
| 11 | + embedding_dim: int = 384 |
| 12 | + vector_type: str = "FLOAT32" |
| 13 | + other_vector_config: str = "distance=cosine" # e.g. distance=metric,other=value,... |
| 14 | + |
| 15 | + chunk_size: int = 12000 |
| 16 | + # Token overlap between chunks |
| 17 | + chunk_overlap: int = 1200 |
| 18 | + |
| 19 | + # Whether to quantize the vector for faster search |
| 20 | + quantize_scan: bool = True |
| 21 | + |
| 22 | + |
| 23 | +class SettingsManager: |
| 24 | + def __init__(self, connection: sqlite3.Connection): |
| 25 | + self.connection = connection |
| 26 | + self._ensure_table_exists() |
| 27 | + |
| 28 | + def _ensure_table_exists(self): |
| 29 | + cursor = self.connection.cursor() |
| 30 | + cursor.execute( |
| 31 | + """ |
| 32 | + CREATE TABLE IF NOT EXISTS settings ( |
| 33 | + id TEXT PRIMARY KEY, |
| 34 | + settings JSON NOT NULL |
| 35 | + ); |
| 36 | + """ |
| 37 | + ) |
| 38 | + self.connection.commit() |
| 39 | + |
| 40 | + def load_settings(self) -> Settings | None: |
| 41 | + cursor = self.connection.cursor() |
| 42 | + |
| 43 | + cursor.execute("SELECT settings FROM settings LIMIT 1") |
| 44 | + row = cursor.fetchone() |
| 45 | + |
| 46 | + if not row: |
| 47 | + return None |
| 48 | + |
| 49 | + current_settings = json.loads(row[0]) |
| 50 | + |
| 51 | + # Start from defaults, update with values from db (ignore extra keys) |
| 52 | + defaults = Settings() |
| 53 | + valid_keys = {f.name for f in fields(Settings)} |
| 54 | + filtered = {k: v for k, v in current_settings.items() if k in valid_keys} |
| 55 | + |
| 56 | + # Use defaults as base, update with valid properties |
| 57 | + settings_dict = {**asdict(defaults), **filtered} |
| 58 | + return Settings(**settings_dict) |
| 59 | + |
| 60 | + def store(self, settings: Settings): |
| 61 | + cursor = self.connection.cursor() |
5 | 62 |
|
6 | | - self.embedding_dim = 384 |
7 | | - self.vector_type = "FLOAT32" |
| 63 | + settings_json = json.dumps(asdict(settings)) |
8 | 64 |
|
9 | | - self.model_config = "n_ctx=384" # See: https://github.com/sqliteai/sqlite-ai/blob/e172b9c9b9d76435be635d1e02c1e88b3681cc6e/src/sqlite-ai.c#L51-L57 |
| 65 | + # Upsert the settings |
| 66 | + cursor.execute( |
| 67 | + """ |
| 68 | + INSERT INTO settings (id, settings) |
| 69 | + VALUES ('1', ?) |
| 70 | + ON CONFLICT(id) DO UPDATE SET settings = excluded.settings; |
| 71 | + """, |
| 72 | + (settings_json,), |
| 73 | + ) |
10 | 74 |
|
11 | | - self.chunk_size = 256 # Maximum tokens per chunk |
12 | | - self.chunk_overlap = 32 # Token overlap between chunks |
| 75 | + self.connection.commit() |
| 76 | + return settings |
13 | 77 |
|
14 | | - self.quantize_scan = True # Whether to quantize the vector for faster search |
| 78 | + def has_critical_changes( |
| 79 | + self, new_settings: Settings, current_settings: Settings |
| 80 | + ) -> bool: |
| 81 | + """Check if the new settings have critical changes compared to the current settings.""" |
| 82 | + return ( |
| 83 | + new_settings.model_path_or_name != current_settings.model_path_or_name |
| 84 | + or new_settings.embedding_dim != current_settings.embedding_dim |
| 85 | + or new_settings.vector_type != current_settings.vector_type |
| 86 | + ) |
0 commit comments