fix: address embedder review feedback

This commit is contained in:
2026-04-13 14:14:00 -04:00
parent 6e7f347bd3
commit 0948d2dcb7
2 changed files with 85 additions and 12 deletions

View File

@@ -5,10 +5,18 @@ import httpx
class OllamaEmbedder:
def __init__(self, base_url: str, model: str, batch_size: int):
def __init__(
self,
base_url: str,
model: str,
batch_size: int,
timeout: float = 300.0,
):
self.base_url = base_url.rstrip("/")
self.model = model
self.batch_size = batch_size
self.timeout = timeout
self._client = httpx.Client(timeout=timeout)
def embed(
self, texts: List[str], retries: int = 3, backoff: float = 1.0
@@ -22,23 +30,41 @@ class OllamaEmbedder:
for attempt in range(retries):
try:
with httpx.Client(timeout=300.0) as client:
response = client.post(
url,
json={"model": self.model, "input": batch},
response = self._client.post(
url,
json={"model": self.model, "input": batch},
)
response.raise_for_status()
data = response.json()
embeddings = data["embeddings"]
# Validate response count matches batch count
if len(embeddings) != len(batch):
raise ValueError(
f"Ollama returned {len(embeddings)} embeddings for {len(batch)} texts"
)
response.raise_for_status()
data = response.json()
embeddings = data["embeddings"]
all_embeddings.extend(embeddings)
break
except Exception as exc:
all_embeddings.extend(embeddings)
break
except (httpx.HTTPError, httpx.RequestError) as exc:
last_exception = exc
if attempt < retries - 1:
time.sleep(backoff * (2**attempt))
else:
raise RuntimeError(
f"Failed to embed batch after {retries} retries"
f"Failed to embed batch after {retries} retries. "
f"Last error: {last_exception}"
) from last_exception
return all_embeddings
def close(self) -> None:
"""Close the HTTP client."""
self._client.close()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
return False