forked from Yinglianchun/Ombre-Brain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedding_engine.py
More file actions
276 lines (246 loc) · 10.2 KB
/
Copy pathembedding_engine.py
File metadata and controls
276 lines (246 loc) · 10.2 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# ============================================================
# Module: Embedding Engine (embedding_engine.py)
# 模块:向量化引擎
#
# Generates embeddings via Gemini API (OpenAI-compatible),
# stores them in SQLite, and provides cosine similarity search.
# 通过 Gemini API(OpenAI 兼容)生成 embedding,
# 存储在 SQLite 中,提供余弦相似度搜索。
#
# Depended on by: server.py, bucket_manager.py
# 被谁依赖:server.py, bucket_manager.py
# ============================================================
import os
import json
import math
import sqlite3
import logging
import asyncio
from pathlib import Path
from openai import AsyncOpenAI
logger = logging.getLogger("ombre_brain.embedding")
class EmbeddingEngine:
"""
Embedding generation + SQLite vector storage + cosine search.
向量生成 + SQLite 向量存储 + 余弦搜索。
"""
def __init__(self, config: dict):
dehy_cfg = config.get("dehydration", {})
embed_cfg = config.get("embedding", {})
self.api_key = embed_cfg.get("api_key") or dehy_cfg.get("api_key", "")
self.base_url = (
embed_cfg.get("base_url")
or dehy_cfg.get("base_url")
or "https://generativelanguage.googleapis.com/v1beta/openai/"
)
self.model = embed_cfg.get("model", "gemini-embedding-001")
self.enabled = bool(self.api_key) and embed_cfg.get("enabled", True)
self.max_chars = self._int_between(embed_cfg.get("max_chars", 6000), 6000, 500, 32000)
self.query_instruction = str(
embed_cfg.get("query_instruction")
or "Given a memory search query, retrieve relevant long-term memory passages."
).strip()
self.document_instruction = str(embed_cfg.get("document_instruction") or "").strip()
# --- SQLite path: buckets_dir/embeddings.db ---
db_path = os.path.join(config["buckets_dir"], "embeddings.db")
self.db_path = db_path
# --- Initialize client ---
if self.enabled:
self.client = AsyncOpenAI(
api_key=self.api_key,
base_url=self.base_url,
timeout=30.0,
)
else:
self.client = None
# --- Initialize SQLite ---
self._init_db()
def _init_db(self):
"""Create embeddings table if not exists."""
os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
conn = sqlite3.connect(self.db_path)
conn.execute("""
CREATE TABLE IF NOT EXISTS embeddings (
bucket_id TEXT PRIMARY KEY,
embedding TEXT NOT NULL,
model TEXT,
dimension INTEGER,
updated_at TEXT NOT NULL
)
""")
self._ensure_column(conn, "embeddings", "model", "TEXT")
self._ensure_column(conn, "embeddings", "dimension", "INTEGER")
conn.commit()
conn.close()
async def generate_and_store(self, bucket_id: str, content: str) -> bool:
"""
Generate embedding for content and store in SQLite.
为内容生成 embedding 并存入 SQLite。
Returns True on success, False on failure.
"""
if not self.enabled or not content or not content.strip():
return False
try:
embedding = await self._generate_embedding(content, kind="document")
if not embedding:
return False
self._store_embedding(bucket_id, embedding)
return True
except Exception as e:
logger.warning(f"Embedding generation failed for {bucket_id}: {e}")
return False
async def _generate_embedding(self, text: str, *, kind: str = "document") -> list[float]:
"""Call API to generate embedding vector."""
# Truncate to avoid token limits
prepared = self._prepare_embedding_input(text, kind=kind)
truncated = prepared[: self.max_chars]
try:
response = await self.client.embeddings.create(
model=self.model,
input=truncated,
)
if response.data and len(response.data) > 0:
return response.data[0].embedding
return []
except Exception as e:
logger.warning(f"Embedding API call failed: {e}")
return []
def _store_embedding(self, bucket_id: str, embedding: list[float]):
"""Store embedding in SQLite."""
from utils import now_iso
conn = sqlite3.connect(self.db_path)
conn.execute(
"""
INSERT OR REPLACE INTO embeddings (bucket_id, embedding, model, dimension, updated_at)
VALUES (?, ?, ?, ?, ?)
""",
(bucket_id, json.dumps(embedding), self.model, len(embedding), now_iso()),
)
conn.commit()
conn.close()
def delete_embedding(self, bucket_id: str):
"""Remove embedding when bucket is deleted."""
conn = sqlite3.connect(self.db_path)
conn.execute("DELETE FROM embeddings WHERE bucket_id = ?", (bucket_id,))
conn.commit()
conn.close()
async def get_embedding(self, bucket_id: str) -> list[float] | None:
"""Retrieve stored embedding for a bucket. Returns None if not found."""
conn = sqlite3.connect(self.db_path)
row = conn.execute(
"SELECT embedding, model, dimension FROM embeddings WHERE bucket_id = ?", (bucket_id,)
).fetchone()
conn.close()
if row:
try:
embedding = json.loads(row[0])
if not self._row_matches_current_model(row[1], row[2], embedding):
return None
return embedding
except json.JSONDecodeError:
return None
return None
async def get_embeddings(self, bucket_ids: list[str]) -> dict[str, list[float]]:
"""Retrieve stored embeddings for several buckets with one SQLite read."""
unique_ids = list(
dict.fromkeys(
str(item or "").strip()
for item in bucket_ids
if str(item or "").strip()
)
)
if not unique_ids:
return {}
placeholders = ",".join("?" for _ in unique_ids)
conn = sqlite3.connect(self.db_path)
try:
rows = conn.execute(
f"SELECT bucket_id, embedding, model, dimension FROM embeddings WHERE bucket_id IN ({placeholders})",
unique_ids,
).fetchall()
finally:
conn.close()
output: dict[str, list[float]] = {}
for bucket_id, payload, model, dimension in rows:
try:
embedding = json.loads(payload)
except (json.JSONDecodeError, TypeError):
continue
if self._row_matches_current_model(model, dimension, embedding):
output[str(bucket_id)] = embedding
return output
async def search_similar(self, query: str, top_k: int = 10) -> list[tuple[str, float]]:
"""
Search for buckets similar to query text.
Returns list of (bucket_id, similarity_score) sorted by score desc.
搜索与查询文本相似的桶。返回 (bucket_id, 相似度分数) 列表。
"""
if not self.enabled:
return []
try:
query_embedding = await self._generate_embedding(query, kind="query")
if not query_embedding:
return []
except Exception as e:
logger.warning(f"Query embedding failed: {e}")
return []
# Load all embeddings from SQLite
conn = sqlite3.connect(self.db_path)
rows = conn.execute("SELECT bucket_id, embedding, model, dimension FROM embeddings").fetchall()
conn.close()
if not rows:
return []
# Calculate cosine similarity
results = []
for bucket_id, emb_json, model, dimension in rows:
try:
stored_embedding = json.loads(emb_json)
if not self._row_matches_current_model(model, dimension, stored_embedding):
continue
sim = self._cosine_similarity(query_embedding, stored_embedding)
results.append((bucket_id, sim))
except (json.JSONDecodeError, Exception):
continue
results.sort(key=lambda x: x[1], reverse=True)
return results[:top_k]
def _prepare_embedding_input(self, text: str, *, kind: str) -> str:
raw = str(text or "")
if kind == "query" and self.query_instruction:
return f"Instruct: {self.query_instruction}\nQuery: {raw}"
if kind == "document" and self.document_instruction:
return f"Instruct: {self.document_instruction}\nDocument: {raw}"
return raw
def _row_matches_current_model(self, model: str | None, dimension: int | None, embedding: list[float]) -> bool:
if not embedding:
return False
if model != self.model:
return False
try:
stored_dimension = int(dimension)
except (TypeError, ValueError):
return False
return stored_dimension == len(embedding)
@staticmethod
def _ensure_column(conn: sqlite3.Connection, table: str, column: str, column_type: str) -> None:
rows = conn.execute(f"PRAGMA table_info({table})").fetchall()
if any(row[1] == column for row in rows):
return
conn.execute(f"ALTER TABLE {table} ADD COLUMN {column} {column_type}")
@staticmethod
def _int_between(value, default: int, min_value: int, max_value: int) -> int:
try:
number = int(value)
except (TypeError, ValueError):
number = default
return max(min_value, min(max_value, number))
@staticmethod
def _cosine_similarity(a: list[float], b: list[float]) -> float:
"""Calculate cosine similarity between two vectors."""
if len(a) != len(b) or not a:
return 0.0
dot = sum(x * y for x, y in zip(a, b))
norm_a = math.sqrt(sum(x * x for x in a))
norm_b = math.sqrt(sum(x * x for x in b))
if norm_a == 0 or norm_b == 0:
return 0.0
return dot / (norm_a * norm_b)