Spaces:
No application file
No application file
Download app/services/embeddings_service.py from codersHub12/chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.24 kB
-
https://huggingface.co/spaces/codersHub12/chatbot/resolve/main/app/services/embeddings_service.py
- Command line
-
hf download hf://spaces/codersHub12/chatbot/app/services/embeddings_service.py
-
curl -L -o embeddings_service.py https://huggingface.co/spaces/codersHub12/chatbot/resolve/main/app/services/embeddings_service.py
1.24 kB
| 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'] |