Spaces:
No application file
No application file
| from openai import OpenAI | |
| from app.config import settings | |
| import httpx | |
| import asyncio | |
| import google.generativeai as genai | |
| class EmbeddingsService: | |
| def __init__(self): | |
| # Use httpx client without problematic kwargs | |
| http_client = httpx.Client() | |
| self.client = OpenAI(api_key=settings.OPENAI_API_KEY, http_client=http_client) | |
| self.model = "text-embedding-3-small" | |
| async def create_embedding(self, text: str): | |
| text = text.replace("\n", " ") | |
| # Run the blocking OpenAI call in a thread pool | |
| response = await asyncio.to_thread( | |
| self.client.embeddings.create, | |
| input=[text], | |
| model=self.model | |
| ) | |
| return response.data[0].embedding | |
| class GeminiEmbeddingsService: | |
| def __init__(self): | |
| genai.configure(api_key=settings.GEMINI_API_KEY) | |
| self.model = "models/" + settings.GEMINI_MODEL_EMBEDDING | |
| async def create_embedding(self, text: str): | |
| text = text.replace("\n", " ") | |
| # Run the blocking Gemini call in a thread pool | |
| response = await asyncio.to_thread( | |
| genai.embed_content, | |
| model=self.model, | |
| content=text | |
| ) | |
| return response['embedding'] |