領先一步
VMware 提供培訓和認證,助您加速進步。
瞭解更多我愛 Spring AI。這是一個很棒的專案,旨在將 AI 工程的模式和實踐帶給 Spring Boot 開發者。它提供了簡潔、慣用的抽象,讓任何 Spring 開發者都能感到賓至如歸,並且它還集成了各種不同的向量儲存、嵌入模型、轉錄模型、影像模型和聊天模型。
新版本 m4,基於 Spring Boot 3.4 構建,並增加了大量新功能。一如既往,我無法涵蓋所有新功能,但釋出說明做得非常出色。
The evolving nature of the functional callback support has so enamored me. Spring AI aims to make connecting your AI models with your data and business logic easy. Remember: the name of the game here is integration. Most people aren’t going to build their models. They’re going to integrate existing ones into their business logic and services. And where does all that stuff live? In Spring, of course. Spring AI is a natural! And it keeps getting easier. In this release, there’s new support for describing and then letting models invoke functions if they decide they’ve got the need to do so.
Here’s a simple example demonstrating the definition of a FunctionCallback and the Spring AI ChatClient, which is your first port of call for all interactions with a 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) {
}
It’s a pretty trivial example: given a WeatherRequest specifying a city, we make up and return some temperature. In this case, I’ve got a hardcoded case for San Francisco.
We put all of this to work in the test, knowing that the model won’t know the current weather in a given city, and so we will have to defer to a function that we provide. It knows about the nature of the function because we’ve given it a description when we configured the FunctionCallback. It knows that the city parameter is Stringand that San Francisco is a city, so it passes the stringSan Francisco` to our function, allowing us to provide the expected response. We verify as much with the test, asserting that the response is the hardcoded magic number.
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"));
}
}
And just like that, we’ve given our AI model the ability to ask questions about our systems and services and to support a more agentic workflow. Easy!