Base Module#
The base module provides core components for the LLM4AD platform, including:
Code Representation - Classes for representing Python functions and programs as structured objects
Evaluation - Interfaces for evaluating generated algorithms
Sampling - Utilities for interacting with Large Language Models
Code Modification - Tools for programmatically modifying Python source code
Submodules#
Module Overview#
Code Module (code.py)#
The code module defines the fundamental data structures for representing Python code:
Function- A parsed Python function with name, arguments, body, return type, and docstringProgram- A complete Python program containing imports and functionsTextFunctionProgramConverter- Utilities for converting between text and structured code objects
Evaluate Module (evaluate.py)#
The evaluate module provides evaluation capabilities:
Evaluation- Abstract base class for defining evaluation logicSecureEvaluator- Wrapper for safe evaluation with timeout and process isolation
Sample Module (sample.py)#
The sample module handles LLM interaction:
LLM- Abstract interface for language modelsSampleTrimmer- Utilities for cleaning LLM-generated code
ModifyCode Module (modify_code.py)#
The modify_code module provides code transformation utilities:
ModifyCode- Static methods for modifying Python source code
Quick Start#
Working with Functions and Programs#
from llm4ad.base.code import Function, Program, TextFunctionProgramConverter
# Parse code string to Program
code_str = '''
import numpy as np
def target_func(arr):
return np.sum(arr)
'''
program = TextFunctionProgramConverter.text_to_program(code_str)
func = program.functions[0]
print(f"Function: {func.name}, Body: {func.body}")
Defining Evaluation#
from llm4ad.base.evaluate import Evaluation, SecureEvaluator
from llm4ad.base.code import Program
template = '''
def objective(x):
return 0
'''
class MyEvaluator(Evaluation):
def evaluate_program(self, program_str, callable_func, **kwargs):
return callable_func(10)
evaluator = MyEvaluator(template_program=template, timeout_seconds=10)
secure_eval = SecureEvaluator(evaluator)
Processing LLM Output#
from llm4ad.base.sample import LLM, SampleTrimmer
class CustomLLM(LLM):
def draw_sample(self, prompt, *args, **kwargs):
# Call your LLM API here
return "def target(x): return x + 1"
llm = CustomLLM(do_auto_trim=True)
trimmer = SampleTrimmer(llm)
result = trimmer.draw_sample("Generate a function")