vamseelatha2002 commited on
Commit
ce5c1a6
·
verified ·
1 Parent(s): 52ea1f0

Update generator.py

Browse files
Files changed (1) hide show
  1. 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
- def generate_response_from_document(query, retrieved_docs):
9
- if not retrieved_docs: # Check if no documents were retrieved
10
- return "I cannot answer the question due to insufficient information in the documents."
11
-
12
- # context = " ".join([doc.page_content for doc in retrieved_docs]) # Now iterates over Document objects
13
- context = " ".join([doc for doc in retrieved_docs])
14
- # prompt = """
15
- # "You are an accurate and reliable AI assistant that can answer questions with the help of external documents.
16
- # Please note that external documents may contain noisy or factually incorrect information.
17
- # If the information in the document contains the correct answer, you will give an accurate answer.
18
- # 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.‘.
19
- # 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."
20
-
21
- # Context or Document: {context}
22
- # Query: {query}
23
- # """
24
- prompt = (
25
- "You are a highly intelligent assistant tasked with answering a question based strictly on the provided context. "
26
- f"Given Question: {query} \n\n"
27
- f"Context: {context} \n"
28
- f"Answer the question directly and concisely using only the information available in the context."
29
- "Do not include any other information which is not there in the context."
30
- )
31
-
32
- try:
33
- response = openai.chat.completions.create( # Use the new chat completions API
34
- model= "gpt-3.5-turbo", #"gpt-4", #"gpt-3.5-turbo" Or use another suitable model like gpt-4
35
- messages=[{"role": "user", "content": prompt}],
36
- max_tokens=300,
37
- temperature=0.7,
38
- )
39
- return response.choices[0].message.content.strip() # Extract content from message
40
- except Exception as e:
 
 
 
 
 
 
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)}"