-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembeddings_manager.py
99 lines (88 loc) · 3.83 KB
/
embeddings_manager.py
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
from sentence_transformers import SentenceTransformer
import chromadb
from chromadb.utils import embedding_functions
import toml
from pathlib import Path
import threading
config = toml.load("config.toml")
embeddings_config = config["embeddings"]
files_config = config["files"]
_model = None
_chroma_client = None
_collection = None
_lock = threading.Lock()
def get_embedding_model():
"""Lazily initializes and returns the SentenceTransformer model."""
global _model
if _model is None:
with _lock:
if _model is None:
print("Initializing SentenceTransformer model...")
_model = SentenceTransformer(embeddings_config["embeddings_function"])
print("SentenceTransformer model initialized.")
return _model
def get_chroma_collection():
"""Lazily initializes and returns the ChromaDB client and collection."""
global _chroma_client, _collection
if _collection is None:
with _lock:
if _collection is None:
# print("Initializing ChromaDB client...")
_chroma_client = chromadb.PersistentClient(path=embeddings_config["embeddings_path"])
# print("ChromaDB client initialized.")
collection_name = embeddings_config["collection_name"]
# print(f"Getting or creating ChromaDB collection: {collection_name}...")
_collection = _chroma_client.get_or_create_collection(
name=collection_name,
embedding_function=embedding_functions.SentenceTransformerEmbeddingFunction(
model_name=embeddings_config["embeddings_function"]
)
)
print("ChromaDB collection ready.")
return _collection
def remove_document_from_collection(doc_id):
"""Remove a document from the collection by its ID."""
collection = get_chroma_collection()
try:
collection.delete(ids=[doc_id])
print(f"Successfully removed document with ID: {doc_id}")
except Exception as e:
print(f"Error removing document {doc_id}: {e}")
def process_file_for_embeddings(file_path, base_dir):
"""Process a single file and add its embeddings to the collection."""
model = get_embedding_model()
collection = get_chroma_collection()
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
path_obj = Path(file_path)
base_dir_obj = Path(base_dir).resolve()
try:
doc_id = str(path_obj.relative_to(base_dir_obj))
except ValueError:
print(f"Warning: Could not make {path_obj} relative to {base_dir_obj}. Using absolute path as ID.")
doc_id = str(path_obj.resolve())
title = content.split('\n', 1)[0].strip('# ').strip() if content else "Untitled"
metadata = {"title": title, "source": doc_id}
if not content.strip():
print(f"Skipping empty file: {file_path}")
return
try:
embedding = model.encode([content])[0].tolist()
except Exception as encode_err:
print(f"Error encoding content from {file_path}: {encode_err}")
return
try:
collection.add(
documents=[content],
embeddings=[embedding],
ids=[doc_id],
metadatas=[metadata]
)
print(f"Successfully added/updated document: {doc_id}")
except Exception as add_err:
print(f"Error adding document {doc_id} to collection: {add_err}")
except FileNotFoundError:
print(f"Error: File not found during processing: {file_path}")
except Exception as e:
print(f"Error processing file {file_path}: {e}")