response_parser.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. workflow_summary_markers = [
  2. 'WORKFLOW SUMMARY',
  3. 'WORKFLOW_SUMMARY',
  4. 'WORKFLOW-SUMMARY',
  5. 'Workflow Summary',
  6. ]
  7. final_answer_markers = [
  8. 'FINAL ANSWER',
  9. 'FINAL_ANSWER',
  10. 'FINAL-ANSWER',
  11. 'Final Answer',
  12. 'Scientific Hypothesis',
  13. 'Hypothesis',
  14. ]
  15. next_agent_markers = [
  16. 'NEXT AGENT',
  17. 'NEXT-AGENT',
  18. 'NEXT_AGENT',
  19. 'FEEDBACK',
  20. ]
  21. def extract_between(content, start_markers, end_markers=None):
  22. for marker in start_markers:
  23. if marker in content:
  24. result = content.split(marker, 1)[1]
  25. if end_markers:
  26. for end_marker in end_markers:
  27. if end_marker in result:
  28. result = result.split(end_marker, 1)[0]
  29. return result
  30. return ''
  31. def extract_gen_hypo_from_logs(content: str):
  32. error = ''
  33. gen_workflow = extract_between(
  34. content, workflow_summary_markers, final_answer_markers
  35. )
  36. if not gen_workflow:
  37. error += 'No Workflow Summary found in the line. | '
  38. gen_hypothesis = extract_between(content, final_answer_markers, next_agent_markers)
  39. if not gen_hypothesis:
  40. error += 'No Final Answer in the line.'
  41. return gen_hypothesis, gen_workflow, error