太強了!Spring AI調(diào)用本地函數(shù),實時獲取最新數(shù)據(jù)
環(huán)境:SpringBoot3.4.2
1. 簡介
當我們問大模型當前的天氣情況時,它通常是無法直接給出準確答案的。如下示例,我們通過阿里的 "qwen-turbo" 模型問當前天氣情況時,輸出的結(jié)果如下:
圖片
在這種情況下,如果大模型被設(shè)計為能夠與外部工具進行交互(即工具調(diào)用),它就可以請求一個專門用于天氣查詢的工具來獲取當前的天氣情況。然后,模型可以處理這個工具返回的數(shù)據(jù),并以自然語言的形式將天氣信息傳達給我們。
函數(shù)調(diào)用(Spring AI中已改為工具調(diào)用)是AI應(yīng)用中的一種常見模式,它允許模型與一組API或工具進行交互,從而增強其能力。
工具主要用于以下兩個方面:
- 信息檢索:此類別的工具可用于從外部源(如數(shù)據(jù)庫、網(wǎng)絡(luò)服務(wù)、文件系統(tǒng)或網(wǎng)頁搜索引擎)檢索信息。其目標是增強模型的知識儲備,使其能夠回答原本無法回答的問題。因此,它們可以用于檢索增強生成(RAG)場景。例如,可以使用工具檢索給定位置的當前天氣、查詢數(shù)據(jù)庫中的特定記錄。
- 執(zhí)行操作:此類別的工具可用于在軟件系統(tǒng)中執(zhí)行操作,如發(fā)送電子郵件、在數(shù)據(jù)庫中創(chuàng)建新記錄、提交表單或觸發(fā)工作流。其目標是自動化那些原本需要人工干預(yù)或明確編程的任務(wù)。例如,可以使用工具為與聊天機器人交互的客戶預(yù)訂航班、填寫網(wǎng)頁上的表單等等。
Spring AI提供了便捷的API來定義工具、解析來自模型的工具調(diào)用請求并執(zhí)行工具調(diào)用。如下圖是函數(shù)調(diào)用(工具調(diào)用)在整個問答流程中的調(diào)用原理:
The main sequence of actions for tool calling
接下來,我們將通過2個示例來講解如何通過Spring AI的工具調(diào)用(函數(shù)調(diào)用)來增強大模型的輸出能力。
2. 實戰(zhàn)案例
2.1 環(huán)境準備
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>1.0.0-M6.1</version>
</dependency>
配置如下:
spring:
ai:
dashscope:
api-key: sk-0c06a5b215********d6250417
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
chat:
options:
model: qwen-turbo
我們使用阿里的大模型 "qwen-turbo" 。
2.2 獲取當前時間
首先,我們編寫如下的接口,獲取當前的時間。
@RestController
@RequestMapping("/tools")
public class ToolController {
private final ChatClient chatClient ;
public ToolController(ChatClient.Builder aiClientBuilder) {
this.chatClient = aiClientBuilder.build() ;
}
@GetMapping("/getDate")
public ResponseEntity<?> getDate(String prompt) {
String response = this.chatClient
.prompt(prompt)
.call().content() ;
return ResponseEntity.ok(response) ;
}
}
我們先直接訪問該接口,如下:
無法回答當前時間,接下來,我們通過定義本地的工具來增強模型的輸出能力。
public class DateTimeTools {
@Tool(description = "獲取用戶時區(qū)中的當前日期和時間")
String getCurrentDateTime() {
System.err.println("DateTimeTools被調(diào)用了") ;
return LocalDateTime.now()
.atZone(LocaleContextHolder.getTimeZone().toZoneId())
.toString() ;
}
}
@Tool 注解說明:
- name:工具的名稱。如果未提供,將使用方法名作為工具名稱。AI模型在調(diào)用工具時會使用這個名稱來識別它。因此,在同一個類中不允許有兩個具有相同名稱的工具。對于特定的聊天請求,該名稱必須在模型可用的所有工具中是唯一的。
- description:工具的描述,模型可以使用這個描述來理解何時以及如何調(diào)用該工具。如果未提供,將使用方法名作為工具描述。但是,強烈建議提供一個詳細的描述,因為這對于模型理解工具的目的以及如何使用它至關(guān)重要。如果未能提供良好的描述,可能會導(dǎo)致模型在應(yīng)該使用工具時未使用,或者錯誤地使用工具。
- returnDirect:工具的結(jié)果是否應(yīng)該直接返回給客戶端,還是傳回給模型。
- resultConverter:用于將工具調(diào)用的結(jié)果轉(zhuǎn)換為字符串對象(以便發(fā)送回AI模型)的ToolCallResultConverter實現(xiàn)。
下一步,我們需要將上面的工具添加到ChatClient中
@GetMapping("/getDate")
public ResponseEntity<?> getDate(String prompt) {
String response = this.chatClient
.prompt(prompt)
.tools(new DateTimeTools())
.call().content() ;
return ResponseEntity.ok(response) ;
}
通過tools方法配置我們上面定義的工具,這里你可以設(shè)置多個工具。
如上配置后,我們接下來就可以請求了。
正確的輸出了結(jié)果,通過控制臺的輸出也說明調(diào)用了我們本地配置的工具(函數(shù))。
2.3 獲取天氣情況
接下來,我們再定義一個工具,該工具用來訪問天氣情況。該工具要求接收一個參數(shù),將會根據(jù)輸入?yún)?shù)中的地理位置獲取當前的天氣。
public class WeatherTools {
@Tool(description = "獲取當前天氣預(yù)報")
String getCurrentWeather(String city) {
RestClient client = RestClient.create(
URI.create("https://api.vvhan.com")) ;
Map<?, ?> result = client.get()
.uri("/api/weather?city={0}", city)
.retrieve()
.body(Map.class) ;
try {
return new ObjectMapper().writeValueAsString(result) ;
} catch (JsonProcessingException e) {
throw new RuntimeException(e) ;
}
}
}
這里非常的簡單,我們通過RestTemplate來請求API接口獲取天氣情況。
配置工具
@GetMapping("/weather")
public ResponseEntity<String> getCurrentWeather(String prompt) {
String response = this.chatClient
.prompt(prompt)
.tools(new WeatherTools())
.call().content() ;
return ResponseEntity.ok(response) ;
}
請求該接口
成功獲取到了天氣情況
我們還可以將參數(shù)構(gòu)造為對象類型,如下示例:
// 請求參數(shù)
public record WeatherRequest(@ToolParam(description = "城市", required = true) String city) {
}
// 響應(yīng)結(jié)果
public record WeatherResponse(String info) {
}
工具定義
@Tool(description = "獲取當前天氣預(yù)報")
WeatherResponse getCurrentWeather(WeatherRequest request) {
RestClient client = RestClient.create(URI.create("https://api.vvhan.com")) ;
Map<?, ?> result = client.get()
.uri("/api/weather?city={0}", request.city())
.retrieve()
.body(Map.class) ;
return new WeatherResponse(new ObjectMapper().writeValueAsString(result)) ;
}
2.4 使用函數(shù)作為工具
Spring AI 為從函數(shù)指定工具提供了內(nèi)置支持,內(nèi)部會將一個函數(shù)式類型(Function、Supplier、Consumer 或 BiFunction)轉(zhuǎn)換為一個工具。
我們可以將任何 Function、Supplier、Consumer 或 BiFunction bean 用作工具。bean 的名稱將用作工具的名稱,并且可以使用 Spring 中的 @Description 注解為工具提供描述,該描述由模型使用,以了解何時以及如何調(diào)用工具。
函數(shù)的輸入和輸出可以是 Void 或 POJO(Plain Old Java Object,簡單的 Java 對象)。輸入和輸出的 POJO 必須是可序列化的,因為結(jié)果將被序列化并發(fā)送回模型。函數(shù)以及輸入和輸出類型都必須是public的。
我們這里還是以獲取天氣為例講解,如何使用:
public static final String CURRENT_WEATHER = "currentWeather" ;
@Bean(CURRENT_WEATHER)
@Description("獲取當前天氣預(yù)報")
Function<WeatherRequest, WeatherResponse> currentWeather() {
RestClient client = RestClient.create(
URI.create("https://api.vvhan.com")) ;
return city -> {
String result = client.get()
.uri("/api/weather?city={0}", city)
.retrieve()
.body(String.class) ;
return new WeatherResponse(result) ;
} ;
}
注冊工具
@GetMapping("/weather")
public ResponseEntity<String> getCurrentWeather(String prompt) {
String response = this.chatClient
.prompt(prompt)
// 這里是我們的beanName
.tools(WeatherTools.CURRENT_WEATHER)
.call().content() ;
return ResponseEntity.ok(response) ;
}
調(diào)用結(jié)果
圖片