Bootiful Spring Boot 3.4:Spring AI

工程 | Josh Long | 2024 年 11 月 24 日 | ...

我愛 Spring AI。這是一個很棒的專案,旨在將 AI 工程的模式和實踐帶給 Spring Boot 開發者。它提供了簡潔、慣用的抽象,讓任何 Spring 開發者都能感到賓至如歸,並且它還集成了各種不同的向量儲存、嵌入模型、轉錄模型、影像模型和聊天模型。

新版本 m4,基於 Spring Boot 3.4 構建,並增加了大量新功能。一如既往,我無法涵蓋所有新功能,但釋出說明做得非常出色。

  • 增加了對 Amazon Bedrock Converses 的新支援
  • 在 Java 和 Kotlin 中,對更具表現力的函式呼叫進行了大量工作
  • 這是對 AI 社群中關於“高階和模組化 RAG”的想法的首次實現。RAG,當然是檢索增強生成,指的是使用系統或服務中的資料來告知聊天模型生成的響應。這些想法在本 篇論文這篇論文中有所闡述,而構建塊正在這個新版本中生根發芽。太棒了!
  • across the board to various vector store integrations and chat models
  • there’saeven a comprehensive chat model comparison page in the documentation
  • vector storage and embedding improvements, including enhancements to vector stores like Azure and Milvus

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!

獲取 Spring 新聞通訊

透過 Spring 新聞通訊保持聯絡

訂閱

領先一步

VMware 提供培訓和認證,助您加速進步。

瞭解更多

獲得支援

Tanzu Spring 提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位制檔案,只需一份簡單的訂閱。

瞭解更多

即將舉行的活動

檢視 Spring 社群所有即將舉行的活動。

檢視所有