对接各种AI大模型(AI工程领域的应用程序框架)







API-Key:sk-3sfER03LDLG3SDFsdlwe283JSdw023lkrmrHDND32fmREKFD
(格式长这样,这个不可用)
免费使用
https://api.chatanywhere.tech (国内中转,延时更低,host1和host2二选一)https://api.chatanywhere.com.cn (国内中转,延时更低,host1和host2二选一)https://api.chatanywhere.cn (国外使用,国内需要全局代理)
- "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.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>3.2.5version>
- <relativePath/>
- parent>
- <groupId>com.zzqgroupId>
- <artifactId>spring-ai-chat01artifactId>
-
- <version>1.0.1-SNAPSHOTversion>
- <name>spring-ai-chat01name>
- <description>spring-ai-chat01description>
- <properties>
- <java.version>17java.version>
- <spring-ai.version>1.0.0-SNAPSHOTspring-ai.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.aigroupId>
- <artifactId>spring-ai-openai-spring-boot-starterartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-devtoolsartifactId>
- <scope>runtimescope>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.aigroupId>
- <artifactId>spring-ai-bomartifactId>
- <version>${spring-ai.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- dependencies>
- dependencyManagement>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <configuration>
- <excludes>
- <exclude>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- exclude>
- excludes>
- configuration>
- plugin>
- plugins>
- build>
-
-
- <repositories>
- <repository>
- <id>spring-snapshotid>
- <name>Spring Snapshotsname>
- <url>https://repo.spring.io/snapshoturl>
- <releases>
- <enabled>falseenabled>
- releases>
- repository>
- repositories>
- project>

文件内容:
- spring:
- application:
- name:spring-ai-01-chat
- ai:
- openai:
- api-key: ${OPENAI_API_KEY}
- base-url: ${OPENAI_API_URL}
- chat:
- option:
- #model:gpt-4-32k
- model:gpt-3.5-turbo
- temperature:0.3F
http://t.csdnimg.cn/32fqr小白如何设置openai api key的环境变量
controller文件内容:
- package com.zzq.controller;
-
- import jakarta.annotation.Resource;
- import org.springframework.ai.chat.ChatResponse;
- import org.springframework.ai.chat.prompt.Prompt;
- import org.springframework.ai.openai.OpenAiChatClient;
- import org.springframework.ai.openai.OpenAiChatOptions;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import reactor.core.publisher.Flux;
-
- @RestController
- public class ChatController {
- /**
- * OpenAi自动装配,可以直接注入使用
- */
- @Resource
- private OpenAiChatClient openAiChatClient;
-
- /**
- * 调用OpenAi的接口,call方法为同步的api
- * @param msg 你要问的问题
- * @return
- */
- @RequestMapping("/ai/chat")
- public String chat(@RequestParam("msg") String msg) {
- String called = openAiChatClient.call(msg);
- return called;
- }
-
- /**
- * 调用OpenAi的接口
- * @param msg 你要问的问题
- * @return Object--json对象
- */
- @RequestMapping ("/ai/chat1")
- public Object chat1(@RequestParam("msg") String msg) {
- ChatResponse response = openAiChatClient.call(new Prompt(msg));
- return response;
- // return response.getResult().getOutput().getContent();//只拿到内容
- }
-
- /**
- * 调用OpenAi的接口
- * @param msg 你要问的问题
- * @return
- */
- @RequestMapping ("/ai/chat3")
- public String chat3(@RequestParam("msg") String msg) {
- //可选参数在yml配置,同时在代码中也配置,那么会以代码为准
- ChatResponse response = openAiChatClient.call(new Prompt(msg, OpenAiChatOptions.builder()
- // .withModel("gpt-4-32k")//使用的模型,32k是参数量//参数量越高,准确率越高
- .withTemperature(0.3F)//温度越高回答越慢,温度越低回答越快
- .build()));
- return response.getResult().getOutput().getContent();
- }
-
- /**
- * 调用OpenAi的接口 stream是流式的api
- * @param msg 你要问的问题
- * @return
- */
- @RequestMapping ("/ai/chat4")
- public Object chat4(@RequestParam("msg") String msg) {
- //可选参数在yml配置,同时在代码中也配置,那么会以代码为准
- Flux
flux = openAiChatClient.stream(new Prompt(msg, OpenAiChatOptions.builder() - // .withModel("gpt-3.5")//使用的模型
- .withTemperature(0.3F)//温度越高回答越慢,温度越低回答越快
- .build()));
- flux.toStream().forEach(chatResponse ->{
- System.out.println(chatResponse.getResult().getOutput().getContent());
- });
- return flux.collectList();//数据的序列,一序列的数据,一个一个的数据返回
- }
- }
spring-ai-02-image
application文件内容(代码配置的会覆盖yml文件中的)
更改可以生成image的api-key
画图模型版本
n:1(生成图片两张)
height:1024(高度)
width:1024(宽度)
quality:hd(高清)
- spring:
- application:
- name:spring-ai-02-image
- ai:
- openai:
- api-key: ${OPENAI_API_KEY}
- base-url: ${OPENAI_API_URL}
- image:
- options:
- model:gpt-4-dalle
controller内容:
图片无法使用流式api
- package com.zzq.controller;
- import jakarta.annotation.Resource;
- import org.springframework.ai.image.ImageOptionsBuilder;
- import org.springframework.ai.image.ImagePrompt;
- import org.springframework.ai.image.ImageResponse;
- import org.springframework.ai.openai.OpenAiImageClient;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- public class ImageController {
- @Resource
- private OpenAiImageClient openAiImageClient;
-
- @RequestMapping("/ai/image")
- private Object image(@RequestParam(value="msg")String msg){
- ImageResponse imageResponse=openAiImageClient.call(new ImagePrompt(msg));
- System.out.println(imageResonse);
- //把图片进行业务处理
- String imageUrl=return response.getResult().getOutput().getUrl();
-
- return response.getResult().getOutput().getUrl();
-
- /* @GetMapping("/ai/draw")
- public String drawImage(@RequestParam(value = "msg") String msg){
- ImageResponse response = openAiImageClient.call(new ImagePrompt(msg,
- ImageOptionsBuilder
- .builder()
- .withQuality("hd")//高清图像
- .withModel("dall-e-3") //绘画模型
- .withN(1) //生成图像的个数
- .withWidth(1024) //图像宽度 默认值
- .withHeight(1024) //图像高度 默认值
- .build()
- )
- );
- //返回结果图片的地址
- return response.getResult().getOutput().getUrl();
- */
- }
- }
spring-ai-03-transcription
更改可以生成声音的api-key
声音模型版本
application文件内容
- spring:
- application:
- name:spring-ai-03-trascription
- ai:
- openai:
- api-key: ${OPENAI_API_KEY}
- base-url: ${OPENAI_API_URL}
- import jakarta.annotation.Resource;
- import org.springframework.ai.openai.OpenAiAudioSpeechClient;
- import org.springframework.ai.openai.OpenAiAudioTranscriptionClient;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import static com.ysl.utisl.save2File.save2File;
-
-
- @RestController
- public class TranscriptionController {
- @Resource
- private OpenAiudioTranscriptionClient openAiAudioTranscriptionClient;
- @RequestMapping(value="/ai/transcription")
- public Object transcription(){
- org,springframework.core.io.Resourse audioFile = new ClassPathResourse("文件名jfk.mp3")
- String called = openAiAudioTranscriptiomClient.call(audioFile)
- System.out.println(called);
- return called;
- }
-
- /*
- @RestController
- public class TranscriptionController {
- //将音频转文字时使用
- @Resource
- private OpenAiAudioTranscriptionClient transcriptionClient;
- //将文字转语音时使用
- @Resource
- private OpenAiAudioSpeechClient speechClient;
- /**
- * 将音频转文字
- * @return
- */
- /* @RequestMapping("/ai/transcription")
- public Object transcription() {
- //读取的是磁盘的路径
- //FileSystemResource audioFile = new FileSystemResource("C:\\Users\\DELL\\Desktop\\luyin.m4a");
- //读取的是classpath静态资源下的文件
- ClassPathResource audioFile = new ClassPathResource("luyin.m4a");
- String call = transcriptionClient.call(audioFile);
- System.out.println(call);
- return call;
- }
- /**
- * 将文字转音频
- * @return
- */
- /* @RequestMapping("/ai/tts")
- public Object tts() {
- String text = "Spring AI 是 AI 工程的应用框架。其目标是将 Spring 生态系统设计原则(如可移植性和模块化设计)应用于 AI,并推广使用 POJO 作为 AI 领域应用程序的构建块。 跨 AI 提供商的可移植 API 支持,适用于聊天、文本到图像和嵌入模型。支持同步和流 API 选项。还支持下拉以访问特定于模型的功能";
- byte[] bytes = speechClient.call(text);
- save2File("C:\\Users\\DELL\\Desktop\\test.mp3",bytes);
- return "OK";
- }
- */
- /*
- @RequestMapping("/ai/tts")
- public Object tts() {
- String text = "Spring AI 是 AI 工程的应用框架。其目标是将 Spring 生态系统设计原则(如可移植性和模块化设计)应用于 AI,并推广使用 POJO 作为 AI 领域应用程序的构建块。 跨 AI 提供商的可移植 API 支持,适用于聊天、文本到图像和嵌入模型。支持同步和流 API 选项。还支持下拉以访问特定于模型的功能";
- byte[] bytes = openAiAudioSpeechClient..call(text);
- FileUtilssave2File("C:\\Users\\DELL\\Desktop\\test.mp3",bytes);
- return "OK";
- }
- */
-
- }
多模态API
多模态是指模型同时理解和处理来自各种来源信息的能力,包括文本、图像、音频和其他数据格式
多模型大语言(LLM)特征使模型能够结合其他模态(如图像、音频或视频)来处理和生成文本
spring-ai-05-multimodel
- package com.zzq.controller;
-
- import jakarta.annotation.Resource;
- import org.springframework.ai.chat.ChatClient;
- import org.springframework.ai.chat.ChatResponse;
- import org.springframework.ai.chat.messages.Media;
- import org.springframework.ai.chat.messages.UserMessage;
- import org.springframework.ai.chat.prompt.Prompt;
- import org.springframework.ai.openai.OpenAiChatOptions;
- import org.springframework.ai.openai.api.OpenAiApi;
- import org.springframework.util.MimeTypeUtils;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.List;
-
- @RestController
- public class MultiModelController {
- @Resource
- private ChatClient chatClient;
- @RequestMapping(value = "/ai/multi")
- public Object multi(String msg,String imageUrl){
- UserMessage userMessage=new UserMessage(msg,
- List.of(new Media(MimeTypeUtils.IMAGE_PNG,imageUrl))
- );
- ChatResponse response=chatClient.call(new Prompt(List.of(userMessage),
- OpenAiChatOptions.builder()
- .withModel(OpenAiApi.ChatModel.GPT_4_VISION_PREVIEW.getValue())
- .build()
- ));
- return response.getResult().getOutput().getContent();
- }
- }