Universal Deep Research (UDR)

repository·main·Indexed 19 days ago

https://github.com/nvlabs/universaldeepresearch

A research demonstration prototype that automates deep research by combining user-defined strategies, intelligent web search via Tavily, content analysis, and automated report generation using LLMs. It consists of a FastAPI backend (UDR-B) and a Next.js frontend, featuring endpoints for initiating research workflows (/api/research and /api/research2) and real-time progress updates via Server-Sent Events (SSE).

Tokens
5.4K
Snippets
14
Records
23
Agent score
67%

What's inside Universal Deep Research

  1. Install and set up the UDR-B backend

    main

    The Universal Deep Research Backend (UDR-B) can be installed using an automated setup script or manually.

    1. Clone the repository and enter the backend directory.
    2. Run python3 setup.py. This script handles directory creation (logs/, instances/, mock_instances/), environment configuration, and dependency installation.
    3. Create API key files: tavily_api.txt and either nvdev_api.txt or openai_api.txt containing your keys.
    4. Start the server using ./launch_server.sh.

    Manual Setup

    1. Clone the repository and enter the backend directory.
    2. Create and activate a Python virtual environment.
    3. Install dependencies via pip install -r requirements.txt.
    4. Create directories: mkdir -p logs instances mock_instances.
    5. Copy env.example to .env and configure it.
    6. Create API key files (tavily_api.txt, nvdev_api.txt, or openai_api.txt).
    7. Start the server using ./launch_server.sh.

    The server defaults to http://localhost:8000.

    # Automated Setup
    git clone <repository-url>
    cd backend
    python3 setup.py
    
    # Configure API keys
    echo "your-tavily-api-key" > tavily_api.txt
    echo "your-llm-api-key" > nvdev_api.txt
    
    # Start server
    ./launch_server.sh
  2. Run the Universal Deep Research (UDR) prototype

    main

    To run the full UDR prototype, you must start both the FastAPI backend and the Next.js frontend services. The backend typically runs on port 8000 and the frontend on port 3000.

    1. Start the backend server:
      cd backend
      ./launch_server.sh
    2. Start the frontend development server:
      cd frontend
      npm run dev
    3. Access the application: Open your browser and navigate to http://localhost:3000 (or the port specified by the frontend terminal output).
    cd backend
    ./launch_server.sh
    
    cd frontend
    npm run dev
  3. Install and run the NVR UDR Frontend

    main

    To set up the Next.js frontend for the NVR Universal Deep Research system, follow these steps:

    1. Install dependencies: Run npm install to install all required packages.
    2. Configure environment variables: Copy the example environment file to .env.local and edit it with your specific configuration.
    3. Run the development server: Execute npm run dev to start the server with Turbopack.
    4. Access the app: Open your browser and navigate to http://localhost:3000.

    Note: This frontend requires a compatible backend server running the NVR Universal Deep Research API that supports SSE (Server-Sent Events) for real-time updates.

    npm install
    cp env.example .env.local
    npm run dev
  4. Configure the UDR backend

    main

    The backend is a FastAPI service located in the /backend directory. Detailed setup requires configuring a Python environment and providing necessary API keys.

    Requirements:

    • Python 3.8+
    • API keys for LLM providers (e.g., NVIDIA NGC, OpenAI, etc.)
    • Tavily API key (for web search functionality)

    Refer to backend/README.md for specific environment variable names and server startup commands.

  5. Configure the UDR frontend

    main

    The frontend is a Next.js application located in the /frontend directory.

    Requirements:

    • Node.js 18+

    Refer to frontend/README.md for detailed instructions on installing Node.js dependencies, environment configuration, and starting the development server.

  6. Deploy the NVR UDR Frontend

    main

    The application is a Next.js app and can be deployed to any platform supporting Next.js (e.g., Vercel, Netlify, Railway, or self-hosted).

    Deployment Steps:

    1. Build the application: npm run build.
    2. Start the production server: npm run start.
    3. Ensure all required NEXT_PUBLIC_ environment variables are set in your deployment platform's dashboard or environment configuration.
  7. Configure the Frontend via Environment Variables

    main

    The frontend is configured using environment variables prefixed with NEXT_PUBLIC_. You should create a .env.local file for local development.

    Backend API Configuration

    • NEXT_PUBLIC_BACKEND_BASE_URL: The base URL of your backend server (default: http://localhost).
    • NEXT_PUBLIC_BACKEND_PORT: The port your backend server is running on (default: 8000).
    • NEXT_PUBLIC_API_VERSION: API version to use - v1 or v2 (default: v2).

    Runtime Configuration

    • NEXT_PUBLIC_DRY_RUN: Enable dry run mode - true or false (default: false). Use true during development to avoid actual API calls.
    • NEXT_PUBLIC_ENABLE_V2_API: Enable V2 API - true or false (default: true).

    Frontend Configuration

    • NEXT_PUBLIC_FRONTEND_PORT: The port for the frontend development server (default: 3000).
    • NEXT_PUBLIC_FRONTEND_HOST: The host for the frontend development server (default: localhost).
    # Example for local development with dry run enabled
    NEXT_PUBLIC_BACKEND_BASE_URL=http://localhost
    NEXT_PUBLIC_BACKEND_PORT=8000
    NEXT_PUBLIC_API_VERSION=v2
    NEXT_PUBLIC_DRY_RUN=true
    NEXT_PUBLIC_ENABLE_V2_API=true
  8. Understand the ApplicationState lifecycle

    main

    The ApplicationState type defines the possible states of the Universal Deep Research frontend. It is a discriminated union where the type field determines the current phase of the research process. This state tracks session metadata, timing, and research progress via ResearchEvent objects.

    Possible states include:

    • idle: The initial state before research begins.
    • researching: The active phase where searches and queries are being performed.
    • finalizing: The phase where research results are being processed or synthesized.
    • stopped: The state when a research session has been manually or prematurely halted.
    • error: An error state containing an error string describing the failure.
    • done: The completed state after successful research and finalization.

    Common fields across most states include:

    • sessionKey: A unique identifier for the current research session.
    • researchStartTimestamp / researchEndTimestamp: Timestamps for the research phase.
    • searchCount / queryCount: Counters for the number of searches and queries performed.
    • finalizationStartTimestamp / finalizationEndTimestamp: Timestamps for the finalization phase.
    • events: An array of ResearchEvent objects tracking progress.
    // Example of an error state structure
    const state: ApplicationState = {
      type: 'error',
      error: 'Connection timed out',
      sessionKey: 'abc-123',
      researchStartTimestamp: 1715000000000,
      researchEndTimestamp: 0,
      searchCount: 5,
      queryCount: 3,
      finalizationStartTimestamp: 0,
      finalizationEndTimestamp: 0,
      events: []
    };
  9. Understand the research streaming event format

    main

    The backend streams research progress as newline-delimited JSON (NDJSON). Each line represents a single event containing a session_key and an event object.

    Events are generated by the make_message utility and typically include a type and a description or other metadata. Common event types include:

    • started: Indicates the backend is initializing.
    • generic: Used for progress updates (e.g., "Processing agentic instructions: X of Y").
    • completed: Indicates research and reporting are finished.
    • cancelled: Indicates the research process was interrupted.
    • error: Indicates a failure in the research phase.

    Every event is timestamped using the current ISO format.

  10. Configure UDR-B environment variables

    main

    Configure the backend behavior by editing the .env file in the backend directory. Key configuration groups include:

    • Server: HOST, PORT, LOG_LEVEL.
    • CORS: FRONTEND_URL (set this to your frontend's URL, e.g., http://localhost:3000).
    • Model: DEFAULT_MODEL, LLM_BASE_URL, LLM_API_KEY_FILE.
    • Search: TAVILY_API_KEY_FILE.
    • Research: MAX_TOPICS, MAX_SEARCH_PHRASES, MOCK_DIRECTORY.
    • Logging: LOG_DIR, TRACE_ENABLED.
    # Server Configuration
    HOST=0.0.0.0
    PORT=8000
    LOG_LEVEL=info
    
    # CORS Configuration
    FRONTEND_URL=http://localhost:3000
    
    # Model Configuration
    DEFAULT_MODEL=llama-3.1-nemotron-253b
    LLM_BASE_URL=https://integrate.api.nvidia.com/v1
    LLM_API_KEY_FILE=nvdev_api.txt
    
    # Search Configuration
    TAVILY_API_KEY_FILE=tavily_api.txt
    
    # Research Configuration
    MAX_TOPICS=1
    MAX_SEARCH_PHRASES=1
    MOCK_DIRECTORY=mock_instances/stocks_24th_3_sections
    
    # Logging Configuration
    LOG_DIR=logs
    TRACE_ENABLED=true
  11. Configure LLM providers in clients.py

    main

    To add or modify LLM providers, edit the MODEL_CONFIGS dictionary in clients.py. Each configuration entry requires a base_url, api_type, and a completion_config object containing parameters like model, temperature, top_p, max_tokens, and stream.

    MODEL_CONFIGS = {
        "llama-3.1-8b": {
            "base_url": "https://integrate.api.nvidia.com/v1",
            "api_type": "nvdev",
            "completion_config": {
                "model": "nvdev/meta/llama-3.1-8b-instruct",
                "temperature": 0.2,
                "top_p": 0.7,
                "max_tokens": 2048,
                "stream": True
            }
        },
        # Add more models as needed
    }
  12. Troubleshoot Frontend connection and API issues

    main

    If you encounter issues with the frontend, check the following:

    • Backend Connection Errors: Verify that your backend server is running and that NEXT_PUBLIC_BACKEND_BASE_URL and NEXT_PUBLIC_BACKEND_PORT match your backend's actual address.
    • CORS Errors: Ensure your backend server is configured to allow requests from your frontend's domain.
    • API Version Issues: If you see compatibility errors, toggle between v1 and v2 using the NEXT_PUBLIC_API_VERSION environment variable.

    Tip: Use NEXT_PUBLIC_DRY_RUN=true during development to test the UI without triggering actual research processes.