-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoci_cohere_embeddings_utils.py
40 lines (31 loc) · 1.01 KB
/
oci_cohere_embeddings_utils.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
"""
Author: Luigi Saetta
Date created: 2024-04-27
Date last modified: 2024-04-27
Python Version: 3.11
"""
from tqdm.auto import tqdm
from langchain_community.embeddings import OCIGenAIEmbeddings
from config import EMBED_BATCH_SIZE
#
# extend OCIGenAIEmbeddings adding batching
#
class OCIGenAIEmbeddingsWithBatch(OCIGenAIEmbeddings):
"""
add batching to OCIEmebeddings
with Cohere max # of texts is: 96
"""
def embed_documents(self, texts):
batch_size = EMBED_BATCH_SIZE
embeddings = []
if len(texts) > batch_size:
# do in batch
for i in tqdm(range(0, len(texts), batch_size)):
batch = texts[i : i + batch_size]
embeddings_batch = super().embed_documents(batch)
# add to the final list
embeddings.extend(embeddings_batch)
else:
# this way we don't display progress bar when we embed a query
embeddings = super().embed_documents(texts)
return embeddings