R2RBuilder
Learn how to build and customize R2R
Introduction
R2R uses two key components for assembling and customizing applications: the R2RBuilder
and a set of factory classes, R2RFactory*
. These components work together to provide a flexible and intuitive way to construct R2R instances with custom configurations, providers, pipes, and pipelines.
R2RBuilder
The R2RBuilder
is the central component for assembling R2R applications. It employs the Builder pattern for simple application customization.
Key Features
- Flexible Configuration: Supports loading configurations from TOML files or using predefined configurations.
- Component Overrides: Allows overriding default providers, pipes, and pipelines with custom implementations.
- Factory Customization: Supports custom factory implementations for providers, pipes, and pipelines.
- Fluent Interface: Provides a chainable API for easy and readable setup.
Basic Usage
Here’s a simple example of how to use the R2RBuilder:
Factories
R2R uses a set of factory classes to create various components of the system. These factories allow for easy customization and extension of R2R’s functionality.
Main Factory Classes
- R2RProviderFactory: Creates provider instances (e.g., DatabaseProvider, EmbeddingProvider).
- R2RPipeFactory: Creates individual pipes used in pipelines.
- R2RPipelineFactory: Creates complete pipelines by assembling pipes.
Factory Methods
Each factory class contains methods for creating specific components. For example, the R2RPipeFactory
includes methods like:
create_parsing_pipe()
create_embedding_pipe()
create_vector_search_pipe()
create_rag_pipe()
Customizing Factories
You can customize the behavior of R2R by extending these factory classes. Here’s a simplified example of extending the R2RPipeFactory
:
Builder + Factory in action
The R2RBuilder provides methods to override various components of the R2R system:
Provider Overrides
Available provider override methods:
with_auth_provider
with_database_provider
with_embedding_provider
with_eval_provider
with_llm_provider
with_crypto_provider
Pipe Overrides
Available pipe override methods:
with_parsing_pipe
with_embedding_pipe
with_vector_storage_pipe
with_vector_search_pipe
with_rag_pipe
with_streaming_rag_pipe
with_eval_pipe
with_kg_pipe
with_kg_storage_pipe
with_kg_search_pipe
Pipeline Overrides
Available pipeline override methods:
with_ingestion_pipeline
with_search_pipeline
with_rag_pipeline
with_streaming_rag_pipeline
with_eval_pipeline
Factory Overrides
Advanced Usage
For more complex scenarios, you can chain multiple customizations:
This approach allows you to create highly customized R2R instances tailored to your specific needs.
Best Practices
- Configuration Management: Use separate configuration files for different environments (development, staging, production).
- Custom Components: When creating custom providers, pipes, or pipelines, ensure they adhere to the respective interfaces defined in the R2R framework.
- Testability: Create factory methods or builder configurations specifically for testing to easily mock or stub components.
- Logging: Enable appropriate logging in your custom components to aid in debugging and monitoring.
- Error Handling: Implement proper error handling in custom components and provide meaningful error messages.
By leveraging the R2RBuilder and customizing factories, you can create flexible, customized, and powerful R2R applications tailored to your specific use cases and requirements.