AlirezaHSZ commited on
Commit
c721836
·
verified ·
1 Parent(s): c00c636

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -13
app.py CHANGED
@@ -42,8 +42,13 @@ def get_vector_store(chunks):
42
  raise RuntimeError(f"Error creating vector store: {e}")
43
 
44
  # ========== Map-Reduce QA chain (بدون load_qa_chain) ==========
 
 
 
 
 
45
  def get_conversational_chain():
46
- # Map: برای هر تکه، خلاصه/نکات مرتبط با سؤال را بیرون بکش
47
  map_prompt_tmpl = PromptTemplate(
48
  template=(
49
  "You are a helpful assistant. Extract a concise summary strictly from the context "
@@ -54,7 +59,8 @@ def get_conversational_chain():
54
  ),
55
  input_variables=["context", "question"],
56
  )
57
- # Reduce/Combine: پاسخ نهایی با اتکا به خلاصه‌های مرحله Map
 
58
  combine_prompt_tmpl = PromptTemplate(
59
  template=(
60
  "You are a helpful assistant. Answer the question using ONLY the provided summaries. "
@@ -66,34 +72,52 @@ def get_conversational_chain():
66
  input_variables=["summaries", "question"],
67
  )
68
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  model = ChatGoogleGenerativeAI(model="gemini-2.5-pro", client=genai, temperature=0.3)
70
 
71
- # LLM chains برای دو مرحله
72
- map_llm_chain = LLMChain(llm=model, prompt=map_prompt_tmpl) # expects: context, question
73
- combine_llm_chain = LLMChain(llm=model, prompt=combine_prompt_tmpl) # expects: summaries, question
 
74
 
75
- # StuffDocumentsChain برای ترکیب خلاصه‌ها
76
  combine_documents_chain = StuffDocumentsChain(
77
  llm_chain=combine_llm_chain,
78
- document_variable_name="summaries" # باید با ورودی پرامپت combine یکی باشد
 
 
 
 
79
  )
80
 
81
- # Reduce chain
82
  reduce_chain = ReduceDocumentsChain(
83
  combine_documents_chain=combine_documents_chain,
84
- collapse_documents_chain=None, # می‌تونی در صورت خیلی زیاد بودن خلاصه‌ها یک collapse هم اضافه کنی
85
- token_max=None
86
  )
87
 
88
- # Map-Reduce chain اصلی
89
  chain = MapReduceDocumentsChain(
90
- llm_chain=map_llm_chain, # expects: context, question
91
  reduce_documents_chain=reduce_chain,
92
- document_variable_name="context", # باید با ورودی پرامپت map یکی باشد
93
  return_intermediate_steps=False,
94
  )
95
  return chain
96
 
 
97
  # ========== Chat history utils ==========
98
  def clear_chat_history():
99
  st.session_state.messages = [{"role": "assistant", "content": "در خدمتیم"}]
 
42
  raise RuntimeError(f"Error creating vector store: {e}")
43
 
44
  # ========== Map-Reduce QA chain (بدون load_qa_chain) ==========
45
+ from langchain.prompts import PromptTemplate
46
+ from langchain.chains import LLMChain
47
+ from langchain.chains import MapReduceDocumentsChain, ReduceDocumentsChain, StuffDocumentsChain
48
+ from langchain_google_genai import ChatGoogleGenerativeAI
49
+
50
  def get_conversational_chain():
51
+ # مرحله Map: خلاصه یا نِکات مرتبط از هر تکه
52
  map_prompt_tmpl = PromptTemplate(
53
  template=(
54
  "You are a helpful assistant. Extract a concise summary strictly from the context "
 
59
  ),
60
  input_variables=["context", "question"],
61
  )
62
+
63
+ # مرحله Combine: پاسخ نهایی فقط بر اساس خلاصه‌ها
64
  combine_prompt_tmpl = PromptTemplate(
65
  template=(
66
  "You are a helpful assistant. Answer the question using ONLY the provided summaries. "
 
72
  input_variables=["summaries", "question"],
73
  )
74
 
75
+ # مرحله Collapse: اگر خلاصه‌ها زیاد شد، کوتاه‌شان کن (برای رد شدن از سقف توکن)
76
+ collapse_prompt_tmpl = PromptTemplate(
77
+ template=(
78
+ "Condense the following summaries into a shorter consolidated summary, keeping only "
79
+ "information relevant to the question.\n\n"
80
+ "Question:\n{question}\n\n"
81
+ "Summaries:\n{summaries}\n\n"
82
+ "Shorter summaries:"
83
+ ),
84
+ input_variables=["summaries", "question"],
85
+ )
86
+
87
  model = ChatGoogleGenerativeAI(model="gemini-2.5-pro", client=genai, temperature=0.3)
88
 
89
+ # LLMChain ها
90
+ map_llm_chain = LLMChain(llm=model, prompt=map_prompt_tmpl) # expects: context, question
91
+ combine_llm_chain = LLMChain(llm=model, prompt=combine_prompt_tmpl) # expects: summaries, question
92
+ collapse_llm_chain = LLMChain(llm=model, prompt=collapse_prompt_tmpl) # expects: summaries, question
93
 
94
+ # زنجیره‌های ترکیب و فشرده‌سازی
95
  combine_documents_chain = StuffDocumentsChain(
96
  llm_chain=combine_llm_chain,
97
+ document_variable_name="summaries"
98
+ )
99
+ collapse_documents_chain = StuffDocumentsChain(
100
+ llm_chain=collapse_llm_chain,
101
+ document_variable_name="summaries"
102
  )
103
 
104
+ # Reduce با عدد صحیح برای token_max (مثلاً 8000)
105
  reduce_chain = ReduceDocumentsChain(
106
  combine_documents_chain=combine_documents_chain,
107
+ collapse_documents_chain=collapse_documents_chain,
108
+ token_max=8000 # ✅ عدد صحیح؛ می‌تونی بر اساس نیازت کم/زیادش کنی
109
  )
110
 
111
+ # زنجیره‌ی Map Reduce
112
  chain = MapReduceDocumentsChain(
113
+ llm_chain=map_llm_chain,
114
  reduce_documents_chain=reduce_chain,
115
+ document_variable_name="context", # باید با ورودی map_prompt یکی باشد
116
  return_intermediate_steps=False,
117
  )
118
  return chain
119
 
120
+
121
  # ========== Chat history utils ==========
122
  def clear_chat_history():
123
  st.session_state.messages = [{"role": "assistant", "content": "در خدمتیم"}]