主题
GraalVM Polyglot/Truffle
DANGER
⚠️ 安全警告:高风险代码执行
该模块通过 GraalVM 执行任意 Python/JavaScript 代码,本质上具有危险性。
❗ 请勿在生产环境中使用!
Maven 依赖
xml
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-code-execution-engine-graalvm-polyglot</artifactId>
<version>1.22.0-beta32</version>
</dependency>API
GraalVmJavaScriptExecutionEngineGraalVmJavaScriptExecutionToolGraalVmPythonExecutionEngineGraalVmPythonExecutionTool
返回值
execute(String code) 返回单个字符串,其中同时包含代码打印到 标准输出(stdout)和标准错误(stderr)的内容,以及代码求值所得的值。
只求值为一个值的代码会返回该值:
java
engine.execute("40 + 2");
// 42只有打印输出的代码会返回所打印的内容,并以 Output: 为前缀:
java
engine.execute("print('hello')");
// Output:
// hello既有打印又有求值的代码会先返回打印内容,再返回该值:
java
engine.execute("print('hello')\n42");
// Output:
// hello
// Result:
// 42既无打印也无求值的代码返回空字符串:
java
engine.execute("x = 1");
// (empty string)如果代码执行失败,会抛出 PolyglotException, 且失败前打印的任何内容都会丢失。