naman1102 commited on
Commit
6b46fb5
·
1 Parent(s): 4ae17c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -24
app.py CHANGED
@@ -388,15 +388,17 @@ QUESTION:
388
  MATERIALS:
389
  {search_block}
390
 
391
- Write ANSWER: <answer> on its own line.
 
392
  """
393
  try:
394
  raw = self._call_llm(prompt, 300)
395
- answer = raw.split("ANSWER:")[-1].strip()
396
-
397
- # If first attempt fails or is empty, try a more direct prompt
398
- if not answer or "ANSWER:" not in raw or any(k in answer.lower() for k in ["cannot", "sorry", "don't know"]):
399
- print("\nFirst attempt failed, trying direct prompt...")
 
400
  direct_prompt = f"""
401
  Answer this question directly and concisely. Use the materials provided.
402
 
@@ -406,28 +408,33 @@ QUESTION:
406
  MATERIALS:
407
  {search_block}
408
 
409
- If you cannot find an exact answer, provide the most relevant information from the materials.
410
- Write ANSWER: <answer> on its own line.
411
  """
412
  raw = self._call_llm(direct_prompt, 300)
413
- answer = raw.split("ANSWER:")[-1].strip()
414
-
415
- # Final validation and fallback
416
- if not answer or "ANSWER:" not in raw:
417
- print("\nBoth attempts failed, using fallback answer...")
418
- if materials:
419
- # If we have materials but no answer, summarize what we know
420
- summary_prompt = f"""
421
- Summarize the key information from these materials in one sentence:
 
 
422
 
 
423
  {search_block}
424
-
425
- Write ANSWER: <answer> on its own line.
426
  """
427
- raw = self._call_llm(summary_prompt, 150)
428
- answer = raw.split("ANSWER:")[-1].strip()
429
- else:
430
- answer = "I cannot provide a definitive answer at this time."
 
 
 
 
431
 
432
  state["final_answer"] = answer
433
  state["current_step"] = "done"
@@ -508,7 +515,7 @@ Write ANSWER: <answer> on its own line.
508
  }
509
 
510
  final_state = self.workflow.invoke(state)
511
- return final_state["final_answer"]
512
 
513
  def _download_file(self, url: str) -> bytes:
514
  """Download a file from a URL."""
 
388
  MATERIALS:
389
  {search_block}
390
 
391
+ Return ONLY a JSON object in this format:
392
+ {{"ANSWER": "your answer here"}}
393
  """
394
  try:
395
  raw = self._call_llm(prompt, 300)
396
+ try:
397
+ result = json.loads(raw)
398
+ answer = result.get("ANSWER", "")
399
+ except json.JSONDecodeError:
400
+ print("\nJSON parse error, trying direct prompt...")
401
+ # If first attempt fails, try a more direct prompt
402
  direct_prompt = f"""
403
  Answer this question directly and concisely. Use the materials provided.
404
 
 
408
  MATERIALS:
409
  {search_block}
410
 
411
+ Return ONLY a JSON object in this format:
412
+ {{"ANSWER": "your answer here"}}
413
  """
414
  raw = self._call_llm(direct_prompt, 300)
415
+ try:
416
+ result = json.loads(raw)
417
+ answer = result.get("ANSWER", "")
418
+ except json.JSONDecodeError:
419
+ print("\nBoth attempts failed, using fallback answer...")
420
+ if materials:
421
+ # If we have materials but no answer, summarize what we know
422
+ summary_prompt = f"""
423
+ Summarize the key information from these materials in one sentence.
424
+ Return ONLY a JSON object in this format:
425
+ {{"ANSWER": "your summary here"}}
426
 
427
+ MATERIALS:
428
  {search_block}
 
 
429
  """
430
+ raw = self._call_llm(summary_prompt, 150)
431
+ try:
432
+ result = json.loads(raw)
433
+ answer = result.get("ANSWER", "")
434
+ except json.JSONDecodeError:
435
+ answer = "I cannot provide a definitive answer at this time."
436
+ else:
437
+ answer = "I cannot provide a definitive answer at this time."
438
 
439
  state["final_answer"] = answer
440
  state["current_step"] = "done"
 
515
  }
516
 
517
  final_state = self.workflow.invoke(state)
518
+ return final_state["final_answer"] # Return the answer string directly, not JSON encoded
519
 
520
  def _download_file(self, url: str) -> bytes:
521
  """Download a file from a URL."""