Analytics & Observability

Learn how to use analytics and and logging with R2R

Introduction

This guide demonstrates how to leverage R2R’s powerful analytics and logging features. These capabilities allow you to monitor system performance, track usage patterns, and gain valuable insights into your RAG application’s behavior.

The features described in this cookbook are typically restricted to superusers. Ensure you have the necessary permissions before attempting to access these features.

For more information on user roles and permissions, including how to set up and manage superuser accounts, please refer to our User Auth Cookbook.

Setup

Before diving into the authentication features, ensure you have R2R installed and configured as described in the installation guide. For this guide, we’ll use the default configuration. Further, r2r serve must be called to serve R2R in either your local environment or local Docker engine.

Basic Usage

Logging

R2R automatically logs various events and metrics during its operation.

To fetch our logs using the client-server architecture, use the following:

$r2r logs

Expected Output:

1[
2 {
3 'run_id': UUID('27f124ad-6f70-4641-89ab-f346dc9d1c2f'),
4 'run_type': 'rag',
5 'entries': [
6 {'key': 'search_query', 'value': 'Who is Aristotle?'},
7 {'key': 'search_latency', 'value': '0.39'},
8 {'key': 'search_results', 'value': '["{\\"id\\":\\"7ed3a01c-88dc-5a58-a68b-6e5d9f292df2\\",...}"]'},
9 {'key': 'rag_generation_latency', 'value': '3.79'},
10 {'key': 'llm_response', 'value': 'Aristotle (Greek: Ἀριστοτέλης Aristotélēs; 384–322 BC) was...'}
11 ]
12 },
13 # More log entries...
14]

These logs provide detailed information about each operation, including search results, queries, latencies, and LLM responses.

To fetch the logs directly from an instantiated R2R object:

1app = R2R()
2
3# Perform some searches / RAG completions
4# ...
5
6# Get the latest logs
7logs = app.logs()
8print(logs)

Analytics

R2R offers an analytics feature that allows you to aggregate and analyze log data:

The relevant command

$r2r analytics --filters '{"search_latencies": "search_latency"}' --analysis-types '{"search_latencies": ["basic_statistics", "search_latency"]}'

Expected Output:

1{
2 'results': {
3 'filtered_logs': {
4 'search_latencies': [
5 {
6 'timestamp': '2024-06-20 21:29:06',
7 'log_id': UUID('0f28063c-8b87-4934-90dc-4cd84dda5f5c'),
8 'key': 'search_latency',
9 'value': '0.66',
10 'rn': 3
11 },
12 ...
13 ]
14 },
15 'search_latencies': {
16 'Mean': 0.734,
17 'Median': 0.523,
18 'Mode': 0.495,
19 'Standard Deviation': 0.213,
20 'Variance': 0.0453
21 }
22 }
23}

To fetch the analytics directly from an instantiated R2R object:

1from r2r import FilterCriteria, AnalysisTypes
2
3filter_criteria = FilterCriteria(filters={"search_latencies": "search_latency"})
4analysis_types = AnalysisTypes(analysis_types={"search_latencies": ["basic_statistics", "search_latency"]})
5
6analytics_results = app.analytics(filter_criteria, analysis_types)
7print(analytics_results)

The boilerplate analytics implementation allows you to:

  1. Filter logs based on specific criteria
  2. Perform statistical analysis on various metrics (e.g., search latencies)
  3. Track performance trends over time
  4. Identify potential bottlenecks or areas for optimization

Experimental Features

Advanced analytics features are still in an experimental state - please reach out to the R2R team if you are interested in configuring / using these additional features.

Custom Analytics

R2R’s analytics system is flexible and allows for custom analysis. You can specify different filters and analysis types to focus on specific aspects of your application’s performance.

1# Analyze RAG latencies
2rag_filter = FilterCriteria(filters={"rag_latencies": "rag_generation_latency", "rag_eval": "rag_eval_metric"})
3rag_analysis = AnalysisTypes(analysis_types={"rag_latencies": ["basic_statistics", "rag_generation_latency"]})
4rag_analytics = app.analytics(rag_filter, rag_analysis)
5
6# Track usage patterns by user
7user_filter = FilterCriteria(filters={"user_patterns": "user_id"})
8user_analysis = AnalysisTypes(analysis_types={"user_patterns": ["bar_chart", "user_id"]})
9user_analytics = app.analytics(user_filter, user_analysis)
10
11# Monitor error rates
12error_filter = FilterCriteria(filters={"error_rates": "error"})
13error_analysis = AnalysisTypes(analysis_types={"error_rates": ["basic_statistics", "error"]})
14error_analytics = app.analytics(error_filter, error_analysis)

Preloading Data for Analysis

To get meaningful analytics, you need a substantial amount of data. Here’s a script to preload your database with random searches:

1import random
2from r2r import R2R, GenerationConfig
3
4app = R2R()
5
6# List of sample queries
7queries = [
8 "What is artificial intelligence?",
9 "Explain machine learning.",
10 "How does natural language processing work?",
11 "What are neural networks?",
12 "Describe deep learning.",
13 # Add more queries as needed
14]
15
16# Perform random searches
17for _ in range(1000):
18 query = random.choice(queries)
19 app.rag(query, GenerationConfig(model="openai/gpt-4o-mini"))
20
21print("Preloading complete. You can now run analytics on this data.")

After running this script, you’ll have a rich dataset to analyze using the analytics features described above.

User-Level Analytics

To get analytics for a specific user:

1user_id = "your_user_id_here"
2
3user_filter = FilterCriteria(filters={"user_analytics": "user_id"})
4user_analysis = AnalysisTypes(analysis_types={
5 "user_analytics": ["basic_statistics", "user_id"],
6 "user_search_latencies": ["basic_statistics", "search_latency"]
7})
8
9user_analytics = app.analytics(user_filter, user_analysis)
10print(f"Analytics for user {user_id}:")
11print(user_analytics)

This will give you insights into the behavior and performance of specific users in your system.

Summary

R2R’s logging and analytics features provide powerful tools for understanding and optimizing your RAG application. By leveraging these capabilities, you can:

  • Monitor system performance in real-time
  • Analyze trends in search and RAG operations
  • Identify potential bottlenecks or areas for improvement
  • Track user behavior and usage patterns
  • Make data-driven decisions to enhance your application’s performance and user experience

For detailed setup and basic functionality, refer back to the R2R Quickstart. For more advanced usage and customization options, join the R2R Discord community.