Update app.py
Browse files
app.py
CHANGED
@@ -283,21 +283,34 @@ if st.session_state.pending_prompt:
|
|
283 |
context = "\n".join([doc.page_content for doc in docs])
|
284 |
|
285 |
# ساخت پرامپت نهایی برای LLM
|
286 |
-
final_prompt = f"""با توجه به اطلاعات
|
287 |
-
|
|
|
288 |
"""
|
289 |
|
|
|
290 |
# ارسال به LLM
|
291 |
response = llm.invoke(final_prompt)
|
292 |
-
|
293 |
-
|
294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
295 |
|
296 |
# تایپ کردن تدریجی پاسخ
|
297 |
thinking.empty()
|
298 |
full_response = ""
|
299 |
placeholder = st.empty()
|
300 |
-
for word in
|
301 |
full_response += word + " "
|
302 |
placeholder.markdown(full_response + "▌")
|
303 |
time.sleep(0.03)
|
@@ -308,4 +321,4 @@ if st.session_state.pending_prompt:
|
|
308 |
|
309 |
except Exception as e:
|
310 |
thinking.empty()
|
311 |
-
st.error(f"خطا در پاسخدهی: {str(e)}")
|
|
|
283 |
context = "\n".join([doc.page_content for doc in docs])
|
284 |
|
285 |
# ساخت پرامپت نهایی برای LLM
|
286 |
+
final_prompt = f"""با توجه به اطلاعات زیر، بهترین جواب رو فقط از داخل این اطلاعات به زبان فارسی بده. لطفاً از خارج از این اطلاعات استفاده نکن و اگر اطلاعات کافی برای جواب دادن نبود، بهترین پاسخ رو بده.
|
287 |
+
|
288 |
+
🔹 اطلاعات:\n{context}\n\n❓ سؤال: {query}
|
289 |
"""
|
290 |
|
291 |
+
|
292 |
# ارسال به LLM
|
293 |
response = llm.invoke(final_prompt)
|
294 |
+
raw_answer = response.content.strip()
|
295 |
+
|
296 |
+
# فیلتر کردن خطوط غیرپاسخ (مثلاً <think> یا توضیح مدل)
|
297 |
+
answer_lines = raw_answer.split('\n')
|
298 |
+
filtered_lines = [
|
299 |
+
line for line in answer_lines
|
300 |
+
if not line.strip().startswith("<")
|
301 |
+
and not line.strip().lower().startswith("think")
|
302 |
+
and not line.strip().startswith("#")
|
303 |
+
and not line.strip().lower().startswith("note")
|
304 |
+
]
|
305 |
+
clean_answer = "\n".join(filtered_lines).strip()
|
306 |
+
if not clean_answer:
|
307 |
+
clean_answer = "متأسفم، اطلاعات دقیقی در این مورد ندارم."
|
308 |
|
309 |
# تایپ کردن تدریجی پاسخ
|
310 |
thinking.empty()
|
311 |
full_response = ""
|
312 |
placeholder = st.empty()
|
313 |
+
for word in clean_answer.split():
|
314 |
full_response += word + " "
|
315 |
placeholder.markdown(full_response + "▌")
|
316 |
time.sleep(0.03)
|
|
|
321 |
|
322 |
except Exception as e:
|
323 |
thinking.empty()
|
324 |
+
st.error(f"خطا در پاسخدهی: {str(e)}")
|