Introduction
Configuration, Builder, and Factories
Overview
R2R uses a flexible and modular assembly process to create customizable applications. This process involves three key components: R2RConfig
, R2RBuilder
, and R2RFactory*
. These components work together to allow developers to easily customize and extend R2R’s functionality while maintaining a consistent and robust structure.
Key Components
Configuration
The R2R configuration system allows developers to specify settings for various components of the application. Key features include:
- JSON-based: Configurations are typically defined in JSON format.
- Hierarchical Structure: Settings are organized into sections for different components (e.g., embedding, vector database, logging).
- Default Values: A base configuration is provided, which can be overridden as needed.
R2RBuilder
The R2RBuilder
is the central component for assembling R2R applications. It uses the builder pattern to allow for flexible configuration and component overrides. Key features include:
- Fluent Interface: Allows chaining of configuration methods.
- Component Overrides: Provides methods to override default providers, pipes, and pipelines.
- Factory Customization: Supports custom factory implementations.
Factories
R2R uses three main types of factories to create the components of an application:
- R2RProviderFactory: Creates provider instances (e.g., VectorDBProvider, EmbeddingProvider).
- R2RPipeFactory: Creates individual pipes used in pipelines.
- R2RPipelineFactory: Creates complete pipelines by assembling pipes.
These factories can be customized to change how components are created and configured.
Assembly Process
The following diagram illustrates the R2R assembly process:
This diagram shows how the developer interacts with the configuration and builder, and how the factories are used to create the final R2RCore instance.
Using the Assembly Components
Configuration
To create a custom configuration:
from r2r import R2RConfig
config = R2RConfig.from_json("path/to/my_config.json")
R2RBuilder
To build an R2R application with custom components:
from r2r import R2RBuilder
app = (
R2RBuilder(config)
.with_vector_db_provider(my_custom_vector_db)
.with_embedding_provider(my_custom_embedding)
.build()
)
Custom Factories
To use a custom factory:
class MyCustomPipeFactory(R2RPipeFactory):
def create_vector_search_pipe(self, *args, **kwargs):
# Custom implementation
app = (
R2RBuilder(config)
.with_pipe_factory(MyCustomPipeFactory)
.build()
)
Strengths of R2R’s Assembly Approach
R2R’s assembly approach offers significant advantages in terms of flexibility, modularity, configurability, extensibility, and separation of concerns, allowing developers to easily adapt applications to various requirements and extend functionality as needed. When working with this system, it’s crucial to understand default configurations, utilize environment variables for sensitive data, validate custom components against R2R interfaces, maintain clear documentation of customizations, and thoroughly test assembled applications. By adhering to these best practices, developers can fully leverage the power and flexibility of R2R’s assembly process while ensuring robust and maintainable applications.
Conclusion
R2R’s assembly process, combining Configuration, R2RBuilder, and Factories, provides a powerful and flexible way to create customized retrieval and response systems. By understanding these components and their interactions, developers can leverage R2R’s robust foundation while tailoring applications to their specific needs.
For more detailed information on specific components, refer to the following pages:
Was this page helpful?