PaperQA2

repository·main·Indexed 27 days ago

https://github.com/future-house/paper-qa

A high-accuracy agentic retrieval augmented generation (RAG) package optimized for scientific literature. PaperQA2 supports querying PDFs, text files, Microsoft Office documents, and source code to provide grounded responses with in-text citations. It features metadata enrichment via Crossref and Semantic Scholar, LLM-based re-ranking, and a CLI for indexing and querying research paper repositories. The ecosystem includes specialized PDF readers such as paper-qa-docling, paper-qa-nemotron, paper-qa-pymupdf, and paper-qa-pypdf.

Tokens
18.4K
Snippets
44
Records
91
Agent score
92%

What's inside paper-qa

  1. Overview of PaperQA2 features and capabilities

    main

    PaperQA2 is an agentic RAG (Retrieval-Augmented Generation) model designed for scientific papers. Key features include:

    • Grounded Responses: Provides answers with in-text citations.
    • Advanced RAG: Uses document metadata-awareness in embeddings, LLM-based re-ranking, and Contextual Summarization (RCS).
    • Agentic Workflows: Supports iterative query refinement and answer generation via language agents.
    • Metadata Enrichment: Automatically fetches citation and journal quality data from providers like Semantic Scholar and Crossref.
    • Search Engine: Includes a full-text search engine for local PDF/text repositories.
    • Extensibility: Supports any LiteLLM-compatible model or embedding provider.
  2. Quickstart with PaperQA2 CLI

    main

    You can quickly perform RAG (Retrieval Augmented Generation) on a folder of research papers using the pqa command-line interface. This process automatically handles metadata extraction (including citation counts and retraction checks), parses PDFs, caches them into a full-text search index, and uses an LLM agent to answer questions.

    Follow these steps to set up a local directory of papers and ask a question:

    pip install paper-qa
    mkdir my_papers
    curl -o my_papers/PaperQA2.pdf https://arxiv.org/pdf/2409.13740
    cd my_papers
    pqa ask 'What is PaperQA2?'
  3. Install paper-qa-nemotron

    main

    Install the paper-qa-nemotron package or include it as an extra in the main paper-qa installation. If you intend to use nemotron-parse hosted on AWS SageMaker, install the sagemaker extra.

    # Install with paper-qa extras
    pip install paper-qa[nemotron]
    
    # Or install the package directly
    pip install paper-qa-nemotron
    
    # For AWS SageMaker support
    pip install paper-qa-nemotron[sagemaker]
    pip install paper-qa[nemotron]
    # Or
    pip install paper-qa-nemotron
    
    # If you want to prompt nemotron-parse hosted on AWS SageMaker:
    pip install paper-qa-nemotron[sagemaker]
  4. Reproduce LitQA2 evaluation results

    main

    To reproduce results from the research papers, use the question IDs and paper DOIs specified in docs/2024-10-16_litqa2-splits.json5.

    Specific papers for reproduction:

    • skarlinski2024language: Supports train and eval splits.
    • narayanan2024aviarytraininglanguageagents: Supports train, eval, and test splits.
  5. Configure rate limits in PaperQA2

    main

    If you encounter rate limits (e.g., OpenAI Tier 1), you can use pre-built settings or manual rate limit strings.

    Using pre-built tier settings:

    pqa --settings 'tier1_limits' ask 'What is PaperQA2?'

    Using manual rate limit strings via CLI: Use the --summary_llm_config flag with a JSON string matching the limits specification.

    pqa --summary_llm_config '{"rate_limit": {"gpt-4o-2024-11-20": "30000 per 1 minute"}}' ask 'What is PaperQA2?'

    Using manual rate limit strings via Python API: Pass a Settings object to the ask function.

    from paperqa import Settings, ask
    
    answer_response = ask(
        "What is PaperQA2?",
        settings=Settings(
            llm_config={"rate_limit": {"gpt-4o-2024-11-20": "30000 per 1 minute"}},
            summary_llm_config={"rate_limit": {"gpt-4o-2024-11-20": "30000 per 1 minute"}},
        ),
    )
  6. Query clinical trials using PaperQA2

    main

    PaperQA2 supports querying clinical trials via the clinical_trials_search tool, which retrieves data from the clinicaltrials.gov API. As of January 2025, this tool is not enabled by default.

    You can query clinical trials exclusively (without using local documents) by using the search_only_clinical_trials setting name.

    from paperqa import Settings, agent_query
    
    answer_response = await agent_query(
        query="What drugs have been found to effectively treat Ulcerative Colitis?",
        settings=Settings.from_name("search_only_clinical_trials"),
    )
    
    print(answer_response.session.answer)
  7. Enable clinical trials search in custom Settings

    main

    To combine clinical trial searches with your own documents, add clinical_trials_search to the tool_names list within the agent configuration of your Settings object. You can use DEFAULT_TOOL_NAMES from paperqa.agents.tools as a base.

    from pathlib import Path
    from paperqa import Settings, agent_query
    from paperqa.agents.tools import DEFAULT_TOOL_NAMES
    
    # Query using standard tools + clinical_trials and a local paper directory
    answer_response = await agent_query(
        query="What drugs have been found to effectively treat Ulcerative Colitis?",
        settings=Settings(
            paper_directory="my_papers",
            agent={"tool_names": DEFAULT_TOOL_NAMES + ["clinical_trials_search"]},
        ),
    )
    
    print(answer_response.session.formatted_answer)
  8. Use the PaperQA2 CLI to ask questions

    main

    The fastest way to test PaperQA2 is via the pqa CLI. Navigate to a directory containing PDF papers and use the ask command to query them. PaperQA2 will index the local files (using Crossref and Semantic Scholar for metadata), chunk the evidence, and generate an answer. Subsequent queries in the same directory will skip indexing if no changes are detected.

    To run with a specific parameter like temperature:

    pqa --temperature 0.5 ask 'What is PaperQA2?'
    pqa ask 'What is PaperQA2?'