Spaces:
Sleeping
Sleeping
Update generator.py
Browse files- generator.py +46 -40
generator.py
CHANGED
@@ -1,41 +1,47 @@
|
|
1 |
-
import openai
|
2 |
-
import time
|
3 |
-
import os
|
4 |
-
|
5 |
-
# Set your OpenAI API key
|
6 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
#
|
22 |
-
#
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
return f"Error generating response: {str(e)}"
|
|
|
1 |
+
import openai
|
2 |
+
import time
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Set your OpenAI API key
|
6 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
+
# Check if the API key is set correctly
|
8 |
+
if openai_api_key is None:
|
9 |
+
print("API key is not set!")
|
10 |
+
else:
|
11 |
+
print("API key loaded successfully!")
|
12 |
+
|
13 |
+
|
14 |
+
def generate_response_from_document(query, retrieved_docs):
|
15 |
+
if not retrieved_docs: # Check if no documents were retrieved
|
16 |
+
return "I cannot answer the question due to insufficient information in the documents."
|
17 |
+
|
18 |
+
# context = " ".join([doc.page_content for doc in retrieved_docs]) # Now iterates over Document objects
|
19 |
+
context = " ".join([doc for doc in retrieved_docs])
|
20 |
+
# prompt = """
|
21 |
+
# "You are an accurate and reliable AI assistant that can answer questions with the help of external documents.
|
22 |
+
# Please note that external documents may contain noisy or factually incorrect information.
|
23 |
+
# If the information in the document contains the correct answer, you will give an accurate answer.
|
24 |
+
# If the information in the document does not contain the answer, you will generate ’I can not answer the question because of the insufficient information in documents.‘.
|
25 |
+
# If there are inconsistencies with the facts in some of the documents, please generate the response 'There are factual errors in the provided documents.' and provide the correct answer."
|
26 |
+
|
27 |
+
# Context or Document: {context}
|
28 |
+
# Query: {query}
|
29 |
+
# """
|
30 |
+
prompt = (
|
31 |
+
"You are a highly intelligent assistant tasked with answering a question based strictly on the provided context. "
|
32 |
+
f"Given Question: {query} \n\n"
|
33 |
+
f"Context: {context} \n"
|
34 |
+
f"Answer the question directly and concisely using only the information available in the context."
|
35 |
+
"Do not include any other information which is not there in the context."
|
36 |
+
)
|
37 |
+
|
38 |
+
try:
|
39 |
+
response = openai.chat.completions.create( # Use the new chat completions API
|
40 |
+
model= "gpt-3.5-turbo", #"gpt-4", #"gpt-3.5-turbo" Or use another suitable model like gpt-4
|
41 |
+
messages=[{"role": "user", "content": prompt}],
|
42 |
+
max_tokens=300,
|
43 |
+
temperature=0.7,
|
44 |
+
)
|
45 |
+
return response.choices[0].message.content.strip() # Extract content from message
|
46 |
+
except Exception as e:
|
47 |
return f"Error generating response: {str(e)}"
|