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