首页 > 其他分享 >AI框架之Spring AI与Spring Cloud Alibaba AI使用讲解

AI框架之Spring AI与Spring Cloud Alibaba AI使用讲解

时间:2024-06-06 17:13:32浏览次数:7  
标签:String AI Spring 模型 Alibaba spring public

目录

1 AI框架

1.1 Spring AI 简介

在软件开发的世界中,Java一直是企业级应用的主力军。而Spring框架,尤其是Spring Boot,以其生态系统的丰富性,为开发者提供了无与伦比的便利。现在,Spring Boot正迈向一个新的纪元——人工智-能的时代。
Spring AI项目的推出,不仅标志着Spring生态的进一步扩展,也为广大Java开发者开启了一个全新的编程领域。

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

Spring AI功能,可以看Spring推出的官方文档:https://spring.io/projects/spring-ai
我们可以从Spring AI的官网描述中,总结出Spring AI的几个核心的关键词:

  • 提供抽象能力
  • 简化AI应用的开发
  • 模型与向量支持
  • AI集成与自动配置

Spring AI 简化了我们构建大型复杂的AI应用的过程,当然如果你的项目仅仅是需要调用一个AI接口,那其实直接调用官方SDK反而更方便。

Spring AI提供的功能如下:

  • SQL类过滤器API: 提供类似SQL的元数据过滤器API,实现跨供应商的一致性。
  • Spring Boot集成: 专为Spring Boot设计的自动配置和启动器,让AI集成变得轻而易举。
  • API可移植性,支持所有主要的模型提供商,如OpenAI,Microsoft,Amazon,Google和Huggingface。支持的模型类型包括聊天和文本到图像。
  • 跨 AI 提供商的可移植 API,用于聊天和嵌入模型。支持同步和流 API 选项。还支持下拉以访问特定于模型的功能。
  • 将 AI 模型输出映射到 POJO。
  • 支持所有主要的向量数据库,例如 Azure Vector Search、Chroma、Milvus、Neo4j、PostgreSQL/PGVector、PineCone、Qdrant、Redis 和 Weaviate。
  • 跨 Vector Store 提供程序的可移植 API,包括新颖的类似 SQL 的元数据过滤器 API,该 API 也是可移植的。
  • AI 模型和矢量存储的 Spring Boot stater。
  • 用于数据工程的 ETL 框架

1.2 Spring AI 使用

1.2.1 pom.xml

添加Maven存储库: 在项目的pom.xml中添加Spring MilestoneSnapshot存储库。

<repositories>
    <!-- Spring Milestone Repository for milestones -->
    <repository>
        <id>spring-milestones</id>
        <url>https://repo.spring.io/milestone</url>
    </repository>
    <!-- Spring Snapshot Repository for snapshots -->
    <repository>
        <id>spring-snapshots</id>
        <url>https://repo.spring.io/snapshot</url>
    </repository>
</repositories>

注意:在集成 Spring AI 或其他较新的 Spring 生态系统组件时,添加 Spring MilestoneSnapshot 存储库是为了确保能够访问到最新的开发版本、里程碑版本和快照版本。这些版本可能尚未发布到 Maven Central 这样的稳定仓库,但包含了最新的特性、修复和改进

导入Spring AI BOM: 使用Spring AI BOM定义,可以确保你使用的是测试过的、兼容的库版本。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>0.8.1-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

添加AI功能: 根据你的需求,添加相关的AI模块依赖项到pom.xml。

<dependencies>
    <!-- 示例:添加OpenAI的支持 -->
    <dependency>
        <groupId>org.springframework.experimental.ai</groupId>
        <artifactId>spring-ai-openai</artifactId>
    </dependency>
</dependencies>

1.2.2 可实现的功能

可实现的功能:

  • 生成式AI:利用Spring AI,你可以通过简单的API调用,实现文本的生成、翻译、摘要等功能。
  • 矢量数据库:当你需要对文本数据进行语义搜索时,Spring AI提供的矢量数据库支持使得相关操作变得简单高-效。
  • AI绘画:对于需要将文本转换为图像的应用场景,Spring AI的绘画功能可以无缝集成到你的应用中。

1.3 Spring Cloud Alibaba AI

原始的Spring AI并没有国内相关大模型的接入,对国内开发者不太友好。
总的来说,Spring Cloud Alibaba AI 目前基于 Spring AI 0.8.1 版本 API 完成通义系列大模型的接入。

