Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Code Executor Module

The code-executor module in light-genai-4j provides a robust environment for executing code snippets generated by LLMs. It leverages GraalVM Polyglot to execute code in various languages (JavaScript, Python, Ruby, R, etc.) securely within the Java application.

Overview

This module is designed to integrate seamlessly with langchain4j agents that require tool execution capabilities. By using GraalVM, it avoids the need for external execution environments or heavy Docker containers for many use cases, while still providing a degree of isolation and high performance.

Dependencies

To use the code executor, add the following dependency to your pom.xml:

<dependency>
    <groupId>com.networknt</groupId>
    <artifactId>code-executor</artifactId>
    <version>${version.light-genai-4j}</version>
</dependency>

You also need to ensure you have the necessary GraalVM dependencies. The module by default includes support for JavaScript.

<dependency>
    <groupId>org.graalvm.polyglot</groupId>
    <artifactId>js</artifactId>
    <version>${version.graalvm}</version>
    <type>pom</type>
</dependency>

If you need to execute other languages (e.g., Python), you must add the corresponding GraalVM language dependency:

<!-- For Python support -->
<dependency>
    <groupId>org.graalvm.polyglot</groupId>
    <artifactId>python</artifactId>
    <version>${version.graalvm}</version>
    <type>pom</type>
</dependency>

Usage

The core component is the CodeExecutionEngine. You can instantiate a GraalVmJavaScriptExecutionEngine (or other language-specific engines provided by LangChain4j) to execute code.

Example: Executing JavaScript

import dev.langchain4j.code.CodeExecutionEngine;
import dev.langchain4j.code.graalvm.GraalVmJavaScriptExecutionEngine;

public class CodeExecutorExample {
    public static void main(String[] args) {
        CodeExecutionEngine engine = new GraalVmJavaScriptExecutionEngine();
        String code = "var x = 10; var y = 20; x + y;";
        String result = engine.execute(code);
        System.out.println("Result: " + result); // Output: 30
    }
}

Integration with LangChain4j Agents

This module effectively provides the implementation for LangChain4j’s CodeExecutionTool. You can register this tool with your agent to allow it to write and execute code to solve complex problems.

// Example setup with an agent (conceptual)
CodeExecutionEngine engine = new GraalVmJavaScriptExecutionEngine();
ToolSpecification codeExecutionTool = ToolSpecification.builder()
    .name("execute_javascript")
    .description("Executes JavaScript code and returns the result")
    .addArgument("code", JsonSchemaProperty.STRING, "The JavaScript code to execute")
    .build();

// ... register tool with your ChatModel or Agent ...

Configuration

The GraalVM execution engine can be configured to restrict access to host resources (file system, network, etc.) for security. By default, LangChain4j’s implementation provides a reasonable level of sandboxing, but you should review the GraalVM Security Guide for production deployments, especially if executing untrusted code.