STORM (Synthesis of Topic Outlines through Retrieval and Multi-perspective Question Asking)

repository·main·Indexed 12 days ago

https://github.com/stanford-oval/storm

An LLM-based system that automates the research and writing process to create Wikipedia-like articles with citations. It features a pre-writing stage for research and outline generation and a writing stage for final article production. The system includes Co-STORM, a collaborative version enabling human-AI interaction via a discourse protocol and dynamic mind maps. It supports various language models via litellm, VLLM, TGI, and Together.ai, and allows grounding on custom corpora using VectorRM.

Tokens
3.7K
Snippets
6
Records
13
Agent score
97%

What's inside STORM

  1. How STORM and Co-STORM work

    main

    STORM is an LLM system designed to write Wikipedia-like articles from scratch using Internet research. It operates in two main stages:

    1. Pre-writing stage: Conducts Internet-based research to collect references and generates an outline.
    2. Writing stage: Uses the outline and references to generate a full-length article with citations.

    To ensure high-quality research, STORM uses two strategies for question asking:

    • Perspective-Guided Question Asking: Discovers different perspectives by surveying existing articles on similar topics.
    • Simulated Conversation: Simulates a dialogue between a Wikipedia writer and a topic expert grounded in Internet sources to refine understanding and generate follow-up questions.

    Co-STORM (Collaborative STORM) extends this by enabling human-AI collaboration through a collaborative discourse protocol. It involves three participants:

    • Co-STORM LLM experts: Agents that generate grounded answers or raise follow-up questions.
    • Moderator: An agent that generates thought-provoking questions based on retrieved information.
    • Human user: A user who can observe the discourse or actively inject utterances to steer the discussion.

    Co-STORM also maintains a dynamic mind map to organize information into a hierarchical concept structure, helping build a shared conceptual space between the human and the system.

  2. Configure language models for STORM

    main

    STORM is a modular LLM system where different components can be powered by different models to balance cost and quality. When configuring your pipeline, consider the following best practices:

    • conv_simulator_lm: Use a cheaper/faster model. This component is responsible for splitting queries and synthesizing answers within the conversation.
    • article_gen_lm: Use a more powerful model. This component generates the final verifiable text with citations.

    For advanced configurations, refer to the STORMWikiRunnerArguments class.

  3. Configure API keys via secrets.toml

    main

    To simplify setup, create a secrets.toml file in your project's root directory to manage API keys for language models, retrievers, and encoders.

    # Language model configurations
    OPENAI_API_KEY="your_openai_api_key"
    OPENAI_API_TYPE="openai" # or "azure"
    # If Azure:
    # AZURE_API_BASE="your_azure_api_base_url"
    # AZURE_API_VERSION="your_azure_api_version"
    
    # Retriever configurations
    BING_SEARCH_API_KEY="your_bing_search_api_key"
    
    # Encoder configurations
    ENCODER_API_TYPE="openai"
  4. Run STORM with a custom corpus using VectorRM

    main

    By default, STORM uses the internet for grounding. To use your own data, use VectorRM to ground STORM on a custom corpus.

    1. Prepare your CSV corpus

    Your documents must be in a single CSV file with the following columns:

    • content: (Required) The main text of the document.
    • url: (Required) A unique identifier for the document. Ensure all documents have unique URLs.
    • title: (Optional) The title of the document.
    • description: (Optional) A description of the document.

    2. Configure API Keys

    • Set up your OpenAI API key.
    • If using a Qdrant cloud server for the vector store, set QDRANT_API_KEY in secrets.toml.

    3. Execution Modes

    Offline Mode

    Use this to save the vector store locally:

    python examples/storm_examples/run_storm_wiki_gpt_with_VectorRM.py \
        --output-dir $OUTPUT_DIR \
        --vector-db-mode offline \
        --offline-vector-db-dir $OFFLINE_VECTOR_DB_DIR \
        --csv-file-path $CSV_FILE_PATH \
        --device $DEVICE_FOR_EMBEDDING(mps, cuda, cpu) \
        --do-research \
        --do-generate-outline \
        --do-generate-article \
        --do-polish-article

    Online Mode

    Use this to connect to a Qdrant server:

    python examples/storm_examples/run_storm_wiki_gpt_with_VectorRM.py \
        --output-dir $OUTPUT_DIR \
        --vector-db-mode online \
        --online-vector-db-url $ONLINE_VECTOR_DB_URL \
        --csv-file-path $CSV_FILE_PATH \
        --device $DEVICE_FOR_EMBEDDING(mps, cuda, cpu) \
        --do-research \
        --do-generate-outline \
        --do-generate-article \
        --do-polish-article
  5. Use the Co-STORM engine for collaborative knowledge curation

    main

    Co-STORM allows for collaborative discourse between the system and the user. It uses a multi-agent LLM paradigm including experts and a moderator.

    To use Co-STORM, initialize a CoStormRunner with CollaborativeStormLMConfigs, a RunnerArgument object, a LoggingWrapper, and a retrieval module (e.g., BingSearch).

    Workflow:

    1. Call warm_start() to build a shared conceptual space.
    2. Call step() to observe a conversation turn, or step(user_utterance="...") to inject user input and steer the conversation.
    3. Call knowledge_base.reorganize() and generate_report() to produce the final article.
    from knowledge_storm.collaborative_storm.engine import CollaborativeStormLMConfigs, RunnerArgument, CoStormRunner
    from knowledge_storm.lm import LitellmModel
    from knowledge_storm.logging_wrapper import LoggingWrapper
    from knowledge_storm.rm import BingSearch
    
    # 1. Setup Configs and Models
    lm_config = CollaborativeStormLMConfigs()
    # (Configure various LMs using set_question_answering_lm, set_discourse_manage_lm, etc.)
    
    # 2. Setup Runner
    runner_argument = RunnerArgument(topic='Topic Name', ...)
    logging_wrapper = LoggingWrapper(lm_config)
    bing_rm = BingSearch(bing_search_api_key=os.environ.get("BING_SEARCH_API_KEY"), k=runner_argument.retrieve_top_k)
    costorm_runner = CoStormRunner(lm_config=lm_config, runner_argument=runner_argument, logging_wrapper=logging_wrapper, rm=bing_rm)
    
    # 3. Interactive Loop
    costorm_runner.warm_start()
    conv_turn = costorm_runner.step() # Observe
    costorm_runner.step(user_utterance="Tell me more about X") # Steer
    
    # 4. Generate Result
    costorm_runner.knowledge_base.reorganize()
    article = costorm_runner.generate_report()
    print(article)
  6. Run STORM with open-weight models via VLLM

    main

    You can run STORM using open-weight models hosted on a VLLM server. This allows you to use models like Mistral-7B-Instruct-v0.2 instead of closed-source APIs.

    To use this method:

    1. Set up a VLLM server running your desired model.
    2. Execute the run_storm_wiki_mistral.py script from the repository root, providing the server's URL and port.

    STORM is also compatible with TGI (Text Generation Inference) servers or Together.ai endpoints.

    python examples/storm_examples/run_storm_wiki_mistral.py \
       --url $URL \
       --port $PORT \
       --output-dir $OUTPUT_DIR \
       --retriever you \
       --do-research \
       --do-generate-outline \
       --do-generate-article \
       --do-polish-article
  7. Install the knowledge-storm library

    main

    You can install the library directly via pip for standard usage:

    pip install knowledge-storm

    To install from source to allow direct modifications to the STORM engine, follow these steps:

    1. Clone the repository:
      git clone https://github.com/stanford-oval/storm.git
      cd storm
    2. Set up a Conda environment and install dependencies:
      conda create -n storm python=3.11
      conda activate storm
      pip install -r requirements.txt
    pip install knowledge-storm
  8. Setup the STORM Minimal User Interface

    main

    To run the minimal user interface for STORMWikiRunner, follow these steps:

    1. Prerequisites: Ensure knowledge-storm is installed or the source code is correctly set up.
    2. Install Dependencies: Install the required Python packages using:
      pip install -r requirements.txt
    3. Configure API Keys: Follow the main repository instructions to set up API keys. Specifically, create a copy of secrets.toml and place it in the .streamlit/ directory.
    4. Launch the UI: Run the Streamlit application:
      streamlit run storm.py

    Note: The UI will automatically create a DEMO_WORKING_DIR directory in your current working directory to store generated outputs.

    pip install -r requirements.txt
    streamlit run storm.py
  9. Run STORM or Co-STORM via CLI examples

    main

    You can run pre-configured example scripts from the examples/ directory. Ensure you have set up your secrets.toml first.

    Run STORM (GPT models):

    python examples/storm_examples/run_storm_wiki_gpt.py \
        --output-dir $OUTPUT_DIR \
        --retriever bing \
        --do-research \
        --do-generate-outline \
        --do-generate-article \
        --do-polish-article

    Run Co-STORM (GPT models):

    python examples/costorm_examples/run_costorm_gpt.py \
        --output-dir $OUTPUT_DIR \
        --retriever bing
  10. Quick test with Kaggle arXiv Paper Abstracts

    main

    You can quickly test STORM's custom corpus capabilities using the Kaggle arXiv Paper Abstracts dataset.

    1. Download and Process: Download arxiv_data_210930-054931.csv from Kaggle and use the helper script to format it for STORM:

      python examples/storm_examples/helper/process_kaggle_arxiv_abstract_dataset.py --input-path $PATH_TO_THE_DOWNLOADED_FILE --output-path $PATH_TO_THE_PROCESSED_CSV
    2. Run STORM: Run the script using the processed CSV. You can input a topic like "The progress of multimodal models in computer vision".

      python examples/storm_examples/run_storm_wiki_gpt_with_VectorRM.py \
          --output-dir $OUTPUT_DIR \
          --vector-db-mode offline \
          --offline-vector-db-dir $OFFLINE_VECTOR_DB_DIR \
          --csv-file-path $PATH_TO_THE_PROCESSED_CSV \
          --device $DEVICE_FOR_EMBEDDING(mps, cuda, cpu) \
          --do-research \
          --do-generate-outline \
          --do-generate-article \
          --do-polish-article
    3. Fastest Method: Alternatively, download a pre-embedded vector store to skip the embedding step:

      python examples/storm_examples/run_storm_wiki_gpt_with_VectorRM.py \
          --output-dir $OUTPUT_DIR \
          --vector-db-mode offline \
          --offline-vector-db-dir $DOWNLOADED_VECTOR_DB_DR \
          --do-research \
          --do-generate-outline \
          --do-generate-article \
          --do-polish-article
  11. Use the STORM engine for automated knowledge curation

    main

    STORM (Synthesis of Topic Outlines through Retrieval and Multi-perspective Question Asking) is a knowledge curation engine that automates research by simulating conversations to collect information, generating outlines, and writing articles.

    To use STORM, you must configure a STORMWikiLMConfigs object with different language models (it is recommended to use faster models for simulation and more powerful models for article generation) and a retrieval module (RM) like YouRM or BingSearch. The engine is controlled via the STORMWikiRunner class.

    import os
    from knowledge_storm import STORMWikiRunnerArguments, STORMWikiRunner, STORMWikiLMConfigs
    from knowledge_storm.lm import LitellmModel
    from knowledge_storm.rm import YouRM
    
    # 1. Configure Language Models
    lm_configs = STORMWikiLMConfigs()
    openai_kwargs = {'api_key': os.getenv("OPENAI_API_KEY"), 'temperature': 1.0, 'top_p': 0.9}
    
    gpt_35 = LitellmModel(model='gpt-3.5-turbo', max_tokens=500, **openai_kwargs)
    gpt_4 = LitellmModel(model='gpt-4o', max_tokens=3000, **openai_kwargs)
    
    lm_configs.set_conv_simulator_lm(gpt_35)
    lm_configs.set_question_asker_lm(gpt_35)
    lm_configs.set_outline_gen_lm(gpt_4)
    lm_configs.set_article_gen_lm(gpt_4)
    lm_configs.set_article_polish_lm(gpt_4)
    
    # 2. Configure Retrieval Module
    engine_args = STORMWikiRunnerArguments(...)
    rm = YouRM(ydc_api_key=os.getenv('YDC_API_KEY'), k=engine_args.search_top_k)
    
    # 3. Initialize and Run
    runner = STORMWikiRunner(engine_args, lm_configs, rm)
    topic = 'Your Research Topic'
    runner.run(
        topic=topic,
        do_research=True,
        do_generate_outline=True,
        do_generate_article=True,
        do_polish_article=True,
    )
    runner.post_run()
    runner.summary()
  12. Customize the STORMWikiRunner in the Minimal UI

    main

    The minimal user interface is powered by STORMWikiRunner. You can customize its behavior (such as changing STORMWikiRunnerArguments, STORMWikiLMConfigs, or the retrieval model) by modifying the initialization logic in demo_util.py.

    Locate the set_storm_runner() function in demo_util.py to apply your custom configurations.