Spring AI 开发 - 快速入门

先看效果

项目搭建

Spring AI 是 Spring 推出的一个项目,目标是提供统一的API抽象层,屏蔽不同AI模型和服务的底层差异,实现跨平台兼容性。

演示使用的模型是阿里的 qwq-32b。

环境要求:

JDK :17以上(包括17)

Spring Boot 版本:目前支持 3.4.X,3.5.X 发布后也会同步支持。

主要的 maven 依赖:

XML 复制代码
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
    <version>1.0.0-M6</version>
</dependency>

配置文件:

qwq-32b的接口类似于**openai ,**所以直接使用 openai 的配置即可,其他类 openai 的模型也是类似。

java 复制代码
spring:
  ai:
    openai:
      api-key: #申请的 key
      base-url: https://dashscope.aliyuncs.com/compatible-mode #api地址
      chat:
        options:
          model: qwq-32b # 模型名称

编写接口

创建 ChatClient。ChatClient 提供了与 AI 模型通信的 Fluent API,它支持同步和反应式(Reactive)编程模型。

java 复制代码
this.chatClient = ChatClient.builder(chatModel)
        .defaultSystem(DEFAULT_PROMPT)
        .defaultAdvisors(new MessageChatMemoryAdvisor(new InMemoryChatMemory()))
        .defaultAdvisors(new SimpleLoggerAdvisor())
        .defaultOptions(OpenAiChatOptions.builder().temperature(0.7d).build())
        .build();

创建接口,使用 SSE 响应结果,SSE 是一个单向的、从服务端向客户端推送数据的技术。使用SSE可以实时的将大模型的结果推送到前端。

java 复制代码
@GetMapping
public SseEmitter chat(String message) {
    SseEmitter emitter = new SseEmitter();
    // 先把结果返回,后续逐渐响应结果
    CompletableFuture.runAsync(() -> {
        chatClient.prompt(message).stream()
                .content()
                .doOnComplete(emitter::complete)
                .subscribe(result -> {
                    try {
                        emitter.send(result);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                });
    });


    return emitter;
}

完整的接口代码如下:

java 复制代码
package cn.salim.ai.controller;


import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;


import java.io.IOException;
import java.util.concurrent.CompletableFuture;


@RestController
@RequestMapping("ai")
public class AiController {
    private static final String DEFAULT_PROMPT = "你是一个资深Java程序员,你熟悉各种JAVA框架,包括SpringBoot、Mybatis等等";


    private final ChatClient chatClient;


    public AiController(OpenAiChatModel chatModel) {
        this.chatClient = ChatClient.builder(chatModel)
                .defaultSystem(DEFAULT_PROMPT)
                .defaultAdvisors(new MessageChatMemoryAdvisor(new InMemoryChatMemory()))
                .defaultAdvisors(new SimpleLoggerAdvisor())
                .defaultOptions(OpenAiChatOptions.builder().temperature(0.7d).build())
                .build();
    }




    @GetMapping
    public SseEmitter chat(String message) {
        SseEmitter emitter = new SseEmitter();


        CompletableFuture.runAsync(() -> {
            chatClient.prompt(message).stream()
                    .content()
                    .doOnComplete(emitter::complete)
                    .subscribe(result -> {
                        try {
                            emitter.send(result);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    });
        });


        return emitter;
    }




}
相关推荐
Easy_API6 小时前
2.8万亿参数,编程反超GPT-6 Astra:Kimi K3.1 Pro把开源模型推到了哪一步
大数据·人工智能·gpt·深度学习
GlobalInfo6 小时前
2026年机器人多指灵巧手市场分析报告——涵盖行业调查、趋势分析与发展前景
大数据·人工智能·microsoft·微服务·机器人
紧跟先前的步伐6 小时前
Go AI Agent 实战 24|工具系统工程化收尾与阶段验收
人工智能·ai·golang·1024程序员节
学长毕设6 小时前
基于SpringBoot的实验室资产管理系统
java·spring boot·后端
释厄6237 小时前
AI学习层更新与跨体转移律——人类社会同构·不缺理解缺 U
人工智能·学习
老金带你玩AI7 小时前
集万家所长,一键复制的Agent.md!
人工智能
ZhuoAIStage7 小时前
舞台工艺多专业审图提速:用 AI 智能体编排 A+B+C+G 四段读图流水线
人工智能·自动化·工作流编排·舞台工艺·审图智能体
zzu123zsw7 小时前
从臃肿的模板调用到声明式契约:深入 UniHttp 动态代理与多渠道生命周期扩展
java·spring boot·microsoft·unihttp
小奶包他干奶奶8 小时前
AI学习——1、机器学习概念
人工智能·机器学习
展讯快报8 小时前
悬浮提词器免费App评估报告:说得AI的免费策略与可用性分析
人工智能