Skip to content

Commit 3b40d2b

Browse files
committed
add python tool impl
Change-Id: I4b91338512455999f241e2b6a1c25810e3a94ad7
1 parent a37d9b9 commit 3b40d2b

File tree

7 files changed

+409
-3
lines changed

7 files changed

+409
-3
lines changed

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
<module>tool-calls/spring-ai-alibaba-starter-tool-calling-worldbankdata</module>
154154
<module>tool-calls/spring-ai-alibaba-starter-tool-calling-youdaotranslate</module>
155155
<module>tool-calls/spring-ai-alibaba-starter-tool-calling-yuque</module>
156+
<module>tool-calls/spring-ai-alibaba-starter-tool-calling-python</module>
156157

157158
<!-- vector-stores modules -->
158159
<module>vector-stores/spring-ai-alibaba-starter-analyticdb-store</module>
@@ -164,6 +165,7 @@
164165
<module>mcp/spring-ai-alibaba-mcp-common</module>
165166
<module>mcp/spring-ai-alibaba-mcp-registry</module>
166167
<module>mcp/spring-ai-alibaba-mcp-router</module>
168+
<module>mcp/spring-ai-alibaba-mcp-distributed</module>
167169

168170
<module>prompt/spring-ai-alibaba-prompt-nacos</module>
169171

@@ -172,7 +174,6 @@
172174
<module>rag/spring-ai-alibaba-rag</module>
173175

174176
<module>observation</module>
175-
<module>mcp/spring-ai-alibaba-mcp-distributed</module>
176177
<module>auto-configurations/spring-ai-alibaba-autoconfigure-mcp-distributed</module>
177178
<module>spring-boot-starters/spring-ai-alibaba-starter-mcp-distributed</module>
178179
<module>spring-ai-alibaba-extensions-bom</module>
@@ -187,7 +188,7 @@
187188
<properties>
188189

189190
<!-- Spring AI Alibaba Version -->
190-
<revision>1.1.0.0-M4</revision>
191+
<revision>1.1.0.0-M5</revision>
191192

192193
<!-- Maven project basic config -->
193194
<java.version>17</java.version>

spring-ai-alibaba-extensions-bom/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<groupId>com.alibaba.cloud.ai</groupId>
2222
<artifactId>spring-ai-alibaba-extensions-bom</artifactId>
23-
<version>1.1.0.0-M4</version>
23+
<version>1.1.0.0-M5</version>
2424

