Mustafa-albakkar commited on
Commit
f37b453
·
verified ·
1 Parent(s): 9db7465

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -32
app.py CHANGED
@@ -291,13 +291,22 @@ def create_agent_executor(llm, tools: List[Tool], tracer: Optional[Any] = None)
291
  callbacks = []
292
  if tracer is not None:
293
  callbacks.append(tracer)
294
- executor = AgentExecutor(agent=agent, tools=tools, verbose=True, callbacks=callbacks,max_iterations=10,
 
 
 
 
 
295
  handle_parsing_errors=True,
296
- early_stopping_method="force")
 
 
 
297
  return executor
298
 
299
  # -------------------- GAIA Runner --------------------
300
- class GaiaRunner:
 
301
  def __init__(self, agent_executor: AgentExecutor, username: str = "unknown"):
302
  self.agent = agent_executor
303
  self.username = username
@@ -332,38 +341,30 @@ class GaiaRunner:
332
  # ==========================
333
  # 3️⃣ بناء سجل التفكير الكامل
334
  # ==========================
335
- full_log = []
336
- full_log.append(f"Question: {question_text}\n")
337
-
338
- # إذا كان هناك مرفق
 
 
 
 
 
339
  if file_path:
340
- full_log.append(f"Attachment: {file_path}\n")
341
-
342
- # إضافة جميع خطوات ReAct الداخلية
343
- if "intermediate_steps" in result:
344
- for step in result["intermediate_steps"]:
345
- try:
346
- action, observation = step
347
- full_log.append(
348
- f"Action: {action.tool}\n"
349
- f"Action Input: {action.tool_input}\n"
350
- f"Observation: {observation}\n"
351
- )
352
- except Exception as e:
353
- print(f"[⚠️] Failed to parse intermediate step: {e}")
354
-
355
- # إضافة الإجابة النهائية
356
- # output = result.get("output", "")
357
- output = result
358
  full_log.append(f"Final Answer: {output}\n")
359
-
360
- # دمج كل السجل في نص واحد
361
  conversation_log = "\n".join(full_log)
362
-
363
- print("\n🧠 === FULL REACT LOG ===")
364
- print(conversation_log)
365
- print("==========================")
366
-
367
  # ==========================
368
  # 4️⃣ إرسال السجل الكامل إلى وكيل الإجابة النهائية
369
  # ==========================
@@ -417,6 +418,7 @@ class GaiaRunner:
417
  """
418
 
419
  response = requests.get(questions_url, timeout=15)
 
420
  if response.status_code == 200:
421
  questions_data = response.json()
422
  if questions_data:
 
291
  callbacks = []
292
  if tracer is not None:
293
  callbacks.append(tracer)
294
+ executor = AgentExecutor(
295
+ agent=agent,
296
+ tools=tools,
297
+ verbose=True, # إيقاف الطباعة المكثفة — لكن يمكنك تشغيلها أثناء debugging إذا رغبت
298
+ callbacks=callbacks,
299
+ max_iterations=10,
300
  handle_parsing_errors=True,
301
+ early_stopping_method="force",
302
+ return_intermediate_steps=True, # <<< الأهم — اطلب إعادة intermediate_steps
303
+ trim_intermediate_steps=-1 # -1 => لا تقص الخطوات قبل الإرجاع (أو حدد عددًا إن أردت الحد)
304
+ )
305
  return executor
306
 
307
  # -------------------- GAIA Runner --------------------
308
+ class GaiaRunner
309
+
310
  def __init__(self, agent_executor: AgentExecutor, username: str = "unknown"):
311
  self.agent = agent_executor
312
  self.username = username
 
341
  # ==========================
342
  # 3️⃣ بناء سجل التفكير الكامل
343
  # ==========================
344
+ # بعد الحصول على result
345
+ if isinstance(result, dict):
346
+ output = result.get("output") or result.get("text") or str(result)
347
+ intermediate = result.get("intermediate_steps", [])
348
+ else:
349
+ output = getattr(result, "output", str(result))
350
+ intermediate = []
351
+ # بناء السجل
352
+ full_log = [f"Question: {question_text}\n"]
353
  if file_path:
354
+ full_log.append(f"Attachment: {file}\n")
355
+ for step in intermediate:
356
+ try:
357
+ action, observation = step
358
+ full_log.append(
359
+ f"Thought/Action: {getattr(action, 'log', getattr(action, 'tool', str(action)))}\n"
360
+ f"Action Input: {getattr(action, 'tool_input', getattr(action, 'input', ''))}\n"
361
+ f"Observation: {observation}\n"
362
+ )
363
+ except Exception as e:
364
+ # تحصّن أمام صيغ غير متوقعة
365
+ full_log.append(f"[UNPARSEABLE STEP] {step}\n")
 
 
 
 
 
 
366
  full_log.append(f"Final Answer: {output}\n")
 
 
367
  conversation_log = "\n".join(full_log)
 
 
 
 
 
368
  # ==========================
369
  # 4️⃣ إرسال السجل الكامل إلى وكيل الإجابة النهائية
370
  # ==========================
 
418
  """
419
 
420
  response = requests.get(questions_url, timeout=15)
421
+ print(response)
422
  if response.status_code == 200:
423
  questions_data = response.json()
424
  if questions_data: