Skip to content

Commit 151c788

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 192b545 + 560d3aa commit 151c788

File tree

10 files changed

+117
-113
lines changed

10 files changed

+117
-113
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@
173173
<enabled>false</enabled>
174174
</snapshots>
175175
</repository>
176+
<repository>
177+
<id>spring-snapshots</id>
178+
<name>Spring Snapshots</name>
179+
<url>https://repo.spring.io/snapshot</url>
180+
<releases>
181+
<enabled>false</enabled>
182+
</releases>
183+
</repository>
176184
<repository>
177185
<id>aliyunmaven</id>
178186
<name>aliyun</name>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Tool Calling Example
2+
3+
This example showcases the use of Baidu Translate Tool.
4+
5+
More available tools can be found on [this documentation](https://java2ai.com/docs/1.0.0-M5.1/integrations/tools/). For mcp style tools please check [spring-ai-alibaba-mcp-example](../spring-ai-alibaba-mcp-example).
6+
7+
8+
## How to Run
9+
In order to run this example, you need to register and the tokens from Baidu Translate platform first by following [this instruction](https://api.fanyi.baidu.com/).
10+
11+
Once you are all set with Baidu Translate platform, [get the appId and secretKey](](BAIDU_TRANSLATE_APP_ID).) from the platform and set the environments as defined in application.yml.
12+
13+
```yaml
14+
spring:
15+
ai:
16+
alibaba:
17+
toolcalling:
18+
baidutranslate:
19+
enabled: true
20+
app;-id: ${BAIDU_TRANSLATE_APP_ID}
21+
secret-key: ${BAIDU_TRANSLATE_SECRET_KEY}
22+
```

spring-ai-alibaba-function-calling-example/pom.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,28 @@
3636

3737
<properties>
3838
<junit-jupiter.version>5.10.0</junit-jupiter.version>
39-
<spring-ai-alibaba.version>1.0.0-M5.1</spring-ai-alibaba.version>
4039
</properties>
4140

4241
<dependencies>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-web</artifactId>
45+
</dependency>
46+
4347
<dependency>
4448
<groupId>com.alibaba.cloud.ai</groupId>
4549
<artifactId>spring-ai-alibaba-starter</artifactId>
4650
</dependency>
4751

4852
<dependency>
4953
<groupId>com.alibaba.cloud.ai</groupId>
50-
<artifactId>spring-ai-alibaba-starter-function-calling-microsofttranslate</artifactId>
54+
<artifactId>spring-ai-alibaba-starter-tool-calling-baidutranslate</artifactId>
5155
<version>${spring-ai-alibaba.version}</version>
5256
</dependency>
5357

5458
<dependency>
5559
<groupId>com.alibaba.cloud.ai</groupId>
56-
<artifactId>spring-ai-alibaba-starter-function-calling-weather</artifactId>
60+
<artifactId>spring-ai-alibaba-starter-tool-calling-weather</artifactId>
5761
<version>${spring-ai-alibaba.version}</version>
5862
</dependency>
5963

spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/MicroSoftTranslateTest.java

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
* @author 北极星
2424
*/
2525
@SpringBootApplication
26-
public class MainApplication {
26+
public class ToolCallingApplication {
2727

2828
public static void main (String[] args) {
29-
SpringApplication.run(MainApplication.class, args);
29+
SpringApplication.run(ToolCallingApplication.class, args);
3030
}
3131
}

spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/WeatherTest.java

Lines changed: 0 additions & 50 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.alibaba.cloud.ai.functioncalling.controller;
19+
20+
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
21+
22+
import org.springframework.ai.chat.client.ChatClient;
23+
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
24+
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
25+
import org.springframework.ai.chat.memory.InMemoryChatMemory;
26+
import org.springframework.web.bind.annotation.GetMapping;
27+
import org.springframework.web.bind.annotation.RequestMapping;
28+
import org.springframework.web.bind.annotation.RequestParam;
29+
import org.springframework.web.bind.annotation.RestController;
30+
31+
@RestController
32+
@RequestMapping("/tool")
33+
public class ToolCallingController {
34+
35+
private static final String DEFAULT_PROMPT = "你是一个博学的智能聊天助手,请根据用户提问回答!";
36+
37+
private final ChatClient dashScopeChatClient;
38+
39+
// 也可以使用如下的方式注入 ChatClient
40+
public ToolCallingController(ChatClient.Builder chatClientBuilder) {
41+
this.dashScopeChatClient = chatClientBuilder
42+
.defaultSystem(DEFAULT_PROMPT)
43+
// 实现 Chat Memory 的 Advisor
44+
// 在使用 Chat Memory 时,需要指定对话 ID,以便 Spring AI 处理上下文。
45+
.defaultAdvisors(
46+
new MessageChatMemoryAdvisor(new InMemoryChatMemory())
47+
)
48+
.defaultTools("baiduTranslateFunction")
49+
// 实现 Logger 的 Advisor
50+
.defaultAdvisors(
51+
new SimpleLoggerAdvisor()
52+
)
53+
// 设置 ChatClient 中 ChatModel 的 Options 参数
54+
.defaultOptions(
55+
DashScopeChatOptions.builder()
56+
.withTopP(0.7)
57+
.build()
58+
)
59+
.build();
60+
}
61+
62+
/**
63+
* ChatClient 简单调用
64+
*/
65+
@GetMapping("/translate")
66+
public String simpleChat(@RequestParam(value = "query", defaultValue = "帮我把以下内容翻译成英文:你好,世界。")String query) {
67+
return dashScopeChatClient.prompt(query).tools("baiduTranslateFunction").call().content();
68+
}
69+
70+
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
spring:
22
ai:
33
alibaba:
4-
plugin:
5-
translate:
6-
api-key:${Translate-key}
4+
toolcalling:
5+
baidutranslate:
6+
enabled: true
7+
app;-id: ${BAIDU_TRANSLATE_APP_ID}
8+
secret-key: ${BAIDU_TRANSLATE_SECRET_KEY}
79

810
dashscope:
9-
api-key: ${dashscope-key}
11+
api-key: ${AI_DASHSCOPE_API_KEY}

spring-ai-alibaba-mcp-example/starter-example/client/starter-default-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<dependency>
2626
<groupId>com.alibaba.cloud.ai</groupId>
2727
<artifactId>spring-ai-alibaba-starter</artifactId>
28-
<version>1.0.0-M6.2-SNAPSHOT</version>
28+
<version>1.0.0-M6.1</version>
2929
</dependency>
3030

3131
</dependencies>

spring-ai-alibaba-mcp-example/starter-example/client/starter-webflux-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<dependency>
2626
<groupId>com.alibaba.cloud.ai</groupId>
2727
<artifactId>spring-ai-alibaba-starter</artifactId>
28-
<version>1.0.0-M6.2-SNAPSHOT</version>
28+
<version>1.0.0-M6.1</version>
2929
</dependency>
3030

3131
</dependencies>

0 commit comments

Comments
 (0)