Spaces:
Sleeping
Sleeping
Update generate_answer.py
Browse files- generate_answer.py +8 -9
generate_answer.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
### generate_answer.py
|
2 |
-
|
3 |
import os
|
4 |
from glob import glob
|
5 |
import openai
|
@@ -17,16 +15,15 @@ from langchain.memory import ConversationBufferMemory
|
|
17 |
load_dotenv()
|
18 |
api_key = os.getenv("OPENAI_API_KEY")
|
19 |
|
20 |
-
# Corrected line: Set the OpenAI API key correctly
|
21 |
openai.api_key = api_key
|
22 |
|
23 |
def base_model_chatbot(messages):
|
24 |
system_message = [
|
25 |
-
{"role": "system", "content": "You are
|
26 |
]
|
27 |
messages = system_message + messages
|
28 |
response = openai.ChatCompletion.create(
|
29 |
-
model="gpt-3.5-turbo",
|
30 |
messages=messages
|
31 |
)
|
32 |
return response.choices[0].message['content']
|
@@ -60,7 +57,11 @@ class ConversationalRetrievalChain:
|
|
60 |
self.temperature = temperature
|
61 |
|
62 |
def create_chain(self):
|
63 |
-
model = ChatOpenAI(
|
|
|
|
|
|
|
|
|
64 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
65 |
vector_db = VectorDB('docs/')
|
66 |
retriever = vector_db.create_vector_db().as_retriever(search_type="similarity", search_kwargs={"k": 2})
|
@@ -71,9 +72,7 @@ class ConversationalRetrievalChain:
|
|
71 |
)
|
72 |
|
73 |
def with_pdf_chatbot(messages):
|
74 |
-
"""Main function to execute the QA system."""
|
75 |
query = messages[-1]['content'].strip()
|
76 |
-
|
77 |
qa_chain = ConversationalRetrievalChain().create_chain()
|
78 |
result = qa_chain({"query": query})
|
79 |
-
return result['result']
|
|
|
|
|
|
|
1 |
import os
|
2 |
from glob import glob
|
3 |
import openai
|
|
|
15 |
load_dotenv()
|
16 |
api_key = os.getenv("OPENAI_API_KEY")
|
17 |
|
|
|
18 |
openai.api_key = api_key
|
19 |
|
20 |
def base_model_chatbot(messages):
|
21 |
system_message = [
|
22 |
+
{"role": "system", "content": "You are a helpful AI chatbot that provides clear, complete, and coherent responses to User's questions. Ensure your answers are in full sentences."}
|
23 |
]
|
24 |
messages = system_message + messages
|
25 |
response = openai.ChatCompletion.create(
|
26 |
+
model="gpt-3.5-turbo",
|
27 |
messages=messages
|
28 |
)
|
29 |
return response.choices[0].message['content']
|
|
|
57 |
self.temperature = temperature
|
58 |
|
59 |
def create_chain(self):
|
60 |
+
model = ChatOpenAI(
|
61 |
+
model_name=self.model_name,
|
62 |
+
temperature=self.temperature,
|
63 |
+
system_prompt="You are a knowledgeable AI that answers questions based on provided documents. Always give responses in clear, complete sentences."
|
64 |
+
)
|
65 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
66 |
vector_db = VectorDB('docs/')
|
67 |
retriever = vector_db.create_vector_db().as_retriever(search_type="similarity", search_kwargs={"k": 2})
|
|
|
72 |
)
|
73 |
|
74 |
def with_pdf_chatbot(messages):
|
|
|
75 |
query = messages[-1]['content'].strip()
|
|
|
76 |
qa_chain = ConversationalRetrievalChain().create_chain()
|
77 |
result = qa_chain({"query": query})
|
78 |
+
return result['result']
|