Skip to content

Commit 1511464

Browse files
committed
Update research_engine.py
1 parent c95f643 commit 1511464

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

optillm/plugins/deep_research/research_engine.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,45 @@ def research(self, system_prompt: str, initial_query: str) -> Tuple[str, int]:
10501050
print("TTD-DR: Generating preliminary draft...")
10511051
self.current_draft = self.generate_preliminary_draft(system_prompt, initial_query)
10521052
self.draft_history.append(self.current_draft)
1053+
1054+
# PHASE 1.5: INITIAL RESEARCH - Ensure we always gather external sources
1055+
print("TTD-DR: Performing initial research...")
1056+
initial_queries = self.decompose_query(system_prompt, initial_query)
1057+
if initial_queries:
1058+
print(f" - Searching for {len(initial_queries)} initial topics...")
1059+
initial_search_results = self.perform_web_search(initial_queries)
1060+
1061+
# Extract and fetch URLs from initial search
1062+
if initial_search_results and "Web Search Results" in initial_search_results:
1063+
print(" - Extracting initial sources...")
1064+
initial_content, initial_sources = self.extract_and_fetch_urls(initial_search_results)
1065+
1066+
# Register initial sources
1067+
for source in initial_sources:
1068+
if 'url' in source:
1069+
self.citation_counter += 1
1070+
self.citations[self.citation_counter] = source
1071+
1072+
# Store initial research
1073+
self.research_state["content"].append(initial_content)
1074+
self.research_state["sources"].extend([s['url'] for s in initial_sources if 'url' in s])
1075+
1076+
print(f" - Found {len(initial_sources)} initial sources")
1077+
else:
1078+
print(" - No sources found in initial search")
1079+
else:
1080+
print(" - Warning: Could not decompose query for initial research")
1081+
# Fallback: Create simple search queries from the original query
1082+
print(" - Using fallback search strategy...")
1083+
fallback_queries = [initial_query] # At minimum, search for the original query
1084+
fallback_search_results = self.perform_web_search(fallback_queries)
1085+
if fallback_search_results and "Web Search Results" in fallback_search_results:
1086+
fallback_content, fallback_sources = self.extract_and_fetch_urls(fallback_search_results)
1087+
for source in fallback_sources:
1088+
if 'url' in source:
1089+
self.citation_counter += 1
1090+
self.citations[self.citation_counter] = source
1091+
print(f" - Fallback search found {len(fallback_sources)} sources")
10531092

10541093
# PHASE 2: ITERATIVE DENOISING LOOP
10551094
for iteration in range(self.max_iterations):
@@ -1114,6 +1153,14 @@ def research(self, system_prompt: str, initial_query: str) -> Tuple[str, int]:
11141153

11151154
# PHASE 3: FINALIZATION - Polish the final draft
11161155
print("TTD-DR: Finalizing research report...")
1156+
1157+
# Ensure we have gathered some sources
1158+
if len(self.citations) == 0:
1159+
print("⚠️ Warning: No external sources found during research!")
1160+
print(" Deep research should always consult external sources.")
1161+
else:
1162+
print(f"✅ Research completed with {len(self.citations)} sources")
1163+
11171164
final_report = self.finalize_research_report(system_prompt, initial_query, self.current_draft)
11181165

11191166
return final_report, self.total_tokens

0 commit comments

Comments
 (0)