領先一步
VMware 提供培訓和認證,以加速您的進步。
瞭解更多我喜歡 Spring AI。這是一個出色的專案,旨在將 AI 工程的模式和實踐帶給 Spring Boot 開發人員。它具有乾淨的、符合語言習慣的抽象,讓任何 Spring 開發人員都感到賓至如歸,並且它與各種不同的向量儲存、嵌入模型、轉錄模型、影像模式和聊天模型進行了大量整合。
新版本 m4 基於 Spring Boot 3.4 構建,並添加了大量新功能。 和往常一樣,我無法檢視所有新功能,但發行說明做得非常出色。
函式回撥支援的不斷發展讓我著迷。 Spring AI 旨在使您的 AI 模型與您的資料和業務邏輯輕鬆連線。 請記住:這裡的關鍵是整合。 大多數人不會構建他們的模型。 他們會將現有模型整合到他們的業務邏輯和服務中。 所有這些東西都存在於哪裡? 當然是在 Spring 中。 Spring AI 是天作之合! 並且它變得越來越容易。 在此版本中,新增了描述然後讓模型在認為需要時呼叫函式的支援。
這是一個簡單的示例,演示了 FunctionCallback
和 Spring AI ChatClient
的定義,ChatClient
是您與 Sprign AI ChatModel
進行所有互動的首選埠。
package com.example.bootiful_34.ai;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
class AiConfiguration {
@Bean
ChatClient chatClient(ChatClient.Builder builder, FunctionCallback weatherFunctionCallback) {
return builder.defaultFunctions(weatherFunctionCallback).build();
}
@Bean
FunctionCallback weatherFunctionCallback() {
return FunctionCallback.builder()
.description("returns the weather for a given city")
.function("getCurrentWeatherForACity",
(WeatherRequest wr) -> new WeatherResponse(wr.city(),
wr.city().equalsIgnoreCase("san francisco") ? 68.0f : 72.0f))
.inputType(WeatherRequest.class)
.build();
}
}
record WeatherRequest(String city) {
}
record WeatherResponse(String city, float temperature) {
}
這是一個非常簡單的示例:給定一個指定城市的 WeatherRequest
,我們編造並返回一些溫度。 在這種情況下,我為舊金山設定了一個硬編碼的案例。
我們將所有這些應用於測試中,因為我們知道該模型不會知道給定城市中的當前天氣,因此我們將不得不求助於我們提供的函式。 它知道該函式的性質,因為我們在配置 FunctionCallback 時給出了描述。 它知道 city 引數是
String,並且舊金山是一個城市,因此它將字串
San Francisco` 傳遞給我們的函式,允許我們提供預期的響應。 我們透過測試驗證了這一點,斷言響應是硬編碼的幻數。
package com.example.bootiful_34.ai;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class AiConfigurationTest {
@Test
void functionCallbacks(@Autowired ChatClient cc) {
var weatherInSf = cc.prompt("give me the weather for the city of san francisco").call().content();
Assertions.assertNotNull(weatherInSf);
Assertions.assertTrue(weatherInSf.contains("68"));
}
}
就這樣,我們賦予了我們的 AI 模型詢問關於我們的系統和服務的問題,並支援更智慧的工作流程的能力。 簡單!