Spring AI开篇

本文最后更新于 2025年11月9日

1.Spring AI概述

Spring AI(https://docs.spring.io/spring-ai/reference/index.html)是一款Spring官方推出的一款Java调用大模型的工具,用于开发基于Java语言的大模型应用,它能很好的支持一些主流的大模型:比如OpenAI、DeepSeek、Microsoft、Amazon、Google 和 Ollama,覆盖聊天,文生图,音频等多种模型,同时也支持向量数据库:例如Apache Cassandra,Redis等。支持开发Prompt聊天,RAG知识库,Agent(Function calling)智能体等多种模式的大模型应用,作为Spring家族的产品,Spring AI充分利用了Spring Boot的一些特性,大大的简化了开发。

与Spring AI类似的框架还有LangChain4j,截至成文日期,Spring AI对比LangChain4j有以下区别:

Spring AI LangChain4j
Chat 支持 支持
Function 支持 支持
RAG 支持 支持
对话模型 15+ 15+
向量模型 10+ 15+
向量数据库 15+ 20+
多模态模型(图像,音频) 5+ 1
JDK 17+ 1.8,最新版本也要17+

2.快速开始

基于jdk-21创建spring-boot项目,引入spring-boot依赖3.5.7,spring-ai依赖1.0.3,以及整合DeepSeek的spring-ai-starter-model-deepseek

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spring-ai</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.5.7</version>
    </parent>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>1.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-deepseek</artifactId>
        </dependency>
    </dependencies>
    
</project>

application.yml配置中进行配置,并填写DeepSeek的API_KEY

spring:
  ai:
    deepseek:
      base-url: https://api.deepseek.com
      api-key: sk-02**********************d8666

编写一个配置类,声明一个对话客户端,并且注入配置好的DeepSeek模型

package org.example;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ModelConfig {

    @Bean
    public ChatClient chatClient(DeepSeekChatModel model) {
        return ChatClient.builder(model).build();
    }
}

编写一个测试类,调用智能助手,通过user()指定用户输入的指令

package org.example.test;

import jakarta.annotation.Resource;
import org.example.Main;
import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = Main.class)
public class ModelTest {

    @Resource
    private ChatClient chatClient;

    @Test
    public void testHello() {
        String content = chatClient.prompt()
                .user("hi,你是谁?")
                .call()
                .content();

        System.out.println(content);
    }
}

执行完成后,会打印出聊天机器人的回复,一个简单的聊天机器人就实现了

要想开发功能全面一些的聊天机器人,还需要考虑会话记忆和会话历史等功能,详见:Spring AI实现一个简单的对话机器人

3.Spring AI的使用

3.1 入门案例

序号 文章名 概述
1 Spring AI实现一个简单的对话机器人 简单Prompt模式
2 Spring AI实现一个智能客服 实现大模型的Function calling(Tools)
2 Spring AI使用知识库增强对话功能 向量相似度,嵌入模型,向量数据库,RAG

3.2 MCP相关

序号 文章名 概述
1 Spring AI集成MCP Client MCP,Spring AI调用MCP,SSE和Stdio模式
2 Spring AI实现 MCP Server Spring AI实现SSE模式MCP服务

"如果文章对您有帮助,可以请作者喝杯咖啡吗?"

微信二维码

微信支付

支付宝二维码

支付宝


Spring AI开篇
https://blog.liuzijian.com/post/spring/2025/02/15/spring-ai/
作者
Liu Zijian
发布于
2025年2月15日
更新于
2025年11月9日
许可协议