diff options
| author | sigoden <sigoden@gmail.com> | 2024-10-23 17:22:11 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-23 17:22:11 +0800 |
| commit | fbeaa9cb2c5b7a8e0d9114a62c270c6fe8024c31 (patch) | |
| tree | c25b91f61ec2816b4425decedfd451967a003f49 | |
| parent | d499954dbbfd9e4d9911d179092385ac0d161e56 (diff) | |
| download | llm-functions-docker-fbeaa9cb2c5b7a8e0d9114a62c270c6fe8024c31.tar.gz | |
feat: improve execute js/py code and collect results (#117)
| -rwxr-xr-x | scripts/run-agent.js | 6 | ||||
| -rwxr-xr-x | scripts/run-agent.py | 6 | ||||
| -rwxr-xr-x | scripts/run-tool.js | 6 | ||||
| -rwxr-xr-x | scripts/run-tool.py | 6 | ||||
| -rw-r--r-- | tools/execute_js_code.js | 6 | ||||
| -rw-r--r-- | tools/execute_py_code.py | 2 |
6 files changed, 14 insertions, 18 deletions
diff --git a/scripts/run-agent.js b/scripts/run-agent.js index b8dddd0..c59ff6e 100755 --- a/scripts/run-agent.js +++ b/scripts/run-agent.js @@ -87,9 +87,7 @@ async function loadEnv(filePath) { if (!process.env[envName]) { let envValue = valueParts.join("=").trim(); - if (envValue.startsWith('"') && envValue.endsWith('"')) { - envValue = envValue.slice(1, -1); - } else if (envValue.startsWith("'") && envValue.endsWith("'")) { + if ((envValue.startsWith('"') && envValue.endsWith('"')) || (envValue.startsWith("'") && envValue.endsWith("'"))) { envValue = envValue.slice(1, -1); } envVars.set(envName, envValue); @@ -129,7 +127,7 @@ function returnToLLM(value) { } const type = typeof value; if (type === "string" || type === "number" || type === "boolean") { - writer.write(value); + writer.write(value.toString()); } else if (type === "object") { const proto = Object.prototype.toString.call(value); if (proto === "[object Object]" || proto === "[object Array]") { diff --git a/scripts/run-agent.py b/scripts/run-agent.py index 58595e4..780267d 100755 --- a/scripts/run-agent.py +++ b/scripts/run-agent.py @@ -77,9 +77,7 @@ def load_env(file_path): if env_name not in os.environ: env_value = "=".join(value_parts).strip() - if env_value.startswith('"') and env_value.endswith('"'): - env_value = env_value[1:-1] - elif env_value.startswith("'") and env_value.endswith("'"): + if (env_value.startswith('"') and env_value.endswith('"')) or (env_value.startswith("'") and env_value.endswith("'")): env_value = env_value[1:-1] env_vars[env_name] = env_value @@ -150,7 +148,7 @@ def return_to_llm(value): value_type = type(value).__name__ if value_type in ("str", "int", "float", "bool"): - writer.write(value) + writer.write(str(value)) elif value_type == "dict" or value_type == "list": value_str = json.dumps(value, indent=2) assert value == json.loads(value_str) diff --git a/scripts/run-tool.js b/scripts/run-tool.js index e4ac434..4a806f3 100755 --- a/scripts/run-tool.js +++ b/scripts/run-tool.js @@ -74,9 +74,7 @@ async function loadEnv(filePath) { if (!process.env[envName]) { let envValue = valueParts.join("=").trim(); - if (envValue.startsWith('"') && envValue.endsWith('"')) { - envValue = envValue.slice(1, -1); - } else if (envValue.startsWith("'") && envValue.endsWith("'")) { + if ((envValue.startsWith('"') && envValue.endsWith('"')) || (envValue.startsWith("'") && envValue.endsWith("'"))) { envValue = envValue.slice(1, -1); } envVars.set(envName, envValue); @@ -116,7 +114,7 @@ function returnToLLM(value) { } const type = typeof value; if (type === "string" || type === "number" || type === "boolean") { - writer.write(value); + writer.write(value.toString()); } else if (type === "object") { const proto = Object.prototype.toString.call(value); if (proto === "[object Object]" || proto === "[object Array]") { diff --git a/scripts/run-tool.py b/scripts/run-tool.py index 6ea3361..e6c0c8f 100755 --- a/scripts/run-tool.py +++ b/scripts/run-tool.py @@ -72,9 +72,7 @@ def load_env(file_path): if env_name not in os.environ: env_value = "=".join(value_parts).strip() - if env_value.startswith('"') and env_value.endswith('"'): - env_value = env_value[1:-1] - elif env_value.startswith("'") and env_value.endswith("'"): + if (env_value.startswith('"') and env_value.endswith('"')) or (env_value.startswith("'") and env_value.endswith("'")): env_value = env_value[1:-1] env_vars[env_name] = env_value @@ -110,7 +108,7 @@ def return_to_llm(value): value_type = type(value).__name__ if value_type in ("str", "int", "float", "bool"): - writer.write(value) + writer.write(str(value)) elif value_type == "dict" or value_type == "list": value_str = json.dumps(value, indent=2) assert value == json.loads(value_str) diff --git a/tools/execute_js_code.js b/tools/execute_js_code.js index 853ccab..6bad67a 100644 --- a/tools/execute_js_code.js +++ b/tools/execute_js_code.js @@ -1,3 +1,5 @@ +const vm = require('vm'); + /** * Execute the javascript code in node.js. * @typedef {Object} Args @@ -5,5 +7,7 @@ * @param {Args} args */ exports.run = function run({ code }) { - return eval(code); + const context = vm.createContext({}); + const script = new vm.Script(code); + return script.runInContext(context); } diff --git a/tools/execute_py_code.py b/tools/execute_py_code.py index a774e82..3c6beb4 100644 --- a/tools/execute_py_code.py +++ b/tools/execute_py_code.py @@ -3,4 +3,4 @@ def run(code: str): Args: code: Python code to execute, such as `print("hello world")` """ - return exec(code) + return eval(code)
\ No newline at end of file |
