-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedder.py
More file actions
64 lines (44 loc) · 1.89 KB
/
Copy pathembedder.py
File metadata and controls
64 lines (44 loc) · 1.89 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
63
64
import os
import time
import numpy as np
from huggingface_hub import InferenceClient
class Embedder:
def __init__(self):
self.client = InferenceClient(token=os.environ["HF_TOKEN"])
self.model = "BAAI/bge-m3"
def embed(self, chunks, batch_size=16):
all_vectors = []
for i in range(0, len(chunks), batch_size):
batch = chunks[i:i + batch_size]
try:
response = self.client.feature_extraction(
batch,
model=self.model
)
batch_vectors = np.array(response).tolist()
if len(np.shape(batch_vectors)) == 3:
batch_vectors = batch_vectors[0]
all_vectors.extend(batch_vectors)
time.sleep(0.5)
except Exception as e:
if "503" in str(e) or "loading" in str(e).lower():
print("Model is initializing on Hugging Face servers. Waiting 20 seconds...")
time.sleep(20)
return self.embed(chunks[i:], batch_size=batch_size)
else:
print(f"Embedding batch failed: {e}")
raise e
return all_vectors
def embed_q(self, query):
try:
v = self.client.feature_extraction(
query,
model=self.model
)
return np.array(v).flatten().tolist()
except Exception as e:
if "503" in str(e):
print("Model loading. Retrying query embedding in 15 seconds...")
time.sleep(15)
return self.embed_q(query)
raise e