领先一步
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 模型询问关于我们的系统和服务的问题,并支持更智能的工作流程的能力。 简单!