在当前最新版本中,Spring Cloud Alibaba AI 主要完成了几种常见生成式模型的适配,包括对话、文生图、文生语音等,开发者可以使用 Spring Cloud Alibaba AI 开发基于通义的聊天、图片或语音生成 AI 应用,框架还提供 OutParser、Prompt Template、Stuff 等实用能力。
Spring Cloud Alibaba AI 官方还提供了包括聊天对话、文生图、文生语音等多种应用的开发示例,具体可以前往官网查看:https://sca.aliyun.com

1.4 Spring Cloud Alibaba AI 实践操作

首先新建一个Maven项目,JDK选的是17版本。

1.4.1 pom.xml

Maven文件需要引入spring-cloud-alibaba-dependenciesspring-cloud-starter-alibaba-ai两个依赖。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2023.0.1.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-ai</artifactId>
    </dependency>
</dependencies>

1.4.2 配置文件

配置阿里云通义千问的Api-Key,没有的读者可以从官网上申请。

server:
  port: 8080
spring:
  application:
    name: alibaba-spring-ai-demo

  cloud:
    ai:
      tongyi:
          api-key: 你的api-key

1.4.3 对接文本模型

我们首先测试如何对接文本大模型。
新建一个控制器类:新建/simple接口,用来测试基本QA。

@RestController
@RequestMapping("/ai")
@CrossOrigin
public class TongYiController {
    @Autowired
    @Qualifier("tongYiSimpleServiceImpl")
    private TongYiService tongYiSimpleService;

    @GetMapping("/simple")
    public String completion(
            @RequestParam(value = "message", defaultValue = "AI时代下Java开发者该何去何从?")
            String message
    ) {
        return tongYiSimpleService.completion(message);
    }
}

新建一个TongyiService服务类:

public interface TongYiService {

    /**
     * 基本问答
     */
    String completion(String message);
    /**
     * 文生图
     */
    ImageResponse genImg(String imgPrompt);

    /**
     * 语音合成
     */
    String genAudio(String text);

}

具体的实现类如下:由 Spring AI 自动注入 ChatClient、StreamingChatClient,ChatClient 屏蔽底层通义大模型交互细节,后者用于流式调用。

对于QA而言,仅仅通过client.call(prompt)一行代码就可以完成对模型的调用。

@Service
@Slf4j
public class TongYiSimpleServiceImpl extends AbstractTongYiServiceImpl {
  /**
   * 自动注入ChatClient、StreamingChatClient,屏蔽模型调用细节
   */
  private final ChatClient chatClient;

  private final StreamingChatClient streamingChatClient;

  @Autowired
  public TongYiSimpleServiceImpl(ChatClient chatClient, StreamingChatClient streamingChatClient) {
    this.chatClient = chatClient;
    this.streamingChatClient = streamingChatClient;
  }
    /**
    * 具体实现:
    */
  @Override
  public String completion(String message) {
    Prompt prompt = new Prompt(new UserMessage(message));
    return chatClient.call(prompt).getResult().getOutput().getContent();
  }
}

我们发送一个请求,prompt是AI时代下Java开发者该何去何从?测试结果如下:
在这里插入图片描述

1.4.4 文生图模型

这里只给出service的代码,其它代码同上面的文本问答。

可以看到,只需要实例化一个imagePrompt,再调用模型即可。

@Slf4j
@Service
public class TongYiImagesServiceImpl extends AbstractTongYiServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(TongYiService.class);
    private final ImageClient imageClient;
    @Autowired
    public TongYiImagesServiceImpl(ImageClient client) {
       this.imageClient = client;
    }
    @Override
    public ImageResponse genImg(String imgPrompt) {
       var prompt = new ImagePrompt(imgPrompt);
       return imageClient.call(prompt);
    }
}

测试的prompt是:Painting a boy coding in front of the desk, with his dog.,测试结果如下,效果还是很不错的:
在这里插入图片描述
在这里插入图片描述

1.4.5 语音合成模型

@Slf4j
@Service
public class TongYiAudioSimpleServiceImpl extends AbstractTongYiServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(TongYiService.class);
    private final SpeechClient speechClient;
    @Autowired
    public TongYiAudioSimpleServiceImpl(SpeechClient client) {
       this.speechClient = client;
    }
    @Override
    public String genAudio(String text) {
       logger.info("gen audio prompt is: {}", text);
       var resWAV = speechClient.call(text);
       // save的代码省略,就是将音频保存到本地而已
       return save(resWAV, SpeechSynthesisAudioFormat.WAV.getValue());
    }
}

