Skip to main content

Introduction

R2R’s LLMProvider supports multiple third-party Language Model (LLM) providers, offering flexibility in choosing and switching between different models based on your specific requirements. This guide provides an in-depth look at configuring and using various LLM providers within the R2R framework.

Architecture Overview

R2R’s LLM system is built on a flexible provider model:
  1. LLM Provider: An abstract base class that defines the common interface for all LLM providers.
  2. Specific LLM Providers: Concrete implementations for different LLM services (e.g., OpenAI, LiteLLM).
These providers work in tandem to ensure flexible and efficient language model integration.

Providers

LiteLLM Provider (Default)

The default LiteLLMProvider offers a unified interface for multiple LLM services. Key features:
  • Support for OpenAI, Anthropic, Vertex AI, HuggingFace, Azure OpenAI, Ollama, Together AI, and Openrouter
  • Consistent API across different LLM providers
  • Easy switching between models

OpenAI Provider

The OpenAILLM class provides direct integration with OpenAI’s models. Key features:
  • Direct access to OpenAI’s API
  • Support for the latest OpenAI models
  • Fine-grained control over model parameters

Local Models

Support for running models locally using Ollama or other local inference engines, through LiteLLM. Key features:
  • Privacy-preserving local inference
  • Customizable model selection
  • Reduced latency for certain use cases

Configuration

LLM Configuration

Update the completions section in your r2r.toml file:
The provided generation_config is used to establish the default generation parameters for your deployment. These settings can be overridden at runtime, offering flexibility in your application. You can adjust parameters:
  1. At the application level, by modifying the R2R configuration
  2. For individual requests, by passing custom parameters to the rag or get_completion methods
  3. Through API calls, by including specific parameters in your request payload
This allows you to fine-tune the behavior of your language model interactions on a per-use basis while maintaining a consistent baseline configuration.

Security Best Practices

  1. API Key Management: Use environment variables or secure key management solutions for API keys.
  2. Rate Limiting: Implement rate limiting to prevent abuse of LLM endpoints.
  3. Input Validation: Sanitize and validate all inputs before passing them to LLMs.
  4. Output Filtering: Implement content filtering for LLM outputs to prevent inappropriate content.
  5. Monitoring: Regularly monitor LLM usage and outputs for anomalies or misuse.

Custom LLM Providers in R2R

LLM Provider Structure

The LLM system in R2R is built on two main components:
  1. LLMConfig: A configuration class for LLM providers.
  2. LLMProvider: An abstract base class that defines the interface for all LLM providers.

LLMConfig

The LLMConfig class is used to configure LLM providers:

LLMProvider

The LLMProvider is an abstract base class that defines the common interface for all LLM providers:

Creating a Custom LLM Provider

To create a custom LLM provider, follow these steps:
  1. Create a new class that inherits from LLMProvider.
  2. Implement the required methods: get_completion and get_completion_stream.
  3. (Optional) Add any additional methods or attributes specific to your provider.
Here’s an example of a custom LLM provider:

Registering and Using the Custom Provider

To use your custom LLM provider in R2R:
  1. Update the LLMConfig class to include your custom provider:
  1. Update your R2R configuration to use the custom provider:
  1. In your R2R application, register the custom provider:
Now you can use your custom LLM provider seamlessly within your R2R application:
By following this structure, you can integrate any LLM or service into R2R, maintaining consistency with the existing system while adding custom functionality as needed.

Prompt Engineering

R2R supports advanced prompt engineering techniques:
  1. Template Management: Create and manage reusable prompt templates.
  2. Dynamic Prompts: Generate prompts dynamically based on context or user input.
  3. Few-shot Learning: Incorporate examples in your prompts for better results.

Troubleshooting

Common issues and solutions:
  1. API Key Errors: Ensure your API keys are correctly set and have the necessary permissions.
  2. Rate Limiting: Implement exponential backoff for retries on rate limit errors.
  3. Context Length Errors: Be mindful of the maximum context length for your chosen model.
  4. Model Availability: Ensure the requested model is available and properly configured.

Performance Considerations

  1. Batching: Use batching for multiple, similar requests to improve throughput.
  2. Streaming: Utilize streaming for long-form content generation to improve perceived latency.
  3. Model Selection: Balance between model capability and inference speed based on your use case.

Server Configuration

The R2RConfig class handles the configuration of various components, including LLMs. Here’s a simplified version:
This configuration system allows for flexible setup of LLM providers and their default parameters.

Conclusion

R2R’s LLM system provides a flexible and powerful foundation for integrating various language models into your applications. By understanding the available providers, configuration options, and best practices, you can effectively leverage LLMs to enhance your R2R-based projects. For further customization and advanced use cases, refer to the R2R API Documentation and configuration guide.