IT资讯 Spring AI 1.0.0 M1 发布

adam · 2024-05-31 10:00:09 · 热度: 15

Spring AI 1.0.0 Milestone 1 版本现已发布,具有以下新功能和改进:

ChatClient Fluent API

Fluent API 提供了构造 Prompt 的方法,然后将其作为输入传递给 AI 模型。用户可以使用ChatClient.Builder对象创建一个ChatClient。可以从 Spring Boot 自动配置中获取自动配置的ChatClient.Builder对象,也可以通过编程方式创建一个。

示例:

@RestController
class MyController {

    private final ChatClient chatClient;

    MyController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    @GetMapping("/ai")
    String generation(String userInput) {
        return this.chatClient.prompt()
            .user(userInput)
            .call()
            .content();
    }
}

user方法设置 prompt 的用户文本。call方法调用 AI 模型,提供各种重载以返回响应。在本例中,content方法返回一个字符串。

还可以响应式调用 AI 模型(using WebClient under the covers),如下所示。

Flux<String> output = chatClient.prompt()
    .user("Tell me a joke")
    .stream()
    .content();

一个常见用例是从对 AI 模型的调用返回 JSON。使用entity方法可以轻松实现这一点。

record ActorFilms(String actor, List<String> movies) {
}

ActorFilms actorFilms = chatClient.prompt()
    .user("Generate the filmography for a random actor.")
    .call()
    .entity(ActorFilms.class);

@Configuration类中创建时ChatClient,可以指定默认值,例如系统文本。这种设计时和运行时的分离使得运行时代码最少,只需要属性占位符值。例如:

@Configuration
class Config {

    @Bean
    ChatClient chatClient(ChatClient.Builder builder) {
        return builder.defaultSystem("You are a friendly chat bot that answers question in the voice of a {voice}")
                .build();
    }

}

@RestController
class AIController {
	private final ChatClient chatClient
	AIController(ChatClient chatClient) {
		this.chatClient = chatClient;
	}
	@GetMapping("/ai")
	Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message, String voice) {
		return Map.of(
				"completion",
				chatClient.prompt()
						.system(sp -> sp.param("voice", voice))
						.user(message)
						.call()
						.content());
	}
}

评估(Evaluation)

帮助解决 AI 幻觉问题。用户可以使用一个 AI 模型来评估另一个 AI 模型的响应是否准确。初步使用的是一个简单的RelevancyEvaluator,但事实证明也非常有用,在 1.0 M2 中还会有所新增。

dataController.delete();
dataController.load();

String userText = "What is the purpose of Carina?";

ChatResponse response = ChatClient.builder(chatModel)
        .build().prompt()
        .advisors(new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults()))
        .user(userText)
        .call()
        .chatResponse();

var relevancyEvaluator = new RelevancyEvaluator(ChatClient.builder(chatModel));

EvaluationRequest evaluationRequest = new EvaluationRequest(userText,
        (List<Content>) response.getMetadata().get(QuestionAnswerAdvisor.RETRIEVED_DOCUMENTS), response);

EvaluationResponse evaluationResponse = relevancyEvaluator.evaluate(evaluationRequest);

assertTrue(evaluationResponse.isPass(), "Response is not relevant to the question");

新的 AI 模型

更新模型

  • OpenAI - 支持 GPT4o 和多模态
  • Google Gemini Pro 1.5 pro、flash 等
  • Anthropic function calling
  • Ollama 多模态支持
  • 改进了 streaming function calling 支持 - Grogdunn

新的 Vector Stores

Test Container 支持

Spring AI 提供 Testcontainer 支持,用于在测试中或通过 docker compose 启动向量存储数据库。

更多详情可查看官方博客

为您推荐与 spring 相关的帖子:

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册