### REPL Environment Query Examples Source: https://context7.com/alexzhang13/rlm-minimal/llms.txt Demonstrates how to use the `llm_query` function within a REPL environment for simple queries, context-aware analysis, and building up results through iterative summarization. ```python # llm_query is automatically available in the REPL globals # Simple query response = llm_query("What is 2+2?") print(response) # Sub-model's answer # Query with context chunk document_chunk = context[0:50000] # First 50k chars analysis = llm_query(f''' Analyze this document section and extract key entities: {document_chunk} ''') print(f"Entities found: {analysis}") # Build up results buffer pattern summaries = [] sections = context.split('###') for section in sections: if len(section) > 100: # Skip empty sections summary = llm_query(f"Summarize in one sentence: {section}") summaries.append(summary) # Final aggregation query final_answer = llm_query(f''' Based on these section summaries, answer: What is the main theme? Summaries: {summaries} ''') ``` -------------------------------- ### Install Rich Library for Enhanced Logging Source: https://github.com/alexzhang13/rlm-minimal/blob/main/README.md This command installs the 'rich' library, which provides optional utilities for colorful and enhanced logging outputs within the RLM REPL implementation. It is a dependency if you wish to leverage these advanced logging features. ```shell pip install rich ``` -------------------------------- ### Build System Prompt: Initialize RLM Conversation Source: https://context7.com/alexzhang13/rlm-minimal/llms.txt Illustrates how to construct the initial system message for the RLM REPL environment. This prompt defines the capabilities and behavior of the root model. The example shows how to use utility functions to build the system prompt and append subsequent user prompts for multi-turn interactions, including forcing a final answer. ```python from rlm.utils.prompts import build_system_prompt, next_action_prompt # Get the initial system prompt messages = build_system_prompt() print(len(messages)) # 1 print(messages[0]['role']) # 'system' print(messages[0]['content'][:100]) # First 100 chars of instructions # Full conversation initialization query = "What is the answer to my question?" messages = build_system_prompt() # Add first user prompt messages.append(next_action_prompt(query, iteration=0)) # The messages list is now ready for the first LLM call # messages = [ # {"role": "system", "content": "You are tasked with..."}, # {"role": "user", "content": "You have not interacted with the REPL..."} # ] # After model responds and executes code, add next iteration prompt messages.append(next_action_prompt(query, iteration=1)) # When ready to force final answer messages.append(next_action_prompt(query, iteration=5, final_answer=True)) ``` -------------------------------- ### find_code_blocks() for REPL Code Extraction Source: https://context7.com/alexzhang13/rlm-minimal/llms.txt Shows how to use the `find_code_blocks` utility to extract Python code enclosed in ```repl``` tags from model responses. Includes examples of finding multiple blocks, handling cases with no code, and executing the extracted code within a REPL environment. ```python from rlm.utils.utils import find_code_blocks # Example model response model_response = """ Let me analyze the context by chunking it. ```repl chunk_size = 10000 chunks = [context[i:i+chunk_size] for i in range(0, len(context), chunk_size)] print(f"Created {len(chunks)} chunks") ``` Now I'll query each chunk: ```repl results = [] for chunk in chunks[:5]: answer = llm_query(f"Find numbers in: {chunk}") results.append(answer) ``` Done! """ # Extract code blocks code_blocks = find_code_blocks(model_response) print(len(code_blocks)) # 2 print(code_blocks[0]) # "chunk_size = 10000\nchunks = ..." print(code_blocks[1]) # "results = []\nfor chunk in chunks[:5]:..." # No code blocks found response_without_code = "I think the answer is 42." code_blocks = find_code_blocks(response_without_code) print(code_blocks) # [] # Execute each block from rlm.repl import REPLEnv repl = REPLEnv(context_str="sample context") for code in code_blocks: result = repl.code_execution(code) print(f"Output: {result.stdout}") ``` -------------------------------- ### Custom Context Loading: Support for Multiple Formats in RLM Source: https://context7.com/alexzhang13/rlm-minimal/llms.txt Shows how the RLM REPL environment can load context from various data formats, including strings, JSON objects, Python dictionaries, and lists. The loaded context is automatically made available as the 'context' variable within the REPL. Examples demonstrate accessing string data and parsed JSON structures for programmatic use. ```python from rlm.repl import REPLEnv import json # String context repl_str = REPLEnv(context_str="This is a large document...") result = repl_str.code_execution("print(context[:50])") print(result.stdout) # "This is a large document..." # JSON/Dict context context_dict = { "users": [ {"id": 1, "name": "Alice", "score": 95}, {"id": 2, "name": "Bob", "score": 87} ], "metadata": {"total": 2, "date": "2025-01-07"} } repl_json = REPLEnv(context_json=context_dict) code = """ import json print(f"Total users: {context['metadata']['total']}") print(f"User names: {[u['name'] for u in context['users']]}") high_scorers = [u for u in context['users'] if u['score'] > 90] print(f"High scorers: {high_scorers}") """ result = repl_json.code_execution(code) print(result.stdout) # Output: # Total users: 2 # User names: ['Alice', 'Bob'] # High scorers: [{'id': 1, 'name': 'Alice', 'score': 95}] # Both string and JSON context repl_both = REPLEnv( context_str="Header: Important Document", context_json={"sections": ["intro", "body", "conclusion"]} ) # Context is written to temp files and loaded into REPL # String context accessible as string variable # JSON context accessible as parsed dict/list ``` -------------------------------- ### FINAL and FINAL_VAR Functions for Model Responses Source: https://context7.com/alexzhang13/rlm-minimal/llms.txt Explains the `FINAL()` and `FINAL_VAR()` functions used by models to signal task completion and return results. `FINAL()` returns a direct string, while `FINAL_VAR()` returns the value of a REPL variable. Includes examples of their usage and error handling. ```python # Example 1: Direct answer with FINAL() # The model responds with: """ After analyzing the context, I found the answer. FINAL(The magic number is 1298418) """ # Result returned to user: "The magic number is 1298418" # Example 2: Return variable with FINAL_VAR() # The model generates and executes REPL code: code = """ # Process all chunks and build final answer answers = [] for i in range(0, len(context), 100000): chunk = context[i:i+100000] result = llm_query(f"Find the magic number in: {chunk}") answers.append(result) # Combine results final_answer = \"\n\".join(answers) """ # Then model responds with: """ I've stored the complete results in the final_answer variable. FINAL_VAR(final_answer) """ # System retrieves repl.locals['final_answer'] and returns it # Example 3: Error handling # If variable doesn't exist: """ FINAL_VAR(nonexistent_var) """ # Returns: "Variable 'nonexistent_var' not found in REPL environment" ``` -------------------------------- ### Process Code Execution: Handle Model-Generated Code in RLM Source: https://context7.com/alexzhang13/rlm-minimal/llms.txt Details the mechanism for executing code blocks generated by a language model within the RLM framework. This functionality is crucial for the iterative refinement loop, where model outputs are executed, and their results are incorporated back into the conversation. The example shows the initialization of necessary components for code execution and logging. ```python from rlm.utils.utils import process_code_execution, find_code_blocks from rlm.repl import REPLEnv from rlm.logger.repl_logger import REPLEnvLogger from rlm.logger.root_logger import ColorfulLogger # Initialize components repl_env = REPLEnv(context_str="sample context data") repl_logger = REPLEnvLogger(enabled=True) root_logger = ColorfulLogger(enabled=True) ``` -------------------------------- ### OpenAIClient LLM API Wrapper Source: https://context7.com/alexzhang13/rlm-minimal/llms.txt Demonstrates how to use the `OpenAIClient` to interact with OpenAI's chat completions API. Covers initialization, string and message list inputs, passing additional parameters like `max_tokens` and `temperature`, and basic error handling. ```python from rlm.utils.llm import OpenAIClient import os os.environ['OPENAI_API_KEY'] = 'your-api-key-here' # Initialize client client = OpenAIClient( api_key=None, # Uses OPENAI_API_KEY env var if None model="gpt-5" ) # String input - automatically converted to messages response = client.completion("What is the capital of France?") print(response) # "The capital of France is Paris." # Message list input messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Explain quantum computing in one sentence."} ] response = client.completion(messages) print(response) # With additional parameters response = client.completion( messages=messages, max_tokens=100, temperature=0.7, top_p=0.9 ) # Error handling try: response = client.completion("Hello") except RuntimeError as e: print(f"API Error: {e}") ``` -------------------------------- ### RLM Completion Query Interface - Python Source: https://context7.com/alexzhang13/rlm-minimal/llms.txt The main interface for querying the RLM system. It initializes a REPL with a given context and question, then iteratively prompts the root model to analyze the context using REPL code execution and sub-model queries until a final answer is produced. It supports string, dictionary, or list-like message objects as context. ```python from rlm.rlm_repl import RLM_REPL import os # Set up API key os.environ['OPENAI_API_KEY'] = 'your-api-key-here' # Initialize RLM with root and recursive model configuration rlm = RLM_REPL( model="gpt-5", # Root model for orchestration recursive_model="gpt-5-nano", # Sub-model for chunk processing enable_logging=True, # Enable rich console logging max_iterations=10 # Maximum reasoning iterations ) # Large context example - string format large_document = """ Section 1: Introduction This document contains important information... [... millions of tokens ...] The secret code is: ALPHA-2847 [... more content ...] """ # Query the RLM result = rlm.completion( context=large_document, query="What is the secret code mentioned in the document?" ) print(f"Answer: {result}") # Output: ALPHA-2847 # Context can also be a dictionary context_dict = { "title": "Database Records", "records": ["record1", "record2", "..."], "metadata": {"count": 1000000} } result = rlm.completion( context=context_dict, query="How many records are in the database?" ) # Or a list of message-like objects context_list = [ {"role": "user", "content": "First message"}, {"role": "assistant", "content": "Response"}, # ... thousands more messages ] result = rlm.completion( context=context_list, query="Summarize the conversation" ) ``` -------------------------------- ### REPL Environment Code Execution - Python Source: https://context7.com/alexzhang13/rlm-minimal/llms.txt Executes Python code within a sandboxed REPL environment, providing access to the context variable and an `llm_query` function for sub-model interaction. This allows for iterative analysis, state maintenance, and buffer building within the RLM system. The execution results include standard output, errors, local variables, and execution time. ```python from rlm.repl import REPLEnv # Initialize REPL with context context_str = "Document with 1M lines of text..." repl = REPLEnv( recursive_model="gpt-5-nano", context_str=context_str, context_json=None ) # Execute code that chunks and analyzes context code = """ # Context is automatically available as a variable total_lines = len(context.split('\n')) print(f"Total lines: {total_lines}") # Break into chunks for sub-model analysis chunk_size = 10000 chunks = [context[i:i+chunk_size] for i in range(0, len(context), chunk_size)] # Query sub-model for each chunk results = [] for i, chunk in enumerate(chunks[:5]): # First 5 chunks prompt = f"Find any numbers in this text chunk: {chunk}" answer = llm_query(prompt) results.append(answer) print(f"Chunk {i}: {answer}") # Store results in variable for later retrieval final_results = results """ result = repl.code_execution(code) print(result.stdout) # Printed output from code print(result.stderr) # Any errors print(result.locals) # Variables created: final_results, chunks, etc print(result.execution_time) # Execution duration in seconds # Access stored variables final_results_value = repl.locals.get('final_results') print(f"Stored results: {final_results_value}") ``` -------------------------------- ### Process Model Response with Code Execution Source: https://context7.com/alexzhang13/rlm-minimal/llms.txt This snippet demonstrates how to process a model's response that may contain executable code. It takes the model's response and conversation messages, executes any code found within a REPL environment, and updates the messages with the execution results. Dependencies include the REPL environment and logging utilities. ```python messages = [ {"role": "system", "content": "System prompt..."}, {"role": "user", "content": "Find the answer in the context."} ] model_response = """ Let me check the context. ```repl print(f"Context length: {len(context)}") answer = context.split()[0] print(f"First word: {answer}") ``` """ updated_messages = process_code_execution( response=model_response, messages=messages, repl_env=repl_env, repl_env_logger=repl_logger, logger=root_logger ) print(len(updated_messages)) print(updated_messages[-1]['role']) print(updated_messages[-1]['content']) ``` -------------------------------- ### Needle-in-Haystack: Search Large Text with RLM Source: https://context7.com/alexzhang13/rlm-minimal/llms.txt Demonstrates RLM's ability to find a specific value within a massive dataset (1 million lines of text). It involves generating contextual data, initializing the RLM with specific models, and performing a completion query. The process highlights RLM's internal mechanisms for context inspection, chunking, sub-model querying, and result aggregation. ```python from rlm.rlm_repl import RLM_REPL import random import os os.environ['OPENAI_API_KEY'] = 'your-api-key-here' # Generate massive context (1M lines) def generate_massive_context(num_lines=1_000_000, answer="1298418"): print("Generating massive context...") random_words = ["blah", "random", "text", "data", "content", "info"] lines = [] for _ in range(num_lines): num_words = random.randint(3, 8) line = " ".join([random.choice(random_words) for _ in range(num_words)]) lines.append(line) # Hide the needle at random position magic_position = random.randint(400000, 600000) lines[magic_position] = f"The magic number is {answer}" print(f"Magic number inserted at line {magic_position}") return "\n".join(lines) # Generate context with hidden answer answer = str(random.randint(1000000, 9999999)) context = generate_massive_context(num_lines=1_000_000, answer=answer) # Initialize RLM rlm = RLM_REPL( model="gpt-5", recursive_model="gpt-5-nano", enable_logging=True, max_iterations=10 ) # Query the RLM query = "I'm looking for a magic number. What is it?" result = rlm.completion(context=context, query=query) print(f"Result: {result}") print(f"Expected: {answer}") print(f"Match: {answer in result}") # The RLM will typically: # 1. Inspect the context variable to see its structure # 2. Chunk the context into manageable pieces # 3. Query sub-models on each chunk looking for the pattern # 4. Aggregate results and return the final answer ``` -------------------------------- ### LLM Query Function within REPL - Concept Source: https://context7.com/alexzhang13/rlm-minimal/llms.txt The `llm_query` function is a primitive injected into the REPL environment, enabling the root model to call sub-language models. This is fundamental for recursive analysis, as the root model generates Python code that utilizes `llm_query` to process text chunks or perform specific analytical tasks. ```text # This function is available inside REPL code_execution context ``` -------------------------------- ### Detect Final Answer using FINAL() or FINAL_VAR() Source: https://context7.com/alexzhang13/rlm-minimal/llms.txt This function checks model responses for specific patterns that indicate task completion. It supports extracting a direct answer using FINAL('answer') or retrieving an answer stored in a variable using FINAL_VAR('variable_name'). It requires a REPL environment to resolve variable names and a logger for potential errors. Returns the extracted answer or None if no final answer is detected. ```python from rlm.utils.utils import check_for_final_answer from rlm.repl import REPLEnv from rlm.logger.root_logger import ColorfulLogger repl_env = REPLEnv(context_str="context") logger = ColorfulLogger(enabled=False) # Store a variable in REPL repl_env.code_execution("result = 'The answer is 42'") # Response with FINAL() response1 = """ After analyzing everything, I have the answer. FINAL(The answer is 42) """ final = check_for_final_answer(response1, repl_env, logger) print(final) # "The answer is 42" # Response with FINAL_VAR() response2 = """ I've stored the complete analysis in the result variable. FINAL_VAR(result) """ final = check_for_final_answer(response2, repl_env, logger) print(final) # "The answer is 42" # No final answer yet response3 = "Let me continue investigating the context..." final = check_for_final_answer(response3, repl_env, logger) print(final) # None # Variable not found response4 = "FINAL_VAR(nonexistent)" final = check_for_final_answer(response4, repl_env, logger) print(final) # None (logs error) # Use in main loop # for iteration in range(max_iterations): # response = llm.completion(messages) # final_answer = check_for_final_answer(response, repl_env, logger) # if final_answer: # return final_answer # # Otherwise continue iterating ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.