-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
30 lines (26 loc) · 1.01 KB
/
main.py
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
import openai
import os
from dotenv import load_dotenv
from document_loader import load_documents_from_folder
from retriever import create_index, retrieve_documents
load_dotenv()
# Set up API key
openai.api_key = os.getenv("OPENAI_API_KEY")
def generate_response(query, retrieved_docs):
# Prepare the context from retrieved documents
context = "\n\n".join([doc["text"][:1000] for doc in retrieved_docs])
# OpenAI API call with `gpt-3.5-turbo`
messages = [
{"role": "system", "content": "You are a helpful assistant for answering questions based on provided documents."},
{"role": "user", "content": f"Context: {context}\n\nQuestion: {query}"}
]
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # Updated model
messages=messages,
max_tokens=150,
temperature=0.3,
)
return response.choices[0].message['content'].strip()
except openai.error.OpenAIError as e:
return f"An error occurred: {e}"