📄 embeddings.py 1,667 bytes Apr 30, 2026 📋 Raw

"""Embedding client for Icarus.

Provides document embedding vectors via Ollama.
Sovereign: Zero imports from costco_route.
"""

import httpx
from icarus.core.config.staging import OLLAMA_BASE_URL, OLLAMA_EMBED_MODEL as EMBEDDING_MODEL

def get_embedding(text: str, timeout: float = 30.0) -> list[float]:
"""Get an embedding vector from Ollama.

Args:
    text: Text to embed
    timeout: Request timeout in seconds

Returns:
    List of floats (embedding vector)
"""
payload = {
    "model": EMBEDDING_MODEL or "nomic-embed-text",
    "prompt": text
}

try:
    with httpx.Client(timeout=timeout) as client:
        resp = client.post(
            f"{OLLAMA_BASE_URL}/api/embeddings",
            json=payload
        )
        resp.raise_for_status()
        return resp.json()["embedding"]
except Exception as e:
    print(f"Embedding error: {e}")
    # Return zero vector as fallback (will not match anything)
    return [0.0] * 768  # nomic-embed-text dimension

async def get_embedding_async(text: str, timeout: float = 30.0) -> list[float]:
"""Async version of get_embedding."""
payload = {
"model": EMBEDDING_MODEL or "nomic-embed-text",
"prompt": text
}

try:
    async with httpx.AsyncClient(timeout=timeout) as client:
        resp = await client.post(
            f"{OLLAMA_BASE_URL}/api/embeddings",
            json=payload
        )
        resp.raise_for_status()
        return resp.json()["embedding"]
except Exception as e:
    print(f"Embedding error: {e}")
    return [0.0] * 768