File size: 1,236 Bytes
a0c847a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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']