SciPhi R2R Documentation

repository·main·Indexed 27 days ago

https://github.com/sciphi-ai/r2r

R2R (Retrieval-Augmented Generation to Riches) is an open-source RAG answer engine designed to bridge the gap between local LLM experimentation and scalable production. It features Python and JavaScript SDKs, Agentic RAG for iterative research, knowledge graph entity extraction, and support for custom tool definitions. The system can be deployed in light mode via pip or full mode using Docker with a PostgreSQL backend.

Tokens
32.5K
Snippets
106
Records
168
Agent score
93%

What's inside R2R

  1. Overview of R2R Dashboard Features

    main

    The R2R Dashboard provides several management interfaces:

    • Login: Access the dashboard. By default, R2R instances are hosted on port 7272. Update the URL on the login page if your instance is deployed elsewhere.
    • Documents: Manage uploaded documents and metadata. You can upload, update, download, or delete documents, and view document chunks or PDF previews.
    • Collections: Create and manage sets of documents to be shared.
    • Chat: Stream RAG responses using different models and configurable settings. This interface allows interaction with both RAG Agent and RAG endpoints.
    • Users: Manage users and monitor their interactions.
    • Settings: View and edit the configuration and prompts associated with your R2R deployment.
  2. Overview of R2R capabilities

    main

    R2R is an infrastructure platform for implementing Retrieval-Augmented Generation (RAG). It consists of three main components:

    1. Document Processing: Intelligent processing of PDFs, images, audio, and more.
    2. AI-powered Search and Generation: Fast and accurate document search using semantic and keyword matching, plus automatic relationship extraction for knowledge graphs.
    3. Analytics: Tools to monitor performance, understand usage patterns, and improve the system.

    Developers can integrate R2R via Python and JavaScript SDKs or through a RESTful API.

  3. Understand Agentic RAG modes

    main

    R2R's Agentic RAG (Deep Research) provides two primary operating modes for multi-step reasoning:

    1. RAG Mode (Default): Standard retrieval-augmented generation. It uses semantic/hybrid search, document/chunk retrieval, and optional web search (via Serper or Firecrawl) to provide evidence-based responses with citations.
    2. Research Mode: Advanced mode for deep analysis. It includes all RAG capabilities plus a dedicated reasoning system, critique capabilities to identify biases, and a python_executor for computational analysis.
  4. Understand R2R Hybrid Search modes

    main

    R2R provides three search modes to balance semantic understanding and keyword precision:

    • basic: Performs semantic search only. Best for scenarios where meaning is more important than exact term matching.
    • advanced: Performs hybrid search by default. It combines semantic and full-text search using pre-tuned parameters, making it ideal for most users who want hybrid benefits without manual configuration.
    • custom: Provides full control over search settings. Use this to independently toggle semantic and full-text search or to fine-tune weights and limits.
  5. Explore R2R Documentation

    main

    R2R provides documentation organized into several key sections to help you build with Agentic Retrieval-Augmented Generation (RAG):

    • Introduction: System overviews and guides.
    • Documentation: Getting started, general features, retrieval, and advanced features.
    • API & SDKs: Full API reference and SDK-specific documentation.
    • Cookbooks: Practical recipes for Data Processing and System Operations.
    • Self-Hosting: Instructions for Installation, Configuration, and Deployment.
  6. Understand R2R System Architecture

    main

    R2R uses a modular, service-oriented architecture designed for scalability and flexibility. The system is organized into several layers:

    • API Layer: A RESTful API for handling incoming requests.
    • Core Services: Specialized services including Auth Service (authentication/authorization), Retrieval Service (search and RAG), Ingestion Service (document processing), Graph Builder Service (knowledge graphs), and App Management Service.
    • Orchestration: Uses RabbitMQ as a message queue to manage complex workflows and background jobs.
    • Storage: Utilizes Postgres with pgvector for vector storage, full-text search, and relational data, alongside File Storage (S3 or Postgres) for documents and media.
    • Providers: Pluggable components for Embedding, LLM, Auth, and Ingestion that can be swapped without affecting the core system.
    • R2R Application: A React + Next.js interface for managing documents, searches, and settings.
  7. Optimize Agent performance

    main

    To manage response times and large contexts:

    1. For speed: Use smaller max_tokens_to_sample values, select faster models (e.g., claude-3-haiku), avoid unnecessary tools, and enable stream: True for perceived responsiveness.
    2. For large collections: Use search_settings with filters (e.g., $and, $eq, $gt) to narrow down the document chunks the agent retrieves.
    # Using filters to handle large document collections
    filtered_response = client.retrieval.agent(
        message={"role": "user", "content": "Summarize key points from our AI ethics documentation"},
        search_settings={
            "filters": {
                "$and": [
                    {"document_type": {"$eq": "pdf"}},
                    {"metadata.category": {"$eq": "ethics"}},
                    {"metadata.year": {"$gt": 2023}}
                ]
            },
            "limit": 10
        },
        rag_generation_config={
            "max_tokens_to_sample": 500,
            "stream": True
        },
        mode="rag"
    )
  8. Ingest files and extract entities and relationships

    main

    To build a knowledge graph, you must first ingest a file into R2R and then trigger the extraction process to identify entities and relationships. Once ingested, use the document_id from the ingestion response to call extract. You can then retrieve the extracted data using list_entities and list_relationships.

    import requests
    from r2r import R2RClient
    import tempfile
    import os
    
    # Set up the client
    client = R2RClient("http://localhost:7272")
    
    # Fetch the text file
    url = "https://www.gutenberg.org/cache/epub/7256/pg7256.txt"
    response = requests.get(url)
    
    # Create a temporary file
    temp_dir = tempfile.gettempdir()
    temp_file_path = os.path.join(temp_dir, "gift_of_the_magi.txt")
    with open(temp_file_path, 'w') as temp_file:
        temp_file.write(response.text)
    
    # Ingest the file
    ingest_response = client.documents.create(file_path=temp_file_path)
    document_id = ingest_response["results"]["document_id"]
    
    # Extract entities and relationships
    extract_response = client.documents.extract(document_id)
    
    # View extracted knowledge
    entities = client.documents.list_entities(document_id)
    relationships = client.documents.list_relationships(document_id)
    
    # Clean up the temporary file
    os.unlink(temp_file_path)