chatbot2 / app /services /rag_service.py
MuhammadSaad16's picture
Add application file
7054249
import os
import asyncio
from qdrant_client import QdrantClient
from qdrant_client.models import NamedVector
from typing import List
from app.services.openai_service import OpenAIService
from app.services.embeddings_service import EmbeddingsService
class RAGService:
def __init__(self, qdrant_client: QdrantClient, embeddings_service: EmbeddingsService, openai_service: OpenAIService):
self.qdrant_client = qdrant_client
self.embeddings_service = embeddings_service
self.openai_service = openai_service
self.collection_name = os.getenv("QDRANT_COLLECTION_NAME", "book_embeddings")
async def retrieve_context(self, query: str, top_k: int = 3) -> List[str]:
query_vector = await self.embeddings_service.create_embedding(query)
# Run synchronous Qdrant query in thread pool
search_result = await asyncio.to_thread(
self.qdrant_client.query_points,
collection_name=self.collection_name,
query=query_vector,
limit=top_k,
with_payload=True
)
context = [point.payload.get("content", "") for point in search_result.points if point.payload]
return context
async def generate_response(self, query: str, context: List[str]) -> str:
full_prompt = f"""Context: {' '.join(context)}
Question: {query}
Answer:"""
response = await self.openai_service.get_chat_response(full_prompt)
return response