Spaces:
Paused
Paused
File size: 25,657 Bytes
3647b02 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Core research and reporting module for the Universal Deep Research Backend (UDR-B).
This module provides the main research functionality including:
- Query analysis and topic extraction
- Web search using Tavily API
- Content filtering and relevance scoring
- Report generation using LLMs
"""
import asyncio
import random
from typing import Any, AsyncGenerator, Dict, List
from openai import OpenAI
from tavily import TavilyClient
import items
from clients import (
create_lm_client,
create_tavily_client,
get_completion,
is_output_positive,
)
from config import get_config
from sessions import generate_session_key
# Get configuration
config = get_config()
# Use configuration values
MAX_TOPICS: int = config.research.max_topics
MAX_SEARCH_PHRASES: int = config.research.max_search_phrases
def build_research_artifacts_path(session_key: str) -> str:
return f"instances/{session_key}.research_artifacts.jsonl"
def build_reporting_artifacts_path(session_key: str) -> str:
return f"instances/{session_key}.reporting_artifacts.jsonl"
def make_event(
type: str,
description: str,
hidden: bool = False,
deltaSearchCount: int = 0,
deltaQueryCount: int = 0,
report: str | None = None,
) -> Dict[str, Any]:
return {
"type": type,
"description": description,
"hidden": hidden,
"deltaSearchCount": deltaSearchCount,
"deltaQueryCount": deltaQueryCount,
**({"report": report} if report is not None else {}),
}
async def do_research(
session_key: str, prompt: str
) -> AsyncGenerator[Dict[str, Any], None]:
"""
Performs research based on the provided query and yields events as they occur.
This function handles the research phase, including:
- Receipt and analysis of the research query
- Planning the research approach
- Searching for relevant information
- Filtering and categorizing results
- Aggregating findings into a coherent output
Events yielded:
- prompt_received: Initial receipt of the research query
- prompt_analysis: Analysis of the query's requirements
- research_planning: Planning the research approach
- topic_exploration_started: Starting research on each topic
- search_started: Initiating a search for each search phrase
- search_result_processing_started: Processing the search results
- aggregation_started: Beginning the aggregation of relevant segments
- research_completed: Completion of the research phase
Args:
session_key (str): A unique identifier for the research session.
prompt (str): The research query to process.
Yields:
Dict containing:
- type (str): The event type.
- description (str): User-friendly description of the event.
- hidden (bool, default=False): Whether the event is hidden from the user or should be displayed.
- deltaSearchCount (int, optional): The number of search queries performed.
- deltaQueryCount (int, optional): The number of queries performed.
- report (str, optional): The report produced.
"""
research_artifacts_path: str = build_research_artifacts_path(session_key)
items.register_item(research_artifacts_path, {"type": "prompt", "prompt": prompt})
client = create_lm_client()
tavily_client = create_tavily_client()
yield make_event(
"prompt_received",
f"Received research request: '{prompt}'",
)
prompt_valid: bool = check_if_prompt_is_valid(client, prompt)
items.register_item(
research_artifacts_path, {"type": "prompt_validity", "valid": prompt_valid}
)
if not prompt_valid:
yield make_event(
"error",
"It would appear that the prompt is not a valid document research prompt. Please try again with a valid prompt.",
deltaQueryCount=1,
)
return
yield {
"type": "prompt_analysis_started",
"description": "Analyzing the research request...",
"hidden": False,
}
task_prompt, format_prompt = perform_prompt_decomposition(client, prompt)
items.register_item(
research_artifacts_path, {"type": "task_prompt", "task_prompt": task_prompt}
)
items.register_item(
research_artifacts_path,
{"type": "format_prompt", "format_prompt": format_prompt},
)
yield make_event(
"prompt_analysis_completed",
f"Prompt analysis completed. Will analyze the following task assignment: '{task_prompt}'.",
deltaQueryCount=1,
)
topics: List[str] = generate_topics(client, task_prompt)
items.register_item(research_artifacts_path, {"type": "topics", "topics": topics})
yield make_event(
"task_analysis_completed",
f"Task analysis completed. Will be researching {len(topics)}+ topics.",
deltaQueryCount=1,
)
topic_relevant_segments: dict[str, List[str]] = {}
search_result_urls: List[str] = []
all_results: List[Dict[str, Any]] = []
for topic in topics:
yield make_event(
"topic_exploration_started",
f"Researching '{topic}'",
)
search_phrases: List[str] = produce_search_phrases(client, prompt, topic)
items.register_item(
research_artifacts_path,
{
"type": "topic_search_phrases",
"topic": topic,
"search_phrases": search_phrases,
},
)
yield make_event(
"topic_exploration_completed",
f"Will invoke {len(search_phrases)} search phrases to research '{topic}'.",
deltaQueryCount=1,
)
topic_relevant_segments[topic] = []
for search_phrase in search_phrases:
yield make_event(
"search_started",
f"Searching for '{search_phrase}'",
)
search_results: List[Dict[str, Any]] = perform_search(
tavily_client, search_phrase, research_artifacts_path
)
items.register_item(
research_artifacts_path,
{
"type": "topic_search_results",
"topic": topic,
"search_phrase": search_phrase,
"results": search_results,
},
)
original_search_result_urls: List[str] = [
result["url"]
for result in search_results
if result["url"] not in search_result_urls
]
search_result_urls.extend(original_search_result_urls)
original_search_results = [
result
for result in search_results
if result["url"] in original_search_result_urls
]
all_results.extend(original_search_results)
yield make_event(
"search_result_processing_started",
f"Processing {len(original_search_result_urls)} search results.",
deltaSearchCount=1,
)
for search_result in original_search_results:
search_result_content: str = search_result["raw_content"]
search_result_url: str = search_result["url"]
search_result_url_index: int = search_result_urls.index(
search_result_url
)
relevant_segments: List[str] = find_relevant_segments(
client,
prompt,
topic,
search_result_content,
search_result_url_index,
)
topic_relevant_segments[topic].extend(relevant_segments)
items.register_item(
research_artifacts_path,
{
"type": "topic_search_result_relevant_segments",
"topic": topic,
"search_phrase": search_phrase,
"search_result": search_result,
"relevant_segments": relevant_segments,
},
)
yield make_event(
"search_result_processing_completed",
f"Processed search result {search_result_url_index}.",
hidden=True,
deltaQueryCount=1,
)
yield make_event(
"aggregation_started",
"Aggregating relevant information for all topics.",
)
items.register_item(
research_artifacts_path,
{
"type": "topic_relevant_segments",
"topic_relevant_segments": topic_relevant_segments,
"search_result_urls": search_result_urls,
},
)
items.register_item(
research_artifacts_path, {"type": "all_results", "all_results": all_results}
)
# sleep for 0.5s
await asyncio.sleep(0.5)
yield make_event(
"research_completed",
"Research phase completed.",
)
async def do_reporting(session_key: str) -> AsyncGenerator[Dict[str, Any], None]:
"""
Generates a report based on the research findings and yields events as they occur.
This function handles the reporting phase, including:
- Report construction
- Formatting and organization
- Final delivery
Events yielded:
- report_building: Initial report construction
- report_formatting: Formatting and structuring the report
- report_done: Report completion and delivery
Yields:
Dict containing:
- type (str): The event type
- description (str): User-friendly description of the event
- hidden (bool, default=False): Whether the event is hidden from the user or should be displayed
- deltaSearchCount (int, optional): The number of search queries performed
- deltaQueryCount (int, optional): The number of queries performed
- report (str, optional): The report produced
"""
client = create_lm_client()
research_artifacts_path: str = build_research_artifacts_path(session_key)
task_prompt: str = items.find_item_by_type(research_artifacts_path, "task_prompt")[
"task_prompt"
]
format_prompt: str = items.find_item_by_type(
research_artifacts_path, "format_prompt"
)["format_prompt"]
topic_relevant_segments: dict[str, List[str]] = items.find_item_by_type(
research_artifacts_path, "topic_relevant_segments"
)["topic_relevant_segments"]
search_result_urls: List[str] = items.find_item_by_type(
research_artifacts_path, "topic_relevant_segments"
)["search_result_urls"]
all_results: List[Dict[str, Any]] = items.find_item_by_type(
research_artifacts_path, "all_results"
)["all_results"]
yield make_event(
"report_building",
"Building the report...",
)
reporting_artifacts_path: str = build_reporting_artifacts_path(session_key)
report: str = produce_report(
client, task_prompt, format_prompt, topic_relevant_segments
)
items.register_item(reporting_artifacts_path, {"type": "report", "report": report})
# Step 3: Ensure that the report is consistent and formatted correctly in Markdown
yield make_event(
"report_processing",
"Formatting the report...",
deltaQueryCount=1,
)
consistent_report: str = ensure_format_is_respected(
client, task_prompt, format_prompt, report
)
consistent_report += "\n\n"
consistent_report += "---\n"
for search_result_url_index, search_result_url in enumerate(search_result_urls):
result = next(
(result for result in all_results if result["url"] == search_result_url),
None,
)
consistent_report += f" - [[{search_result_url_index}]] [{result['title']}][{search_result_url_index}]\n"
consistent_report += "\n\n"
for search_result_url_index, search_result_url in enumerate(search_result_urls):
consistent_report += f"[{search_result_url_index}]: {search_result_url}\n"
items.register_item(
reporting_artifacts_path,
{"type": "consistent_report", "consistent_report": consistent_report},
)
yield make_event(
"report_done",
"Report completed.",
report=consistent_report,
deltaQueryCount=1,
)
# ERRANDS
def check_if_prompt_is_valid(client: OpenAI, prompt: str) -> bool:
messages = [
{
"role": "system",
"content": "You are a helpful assistant that checks if a prompt is a valid deep information research prompt. A valid prompt is in English and gives a task to research one or more topics and produce a report. Invalid prompts are general language model prompts that ask simple (perhaps even yes or no) questions, ask forexplanations, or attempt to have a conversation. Examples of valid prompts: 'What was the capital of France in 1338?', 'Write a report on stock market situation on during this morning', 'Produce a thorough report on the major event happened in the Christian world on the 21st of April 2025.', 'Produce a report on the differences between the US and European economy health in 2024.', 'What is the short history of the internet?'. Examples of invalid prompts: 'Is the weather in Tokyo good?', 'ppdafsfgr hdafdf', 'Hello, how are you?', 'The following is a code. Can you please explain it to me and then simulate it?'",
},
{
"role": "user",
"content": f"Is the following prompt a valid information research prompt? Respond with 'yes' or 'no'. Do not output any other text.\n\n{prompt}\n\n Reminders: Find out if the above-given prompt is a valid information research prompt. Do not output any other text.",
},
]
return is_output_positive(get_completion(client, messages))
def perform_prompt_decomposition(client: OpenAI, prompt: str) -> List[str]:
messages = [
{
"role": "system",
"content": "You are a helpful assistant that decomposes a prompt into a task to be performed and a format in which the report should be produced. The output should be two prompts separated by a double-newline. The first prompt is the task to be performed, and the second prompt is the format in which the report should be produced. If there is no formatting constraint, output 'No formatting constraint' in the second prompt. Do not output any other text.",
},
{
"role": "user",
"content": f"Decompose the PROMPT into a task to be performed and a format in which the report should be produced. If there is no formatting constraint, output 'No formatting constraint' in the second prompt. Do not output any other text.\n\nEXAMPLE PROMPT:\nWrite a three-chapter report on the differences between the US and European economy health in 2024. The first chapter should be about the US economy health, the second chapter should be about the European economy health, and the third chapter should be about the differences between the two.\n\nEXAMPLE OUTPUT:\nWrite a report on the differences between the US and European economy health in 2024.\n\nThe report should be in the form of a three-chapter report. The first chapter should be about the US economy health, the second chapter should be about the European economy health, and the third chapter should be about the differences between the two.\n\nPROMPT: {prompt}\n\nReminders: The output should be two prompts separated by a double-newline. The first prompt is the task to be performed, and the second prompt is the format in which the report should be produced. If there is no formatting constraint, output 'No formatting constraint' in the second prompt. Do not output any other text.",
},
]
decomposition = get_completion(client, messages).split("\n\n")
if len(decomposition) != 2:
raise ValueError(
f"Failed to perform prompt decomposition; decomposition: {decomposition}"
)
return decomposition
def generate_topics(client: OpenAI, prompt: str) -> List[str]:
messages = [
{
"role": "system",
"content": f"You are a helpful assistant that decomposes a prompt into a list of topics to research. The output should be a list of strings separated by newlines, each representing a topic to research. The topics should be in English and should be specific and focused. Output at most {MAX_TOPICS} topics. Examples:\n\nPrompt: What was the capital of France in 1338?\nThe capital and seat of government of France in 1338\n\nPrompt: Produce a report on the differences between the US and European economy health in 2024\nUS economy health in 2024\nEuropean economy health in 2024\nGeneral differences between the US and European economy health in 2024\n\nPrompt: What is the short history of the internet?:\nThe history of the internet\n\nPrompt: Report on US crude oil prices in relation to Gold prices in 1970-1980\nUS crude oil prices in 1970-1980\nGold prices in 1970-1980\nGold-crude oil correlation in 1970-1980",
},
{
"role": "user",
"content": f"Decompose the following prompt into a list of topics to research:\n\nPrompt: {prompt}\n\nReminders: The output should be a list of strings separated by newlines, each representing a topic to research. The topics should be in English and should be specific and focused. Do not output any other text. Output at most {MAX_TOPICS} topics.",
},
]
completion: str = get_completion(client, messages)
completion_lines: List[str] = completion.split("\n")
ret = [line.strip() for line in completion_lines if line.strip()]
if len(ret) > MAX_TOPICS:
ret = random.sample(ret, MAX_TOPICS)
return ret
def produce_search_phrases(client: OpenAI, prompt: str, topic: str) -> List[str]:
messages = [
{
"role": "system",
"content": f"You are a helpful assistant that produces a list of search phrases for a given topic. The output should be a newline-separated list of short search phrases that can be used in e.g. Google or Bing search engines. Output at most {MAX_SEARCH_PHRASES} search phrases. Examples:\n\nTopic: The capital and seat of government of France in 1338\nSearch phrases: The capital of France in 1338, The seat of government of France in 1338, Government of France in 1338 century, Government of France in the 14th century\n\nTopic: US crude oil prices in relation to Gold prices in 1970-1980\nSearch phrases: US crude oil prices in 1970-1980, Gold prices in 1970-1980, Gold-crude oil correlation in 1970-1980, Gold-crude oil correlation\n\nTopic: {topic}\nSearch phrases:",
},
{
"role": "user",
"content": f"Produce a list of search phrases for the following topic:\n\nPrompt (added for context): {prompt}\n\nTopic: {topic}\n\nReminders: The output should be a list of search phrases for the given topic separated by newlines. The search phrases should be in English and should be specific and focused. Output at most {MAX_SEARCH_PHRASES} search phrases. Do not output any other text.",
},
]
completion: str = get_completion(client, messages)
completion_lines: List[str] = completion.split("\n")
ret = [line.strip() for line in completion_lines if line.strip()]
if len(ret) > MAX_SEARCH_PHRASES:
ret = random.sample(ret, MAX_SEARCH_PHRASES)
return ret
def perform_search(
client: TavilyClient, search_phrase: str, research_artifacts_path: str
) -> List[Dict[str, Any]]:
search_response: Dict[str, Any] = client.search(
search_phrase, include_raw_content=True
)
items.register_item(
research_artifacts_path,
{
"type": "search_response_raw",
"search_phrase": search_phrase,
"response": search_response,
},
)
filtered_results: List[Dict[str, Any]] = [
result
for result in search_response["results"]
if result["raw_content"] is not None and result["raw_content"].strip() != ""
]
items.register_item(
research_artifacts_path,
{
"type": "search_response_filtered",
"search_phrase": search_phrase,
"response": filtered_results,
},
)
return filtered_results
def find_relevant_segments(
client: OpenAI,
prompt: str,
topic: str,
search_result: str,
search_result_url_index: int,
) -> List[str]:
messages = [
{
"role": "system",
"content": "You are a helpful assistant that finds relevant paragraphs in a given search result. The output should be a double-newline-separated list of relevant paragraphs. A paragraph can be a couple of sentences to dozens of sentences if they are really relevant. If there are no relevant paragraphs, just output an empty line or two and stop the generation. Do not output any other text.",
},
{
"role": "user",
"content": f"Find the sentences or paragraphs relevant to the following prompt in the following search result:\n\nSearch result: {search_result}\n\nPrompt (added for context): {prompt}\n\nTopic: {topic}\n\nReminders: The output should be a list of relevant paragraphs for the given topic separated by double-newlines. The relevant paragraphs should be in English and should be genuinely relevant to the prompt. Do not output any other text.",
},
]
ret = get_completion(client, messages).split("\n")
ret = [line.strip() for line in ret if line.strip()]
ret = [f"[[{search_result_url_index}]] {paragraph}" for paragraph in ret]
return ret
def produce_report(
client: OpenAI,
prompt: str,
format_prompt: str,
topic_relevant_segments: dict[str, List[str]],
) -> str:
topic_relevant_segments_str: str = "\n".join(
[
f"Topic: {topic}\n{'\n'.join(segments)}"
for topic, segments in topic_relevant_segments.items()
]
)
messages = [
{
"role": "system",
"content": "You are a helpful assistant that produces a report based on the aggregated per-topic relevant paragraphs while citing sources. The output should be a report in Markdown format. The report should be self-consistent and formatted correctly in Markdown. Do not output any other text.",
},
{
"role": "user",
"content": f"Produce a report based on the following aggregated per-topic relevant paragraphs. Each paragraph contains an index of a source. Make sure to refer to this index in the form [[index]] every time you rely on the information from the source. Respect the format prompt. Do not output any other text.\n\nReport prompt: {prompt}\n\nTopic relevant paragraphs: {topic_relevant_segments_str}\n\nFormat prompt: {format_prompt}\n\nReminders: The output should be a report in Markdown format. The report should be formatted correctly according to the Format prompt in Markdown. Every single mention of an information stemming from one of the sources should be accompanied by the source index in the form [[index]] (or [[index1,index2,...]]) within or after the statement of the information. A list of the source URLs to correspond to the indices will be provided separately -- do not attempt to output it. Do not output any other text.",
},
]
return get_completion(client, messages).strip()
def ensure_format_is_respected(
client: OpenAI, prompt: str, format_prompt: str, report: str
) -> str:
messages = [
{
"role": "system",
"content": "You are a helpful assistant that ensures that a report is properly formatted. The output should be a report in Markdown format and follow the format prompt. The report should be formatted correctly in Markdown. Note that double horizontal rule (.e.g ==== etc.) are not supported in official Markdown. Do not output any other text.",
},
{
"role": "user",
"content": f"Ensure that the following report is properly formatted according to the format prompt. Do not output the Markdown output as code (i.e. enclosed in ```) -- just output the Markdown. Do not remove any references in the form [[index]] -- keep them in the text! The list of sources will be provided separately.\n\nReport: {report}\n\nFormat prompt: {format_prompt}\n\nReminders: The output should be a report in Markdown format. The report should be self-consistent and formatted correctly in Markdown. Do not output the Markdown output as code (i.e. enclosed in ```) -- just output the Markdown. Do not remove any references in the form [[index]] -- keep them in the text! The list of sources will be provided separately. Do not output any other text.",
},
]
return get_completion(client, messages).strip()
|