| """ | |
| Embed text using Ollama's embedding API. | |
| """ | |
| import requests | |
| from typing import List | |
| from config import OLLAMA_BASE_URL, EMBEDDING_MODEL | |
| def embed_texts(texts: List[str]) -> List[List[float]]: | |
| """ | |
| Embed a list of texts using Ollama. | |
| Args: | |
| texts: List of text strings to embed | |
| Returns: | |
| List of embedding vectors | |
| """ | |
| embeddings = [] | |
| for text in texts: | |
| try: | |
| resp = requests.post( | |
| f"{OLLAMA_BASE_URL}/api/embeddings", | |
| json={"model": EMBEDDING_MODEL, "prompt": text} | |
| ) | |
| resp.raise_for_status() | |
| embeddings.append(resp.json()["embedding"]) | |
| except Exception as e: | |
| print(f"Warning: Failed to embed text: {e}") | |
| # Return a zero vector of appropriate size as fallback | |
| # nomic-embed-text produces 768-dimensional vectors | |
| embeddings.append([0.0] * 768) | |
| return embeddings | |
| def main(): | |
| """Test function to verify embedding works correctly.""" | |
| print("Embedder module ready for use.") | |
| if __name__ == "__main__": | |
| main() |