mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
feat: Add Code Interpreter Plugin (#837)
* feat: Add Code Interpreter Plugin Adds a Simple Code Interpreter Plugin. ## Features: - Runs code using local Python Environment ## Issues - Code execution is not sandboxed. * Add Docker Sandbox for Python Server
This commit is contained in:
parent
e2397076a2
commit
3797ec6082
7 changed files with 150 additions and 0 deletions
52
api/app/clients/tools/CodeInterpreter.js
Normal file
52
api/app/clients/tools/CodeInterpreter.js
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
const { Tool } = require('langchain/tools');
|
||||
const WebSocket = require('ws');
|
||||
const { promisify } = require('util');
|
||||
const fs = require('fs');
|
||||
|
||||
class CodeInterpreter extends Tool {
|
||||
constructor(fields) {
|
||||
super();
|
||||
this.name = 'code-interpreter';
|
||||
this.description = `If there is plotting or any image related tasks, save the result as .png file.
|
||||
No need show the image or plot. USE print(variable_name) if you need output.You can run python codes with this plugin.You have to use print function in python code to get any result from this plugin.
|
||||
This does not support user input. Even if the code has input() function, change it to an appropriate value.
|
||||
You can show the user the code with input() functions. But the code passed to the plug-in should not contain input().
|
||||
You should provide properly formatted code to this plugin. If the code is executed successfully, the stdout will be returned to you. You have to print that to the user, and if the user had
|
||||
asked for an explanation, you have to provide one. If the output is "Error From here" or any other error message,
|
||||
tell the user "Python Engine Failed" and continue with whatever you are supposed to do.`;
|
||||
|
||||
// Create a promisified version of fs.unlink
|
||||
this.unlinkAsync = promisify(fs.unlink);
|
||||
}
|
||||
|
||||
async _call(input) {
|
||||
const websocket = new WebSocket('ws://localhost:3380'); // Update with your WebSocket server URL
|
||||
|
||||
// Wait until the WebSocket connection is open
|
||||
await new Promise((resolve) => {
|
||||
websocket.onopen = resolve;
|
||||
});
|
||||
|
||||
// Send the Python code to the server
|
||||
websocket.send(input);
|
||||
|
||||
// Wait for the result from the server
|
||||
const result = await new Promise((resolve) => {
|
||||
websocket.onmessage = (event) => {
|
||||
resolve(event.data);
|
||||
};
|
||||
|
||||
// Handle WebSocket connection closed
|
||||
websocket.onclose = () => {
|
||||
resolve('Python Engine Failed');
|
||||
};
|
||||
});
|
||||
|
||||
// Close the WebSocket connection
|
||||
websocket.close();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = CodeInterpreter;
|
||||
Loading…
Add table
Add a link
Reference in a new issue