Class SessionsREPLTool
java.lang.Object
dev.langchain4j.code.azure.acads.SessionsREPLTool
- All Implemented Interfaces:
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclass
Default implementation of the FileDownloader interface that downloads files from Azure Container Apps.class
Default implementation of the FileLister interface that lists files in Azure Container Apps.class
Default implementation of the FileUploader interface that uploads files to Azure Container Apps.static interface
Interface for downloading files from ACA.static interface
Interface for listing files in ACA.static interface
Interface for uploading files to ACA.static class
Represents metadata of a remote file. -
Constructor Summary
ConstructorsModifierConstructorDescriptionSessionsREPLTool
(String poolManagementEndpoint) Constructs a new SessionsREPLTool with the specified endpoint.SessionsREPLTool
(String poolManagementEndpoint, String sessionId, boolean sanitizeInput) Constructs a new SessionsREPLTool with specified parameters.protected
SessionsREPLTool
(String poolManagementEndpoint, String sessionId, boolean sanitizeInput, HttpClient httpClient, com.azure.identity.DefaultAzureCredential credential) Protected constructor for testing purposes that allows injecting dependencies. -
Method Summary
Modifier and TypeMethodDescriptionImplementation of CodeExecutionEngine.execute methodexecuteCode
(String sessionCode) Executes a query to the code interpreter.Executes the input code and returns the result.String[]
value()
Returns the value of the tool.
-
Constructor Details
-
SessionsREPLTool
Constructs a new SessionsREPLTool with the specified endpoint.- Parameters:
poolManagementEndpoint
- the pool management endpoint URL
-
SessionsREPLTool
Constructs a new SessionsREPLTool with specified parameters.- Parameters:
poolManagementEndpoint
- the pool management endpoint URLsessionId
- the session IDsanitizeInput
- 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 URLsessionId
- the session IDsanitizeInput
- whether to sanitize the input codehttpClient
- a pre-configured HTTP clientcredential
- a pre-configured credential
-
-
Method Details
-
execute
Implementation of CodeExecutionEngine.execute method- Specified by:
execute
in interfaceCodeExecutionEngine
- Parameters:
code
- The code to execute.- Returns:
- The result of the execution as a String in JSON format.
-
use
-
value
-
executeCode
-