AlirezaHSZ commited on
Commit
3570600
·
verified ·
1 Parent(s): 3df9cf7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -9
app.py CHANGED
@@ -11,6 +11,7 @@ from langchain_community.vectorstores import FAISS
11
  from langchain.chains.question_answering import load_qa_chain
12
  from langchain.prompts import PromptTemplate
13
 
 
14
  # Load environment variables
15
  load_dotenv()
16
  api_key = os.getenv("GOOGLE_API_KEY")
@@ -46,26 +47,46 @@ def get_vector_store(chunks):
46
  raise RuntimeError(f"Error creating vector store: {e}")
47
 
48
 
49
- # ✅ Function to get conversational chain
50
  def get_conversational_chain():
51
- prompt_template = """
52
- You are a helpful assistant. Do NOT reveal your identity (Gemini) or the company (Google).
53
- Answer the question as detailed as possible using ONLY the provided context.
54
- If the answer is not in the context, say: "answer is not available in the context".
55
- Do not make up answers.
56
 
57
  Context:
58
  {context}
59
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  Question:
61
  {question}
62
 
63
  Answer:
64
  """
 
65
  try:
66
  model = ChatGoogleGenerativeAI(model="gemini-2.5-pro", client=genai, temperature=0.3)
67
- prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
68
- chain = load_qa_chain(llm=model, chain_type="stuff", prompt=prompt)
 
 
 
 
 
 
 
 
69
  return chain
70
  except Exception as e:
71
  raise RuntimeError(f"Error creating conversational chain: {e}")
@@ -81,7 +102,10 @@ def user_input(user_question):
81
  try:
82
  embeddings = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")
83
  new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
84
- docs = new_db.similarity_search(user_question, k=8)
 
 
 
85
  chain = get_conversational_chain()
86
  response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
87
  return response
 
11
  from langchain.chains.question_answering import load_qa_chain
12
  from langchain.prompts import PromptTemplate
13
 
14
+
15
  # Load environment variables
16
  load_dotenv()
17
  api_key = os.getenv("GOOGLE_API_KEY")
 
47
  raise RuntimeError(f"Error creating vector store: {e}")
48
 
49
 
50
+ # ✅ Function to get conversational chain (map_reduce برای متن‌های بلند)
51
  def get_conversational_chain():
52
+ map_prompt = """
53
+ You are a helpful assistant. Summarize the following context to capture the key points
54
+ that are relevant for answering the final question.
55
+ Do NOT add extra info, just summarize faithfully.
 
56
 
57
  Context:
58
  {context}
59
 
60
+ Summary:
61
+ """
62
+
63
+ combine_prompt = """
64
+ You are a helpful assistant. Answer the question as detailed as possible
65
+ using ONLY the provided summaries.
66
+ If the answer is not in the summaries, say: "answer is not available in the context".
67
+ Do not make up answers.
68
+
69
+ Summaries:
70
+ {summaries}
71
+
72
  Question:
73
  {question}
74
 
75
  Answer:
76
  """
77
+
78
  try:
79
  model = ChatGoogleGenerativeAI(model="gemini-2.5-pro", client=genai, temperature=0.3)
80
+
81
+ map_prompt_template = PromptTemplate(template=map_prompt, input_variables=["context"])
82
+ combine_prompt_template = PromptTemplate(template=combine_prompt, input_variables=["summaries", "question"])
83
+
84
+ chain = load_qa_chain(
85
+ llm=model,
86
+ chain_type="map_reduce",
87
+ map_prompt=map_prompt_template,
88
+ combine_prompt=combine_prompt_template
89
+ )
90
  return chain
91
  except Exception as e:
92
  raise RuntimeError(f"Error creating conversational chain: {e}")
 
102
  try:
103
  embeddings = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")
104
  new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
105
+
106
+ # similarity_search with more results (better recall for long docs)
107
+ docs = new_db.similarity_search(user_question, k=12)
108
+
109
  chain = get_conversational_chain()
110
  response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
111
  return response