MusaR commited on
Commit
162852d
·
verified ·
1 Parent(s): c7f8b81

Update research_agent/agent.py

Browse files
Files changed (1) hide show
  1. research_agent/agent.py +15 -6
research_agent/agent.py CHANGED
@@ -80,15 +80,23 @@ def write_report_stream(config: AgentConfig, writer_model, tavily_client, embedd
80
  detailed_topic = plan["detailed_topic"]
81
  sections = plan["sections"]
82
 
83
- yield f"### Starting Report Generation for: {detailed_topic}\n\n"
84
 
85
- report_state = {"full_report_text": f"# Deep Research Report: {detailed_topic}\n\n", "all_source_urls": set()}
 
 
 
 
86
  rag_pipeline = RAGPipeline(embedding_model, reranker)
87
 
88
  for i, section in enumerate(sections):
89
- yield f"--- \n### Processing Section {i+1}/{len(sections)}: {section.title}...\n"
90
 
91
- previous_sections_context = report_state["full_report_text"]
 
 
 
 
92
 
93
  section_queries = [f"{detailed_topic} - {section.title}"] + section.description.split('\n')[-3:]
94
  section_queries = [q.strip() for q in section_queries if q.strip()]
@@ -121,6 +129,7 @@ def write_report_stream(config: AgentConfig, writer_model, tavily_client, embedd
121
  bibliography = "\n".join(f"[{num}] {url}" for url, num in cited_sources_for_section.items())
122
 
123
  yield f"-> Synthesizing and writing section content...\n"
 
124
  writer_prompt = prompts.writer_prompt_template.format(
125
  writer_system_instruction=prompts.writer_system_instruction,
126
  previous_sections_context=previous_sections_context,
@@ -130,14 +139,14 @@ def write_report_stream(config: AgentConfig, writer_model, tavily_client, embedd
130
 
131
  draft_content = run_gemini_text_completion(writer_model, writer_prompt, config.WRITER_TEMPERATURE)
132
 
133
- yield "-> Fact-checking and verifying section...\n"
134
  final_content = run_verification_step(writer_model, draft_content, context_for_llm)
135
  final_content_with_sources = f"## {section.title}\n\n{final_content}\n\n**Sources Used in this Section**\n{bibliography}\n\n"
136
 
137
  report_state["full_report_text"] += final_content_with_sources
 
138
 
139
  final_bibliography = "\n".join(f"- {url}" for url in sorted(list(report_state["all_source_urls"])))
140
  report_state["full_report_text"] += f"## Master Bibliography\n\n{final_bibliography}"
141
 
142
- yield "\n--- Report Generation Complete ---\n"
143
  return report_state["full_report_text"]
 
80
  detailed_topic = plan["detailed_topic"]
81
  sections = plan["sections"]
82
 
83
+ yield f"### Starting Report Generation for: {detailed_topic}\n\n---\n"
84
 
85
+ report_state = {
86
+ "full_report_text": f"# Deep Research Report: {detailed_topic}\n\n",
87
+ "all_source_urls": set(),
88
+ "completed_section_titles": []
89
+ }
90
  rag_pipeline = RAGPipeline(embedding_model, reranker)
91
 
92
  for i, section in enumerate(sections):
93
+ yield f"### Processing Section {i+1}/{len(sections)}: {section.title}...\n\n"
94
 
95
+ previous_sections_context = "\n".join(f"- {title}" for title in report_state["completed_section_titles"])
96
+ if previous_sections_context:
97
+ previous_sections_context = "The following sections have already been written:\n" + previous_sections_context
98
+ else:
99
+ previous_sections_context = "This is the first section."
100
 
101
  section_queries = [f"{detailed_topic} - {section.title}"] + section.description.split('\n')[-3:]
102
  section_queries = [q.strip() for q in section_queries if q.strip()]
 
129
  bibliography = "\n".join(f"[{num}] {url}" for url, num in cited_sources_for_section.items())
130
 
131
  yield f"-> Synthesizing and writing section content...\n"
132
+ yield f"--> Calling LLM for section '{section.title}'. This may take a moment...\n"
133
  writer_prompt = prompts.writer_prompt_template.format(
134
  writer_system_instruction=prompts.writer_system_instruction,
135
  previous_sections_context=previous_sections_context,
 
139
 
140
  draft_content = run_gemini_text_completion(writer_model, writer_prompt, config.WRITER_TEMPERATURE)
141
 
142
+ yield "-> Fact-checking and verifying section...\n\n---\n"
143
  final_content = run_verification_step(writer_model, draft_content, context_for_llm)
144
  final_content_with_sources = f"## {section.title}\n\n{final_content}\n\n**Sources Used in this Section**\n{bibliography}\n\n"
145
 
146
  report_state["full_report_text"] += final_content_with_sources
147
+ report_state["completed_section_titles"].append(section.title)
148
 
149
  final_bibliography = "\n".join(f"- {url}" for url in sorted(list(report_state["all_source_urls"])))
150
  report_state["full_report_text"] += f"## Master Bibliography\n\n{final_bibliography}"
151
 
 
152
  return report_state["full_report_text"]