测试结果也是成功的:
在这里插入图片描述

标签:String,AI,Spring,模型,Alibaba,spring,public
From: https://www.cnblogs.com/jingzh/p/18235636

相关文章

  • 个人向 godot 源码阅读 - 3 - MainLoop 以及 2D 视口
    3-MainLoop以及2D视口godot默认的主循环类型为SceneTree,在之上则承载了godot中的重要概念之一节点树.SceneTree的源文件位于scene/main/scene_tree.cpp,SceneTree默认将会在Main::start()函数中被创建,然后被设置到OS的mainloop上,现在让我们来看看Scen......
  • springboot 接口返回数据统一加密
      @Aspect@Component@Slf4jpublicclassAESTimeAspect{@Around("execution(*com.trt.sea.xxserx.controller*..*Controller.*(..))")publicObjecthandleAroundControllerMethod(ProceedingJoinPointpjp)throwsThrowable{longst......
  • AI算力暴增至120TOPS 英特尔Lunar Lake架构解析
    随着下一代AIPC硬件核心LunarLake的发布,英特尔4年5个制程节点演进也逐步迎来富有革命性的时刻。面对AI时代指数级的算力需求增长,英特尔LunarLake,也就是第二代酷睿Ultra平台的CPU+GPU+NPU算力突破到了120TOPS,这将为基于其打造的AIPC赋予更加强劲、高效的AI性能体验。在台北电......
  • 超越预期:Containerd 如何成为 Kubernetes 的首选容器运行时
    >作者:尹珉,KubeSphereAmbassado,rKubeSphereContributor,KubeSphere社区用户委员会杭州站站长。踏上Containerd技术之旅容器技术已经成为现代软件开发和部署的核心工具。通过容器,开发者可以创建轻量级、便携的运行环境,从而简化应用程序的开发、测试和部署流程。在容器技术......
  • open-webui无法链接ollama 报错ERROR:apps.ollama.main:Connection error: Cannot con
    一个问题找了很久,网上没有现成的答案,我来写一个,方便再遇到的人一、问题1.ollama是使用官方curl-fsSLhttps://ollama.com/install.sh|sh 安装的2. open-webui是docker启动,docker正常启动,使用github推荐的#WebUI与ollama在同一台机器:sudodockerrun-d-p3000:8080......
  • Spring Boot 不能加载 tcnative-2.dll 库(Can't load library: tcnative-2.dll)
    SpringBoot不能加载tcnative-2.dll库本文将介绍怎样解决SpringBoot在启动时抛出“org.apache.tomcat.jni.LibraryNotFoundError:Can'tloadlibrary:*\tcnative-2.dll”错误。在使用SpringBoot3创建项目的过程中,将日志的级别调为debug,日志打印了“org.apache.tomca......
  • SpringBoot启动流程分析之准备应用上下文refreshContext()(八)
    SpringBoot启动流程分析之准备应用上下文refreshContext()(八)文章目录SpringBoot启动流程分析之准备应用上下文refreshContext()(八)1、准备刷新1.1、子类prepareRefresh()方法1.2父类prepareRefresh()方法2、通知子类刷新内部bean工厂3、准备bean工厂4、允许上下文子类对b......
  • 各家AI大胆帮我预测一下2024年全国高考语文作文 并模拟出题 坐看AI算的准不准
    chatgpt-4o抱歉,我无法预测具体的高考题目。不过,我可以根据近年的趋势和主题为你模拟一个可能的作文题目:2024年全国高考语文作文模拟题目:题目:《共同的家园》要求:以“共同的家园”为主题,写一篇文章。可以采用记叙、议论、描写等文体,自选角度。不少于800字。背景提示:在......
  • SpringBoot+Vue房屋租赁网站(前后端分离)
    技术栈JavaSpringBootMavenMySQLVueElement-UIShiroMybatis-Plus系统角色用户管理员房东系统功能截图......
  • Spring - AOP - @EnableAspectAutoJAutoProxy 与其属性exposeProxy,proxyTargetClass
    这三个属性是Spring框架中与AOP(面向切面编程)相关的配置。 EnableAspectJAutoProxy:这是一个注解,用于开启Spring对AspectJ的自动代理功能。  exposeProxy:这个属性用于配置是否需要将代理对象暴露给AOP创建的Subject,这样就可以在代理对象内部通过AopContext.currentProxy......