Spring AI 系列之一 - 开篇

之前做个几个大模型的应用,都是使用Python语言,后来有一个项目使用了Java,并使用了Spring AI框架。随着Spring AI不断地完善,最近它发布了1.0正式版,意味着它已经能很好的作为企业级生产环境的使用。对于Java开发者来说真是一个福音,其功能已经能满足基于大模型开发企业级应用。借着这次机会,给大家分享一下Spring AI框架。

注意由于框架不同版本改造会有些使用的不同,因此本次系列中使用基本框架是 Spring AI-1.0.0,JDK版本使用的是19
代码参考: https://github.com/forever1986/springai-study

1 关于Spring AI

在Spring AI的官方网站中,摘录下以下这段话:

The Spring AI project aims to streamline the development of applications that incorporate artificial intelligence functionality without unnecessary complexity.
The project draws inspiration from notable Python projects, such as LangChain and LlamaIndex, but Spring AI is not a direct port of those projects. The project was founded with the belief that the next wave of Generative AI applications will not be only for Python developers but will be ubiquitous across many programming languages.

使用翻译软件翻译结果如下:

Spring AI 项目旨在简化融合人工智能功能的应用程序的开发流程,避免不必要的复杂性。
该项目从诸如 LangChain 和 LlamaIndex 等著名的 Python 项目中汲取灵感,但 Spring AI 并非这些项目的直接移植。该项目创立的初衷在于,认为下一代生成式人工智能应用将不仅面向 Python 开发者,而是会在众多编程语言中普及开来。

可以看出几个关键字:人工智能开发流程复杂性
简单来说就是为Java语言开发人工智能大模型应用提供一个便捷框架,如果使用过LangChain的朋友应该知道,LangChain是一个python语言开发人工智能大模型应用的一个便捷框架。虽然Spring AI 并非这些项目的直接移植,但是基本上和LangChain很像,Spring AI 主页写好很多功能和特性,总结主要以下3点:

  • 跨 AI 提供商的可移植 API 支持:也就是不需要为每个模型开发API,这些在Spring AI都已经封装为统一接口
  • 支持所有主要的 AI 模型提供商:包括不同厂商以及不同类型模型(文本、图像、语音等等)
  • 大模型常见的开发流程支持:提示词、向量数据库、文档提取、工具/函数调用、RAG以及最近很火的MCP

当然LangChain有一个LangChain4j的项目,也是为了Java开发者准备。Spring AI 和LangChain4j整体的功能差不多,当然LangChain4j相对来说是LangChain生态,支持更多的语言。如果项目是Spring底座,那么使用Spring AI会更方便,这两个框架看自己项目情况选择。说了那么多,下面以一个示例为例做一个入门演示

2 入门示例

基于智谱大模型进行API调用。(为什么选择智谱,主要是不要钱。。。哈哈哈)

代码参考lesson01子模块

2.1 前期准备

1)前往智谱的AI开放平台:https://open.bigmodel.cn/usercenter,如果你没有账号,需要注册一下

2)添加API KEY
在这里插入图片描述

说明:这个API KEY就是后续程序中需要用到的认证

2.2 项目创建

1)创建一个父项目springai-study,其pom引入如下

<properties>
    <maven.compiler.source>19</maven.compiler.source>
    <maven.compiler.target>19</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring-boot.version>3.3.0</spring-boot.version>
    <spring-ai.version>1.0.0</spring-ai.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- spring ai 插件-->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>${spring-ai.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- maven仓库 -->
<repositories>
    <repository>
        <id>central</id>
        <url>https://maven.aliyun.com/repository/central</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

说明:这里通过dependencyManagement引入插件的版本管理,这里使用SpringAI正式版1.0.0

2)创建一个子模块lesson01,其pom引入如下:

<properties>
    <maven.compiler.source>19</maven.compiler.source>
    <maven.compiler.target>19</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 引入智谱的model插件 -->
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-starter-model-zhipuai</artifactId>
    </dependency>
</dependencies>

说明:这里引入智谱的model插件即可

2.3 编写调用接口

1)在lesson01子模块下,创建ChatClientController类,用于调用智谱API

package com.demo.lesson01.controller;

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

@RestController
public class ChatClientController {

    private ChatClient chatClient;

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

    @GetMapping("/ai/generate")
    public String generate(@RequestParam(value = "message", defaultValue = "你是谁!") String message) {
        return this.chatClient.prompt()
                .user(message)
                .call().content();
    }
}

2)在resources目录下,创建application.properties配置文件,配置如下:

spring.ai.zhipuai.api-key=在智谱开发平台创建的API KEY
spring.ai.zhipuai.chat.options.model=GLM-4-Flash-250414
spring.ai.zhipuai.chat.options.temperature=0.7

说明:这里设置了3个配置
1)配置api-key
2)配置模型,这里选择GLM-4-Flash-250414主要是免费,如果你有token流量,可以改其他智谱模型
3)温度:这个值0~1,值越大代表生成的结果随机性越强。如果是一个聊天,这个值可以大一点。如果是一些严谨的规划,则这个值可以设置小一些

3)创建启动类Lesson01Application,并启动项目:

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

@SpringBootApplication
public class Lesson01Application {

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

4)演示,请求url:http://localhost:8080/ai/generate

在这里插入图片描述

2.4 流式响应

细心的朋友可能发现,上面的输出是一次性给出结果。而在大多数文本大模型的对话中,可以看到其输出像打字一样一个个输出,Spring AI也是提供这样的接口

1)在ChatClientController类中,增加以下方法

@GetMapping(value="/ai/generateStream", produces = "text/html;charset=UTF-8") //设定返回的字符,不然会出现乱码
public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "你是谁!") String message) {
    return this.chatClient.prompt()
            .user(message)
            .stream().content();
}

2)演示,请求url:http://localhost:8080/ai/generateStream

这次虽然输出结果差不多,但是文字的显示是一个字一个字的显示。

从上面的示例可以看到,如果你想对接某个大模型,只需要引入对应的model插件和配置api-key,然后编写不到10行代码的controller即可实现对话功能;如果你想换个模型,你只需将model插件换成对应模型的插件即可,代码都不需要改动。从这里就可以看到SpringAI的 跨 AI 提供商的可移植 API 支持 特性,统一了对外接口。

结语:本章讲述了Spring AI的来源以及功能特性,并使用一个入门示例作为引子,让大家看看编写一个聊天对话是多么简单。接下来将进入更为底层的使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linmoo1986

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值