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 | class VertexTextEmbedding(BaseEmbedding):
embed_mode: VertexEmbeddingMode = Field(description="The embedding mode to use.")
additional_kwargs: Dict[str, Any] = Field(
default_factory=dict, description="Additional kwargs for the Vertex."
)
client_email: Optional[str] = Field(
description="The client email for the VertexAI credentials."
)
token_uri: Optional[str] = Field(
description="The token URI for the VertexAI credentials."
)
private_key_id: Optional[str] = Field(
description="The private key ID for the VertexAI credentials."
)
private_key: Optional[str] = Field(
description="The private key for the VertexAI credentials."
)
_model: TextEmbeddingModel = PrivateAttr()
def __init__(
self,
model_name: str = "textembedding-gecko@003",
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
embed_mode: VertexEmbeddingMode = VertexEmbeddingMode.RETRIEVAL_MODE,
embed_batch_size: int = DEFAULT_EMBED_BATCH_SIZE,
callback_manager: Optional[CallbackManager] = None,
additional_kwargs: Optional[Dict[str, Any]] = None,
num_workers: Optional[int] = None,
client_email: Optional[str] = None,
token_uri: Optional[str] = None,
private_key_id: Optional[str] = None,
private_key: Optional[str] = None,
) -> None:
if credentials is None:
if client_email and token_uri and private_key_id and private_key:
info = {
"client_email": client_email,
"token_uri": token_uri,
"private_key_id": private_key_id,
"private_key": private_key.replace("\\n", "\n"),
}
credentials = service_account.Credentials.from_service_account_info(
info
)
else:
raise ValueError(
"Either provide credentials or all of client_email, token_uri, private_key_id, and private_key."
)
init_vertexai(project=project, location=location, credentials=credentials)
callback_manager = callback_manager or CallbackManager([])
additional_kwargs = additional_kwargs or {}
super().__init__(
embed_mode=embed_mode,
project=project,
location=location,
credentials=credentials,
additional_kwargs=additional_kwargs,
model_name=model_name,
embed_batch_size=embed_batch_size,
callback_manager=callback_manager,
num_workers=num_workers,
client_email=client_email,
token_uri=token_uri,
private_key_id=private_key_id,
private_key=private_key,
)
self._model = TextEmbeddingModel.from_pretrained(model_name)
@classmethod
def class_name(cls) -> str:
return "VertexTextEmbedding"
def _get_text_embeddings(self, texts: List[str]) -> List[Embedding]:
texts = _get_embedding_request(
texts=texts,
embed_mode=self.embed_mode,
is_query=False,
model_name=self.model_name,
)
embeddings = self._model.get_embeddings(texts, **self.additional_kwargs)
return [embedding.values for embedding in embeddings]
def _get_text_embedding(self, text: str) -> Embedding:
return self._get_text_embeddings([text])[0]
async def _aget_text_embedding(self, text: str) -> Embedding:
return (await self._aget_text_embeddings([text]))[0]
async def _aget_text_embeddings(self, texts: List[str]) -> List[Embedding]:
texts = _get_embedding_request(
texts=texts,
embed_mode=self.embed_mode,
is_query=False,
model_name=self.model_name,
)
embeddings = await self._model.get_embeddings_async(
texts, **self.additional_kwargs
)
return [embedding.values for embedding in embeddings]
def _get_query_embedding(self, query: str) -> Embedding:
texts = _get_embedding_request(
texts=[query],
embed_mode=self.embed_mode,
is_query=True,
model_name=self.model_name,
)
embeddings = self._model.get_embeddings(texts, **self.additional_kwargs)
return embeddings[0].values
async def _aget_query_embedding(self, query: str) -> Embedding:
texts = _get_embedding_request(
texts=[query],
embed_mode=self.embed_mode,
is_query=True,
model_name=self.model_name,
)
embeddings = await self._model.get_embeddings_async(
texts, **self.additional_kwargs
)
return embeddings[0].values
|