rlm-minimal Documentation

repository·main·Indexed 21 days ago

https://github.com/alexzhang13/rlm-minimal

A minimal implementation of Recursive Language Models (RLM) providing a REPL-based environment where language model calls can be made recursive. It includes the RLM_REPL class for recursive completions and utilities for testing needle-in-a-haystack problems via generate_massive_context().

Tokens
931
Snippets
3
Records
5
Agent score
24%

What's inside rlm-minimal

  1. Understand the RLM code structure

    main

    The codebase is organized into the following key components:

    • rlm/rlm_repl.py: Contains the RLM_REPL class, which provides a basic implementation of an RLM using a REPL environment. The completion() function is the primary entry point when querying an RLM.
    • rlm/repl.py: Implements a simple exec-based REPL environment that adds an LM sub-call function.
    • rlm/utils/: Contains logic for parsing and handling base LLM clients and includes example prompts.
    • rlm/logger/: Contains optional logging utilities. Requires the rich library for enhanced output.
  2. How Recursive Language Models (RLM) work in this implementation

    main

    This project provides a minimal implementation of Recursive Language Models (RLM). The core concept is that RLM.completion() calls are intended to replace standard LM.completion() calls.

    In this minimal version, recursion is limited to depth=1 using the Sub_RLM class within a REPL environment. To enable deeper recursion, you must replace the Sub_RLM class with the RLM_REPL class. Note that when doing so, you may need to adjust the exec-based REPL environments to ensure sub-RLMs correctly manage their own independent REPL environments.

  3. Install dependencies for RLM

    main

    The project uses openai for LM API calls, dotenv for loading environment variables, and rich for enhanced logging. While the core logic is independent, installing rich is recommended if you want to use the enhanced, colorful logging outputs provided by the rlm/logger/ utilities.

    pip install rich
  4. Use RLM_REPL for recursive completions

    main

    The RLM_REPL class provides a REPL-based interface for performing recursive language model tasks. It is designed to handle complex queries by leveraging a primary model and a recursive model to navigate large contexts.

    Key Methods:

    • completion(context: str, query: str): Executes the recursive completion process using the provided context and query string. Returns the result of the query.

    Initialization Parameters:

    • model (str): The primary model to use (e.g., "gpt-5").
    • recursive_model (str): The model used for the recursive steps (e.g., "gpt-5-nano").
    • enable_logging (bool): Whether to enable logging for the process.
    • max_iterations (int): The maximum number of recursive iterations allowed.
    from rlm.rlm_repl import RLM_REPL
    
    rlm = RLM_REPL(
        model="gpt-5",
        recursive_model="gpt-5-nano",
        enable_logging=True,
        max_iterations=10
    )
    
    context = "... large context ..."
    query = "I'm looking for a magic number. What is it?"
    result = rlm.completion(context=context, query=query)
    print(f"Result: {result}")
  5. Generate massive context with generate_massive_context()

    main

    Use generate_massive_context() to create a large string containing a 'needle' (a specific answer) hidden within a 'haystack' of random text. This is useful for testing model performance on needle-in-a-haystack problems.

    Parameters:

    • num_lines (int): The total number of lines of text to generate. Defaults to 1_000_000.
    • answer (str): The specific string (the 'magic number') to be inserted into the context. Defaults to "1298418".
    from main import generate_massive_context
    
    # Generate 1 million lines of context with a specific answer
    context = generate_massive_context(num_lines=1_000_000, answer="1234567")