-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLMEmbedder.py
207 lines (153 loc) · 6.99 KB
/
LMEmbedder.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
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
import os
import pinecone
import torch
from abc import ABC, abstractmethod
from dotenv import load_dotenv
from langchain.vectorstores import Chroma, Pinecone
# from openai import OpenAIAPI
from transformers import AutoModel, AutoTokenizer
from weaviate import Client
load_dotenv()
class AbstractEmbedder(ABC):
@abstractmethod
def encode(self, sentences):
pass
class MiniLMEmbedder(AbstractEmbedder):
"""
A class to encode sentences using the MiniLM model from HuggingFace.
"""
def __init__(self):
#TODO add model and tokenizer to constructor
self.tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
self.model = AutoModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
def create_pinecone_index(self, api_key, environment, index_name="langchainbook"):
"""
Create a Pinecone index for the given API key and environment.
:param api_key: The API key for the Pinecone service.
:param environment: The environment for the Pinecone service.
:param index_name: The name of the index to create.
"""
pinecone.init(api_key=api_key, environment=environment)
self.index = pinecone.Index(index_name)
def mean_pooling(self, model_output, attention_mask):
"""
Perform mean pooling on the model output using the attention mask.
:param model_output: Model output from HuggingFace model.
:param attention_mask: Attention mask for the input tokens.
:return: Mean-pooled sentence embeddings.
"""
token_embeddings = model_output[0]
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, 1)
sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
return sum_embeddings / sum_mask
def encode(self, sentences):
"""
Encode the given sentences into fixed-size embeddings using the MiniLM model.
:param sentences: A list of sentences to encode.
:return: Sentence embeddings as a tensor.
"""
encoded_input = self.tokenizer(
sentences, padding=True, truncation=True, max_length=128, return_tensors='pt')
with torch.no_grad():
model_output = self.model(**encoded_input)
sentence_embeddings = self.mean_pooling(model_output, encoded_input['attention_mask'])
return sentence_embeddings
def upsert_to_index(self, texts):
"""
Upsert the given texts along with their embeddings to the Pinecone index.
:param texts: A list of texts to upsert to the Pinecone index.
"""
embeddings = [self.encode(text) for text in texts]
embeddings_list = [tensor.tolist() for tensor in embeddings]
self.index.upsert(vectors=(zip(texts, embeddings_list)))
# def __del__(self):
# pinecone.deinit()
# class OpenAIEmbedder(AbstractEmbedder):
# def __init__(self, api_key):
# self.api_key = api_key
# self.openai = OpenAIAPI(api_key=self.api_key)
# # Add additional setup for the OpenAI model and tokenizer, if needed.
# def encode(self, sentences):
# """
# Encode the given sentences into fixed-size embeddings using the OpenAI model.
# :param sentences: A list of sentences to encode.
# :return: Sentence embeddings as a tensor.
# """
# # Implement encoding using the OpenAI model.
# pass
def create_embedder(model_type, api_key=None, environment=None):
"""
Factory function to create an embedder based on the requested model type.
:param model_type: The type of model to use, either 'huggingface' or 'openai'.
:param api_key: The API key for the model service.
:param environment: The environment for the model service.
:return: An instance of the appropriate embedder.
"""
if model_type.lower() == 'huggingface':
return MiniLMEmbedder()
# elif model_type.lower() == 'openai':
# return OpenAIEmbedder(api_key, environment)
else:
raise ValueError("Invalid model_type. Expected 'huggingface' or 'openai'.")
class AbstractVectorStore(ABC):
@abstractmethod
def upsert(self, texts, embeddings):
pass
class PineconeVectorStore(AbstractVectorStore):
def __init__(self, api_key, environment):
self.api_key = api_key
self.environment = environment
pinecone.init(api_key=self.api_key, environment=self.environment)
self.index_name = "langchainbook"
self.index = pinecone.Index(self.index_name)
def upsert(self, texts, embeddings):
embeddings_list = [tensor.tolist() for tensor in embeddings]
self.index.upsert(vectors=(zip(texts, embeddings_list)))
# def __del__(self):
# pinecone.deinit()
class WeaviateVectorStore(AbstractVectorStore):
def __init__(self, weaviate_url, weaviate_auth):
self.client = Client(weaviate_url)
self.client.set_auth(weaviate_auth)
def upsert(self, texts, embeddings):
# Implement the upsert logic for Weaviate.
pass
def __del__(self):
# Clean up resources if needed.
pass
def create_vector_store(store_type, api_key, environment):
if store_type.lower() == 'pinecone':
return PineconeVectorStore(api_key, environment)
elif store_type.lower() == 'weaviate':
# Set Weaviate URL and authentication credentials.
weaviate_url = 'http://localhost:8080'
weaviate_auth = (os.getenv("WEAVIATE_API_KEY"), os.getenv("WEAVIATE_API_SECRET"))
return WeaviateVectorStore(weaviate_url, weaviate_auth)
else:
raise ValueError("Invalid store_type. Expected 'pinecone' or 'weaviate'.")
# if __name__ == "__main__":
# api_key = os.getenv("PINECONE_API_KEY")
# environment = os.getenv("PINECONE_API_ENV")
# embedder = MiniLMEmbedder(api_key, environment)
# texts = ["hello world", "pinecone is cool", "I love HuggingFace"]
# embedder.upsert_to_index(texts)
if __name__ == "__main__":
from DocumentReader import DocumentReader
api_key = os.getenv("PINECONE_API_KEY")
environment = os.getenv("PINECONE_API_ENV")
model_type = 'huggingface' # or 'openai'
embedder = create_embedder(model_type, api_key, environment)
# Read the text from a file using the DocumentReader class
from pathlib import Path
file_path = Path("~/Downloads/PS1.pdf").expanduser() # Replace this with your file path
document_reader = DocumentReader(file_path=file_path)
text = document_reader.read()
# Split the text into sentences or paragraphs (customize as needed)
sentences = text.split("\n")
# Encode the text using MiniLMEmbedder
embeddings = embedder.encode(sentences)
# Upsert the encoded text into the vector store of your choice
store_type = 'pinecone' # or 'weaviate'
vector_store = create_vector_store(store_type, api_key, environment)
vector_store.upsert(sentences, embeddings)