惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
IT之家
IT之家
博客园_首页
量子位
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
博客园 - 聂微东
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
宝玉的分享
宝玉的分享
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

Liu Zijian's Blog | 一个技术博客

使用Certbot自动续签HTTPS证书 使用Filebeat采集Nginx日志到ES Python的协程 Python中的异常 Python中的类和对象 Python的函数 Python的数据结构,推导式、迭代器和生成器 Spring AI集成多模态模型 LangChain4j多模态 LangChain Tools工具使用 Python中的模块和包 Python全局环境和虚拟环境(venv) LangChain Prompt提示词工程 LangChain4j Tools工具使用 基于Dify搭建AI智能体应用 LangChain4j RAG检索增强生成 Spring AI实现MCP Server Spring AI集成MCP Client LangChain4j Prompt提示词工程 Spring AI使用知识库增强对话功能 Spring AI实现一个智能客服 Spring AI实现一个简单的对话机器人 实现MinIO数据的每日备份 自己实现一个DNS服务 简单理解AI智能体 大模型和大模型应用 LangChain开篇 LangChain4j开篇 一个解析Excel2007的POI工具类 DataPermissionInterceptor源码解读
Spring AI开篇
Liu Zijian · 2025-02-15 · via Liu Zijian's Blog | 一个技术博客

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

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

<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>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>21</source>
                <target>21</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

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)
3 Spring AI使用知识库增强对话功能 向量相似度,嵌入模型,向量数据库,RAG
4 Spring AI集成多模态模型 多模态模型(视觉,音频)

3.2 MCP相关

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