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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -6
app.py CHANGED
@@ -42,8 +42,12 @@ def get_vector_store(chunks):
42
 
43
  # Function to get conversational chain
44
  def get_conversational_chain():
45
- prompt_template = """
46
- Answer the question based on the provided context. If the answer isn't in the context, please say, "Answer is not available in the context." Don't make up information.
 
 
 
 
47
 
48
  Context:
49
  {context}
@@ -52,11 +56,34 @@ def get_conversational_chain():
52
  {question}
53
 
54
  Answer:
55
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  try:
57
  model = ChatGoogleGenerativeAI(model="gemini-pro", client=genai, temperature=0.3)
58
- prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
59
- chain = load_qa_chain(llm=model, chain_type="map_reduce", prompt=prompt)
 
 
 
 
60
  return chain
61
  except (GoogleAPIError, InvalidArgument):
62
  raise RuntimeError("Error creating conversational chain. Please try again in a minute.")
@@ -72,7 +99,7 @@ def user_input(user_question):
72
  try:
73
  embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001") # type: ignore
74
  new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
75
- docs = new_db.similarity_search(user_question, k=4) # You can adjust k as needed
76
  chain = get_conversational_chain()
77
  response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
78
  return response
@@ -81,6 +108,7 @@ def user_input(user_question):
81
 
82
 
83
 
 
84
  # Main function to run the Streamlit app
85
  def main():
86
  st.set_page_config(
 
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}
 
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.")
 
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
 
108
 
109
 
110
 
111
+
112
  # Main function to run the Streamlit app
113
  def main():
114
  st.set_page_config(