-
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathembedding_config.py
More file actions
197 lines (171 loc) · 7.04 KB
/
embedding_config.py
File metadata and controls
197 lines (171 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""Embedding provider configuration for Thoth knowledge and documents."""
from __future__ import annotations
import json
import logging
import os
import pathlib
from copy import deepcopy
from typing import Any
logger = logging.getLogger(__name__)
DATA_DIR = pathlib.Path(os.environ.get("THOTH_DATA_DIR", pathlib.Path.home() / ".thoth"))
DATA_DIR.mkdir(parents=True, exist_ok=True)
CONFIG_PATH = DATA_DIR / "embedding_config.json"
LOCAL_MODELS: dict[str, dict[str, Any]] = {
"qwen3-0.6b": {
"label": "Qwen3 0.6B",
"model": "Qwen/Qwen3-Embedding-0.6B",
"description": "Quality multilingual local embeddings. Runtime download.",
"dimension": 1024,
"context": 32768,
"trust_remote_code": False,
},
"nomic-v1.5": {
"label": "Nomic Embed Text v1.5",
"model": "nomic-ai/nomic-embed-text-v1.5",
"description": "Fast local embeddings with lower memory pressure. Runtime download.",
"dimension": 768,
"context": 8192,
"trust_remote_code": True,
"required_packages": ["einops"],
},
"mxbai-large-v1": {
"label": "Mixedbread Embed Large v1",
"model": "mixedbread-ai/mxbai-embed-large-v1",
"description": "Strong English retrieval local embeddings. Runtime download.",
"dimension": 1024,
"context": 512,
"trust_remote_code": False,
},
}
CLOUD_MODELS: dict[str, dict[str, Any]] = {
"openai:text-embedding-3-small": {
"label": "OpenAI text-embedding-3-small",
"provider": "openai",
"model": "text-embedding-3-small",
"dimension": 1536,
"dimension_options": [256, 512, 1024, 1536],
"api_key": "OPENAI_API_KEY",
},
"openai:text-embedding-3-large": {
"label": "OpenAI text-embedding-3-large",
"provider": "openai",
"model": "text-embedding-3-large",
"dimension": 3072,
"dimension_options": [256, 512, 1024, 1536, 3072],
"api_key": "OPENAI_API_KEY",
},
"google:gemini-embedding-001": {
"label": "Google Gemini Embedding",
"provider": "google",
"model": "models/gemini-embedding-001",
"dimension": 3072,
"dimension_options": [768, 1536, 3072],
"api_key": "GOOGLE_API_KEY",
},
}
DEFAULT_CONFIG: dict[str, Any] = {
"provider": "local",
"local_model": "mxbai-large-v1",
"cloud_model": "openai:text-embedding-3-small",
"dimension": None,
"batch_size": 32,
"auto_unload": True,
}
def get_embedding_config() -> dict[str, Any]:
"""Return embedding config with defaults filled."""
cfg = deepcopy(DEFAULT_CONFIG)
if CONFIG_PATH.exists():
try:
stored = json.loads(CONFIG_PATH.read_text(encoding="utf-8"))
if isinstance(stored, dict):
cfg.update(stored)
except (OSError, json.JSONDecodeError):
logger.warning("Failed to load embedding config from %s", CONFIG_PATH, exc_info=True)
cfg["provider"] = cfg.get("provider") if cfg.get("provider") in {"local", "cloud"} else "local"
if cfg.get("local_model") not in LOCAL_MODELS:
cfg["local_model"] = DEFAULT_CONFIG["local_model"]
if cfg.get("cloud_model") not in CLOUD_MODELS:
cfg["cloud_model"] = DEFAULT_CONFIG["cloud_model"]
try:
cfg["batch_size"] = max(1, min(256, int(cfg.get("batch_size") or DEFAULT_CONFIG["batch_size"])))
except (TypeError, ValueError):
cfg["batch_size"] = DEFAULT_CONFIG["batch_size"]
if cfg.get("dimension") in ("", 0, "0"):
cfg["dimension"] = None
return cfg
def save_embedding_config(updates: dict[str, Any]) -> dict[str, Any]:
"""Persist embedding config updates and return the normalized config."""
cfg = get_embedding_config()
cfg.update(updates)
cfg["provider"] = cfg.get("provider") if cfg.get("provider") in {"local", "cloud"} else "local"
if cfg.get("local_model") not in LOCAL_MODELS:
cfg["local_model"] = DEFAULT_CONFIG["local_model"]
if cfg.get("cloud_model") not in CLOUD_MODELS:
cfg["cloud_model"] = DEFAULT_CONFIG["cloud_model"]
try:
cfg["batch_size"] = max(1, min(256, int(cfg.get("batch_size") or DEFAULT_CONFIG["batch_size"])))
except (TypeError, ValueError):
cfg["batch_size"] = DEFAULT_CONFIG["batch_size"]
if cfg.get("dimension") in ("", 0, "0"):
cfg["dimension"] = None
CONFIG_PATH.write_text(json.dumps(cfg, indent=2), encoding="utf-8")
return cfg
def active_embedding_metadata(config: dict[str, Any] | None = None) -> dict[str, Any]:
"""Return stable metadata that identifies compatible FAISS indexes."""
cfg = config or get_embedding_config()
if cfg.get("provider") == "cloud":
model_def = CLOUD_MODELS[str(cfg["cloud_model"])]
provider = str(model_def["provider"])
model_id = str(model_def["model"])
dimension = _normalized_dimension(cfg.get("dimension"), int(model_def["dimension"]))
else:
model_def = LOCAL_MODELS[str(cfg["local_model"])]
provider = "local"
model_id = str(model_def["model"])
dimension = _normalized_dimension(cfg.get("dimension"), int(model_def["dimension"]))
return {
"version": 1,
"provider": provider,
"model": model_id,
"dimension": dimension,
"normalize": True,
}
def metadata_path(vector_dir: pathlib.Path) -> pathlib.Path:
return vector_dir / "embedding_metadata.json"
def read_index_metadata(vector_dir: pathlib.Path) -> dict[str, Any] | None:
path = metadata_path(vector_dir)
if not path.exists():
return None
try:
data = json.loads(path.read_text(encoding="utf-8"))
return data if isinstance(data, dict) else None
except (OSError, json.JSONDecodeError):
logger.warning("Failed to read embedding metadata from %s", path, exc_info=True)
return None
def write_index_metadata(vector_dir: pathlib.Path, metadata: dict[str, Any] | None = None) -> None:
vector_dir.mkdir(parents=True, exist_ok=True)
metadata_path(vector_dir).write_text(
json.dumps(metadata or active_embedding_metadata(), indent=2),
encoding="utf-8",
)
def index_metadata_matches(vector_dir: pathlib.Path, metadata: dict[str, Any] | None = None) -> bool:
current = metadata or active_embedding_metadata()
stored = read_index_metadata(vector_dir)
if not stored:
return False
return all(stored.get(key) == current.get(key) for key in ("provider", "model", "dimension", "normalize"))
def describe_active_embedding(config: dict[str, Any] | None = None) -> str:
cfg = config or get_embedding_config()
if cfg.get("provider") == "cloud":
model_def = CLOUD_MODELS[str(cfg["cloud_model"])]
return f"{model_def['label']} (cloud)"
model_def = LOCAL_MODELS[str(cfg["local_model"])]
return f"{model_def['label']} (local)"
def _normalized_dimension(value: Any, default: int) -> int:
try:
dimension = int(value or default)
except (TypeError, ValueError):
return default
if dimension <= 0:
return default
return min(dimension, default)