2525
<packaging>pom</packaging>
2626

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Spring AI Alibaba Python Tool Starter
2+
3+
This starter provides a Python Tool for Spring AI Alibaba agents that allows executing Python code using GraalVM polyglot.
4+
5+
## Features
6+
7+
- Execute Python code snippets in a sandboxed environment
8+
- Automatic tool registration via Spring Boot auto-configuration
9+
- Secure execution with restricted access (no file I/O, no process creation)
10+
- Support for various Python data types (strings, numbers, booleans, arrays)
11+
12+
## Requirements
13+
14+
- Java 17+
15+
- GraalVM polyglot dependencies (automatically included as optional dependencies)
16+
17+
## Usage
18+
19+
### 1. Add Dependency
20+
21+
Add the starter to your `pom.xml`:
22+
23+
```xml
24+
<dependency>
25+
<groupId>com.alibaba.cloud.ai</groupId>
26+
<artifactId>spring-ai-alibaba-starter-python-tool</artifactId>
27+
<version>${spring-ai-alibaba.version}</version>
28+
</dependency>
29+
```
30+
31+
### 2. Enable Python Tool (Optional)
32+
33+
The Python Tool is enabled by default. To disable it, add the following configuration:
34+
35+
```yaml
36+
spring:
37+
ai:
38+
alibaba:
39+
python:
40+
tool:
41+
enabled: false
42+
```
43+
44+
### 3. Use in Agent
45+
46+
The Python Tool will be automatically registered as a `ToolCallback` and available to your agents. The tool can be used to execute Python code:
47+
48+
**Example Python code executions:**
49+
- Simple calculation: `2 + 2` returns `"4"`
50+
- String operations: `'Hello, ' + 'World'` returns `"Hello, World"`
51+
- List operations: `[1, 2, 3][0]` returns `"1"`
52+
53+
## Security
54+
55+
The Python Tool runs in a sandboxed environment with the following restrictions:
56+
- File I/O is disabled
57+
- Native access is disabled
58+
- Process creation is disabled
59+
- All access is restricted by default
60+
61+
## Customization
62+
63+
You can provide your own `PythonTool` bean to customize the behavior:
64+
65+
```java
66+
@Bean
67+
public PythonTool pythonTool() {
68+
return new PythonTool();
69+
}
70+
```
71+
72+
Or provide a custom `ToolCallback`:
73+
74+
```java
75+
@Bean
76+
public ToolCallback pythonToolCallback(PythonTool pythonTool) {
77+
return PythonTool.createPythonToolCallback("Custom description");
78+
}
79+
```
80+
81+
## License
82+
83+
Copyright 2024-2025 the original author or authors.
84+
85+
Licensed under the Apache License, Version 2.0.
86+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>com.alibaba.cloud.ai</groupId>
6+
<artifactId>spring-ai-alibaba-extensions</artifactId>
7+
<version>${revision}</version>
8+
<relativePath>../../pom.xml</relativePath>
9+
</parent>
10+
<artifactId>spring-ai-alibaba-starter-tool-calling-python</artifactId>
11+
<name>Spring AI Alibaba Python Tool Starter</name>
12+
<description>Spring AI Alibaba Python Tool Starter - Execute Python code using GraalVM polyglot</description>
13+
<url>https://github.com/alibaba/spring-ai-alibaba</url>
14+
<licenses>
15+
<license>
16+
<name>Apache 2.0</name>
17+
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
18+
<distribution>repo</distribution>
19+
</license>
20+
</licenses>
21+
<developers>
22+
<developer>
23+
<id>chickenlj</id>
24+
<name>Jun Liu</name>
25+
<email>[email protected]</email>
26+
<organization>Alibaba Cloud</organization>
27+
<organizationUrl>https://aliyun.com</organizationUrl>
28+
</developer>
29+
</developers>
30+
<scm>
31+
<connection>git://github.com/alibaba/spring-ai-alibaba.git</connection>
32+
<developerConnection>[email protected]:alibaba/spring-ai-alibaba.git</developerConnection>
33+
<url>https://github.com/alibaba/spring-ai-alibaba</url>
34+
</scm>
35+
36+
<properties>
37+
<java.version>17</java.version>
38+
<graalvm.polyglot.version>24.2.1</graalvm.polyglot.version>
39+
</properties>
40+
41+
<dependencies>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter</artifactId>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.springframework.ai</groupId>
49+
<artifactId>spring-ai-commons</artifactId>
50+
</dependency>
51+
52+
<!-- GraalVM Polyglot for Python execution -->
53+
<dependency>
54+
<groupId>org.graalvm.polyglot</groupId>
55+
<artifactId>polyglot</artifactId>
56+
<version>${graalvm.polyglot.version}</version>
57+
<optional>true</optional>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.graalvm.polyglot</groupId>
61+
<artifactId>python-community</artifactId>
62+
<version>${graalvm.polyglot.version}</version>
63+
<type>pom</type>
64+
<optional>true</optional>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter-test</artifactId>
70+
<scope>test</scope>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-configuration-processor</artifactId>
76+
<optional>true</optional>
77+
</dependency>
78+
</dependencies>
79+
80+
<build>
81+
<plugins>
82+
<plugin>
83+
<groupId>org.springframework.boot</groupId>
84+
<artifactId>spring-boot-maven-plugin</artifactId>
85+
</plugin>
86+
</plugins>
87+
</build>
88+
89+
</project>
90+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.cloud.ai.agent.python.autoconfigure;
17+
18+
import com.alibaba.cloud.ai.agent.python.tool.PythonTool;
19+
import org.graalvm.polyglot.Engine;
20+
import org.springframework.ai.tool.ToolCallback;
21+
import org.springframework.boot.autoconfigure.AutoConfiguration;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
25+
import org.springframework.context.annotation.Bean;
26+
27+
/**
28+
* Auto-configuration for Python Tool.
29+
*
30+
* This configuration automatically registers the PythonTool as a ToolCallback
31+
* when GraalVM polyglot is available on the classpath.
32+
*
33+
* @author Spring AI Alibaba
34+
*/
35+
@AutoConfiguration
36+
@ConditionalOnClass({ Engine.class })
37+
@ConditionalOnProperty(prefix = "spring.ai.alibaba.python.tool", name = "enabled", havingValue = "true", matchIfMissing = true)
38+
public class PythonToolAutoConfiguration {
39+
40+
@Bean
41+
@ConditionalOnMissingBean
42+
public PythonTool pythonTool() {
43+
return new PythonTool();
44+
}
45+
46+
@Bean
47+
@ConditionalOnMissingBean(name = "pythonToolCallback")
48+
public ToolCallback pythonToolCallback(PythonTool pythonTool) {
49+
return PythonTool.createPythonToolCallback(PythonTool.DESCRIPTION);
50+
}
51+
52+
}
53+

0 commit comments

Comments
 (0)