无亟 发表于 2025-4-2 12:45:54

使用 Spring AI 在 Spring Boot 中构建生成式 AI 应用

苟日新,日日新,又日新。

生活从不眷顾因循守旧、满足现状者,从不等待不思进取、坐享其成者,而是将更多的机遇留给善于和勇于创新的人们。

在人工智能飞速发展的今天,无论是 AI 相关的技术,还是 AI 相关的产品,都是层出不穷、日新月异。

简介
Spring AI 是一个基于Spring生态的 AI 接入组件,简化人工智能应用的开发流程,提供了抽象接口,作为开发 AI 应用程序的基础。 这些抽象具有多种实现,支持以最少的代码更改轻松交换组件。

它为 Java 开发者提供强大的 AI 接入能力,让开发者可以在 Spring 项目中轻松调用各类 AI 接口,它支持主流的 AI 模型提供商,例如:Anthropic、OpenAI、Microsoft、Amazon、Google 和 Ollama 等。

环境准备
需要准备Java、Maven环境。

Java 17或更高版本
Maven 3.6/gradle 或更高版本
Spring Boot 3.2.x and 3.3.x
安装
在此之前请初始化一个 Spring Boot 项目:https://start.spring.io/ 。

在 pom.xml 中指定 spring io 仓库:
<repositories>
<repository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
</repository>
<repository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <releases>
      <enabled>false</enabled>
    </releases>
</repository>
</repositories>添加依赖管理器:
<dependencyManagement>
    <dependencies>
      <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
      </dependency>
    </dependencies>
</dependencyManagement>然后选择一个模型,并配置相关依赖。

这里以 ollama 为例,请参考 Ollama 官网,安装Ollama,并部署运行一个模型,这里以 deepseek 模型为例:
ollama run deepseek-r1:1.5b在 pom.xml 添加ollama依赖:
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>配置
使用 Ollama,有很多配置,可以参考官网:https://docs.spring.io/spring-ai/reference/api/chat/ollama-chat.html

这里只需要简单配置一下使用的模型即可,地址和端口都是默认的,也可以根据配置指定,在 application.properties 配置中添加:
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.options.model=deepseek-r1:1.5b控制器在项目中创建一个controller,提供一个交互的接口:
package com.example.demo.springai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
class MyController {

    private final ChatClient chatClient;

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

    @GetMapping("/ai")
    String generation(String userInput) {
      return this.chatClient.prompt()
            .user(userInput)
            .call()
            .content();
    }
}
如果需要流式输出,可以将call()改成stream,就像这样:
    @GetMapping("/ai")
    Flux<String> generation(String userInput) throws UnsupportedEncodingException {
      return this.chatClient.prompt()
            .user(userInput)
            .stream()
            .content();
    }这里提供了一个/ai的接口,接收userInput输入参数,然后调用模型交互返回内容。

启动
启动Spring Boot项目,然后可以使用浏览器访问http://localhost:8080/ai?userInput=springai,就可以看到AI的回复了。

最后
除此之外,Spring Boot还支持主流的矢量数据、向量存储、模型评估等功能。目前来看,Spring Boot显然已是Java开发者接入AI应用的必备利器。

抢走你饭碗的不是AI,而是会用AI的人。



页: [1]
查看完整版本: 使用 Spring AI 在 Spring Boot 中构建生成式 AI 应用