-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_embeddings.py
36 lines (27 loc) · 1.1 KB
/
local_embeddings.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
#Portions copyright langchain, ray project, and their respective holders. All other portions copyright 2024 Christian Mirra
from typing import List
from langchain.embeddings.base import Embeddings
from sentence_transformers import SentenceTransformer
class LocalHuggingFaceEmbeddings(Embeddings):
def __init__(self, model_id):
self.model = SentenceTransformer(model_id)
def embed_documents(self, texts: List[str]) -> List[List[float]]:
"""Embed a list of documents using a locally running
Hugging Face Sentence Transformer model
Args:
texts: The list of texts to embed.
Returns:
List of embeddings, one for each text.
"""
embeddings = self.model.encode(texts)
return embeddings
def embed_query(self, text: str) -> List[float]:
"""Embed a query using a locally running HF
Sentence transformer.
Args:
text: The text to embed.
Returns:
Embeddings for the text.
"""
embedding = self.model.encode(text)
return list(map(float, embedding))