Spring框架永远滴神之SpringAI玩转大模型

2024-06-17 18:12

本文主要是介绍Spring框架永远滴神之SpringAI玩转大模型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 一、SpringAI简介
    • 1.什么是SpringAI
    • 2.SpringAI支持的大模型类型
      • (1)聊天模型
      • (2)文本到图像模型
      • (3)转录(音频到文本)模型
      • (4)嵌入模型
      • (5)矢量数据库
    • 3.SpringAI版本
  • 二、SpringAI框架使用,对接OpenAI
    • 1.环境信息
    • 2.初始化
    • 3.配置文件
      • (1)application.yml
      • (2)pom文件
    • 4.聊天代码测试
      • (1)聊天接口
      • (2)流式响应
      • (3)chatModel api
      • (4)文字生成图片
      • (5)文字生成语音
      • (6)预先定义角色
      • (7)functionCall功能
      • (8)如何通过代理的方式访问接口

一、SpringAI简介

1.什么是SpringAI

Spring AI 是一个面向 AI 工程的应用框架,其目标是将 Spring 生态系统的可移植性和模块化设计等设计原则应用到 AI 领域,并推动将 POJO 作为应用的构建块应用于 AI 领域。
简单地说,就是不再需要我们再去封装各种各样的类或者方法,直接用spring框架内置的方法,和大模型进行通信

官网地址https://spring.io/projects/spring-ai

2.SpringAI支持的大模型类型

(1)聊天模型

  • OpenAI
  • Azure Open AI
  • Amazon Bedrock
  • Cohere’s Command
  • AI21 Labs’ Jurassic-2
  • Meta’s LLama 2
  • Amazon’s Titan
  • Google Vertex AI Palm
  • Google Gemini
  • HuggingFace - access thousands of models, including those from Meta such as Llama2
  • Ollama - run AI models on your local machine
  • MistralAI

(2)文本到图像模型

  • OpenAI with DALL-E
  • StabilityAI

(3)转录(音频到文本)模型

  • OpenAI

(4)嵌入模型

  • OpenAI
  • Azure OpenAI
  • Ollama
  • ONNX
  • PostgresML
  • Bedrock Cohere
  • Bedrock Titan
  • Google VertexAI
  • Mistal AI

(5)矢量数据库

  • Azure Vector Search
  • Chroma
  • Milvus
  • Neo4j
  • PostgreSQL/PGVector
  • PineCone
  • Redis
  • Weaviate
  • Qdrant

3.SpringAI版本

目前只出了一个版本1.0.0-M1
请添加图片描述

二、SpringAI框架使用,对接OpenAI

1.环境信息

  • Maven: apache-maven-3.9.6
  • springBoot: 3.3.0
  • JAVA:JDK17

2.初始化

server url里面如果是阿里云的链接,给换掉,因为阿里云的构建springboot里面选不了AI模块
jdk一定要选17
请添加图片描述
引入springweb和openAI两个依赖模块就可以
请添加图片描述

3.配置文件

(1)application.yml

这里面你要去百度上找,或者自己去买openai的api-key,贴在api-key后面就可以,
base-url可以通过代理方式去调用,后面会讲到。

spring:application:name: springAIai:openai:api-key: base-url: https://api.chatanywhere.tech  #国内中转访问

(2)pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.3.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.kdx</groupId><artifactId>springAI</artifactId><version>0.0.1-SNAPSHOT</version><name>springAI</name><description>Demo project for Spring Boot</description><properties><java.version>17</java.version><spring-ai.version>1.0.0-M1</spring-ai.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><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><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><compilerVersion>17</compilerVersion><source>16</source><target>16</target><encoding>UTF-8</encoding><!-- maven 3.6.2及之后加上编译参数,可以让我们在运行期获取方法参数名称。 --><parameters>true</parameters><skip>true</skip></configuration></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>

4.聊天代码测试

新建Controller类,通过接口调用测试

(1)聊天接口

    @Autowiredprivate ChatClient chatClient;//交流@RequestMapping("/chat")public String generation(@RequestParam(value = "message", defaultValue = "讲个笑话") String message) {//prompt:提示词return this.chatClient.prompt()//用户信息.user(message)//请求大模型.call()//返回文本.content();}

启动项目访问接口测试
请添加图片描述

(2)流式响应

流式响应的意思就是,调用接口一次响应太多文字给浏览器需要等很久,使用流式响应真正的像GPT

    @Autowiredprivate ChatClient chatClient;
//流式响应@RequestMapping(value = "/stream", produces = "text/html;charset=UTF-8")public Flux<String> stream(@RequestParam(value = "message", defaultValue = "讲个笑话") String message) {Flux<String> output = chatClient.prompt().user(message)//流式调用.stream().content();return output;}

启动项目访问接口测试
请添加图片描述

(3)chatModel api

chatModel API比ChatClient 更灵活,但是底层还是用的ChatClient ,可以去参考一下chatModel的API,可以设置很多参数,因为下面设置的是gpt-4,api-key必须要和版本对应

    @Autowired(required = false)private ChatModel chatModel;//chatModel api@RequestMapping(value = "/ChatResponse", produces = "text/html;charset=UTF-8")public String ChatResponse(@RequestParam(value = "message") String message) {ChatResponse response = chatModel.call(new Prompt(message,OpenAiChatOptions.builder()//选择gpt版本.withModel("gpt-4-32k").withTemperature(0.4f).build()));return response.getResult().getOutput().getContent();}

