Setup

The quickstart guide which follows below assumes that you have installed R2R locally via pip install r2r and properly setup your environment, as described here.

If you would prefer to do the heavy lifting with Docker instead then refer to the Docker installation instructions and then proceed to the client-server cookbook.

Hello R2R

R2R supports configurable vector search and RAG right out of the box, as the example below shows:


from r2r import Document, GenerationConfig, R2R

app = R2R() # You may pass a custom configuration to `R2R`

app.ingest_documents(
    [
        Document(
            type="txt",
            data="John is a person that works at Google.",
            metadata={},
        )
    ]
)

rag_results = app.rag(
    "Who is john", GenerationConfig(model="gpt-3.5-turbo", temperature=0.0)
)
print(f"Search Results:\n{rag_results.search_results}")
print(f"Completion:\n{rag_results.completion}")

# RAG Results:
# Search Results:
# AggregateSearchResult(vector_search_results=[VectorSearchResult(id=2d71e689-0a0e-5491-a50b-4ecb9494c832, score=0.6848798582029441, metadata={'text': 'John is a person that works at Google.', 'version': 'v0', 'chunk_order': 0, 'document_id': 'ed76b6ee-dd80-5172-9263-919d493b439a', 'extraction_id': '1ba494d7-cb2f-5f0e-9f64-76c31da11381', 'associatedQuery': 'Who is john'})], kg_search_results=None)
# Completion:
# ChatCompletion(id='chatcmpl-9g0HnjGjyWDLADe7E2EvLWa35cMkB', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='John is a person that works at Google [1].', role='assistant', function_call=None, tool_calls=None))], created=1719797903, model='gpt-3.5-turbo-0125', object='chat.completion', service_tier=None, system_fingerprint=None, usage=CompletionUsage(completion_tokens=11, prompt_tokens=145, total_tokens=156))

This guide will demonstrate document ingestion, management, search, and advanced RAG functionalities. For customization, refer to r2r/examples/quickstart.py.

Document Ingestion and Management

R2R efficiently handles diverse document types using PostgreSQL with pgvector, combining relational data management with vector search capabilities. This approach enables seamless ingestion, storage, and retrieval of multimodal data, while supporting flexible document management and user permissions. Expand below to dive deeper:

R2R’s document ingestion and management system efficiently handles diverse file types, offering customizable parsing, chunking, and embedding processes. The flexible architecture allows for easy integration with existing workflows and supports custom extensions to meet specific project requirements. Moreover, the R2R system provides comprehensive document management, which you can read more about in the cookbook here.

R2R offers powerful search capabilities, including vector search, hybrid search, and knowledge graph-enhanced search. These features allow for more accurate and contextually relevant information retrieval.

Behind the scenes, R2R’s RetrievalService handles these search requests. The search method accepts VectorSearchSettings and KGSearchSettings to customize the search behavior:

async def search(
    self,
    query: str,
    vector_search_settings: VectorSearchSettings = VectorSearchSettings(),
    kg_search_settings: KGSearchSettings = KGSearchSettings(),
):
    # ... implementation details ...

This flexible architecture allows for combining different search strategies to provide the most relevant and comprehensive results for your queries.

Retrieval-Augmented Generation (RAG)

R2R is built around a comprehensive Retrieval-Augmented Generation (RAG) engine, allowing you to generate contextually relevant responses based on your ingested documents. The RAG process combines the search functionality with language model generation to produce more accurate and informative answers.

Behind the scenes, R2R’s RetrievalService handles RAG requests, combining the power of vector search, optional knowledge graph integration, and language model generation. The flexible architecture allows for easy customization and extension of the RAG pipeline to meet diverse requirements.

User Management

R2R provides powerful user management capabilities, allowing you to track and manage documents on a per-user basis. This section covers key user management features.

These user management features provide granular control over user data, allowing for efficient organization and management of documents on a per-user basis within the R2R system. Read more about these features in the document management cookbook here.

Observability and Analytics

R2R provides robust observability and analytics features, allowing you to monitor system performance, track usage patterns, and gain insights into your RAG application’s behavior.

These observability and analytics features provide valuable insights into your R2R application’s performance and usage, enabling data-driven optimization and decision-making.

Command Execution and Output

Here are some example commands along with their outputs:

Quickstart GIF