File size: 1,089 Bytes
74e8a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
BGE-M3 embeddings via HuggingFace Inference API, with retry-on-failure.
"""
import time
import random
import numpy as np
from huggingface_hub import InferenceClient

from src.utils.config import HF_KEY

_hf_client = InferenceClient(provider="hf-inference", api_key=HF_KEY)


def embed_texts_with_retry(texts: list[str], max_retries: int = 3) -> list[list[float]]:
    """Embed a list of texts using BGE-M3, with exponential backoff on failure."""
    embeddings = []
    for text in texts:
        for attempt in range(max_retries):
            try:
                vector = np.array(_hf_client.feature_extraction(text, model="BAAI/bge-m3"))
                if vector.ndim > 1:
                    vector = vector.squeeze()
                vector = vector / np.linalg.norm(vector)
                embeddings.append(vector.tolist())
                break
            except Exception as e:
                if attempt == max_retries - 1:
                    raise
                backoff = (2 ** attempt) + random.uniform(0, 0.5)
                time.sleep(backoff)
    return embeddings