23RAG7 / generator.py
cb1716pics's picture
Upload generator.py
634bbff verified
raw
history blame
1.31 kB
import openai
import time
import os
# Set your OpenAI API key
openai.api_key = os.getenv("OPENAI_API_KEY")
def generate_response_from_document(query, retrieved_docs):
if not retrieved_docs: # Check if no documents were retrieved
return "I cannot answer the question due to insufficient information in the documents."
context = " ".join([doc.page_content for doc in retrieved_docs]) # Now iterates over Document objects
prompt = (
"You are a highly intelligent assistant tasked with answering a question based strictly on the provided context. "
f"Given Question: {query} \n\n"
f"Context: {context} \n"
"Answer the question directly and concisely using only the information available in the context."
)
try:
response = openai.chat.completions.create( # Use the new chat completions API
model= "gpt-3.5-turbo", #"gpt-4", #"gpt-3.5-turbo" Or use another suitable model like gpt-4
messages=[{"role": "user", "content": prompt}],
max_tokens=300,
temperature=0.7,
)
return response.choices[0].message.content.strip() # Extract content from message
except Exception as e:
return f"Error generating response: {str(e)}"