Class SessionsREPLTool

java.lang.Object
dev.langchain4j.code.azure.acads.SessionsREPLTool
All Implemented Interfaces:
CodeExecutionEngine

public class SessionsREPLTool extends Object implements CodeExecutionEngine
A tool for executing code in Azure ACA dynamic sessions. See the examples here for more information: https://github.com/langchain4j/langchain4j-examples/tree/main/code-execution/azure-acads-examples Overview: SessionsREPLTool provides a mechanism to execute code snippets within an Azure Container Apps (ACA) dynamic session. It facilitates interaction with a remote code execution environment, allowing for tasks such as performing calculations, running scripts, and managing files. This class implements CodeExecutionEngine, making it compatible with the standard code execution interfaces in langchain4j. Key Components: USER_AGENT: A static string defining the User-Agent header for HTTP requests. API_VERSION: A static string specifying the API version for interacting with the ACA dynamic sessions endpoint. SANITIZE_PATTERN_START and SANITIZE_PATTERN_END: Static patterns used to sanitize input code by removing leading/trailing whitespace or keywords. sanitizeInput: A boolean flag indicating whether to sanitize the input code before execution. poolManagementEndpoint: The URL of the pool management endpoint for the ACA dynamic sessions. sessionId: The unique identifier for the ACA dynamic session. nativeHttpClient: A standard Java HttpClient instance for multipart form data operations. langchainHttpClient: A langchain4j HttpClient abstraction for standard HTTP operations. credential: A DefaultAzureCredential instance for authenticating with Azure services. accessTokenRef: An AtomicReference to store and manage the access token. FileUploader, FileDownloader, FileLister: Interfaces defining file management operations. DefaultFileUploader, DefaultFileDownloader, DefaultFileLister: Implementations of the file management interfaces. RemoteFileMetadata: A class representing metadata for remote files. Functionality: The SessionsREPLTool class provides the following core functionalities: Code Execution: Executes code snippets within the ACA dynamic session and returns the results. - Implements CodeExecutionEngine.execute(String) for standardized execution API - Provides a Tool annotated use(String) method for integration with AI services File Management: Allows uploading, downloading, and listing files within the session's environment. Authentication: Handles authentication with Azure services using the DefaultAzureCredential. Input Sanitization: Provides an option to sanitize input code for security and compatibility. Usage: To use the SessionsREPLTool, you need to: Create an instance of the class, providing the pool management endpoint. Call the use() or execute() method to execute code, passing the code snippet as a string. Use the file management interfaces and implementations to interact with files in the session. Ensure proper error handling and resource management. For testing, use the protected constructor that accepts pre-configured dependencies.
  • Constructor Details

    • SessionsREPLTool

      public SessionsREPLTool(String poolManagementEndpoint)
      Constructs a new SessionsREPLTool with the specified endpoint.
      Parameters:
      poolManagementEndpoint - the pool management endpoint URL
    • SessionsREPLTool

      public SessionsREPLTool(String poolManagementEndpoint, String sessionId, boolean sanitizeInput)
      Constructs a new SessionsREPLTool with specified parameters.
      Parameters:
      poolManagementEndpoint - the pool management endpoint URL
      sessionId - the session ID
      sanitizeInput - whether to sanitize the input code
    • SessionsREPLTool

      protected SessionsREPLTool(String poolManagementEndpoint, String sessionId, boolean sanitizeInput, HttpClient httpClient, com.azure.identity.DefaultAzureCredential credential)
      Protected constructor for testing purposes that allows injecting dependencies.
      Parameters:
      poolManagementEndpoint - the pool management endpoint URL
      sessionId - the session ID
      sanitizeInput - whether to sanitize the input code
      httpClient - a pre-configured HTTP client
      credential - a pre-configured credential
  • Method Details

    • execute

      public String execute(String code)
      Implementation of CodeExecutionEngine.execute method
      Specified by:
      execute in interface CodeExecutionEngine
      Parameters:
      code - The code to execute.
      Returns:
      The result of the execution as a String in JSON format.
    • use

      public String use(String input)
      Executes the input code and returns the result.
      Parameters:
      input - the code or query to execute
      Returns:
      the execution result as a JSON string
    • value

      public String[] value()
      Returns the value of the tool.
      Returns:
      an array containing the tool name
    • executeCode

      public Map<String,Object> executeCode(String sessionCode)
      Executes a query to the code interpreter.
      Parameters:
      sessionCode - the code or query to execute
      Returns:
      a map containing the execution results