Spring Boot + Spring AI快速体验

Spring AI快速体验
  • [1 什么是Spring AI](#1 什么是Spring AI)
  • [2 快速开始](#2 快速开始)
    • [2.1 版本说明](#2.1 版本说明)
    • [2.2 配置文件](#2.2 配置文件)
    • [2.3 pom依赖](#2.3 pom依赖)
      • [2.3.1 spring maven仓库](#2.3.1 spring maven仓库)
      • [2.3.2 核心依赖](#2.3.2 核心依赖)
    • [2.4 定义ChatClient](#2.4 定义ChatClient)
    • [2.5 启动类](#2.5 启动类)
    • [2.6 测试](#2.6 测试)
  • [3 参考链接](#3 参考链接)

1 什么是Spring AI

Spring AI是Spring的一个子项目,是Spring专门面向于AI的应用框架。

Spring AI 项目旨在简化整合人工智能功能的应用程序开发,避免不必要的复杂性。

汲取了著名的 Python 项目 LangChain 和 LlamaIndex 的灵感,但 Spring AI 并不是这些项目的直接移植。该项目的成立的信念:下一波生成式人工智能应用程序不仅将面向Python开发人员,而且将在许多编程语言中无处不在。

主要功能

● 跨 AI 供应商的便携式 API:支持聊天、文生图、嵌入模型;支持同步和流式API选项;访问特定模型功能。

● 支持几乎所有的ai模型提供商:如如 Anthropic、OpenAI、Microsoft、Amazon、Google 和 Ollama,包括国内的千帆、智谱AI等

● 结构化输出:将AI模型输出映射为POJO;

● 支持所有主要向量数据库。

● 功能调用(Tools/Function Calling):工具/功能调用-允许模型请求执行客户端工具和功能,从而根据需要访问必要的实时信息。

● 可观测

● springboot自动装配

● 支持聊天对话记忆和检索增强生成(RAG)

2 快速开始

2.1 版本说明

工具

版本

jdk

17

Spring Boot

3.2.x、3.3.x

Spring AI

1.0.0-SNAPSHOT

2.2 配置文件

注意:使用官方直连的api-key、url;或者使用基于转发的api-key、url。主要区别是官方的价格较贵,需要在程序中设置代理连接;转发类的国内可直接使用,价格便宜,适合学习使用。

2.3 pom依赖

2.3.1 spring maven仓库
<repositories>
    <!-- Spring Milestones 仓库 -->
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>

    <!-- Spring Snapshots 仓库 -->
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>
</repositories>
2.3.2 核心依赖
<!--open ai-->
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<!--Spring AI 物料清单(BOM)声明了 Spring AI 发布版本使用的所有依赖项的推荐版本。
    使用应用程序构建脚本中的 BOM 可以避免自行指定和维护依赖项版本的需要。-->
<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>

2.4 定义ChatClient

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

@Configuration
class Config {
    @Bean
    ChatClient chatClient(ChatClient.Builder builder) {
        return builder.build();
    }
}

2.5 启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringAiDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringAiDemoApplication.class, args);
    }
}

2.6 测试

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class ChatController {

    @Autowired
    private ChatClient chatClient;


    @GetMapping("/chat")
    public String chat(@RequestParam(value = "message", defaultValue = "Hi") String message) {
        return chatClient.prompt()
                .user(message)
                .call()
                .content();
    }
}

文本返回:

流式返回(打字机效果):

@GetMapping(value = "/chat/flux", produces = MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")
public Flux<String> chatFlux(@RequestParam(value = "message", defaultValue = "Hi") String message) {
    Flux<String> output = chatClient.prompt()
            .user(message)
            .stream()
            .content();
    return output;
}

完整代码可参考:https://github.com/xgxizz/spring-ai-demo

3 参考链接

https://docs.spring.io/spring-ai/reference/

相关推荐
智识世界Intelligence5 分钟前
人工智能搜索的层级发展趋势:从信息检索到智能决策
人工智能·深度学习·自然语言处理·知识图谱·学习方法
B站计算机毕业设计超人31 分钟前
计算机毕业设计Python+大模型疲劳驾驶检测系统 自动驾驶 面部多信息特征融合的疲劳驾驶检测系统 驾驶员疲劳驾驶风险检测 深度学习 机器学习 大数据
人工智能·python·深度学习·机器学习·自动驾驶·课程设计·数据可视化
Quz1 小时前
OpenCV:图像轮廓
图像处理·人工智能·opencv·计算机视觉
计算机-秋大田1 小时前
云上考场微信小程序的设计与实现(LW+源码+讲解)
java·前端·spring boot·微信小程序·小程序·课程设计
Quz1 小时前
OpenCV:特征检测总结
图像处理·人工智能·opencv·计算机视觉
用心把天空锁住1 小时前
从零开始:OpenCV 图像处理快速入门教程
图像处理·人工智能·opencv·计算机视觉
四口鲸鱼爱吃盐2 小时前
CVPR2021 | VMI-FGSM & VNI-FGSM | 通过方差调整增强对抗攻击的可迁移性
人工智能·深度学习·神经网络·机器学习·计算机视觉·对抗样本
Lzehui2 小时前
spring boot接收请求常用注解
java·数据库·spring boot
deephub3 小时前
DeepSeek技术报告解析:为什么DeepSeek-R1 可以用低成本训练出高效的模型
人工智能·python·深度学习·机器学习·deepseek
飞翔的佩奇3 小时前
Java项目: 基于SpringBoot+mybatis+maven+mysql实现的装饰工程管理系统(含源码+数据库+毕业论文)
java·数据库·spring boot·mybatis·ssm·毕设·装饰工程