Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>aliyunmaven</id>
<name>aliyun</name>
Expand Down
19 changes: 19 additions & 0 deletions spring-ai-alibaba-function-calling-example/README.md
Original file line number Diff line number Diff line change
@@ -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}
```
10 changes: 7 additions & 3 deletions spring-ai-alibaba-function-calling-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,28 @@

<properties>
<junit-jupiter.version>5.10.0</junit-jupiter.version>
<spring-ai-alibaba.version>1.0.0-M5.1</spring-ai-alibaba.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-function-calling-microsofttranslate</artifactId>
<artifactId>spring-ai-alibaba-starter-tool-calling-baidutranslate</artifactId>
<version>${spring-ai-alibaba.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-function-calling-weather</artifactId>
<artifactId>spring-ai-alibaba-starter-tool-calling-weather</artifactId>
<version>${spring-ai-alibaba.version}</version>
</dependency>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>1.0.0-M6.2-SNAPSHOT</version>
<version>1.0.0-M6.1</version>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>1.0.0-M6.2-SNAPSHOT</version>
<version>1.0.0-M6.1</version>
</dependency>

</dependencies>
Expand Down