|
import logging |
|
import os |
|
from openai import OpenAI |
|
from openai import OpenAIError, RateLimitError |
|
from config import OPENAI_API_KEY |
|
|
|
|
|
|
|
|
|
client = OpenAI(api_key=OPENAI_API_KEY) |
|
|
|
def generate_rag_response(json_output, user_query): |
|
logging.info("Generating RAG response") |
|
|
|
|
|
context_texts = [hit['chunk_text'] for hit in json_output] |
|
|
|
|
|
context = "\n".join(context_texts) |
|
prompt = f"Based on the given context, answer the user query: {user_query}\nContext:\n{context}" |
|
|
|
main_prompt = [ |
|
{"role": "system", "content": "You are a helpful assistant."}, |
|
{"role": "user", "content": prompt} |
|
] |
|
|
|
try: |
|
|
|
chat_completion = client.chat.completions.create( |
|
messages=main_prompt, |
|
model="gpt-4o-mini", |
|
timeout=10 |
|
) |
|
|
|
|
|
logging.info("RAG response generation completed") |
|
logging.info(f"RAG response: {chat_completion.choices[0].message.content}") |
|
return chat_completion.choices[0].message.content, None |
|
|
|
except RateLimitError as e: |
|
logging.error(f"Rate limit exceeded: {e}") |
|
return None, "Rate limit exceeded. Please try again later." |
|
except OpenAIError as e: |
|
logging.error(f"OpenAI API error: {e}") |
|
return None, f"An error occurred: {str(e)}" |
|
except Exception as e: |
|
logging.error(f"Unexpected error: {e}") |
|
return None, str(e) |
|
|