chatbot2 / app /services /embeddings_service.py
MuhammadSaad16's picture
Add application file
7054249
raw
history blame contribute delete
699 Bytes
from openai import OpenAI
from app.config import settings
import httpx
import asyncio
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