From 9977383896d574bcf7b83ea44ecbf4ddca38c787 Mon Sep 17 00:00:00 2001 From: chickenlj Date: Fri, 21 Mar 2025 14:34:05 +0800 Subject: [PATCH 1/2] update function calling and mcp examples --- pom.xml | 8 +++ .../README.md | 19 +++++ .../pom.xml | 18 ++++- .../MicroSoftTranslateTest.java | 52 -------------- ...ation.java => ToolCallingApplication.java} | 4 +- .../cloud/ai/functioncalling/WeatherTest.java | 50 ------------- .../controller/ToolCallingController.java | 70 +++++++++++++++++++ .../src/main/resources/application.yml | 10 +-- .../client/starter-default-client/pom.xml | 2 +- .../client/starter-webflux-client/pom.xml | 2 +- 10 files changed, 122 insertions(+), 113 deletions(-) create mode 100644 spring-ai-alibaba-function-calling-example/README.md delete mode 100644 spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/MicroSoftTranslateTest.java rename spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/{MainApplication.java => ToolCallingApplication.java} (91%) delete mode 100644 spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/WeatherTest.java create mode 100644 spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/controller/ToolCallingController.java diff --git a/pom.xml b/pom.xml index 20698a9f..4a5acb45 100644 --- a/pom.xml +++ b/pom.xml @@ -173,6 +173,14 @@ false + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + false + + aliyunmaven aliyun diff --git a/spring-ai-alibaba-function-calling-example/README.md b/spring-ai-alibaba-function-calling-example/README.md new file mode 100644 index 00000000..988f971d --- /dev/null +++ b/spring-ai-alibaba-function-calling-example/README.md @@ -0,0 +1,19 @@ +# Tool Calling Example + +This example showcases the use of Baidu Translate Tool. + +## How to Run +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/). + +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. + +```yaml +spring: + ai: + alibaba: + toolcalling: + baidutranslate: + enabled: true + app;-id: ${BAIDU_TRANSLATE_APP_ID} + secret-key: ${BAIDU_TRANSLATE_SECRET_KEY} +``` diff --git a/spring-ai-alibaba-function-calling-example/pom.xml b/spring-ai-alibaba-function-calling-example/pom.xml index b98618e4..45506ada 100644 --- a/spring-ai-alibaba-function-calling-example/pom.xml +++ b/spring-ai-alibaba-function-calling-example/pom.xml @@ -36,24 +36,36 @@ 5.10.0 - 1.0.0-M5.1 + + org.springframework.boot + spring-boot-starter-web + + com.alibaba.cloud.ai spring-ai-alibaba-starter + + io.netty + netty-resolver-dns-native-macos + 4.1.119.Final + osx-aarch_64 + + + com.alibaba.cloud.ai - spring-ai-alibaba-starter-function-calling-microsofttranslate + spring-ai-alibaba-starter-tool-calling-baidutranslate ${spring-ai-alibaba.version} com.alibaba.cloud.ai - spring-ai-alibaba-starter-function-calling-weather + spring-ai-alibaba-starter-tool-calling-weather ${spring-ai-alibaba.version} diff --git a/spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/MicroSoftTranslateTest.java b/spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/MicroSoftTranslateTest.java deleted file mode 100644 index cc2055f8..00000000 --- a/spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/MicroSoftTranslateTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.cloud.ai.functioncalling; - -import org.jetbrains.annotations.NotNull; -import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.ai.chat.client.ChatClient; - -/** - * @author 北极星 - */ -public class MicroSoftTranslateTest { - - private static final Logger log = LoggerFactory.getLogger(MicroSoftTranslateTest.class); - - private final ChatClient chatClient; - - public MicroSoftTranslateTest (@NotNull ChatClient.Builder chatClientBuilder) { - this.chatClient = chatClientBuilder.build(); - } - - /** - * 微软翻译 - * - * @link - * 版本号 version 3.0 - * ApplicationYml spring.ai.alibaba.functioncalling.microsofttranslate 传入 api-key - */ - @Test - protected void microSoftTranslateFunctionCallingTest () { - String text = "你好,spring-ai-alibaba!"; - - String ans = chatClient.prompt().functions("microSoftTranslateFunction").user(text).call().content(); - log.info("translated text -> : ${}", ans); - } -} diff --git a/spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/MainApplication.java b/spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/ToolCallingApplication.java similarity index 91% rename from spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/MainApplication.java rename to spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/ToolCallingApplication.java index bd344fce..611e77af 100644 --- a/spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/MainApplication.java +++ b/spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/ToolCallingApplication.java @@ -23,9 +23,9 @@ * @author 北极星 */ @SpringBootApplication -public class MainApplication { +public class ToolCallingApplication { public static void main (String[] args) { - SpringApplication.run(MainApplication.class, args); + SpringApplication.run(ToolCallingApplication.class, args); } } diff --git a/spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/WeatherTest.java b/spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/WeatherTest.java deleted file mode 100644 index 52a31653..00000000 --- a/spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/WeatherTest.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.cloud.ai.functioncalling; - -import org.jetbrains.annotations.NotNull; -import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.ai.chat.client.ChatClient; - -/** - * @author 北极星 - */ -public class WeatherTest { - private static final Logger log = LoggerFactory.getLogger(MicroSoftTranslateTest.class); - - private final ChatClient chatClient; - - public WeatherTest (@NotNull ChatClient.Builder chatClientBuilder) { - this.chatClient = chatClientBuilder.build(); - } - - /** - * 天气服务 - * - * @link - * ApplicationYml spring.ai.alibaba.functioncalling.weather 传入 api-key - */ - @Test - protected void microSoftTranslateFunctionCallingTest () { - String text = "你好,spring-ai-alibaba!"; - - String ans = chatClient.prompt().functions("getWeatherServiceFunction").user(text).call().content(); - log.info("weather text -> : ${}", ans); - } -} diff --git a/spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/controller/ToolCallingController.java b/spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/controller/ToolCallingController.java new file mode 100644 index 00000000..fa983830 --- /dev/null +++ b/spring-ai-alibaba-function-calling-example/src/main/java/com/alibaba/cloud/ai/functioncalling/controller/ToolCallingController.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.cloud.ai.functioncalling.controller; + +import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions; + +import org.springframework.ai.chat.client.ChatClient; +import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor; +import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor; +import org.springframework.ai.chat.memory.InMemoryChatMemory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/tool") +public class ToolCallingController { + + private static final String DEFAULT_PROMPT = "你是一个博学的智能聊天助手,请根据用户提问回答!"; + + private final ChatClient dashScopeChatClient; + + // 也可以使用如下的方式注入 ChatClient + public ToolCallingController(ChatClient.Builder chatClientBuilder) { + this.dashScopeChatClient = chatClientBuilder + .defaultSystem(DEFAULT_PROMPT) + // 实现 Chat Memory 的 Advisor + // 在使用 Chat Memory 时,需要指定对话 ID,以便 Spring AI 处理上下文。 + .defaultAdvisors( + new MessageChatMemoryAdvisor(new InMemoryChatMemory()) + ) + .defaultTools("baiduTranslateFunction") + // 实现 Logger 的 Advisor + .defaultAdvisors( + new SimpleLoggerAdvisor() + ) + // 设置 ChatClient 中 ChatModel 的 Options 参数 + .defaultOptions( + DashScopeChatOptions.builder() + .withTopP(0.7) + .build() + ) + .build(); + } + + /** + * ChatClient 简单调用 + */ + @GetMapping("/translate") + public String simpleChat(@RequestParam(value = "query", defaultValue = "帮我把以下内容翻译成英文:你好,世界。")String query) { + return dashScopeChatClient.prompt(query).tools("baiduTranslateFunction").call().content(); + } + +} diff --git a/spring-ai-alibaba-function-calling-example/src/main/resources/application.yml b/spring-ai-alibaba-function-calling-example/src/main/resources/application.yml index bc78db66..d71886b9 100644 --- a/spring-ai-alibaba-function-calling-example/src/main/resources/application.yml +++ b/spring-ai-alibaba-function-calling-example/src/main/resources/application.yml @@ -1,9 +1,11 @@ spring: ai: alibaba: - plugin: - translate: - api-key:${Translate-key} + toolcalling: + baidutranslate: + enabled: true + app;-id: ${BAIDU_TRANSLATE_APP_ID} + secret-key: ${BAIDU_TRANSLATE_SECRET_KEY} dashscope: - api-key: ${dashscope-key} + api-key: ${AI_DASHSCOPE_API_KEY} diff --git a/spring-ai-alibaba-mcp-example/starter-example/client/starter-default-client/pom.xml b/spring-ai-alibaba-mcp-example/starter-example/client/starter-default-client/pom.xml index 89488436..4e536eae 100644 --- a/spring-ai-alibaba-mcp-example/starter-example/client/starter-default-client/pom.xml +++ b/spring-ai-alibaba-mcp-example/starter-example/client/starter-default-client/pom.xml @@ -25,7 +25,7 @@ com.alibaba.cloud.ai spring-ai-alibaba-starter - 1.0.0-M6.2-SNAPSHOT + 1.0.0-M6.1 diff --git a/spring-ai-alibaba-mcp-example/starter-example/client/starter-webflux-client/pom.xml b/spring-ai-alibaba-mcp-example/starter-example/client/starter-webflux-client/pom.xml index 38d6b9de..da132511 100644 --- a/spring-ai-alibaba-mcp-example/starter-example/client/starter-webflux-client/pom.xml +++ b/spring-ai-alibaba-mcp-example/starter-example/client/starter-webflux-client/pom.xml @@ -25,7 +25,7 @@ com.alibaba.cloud.ai spring-ai-alibaba-starter - 1.0.0-M6.2-SNAPSHOT + 1.0.0-M6.1 From 5ddb9c195973be54dbdff2205bfb08af30d39402 Mon Sep 17 00:00:00 2001 From: chickenlj Date: Fri, 21 Mar 2025 14:35:28 +0800 Subject: [PATCH 2/2] update --- spring-ai-alibaba-function-calling-example/pom.xml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/spring-ai-alibaba-function-calling-example/pom.xml b/spring-ai-alibaba-function-calling-example/pom.xml index 45506ada..5816bd43 100644 --- a/spring-ai-alibaba-function-calling-example/pom.xml +++ b/spring-ai-alibaba-function-calling-example/pom.xml @@ -49,14 +49,6 @@ spring-ai-alibaba-starter - - io.netty - netty-resolver-dns-native-macos - 4.1.119.Final - osx-aarch_64 - - - com.alibaba.cloud.ai spring-ai-alibaba-starter-tool-calling-baidutranslate