-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathembedding_model.py
More file actions
62 lines (51 loc) · 2.03 KB
/
Copy pathembedding_model.py
File metadata and controls
62 lines (51 loc) · 2.03 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
"""
Local embedding model implementation using all-MiniLM-L6-v2.
"""
from typing import List
from langchain_huggingface import HuggingFaceEmbeddings
class EmbeddingModel:
"""
Local embedding model using all-MiniLM-L6-v2 from HuggingFace.
"""
def __init__(self,
api_key: str = None,
model_name: str = "sentence-transformers/all-MiniLM-L6-v2"):
"""
Initialize the embedding model.
Args:
api_key: Not used for local models, kept for compatibility
model_name: The embedding model to use (default: "sentence-transformers/all-MiniLM-L6-v2")
"""
self.model_name = model_name
# Initialize the local HuggingFace embedding model
self.model = HuggingFaceEmbeddings(
model_name=self.model_name,
model_kwargs={'device': 'cpu'}, # Use CPU for compatibility
encode_kwargs={'normalize_embeddings': True} # Normalize for better similarity
)
async def embed(self, text: str) -> List[float]:
"""
Generate embedding for a single text.
Args:
text: The text to embed
Returns:
A float array representing the embedding
"""
# Run in thread pool to avoid blocking the event loop
import asyncio
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, self.model.embed_query, text)
async def embed_batch(self, texts: List[str]) -> List[List[float]]:
"""
Generate embeddings for multiple texts using local model.
Args:
texts: List of texts to embed
Returns:
List of float arrays representing the embeddings
"""
if not texts:
return []
# Run in thread pool to avoid blocking the event loop
import asyncio
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, self.model.embed_documents, texts)