Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -42,53 +42,22 @@ def get_vector_store(chunks):
|
|
| 42 |
|
| 43 |
# Function to get conversational chain
|
| 44 |
def get_conversational_chain():
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
Context:
|
| 53 |
-
{context}
|
| 54 |
-
|
| 55 |
-
Question:
|
| 56 |
-
{question}
|
| 57 |
-
|
| 58 |
Answer:
|
| 59 |
-
"""
|
| 60 |
-
input_variables=["context", "question"]
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
# Combine prompt
|
| 64 |
-
combine_prompt = PromptTemplate(
|
| 65 |
-
template="""
|
| 66 |
-
You are provided with several answers to a question from different parts of a document. Your task is to combine these answers into a single, coherent, and comprehensive response. If the answer isn't available, state "Answer is not available in the context."
|
| 67 |
-
|
| 68 |
-
Answers:
|
| 69 |
-
{summaries}
|
| 70 |
-
|
| 71 |
-
Question:
|
| 72 |
-
{question}
|
| 73 |
-
|
| 74 |
-
Final Answer:
|
| 75 |
-
""",
|
| 76 |
-
input_variables=["summaries", "question"]
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
try:
|
| 80 |
model = ChatGoogleGenerativeAI(model="gemini-pro", client=genai, temperature=0.3)
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
chain_type="map_reduce",
|
| 84 |
-
question_prompt=map_prompt,
|
| 85 |
-
combine_prompt=combine_prompt
|
| 86 |
-
)
|
| 87 |
return chain
|
| 88 |
except (GoogleAPIError, InvalidArgument):
|
| 89 |
raise RuntimeError("Error creating conversational chain. Please try again in a minute.")
|
| 90 |
|
| 91 |
-
|
| 92 |
# Function to clear chat history
|
| 93 |
def clear_chat_history():
|
| 94 |
st.session_state.messages = [
|
|
@@ -99,16 +68,13 @@ def user_input(user_question):
|
|
| 99 |
try:
|
| 100 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001") # type: ignore
|
| 101 |
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
| 102 |
-
docs = new_db.similarity_search(user_question, k=4)
|
| 103 |
chain = get_conversational_chain()
|
| 104 |
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
| 105 |
return response
|
| 106 |
except (GoogleAPIError, InvalidArgument):
|
| 107 |
raise RuntimeError("لطفا پس از چند لحظه دوباره امتحان کنید ")
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
# Main function to run the Streamlit app
|
| 113 |
def main():
|
| 114 |
st.set_page_config(
|
|
|
|
| 42 |
|
| 43 |
# Function to get conversational chain
|
| 44 |
def get_conversational_chain():
|
| 45 |
+
prompt_template = """
|
| 46 |
+
Answer the question as detailed as possible from the provided context.
|
| 47 |
+
If the answer is not in the provided context, just say, "answer is not available in the context".
|
| 48 |
+
Don't provide the wrong answer.\n\n
|
| 49 |
+
Context:\n {context}\n
|
| 50 |
+
Question:\n {question}\n
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
Answer:
|
| 52 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
try:
|
| 54 |
model = ChatGoogleGenerativeAI(model="gemini-pro", client=genai, temperature=0.3)
|
| 55 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
| 56 |
+
chain = load_qa_chain(llm=model, chain_type="stuff", prompt=prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
return chain
|
| 58 |
except (GoogleAPIError, InvalidArgument):
|
| 59 |
raise RuntimeError("Error creating conversational chain. Please try again in a minute.")
|
| 60 |
|
|
|
|
| 61 |
# Function to clear chat history
|
| 62 |
def clear_chat_history():
|
| 63 |
st.session_state.messages = [
|
|
|
|
| 68 |
try:
|
| 69 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001") # type: ignore
|
| 70 |
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
| 71 |
+
docs = new_db.similarity_search(user_question, k=4)
|
| 72 |
chain = get_conversational_chain()
|
| 73 |
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
| 74 |
return response
|
| 75 |
except (GoogleAPIError, InvalidArgument):
|
| 76 |
raise RuntimeError("لطفا پس از چند لحظه دوباره امتحان کنید ")
|
| 77 |
|
|
|
|
|
|
|
|
|
|
| 78 |
# Main function to run the Streamlit app
|
| 79 |
def main():
|
| 80 |
st.set_page_config(
|