AlirezaHSZ commited on
Commit
86d8e9f
·
verified ·
1 Parent(s): 7788c6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -44
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
- from langchain.prompts import PromptTemplate
46
-
47
- # Map prompt
48
- map_prompt = PromptTemplate(
49
- template="""
50
- Answer the following question based on the given context. If the answer isn't in the context, please say, "Answer is not available in the context." Do not provide information not included in the context.
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
- chain = load_qa_chain(
82
- llm=model,
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) # Adjust k if needed
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(