(4)文字生成图片

它也是只有特定的模型才能使用,需要对应的api-key

    @Autowired(required = false)private OpenAiImageModel openaiImageModel;//文生图@RequestMapping(value = "/openaiImageModel", produces = "text/html;charset=UTF-8")public String openaiImageModel(@RequestParam(value = "message") String message) {ImageResponse response = openaiImageModel.call(new ImagePrompt(message,OpenAiImageOptions.builder()//图片质量.withQuality("hd")//生成几张.withN(1)//尺寸.withHeight(1024).withWidth(1024).build()));return response.getResult().getOutput().getUrl();}

(5)文字生成语音

它也是只有特定的模型才能使用,需要对应的api-key

	//文生语音@RequestMapping(value = "/writeByte", produces = "text/html;charset=UTF-8")public String writeByte(@RequestParam(value = "message") String message) {OpenAiAudioSpeechOptions speechOptions = OpenAiAudioSpeechOptions.builder().withModel(OpenAiAudioApi.TtsModel.TTS_1.value).withVoice(OpenAiAudioApi.SpeechRequest.Voice.ALLOY).withResponseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3).withSpeed(1.0f).build();SpeechPrompt speechPrompt = new SpeechPrompt(message, speechOptions);SpeechResponse response = openAiAudioSpeechModel.call(speechPrompt);byte[] body = response.getResult().getOutput();try {writeByte(body,"D:\\Project");} catch (Exception e) {System.out.println(e);}return "ok";}public static void writeByte(byte[] audioBytes, String outputFilePath) throws IOException {FileOutputStream fileOutputStream = new FileOutputStream(outputFilePath + "111.mp3");fileOutputStream.write(audioBytes);fileOutputStream.close();}

(6)预先定义角色

意思就是在调用聊天模型的时候,预先给他设定好一个角色
比如调用聊天接口,他是怎么知道他是java开发工程师的呢?就是通过预先定义角色
请添加图片描述
新建一个AIConfig 配置类,通过一段文字在springboot启动时,预先给他定义他的角色

@Configuration
public class AIConfig {//角色预设@BeanChatClient chatClient(ChatClient.Builder builder) {return builder.defaultSystem("你现在不是chatGPT了,我希望你以一个java工程师的身份来和我对话,你是一个在卡迪熊公司工作的java开发工程师,你叫小鹏!").build();}
}

(7)functionCall功能

目的是可以让其他的应用程序结合GPT使用,可以更精准的返回信息,下面的apply方法就可以调用别的程序,收集信息。

  • 在AIConfig的配置类中加入代码,这里的Description注解就是触发该Function方法的关键字
    @Bean@Description("有多少人")LocationNameFunction LocationNameFunction() {return new LocationNameFunction();}
  • 然后再新建一个LocationNameFunction类,通过关键字"有多少人"触发动作,执行类中apply方法
package com.kdx.springai.functions;import java.util.Objects;
import java.util.function.Function;public class LocationNameFunction implements Function<LocationNameFunction.Request, LocationNameFunction.Response> {@Overridepublic Response apply(Request request) {if (Objects.isNull(request.location) || Objects.isNull(request.name)) {return new Response("缺少参数");}return new Response("有10个人");}//接收提取关键信息public record Request(String name,String location) {@Overridepublic String name() {return name;}@Overridepublic String location() {return location;}}//最终响应给gptpublic record Response(String message) {}}
  • 定义接口
	//functionCall@RequestMapping(value = "/functionCall", produces = "text/html;charset=UTF-8")public String functionCall(@RequestParam(value = "message") String message) {OpenAiChatOptions aiChatOptions = OpenAiChatOptions.builder()//设置实现了function接口的bean名称.withFunction("LocationNameFunction").withModel(OpenAiApi.ChatModel.GPT_3_5_TURBO).build();ChatResponse response = chatModel.call(new Prompt(message, aiChatOptions));return response.getResult().getOutput().getContent();}
  • 测试
    请添加图片描述

(8)如何通过代理的方式访问接口

现在是通过访问国内中转代理的方式,访问GPT的接口,但是怎么直接访问他的接口呢?
可以通过在启动类中加入代理信息。

    public static void main(String[] args) {System.setProperty("proxyType", "4");   //类型System.setProperty("proxyPort", "7890");    //端口System.setProperty("proxyHost", "127.0.0.1");   //ipSystem.setProperty("proxySet", "true");SpringApplication.run(SpringAiApplication.class, args);}

这个时候yaml文件的url就可以配置https://api.chatanywhere.cn,直接访问了

------------------------------------------------------需要源码,可以留言------------------------------------------------------

这篇关于Spring框架永远滴神之SpringAI玩转大模型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1070179

相关文章

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

Spring Boot读取配置文件的五种方式小结

《SpringBoot读取配置文件的五种方式小结》SpringBoot提供了灵活多样的方式来读取配置文件,这篇文章为大家介绍了5种常见的读取方式,文中的示例代码简洁易懂,大家可以根据自己的需要进... 目录1. 配置文件位置与加载顺序2. 读取配置文件的方式汇总方式一:使用 @Value 注解读取配置方式二

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Java中的@SneakyThrows注解用法详解

《Java中的@SneakyThrows注解用法详解》:本文主要介绍Java中的@SneakyThrows注解用法的相关资料,Lombok的@SneakyThrows注解简化了Java方法中的异常... 目录前言一、@SneakyThrows 简介1.1 什么是 Lombok?二、@SneakyThrows