发布时间:2025-06-24 18:01:54  作者:北方职教升学中心  阅读量:558


中国(12%)两大市场均实现两位数增长。i音频和其他数据格式

  • 多模式大语言模型(LLM)特征使模型能够结合其他模态(如图像、音频或视频)来处理和生成文本

  • Spring Al 多模态API提供了所有必要的统一抽象和代码封装来支持多模式LLM;

  • 1.依赖注入

    @RestControllerpublic class TTSController {​    @Resource    private OpenAiAudioSpeechClient openAiAudioSpeechClient;​    @RequestMapping(value = "/ai/tts")    public Object tts() {        String text = "2023年全球汽车销量重回9000万辆大关,同比2022年增长11%。";        byte[] bytes = openAiAudioSpeechClient.call(text);        FileUtils.save2File("D:\\SpringAI\\test.mp3", bytes);        return "OK";    }​    @RequestMapping(value = "/ai/tts2")    public Object tts2() {        String text = "Spring AI is an application framework for AI engineering. Its goal is to apply to the AI domain Spring ecosystem design principles such as portability and modular design and promote using POJOs as the building blocks of an application to the AI domain.";        byte[] bytes = openAiAudioSpeechClient.call(text);        FileUtils.save2File("D:\\SpringAI\\test2.mp3", bytes);        return "OK";    }}

    4.接口测试

    http://localhost:8080/ai/tts

    多模态API

    • 多模态是指模型同时理解和处理来自各种来源的信息的能力,包括文本、面对这样亮眼的数据,全球汽车行业却都对2024年的市场前景表示悲观,宏观数据和企业体感之前的差异并非中国独有,在汽车市场中,这是共性问题。

      AI聊天程序

      1.依赖注入

      ai依赖:

      <dependency>    <groupId>org.springframework.ai</groupId>    <artifactId>spring-ai-openai-spring-boot-starter</artifactId></dependency>

      继承父项目:

      <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>

      配置项目依赖下载的仓库:

      <!--  配置本项目的仓库:因为maven咨中心仓库还没有更新spring ai的jar包   --><repositories>    <!-- 快照版的版本仓库 -->    <repository>        <id>spring-snapshot</id>        <name>Spring Snapshots</name>        <url>https://repo.spring.io/snapshot</url>        <releases>            <enabled>false</enabled>        </releases>    </repository></repositories>

      2.项目配置

      spring:  application:  name: spring-ai-01-chat​  ai:    openai:      api-key: sk-xxxxx      base-url: xxxxx      chat:        options:          model: gpt-3.5-turbo #不支持4 gpt-4-32k          temperature: 0.3Fserver:  port: 8080​

      3.聊天程序Controller

      @RestControllerpublic class ChatController {    /**     * spring-ai 自动装配的,可以直接注入使用     */    @Resource    private OpenAiChatClient openAiChatClient;​    /**     * 调用OpenAI的接口     * @param msg 我们提的问题     * @return 返回的答案     */    @RequestMapping(value = "/ai/chat")    public String chat(@RequestParam(value = "msg")String msg){        String called = openAiChatClient.call(msg);​        return called;    }​    /**     * 调用OpenAI的接口     * @param msg 我们提的问题     * @return 返回的答案     */    @RequestMapping(value = "/ai/chat2")    public Object chat2(@RequestParam(value = "msg")String msg){        ChatResponse chatResponse = openAiChatClient.call(new Prompt(msg));        return chatResponse.getResult().getOutput().getContent();    }​    /**     * 调用OpenAI的接口     * @param msg 我们提的问题     * @return 返回的答案     */    @RequestMapping(value = "/ai/chat3")    public Object chat3(@RequestParam(value = "msg")String msg){        //可选参数在配置文件中配置了,在代码中也配置了,那么以代码的配置为准,也就是代码的配置会覆盖掉配置文件中的配置        ChatResponse chatResponse = openAiChatClient.call(new Prompt(msg, OpenAiChatOptions.builder()                .withModel("gpt-4-32k") //gpt版本,32k是参数量 不支持4 gpt-4-32k                .withTemperature(0.4F) //温度越高,回答得比较有创新性,但是准确率会下降,温度越低,回答的准确率会更高                .build()));        return chatResponse.getResult().getOutput().getContent();    }​    /**     * 调用OpenAI的Stream流的接口     * @param msg 我们提的问题     * @return 返回的答案     */    @RequestMapping(value = "/ai/chat4")    public Object chat4(@RequestParam(value = "msg")String msg){        //可选参数在配置文件中配置了,在代码中也配置了,那么以代码的配置为准,也就是代码的配置会覆盖掉配置文件中的配置        Flux<ChatResponse> flux = openAiChatClient.stream(new Prompt(msg, OpenAiChatOptions.builder()                .withModel("gpt-4-32k") //gpt版本,32k是参数量 不支持4 gpt-4-32k                .withTemperature(0.4F) //温度越高,回答得比较有创新性,但是准确率会下降,温度越低,回答的准确率会更高                .build()));        flux.toStream().forEach(chatResponse -> {            System.out.println(chatResponse.getResult().getOutput().getContent());        });        return flux.collectList();//数据的序列,一序列的数据,一个一个的数据返回    }}​

      4.接口测试

      http://localhost:8080/ai/chat?msg = xxxx

      http://localhost:8080/ai/chat2?msg = xxxx

      http://localhost:8080/ai/chat3?msg = xxxx

      http://localhost:8080/ai/chat4?msg = xxxx

      AI图像程序

      1.依赖注入

      @RestControllerpublic class ChatController {    /**     * spring-ai 自动装配的,可以直接注入使用     */    @Resource    private OpenAiChatClient openAiChatClient;​    /**     * 调用OpenAI的接口     * @param msg 我们提的问题     * @return 返回的答案     */    @RequestMapping(value = "/ai/chat")    public String chat(@RequestParam(value = "msg")String msg){        String called = openAiChatClient.call(msg);​        return called;    }​    /**     * 调用OpenAI的接口     * @param msg 我们提的问题     * @return 返回的答案     */    @RequestMapping(value = "/ai/chat2")    public Object chat2(@RequestParam(value = "msg")String msg){        ChatResponse chatResponse = openAiChatClient.call(new Prompt(msg));        return chatResponse.getResult().getOutput().getContent();    }​    /**     * 调用OpenAI的接口     * @param msg 我们提的问题     * @return 返回的答案     */    @RequestMapping(value = "/ai/chat3")    public Object chat3(@RequestParam(value = "msg")String msg){        //可选参数在配置文件中配置了,在代码中也配置了,那么以代码的配置为准,也就是代码的配置会覆盖掉配置文件中的配置        ChatResponse chatResponse = openAiChatClient.call(new Prompt(msg, OpenAiChatOptions.builder()                .withModel("gpt-4-32k") //gpt版本,32k是参数量 不支持4 gpt-4-32k                .withTemperature(0.4F) //温度越高,回答得比较有创新性,但是准确率会下降,温度越低,回答的准确率会更高                .build()));        return chatResponse.getResult().getOutput().getContent();    }​    /**     * 调用OpenAI的Stream流的接口     * @param msg 我们提的问题     * @return 返回的答案     */    @RequestMapping(value = "/ai/chat4")    public Object chat4(@RequestParam(value = "msg")String msg){        //可选参数在配置文件中配置了,在代码中也配置了,那么以代码的配置为准,也就是代码的配置会覆盖掉配置文件中的配置        Flux<ChatResponse> flux = openAiChatClient.stream(new Prompt(msg, OpenAiChatOptions.builder()                .withModel("gpt-4-32k") //gpt版本,32k是参数量 不支持4 gpt-4-32k                .withTemperature(0.4F) //温度越高,回答得比较有创新性,但是准确率会下降,温度越低,回答的准确率会更高                .build()));        flux.toStream().forEach(chatResponse -> {            System.out.println(chatResponse.getResult().getOutput().getContent());        });        return flux.collectList();//数据的序列,一序列的数据,一个一个的数据返回    }}​

      2.项目配置

      spring:  application:    name: spring-ai-02-image​  ai:    openai:      api-key: ${api-key}      base-url: ${base-url}      image:        options:          model: gpt-4-dalle          n: 1 #图片数量          height: 1024 #高度          width: 1024 #宽度          quality: hd #高清

      3.ai生成图片Controller

      @RestControllerpublic 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(imageResponse);​        String imageUrl = imageResponse.getResult().getOutput().getUrl();        //把图片进行业务处理        return imageResponse.getResult().getOutput();    }​    @RequestMapping("/ai/image2")    private Object image2(@RequestParam(value = "msg")String msg){        ImageResponse imageResponse = openAiImageClient.call(new ImagePrompt(msg,OpenAiImageOptions.builder()                .withQuality("hd")//高清图像                .withN(1)//生成图片的数量                .withHeight(1024)//生成图片的高度                .withWidth(1024)//生成图片的宽度                .build()));        System.out.println(imageResponse);​        String imageUrl = imageResponse.getResult().getOutput().getUrl();        //把图片进行业务处理        return imageResponse.getResult().getOutput();    }​​​}

      4.接口测试

      http://localhost:8080/ai/image?msg = xxxx

      http://localhost:8080/ai/image2?msg = xxxx

      AI音频转文本

      1.依赖注入

      @RestControllerpublic 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(imageResponse);​        String imageUrl = imageResponse.getResult().getOutput().getUrl();        //把图片进行业务处理        return imageResponse.getResult().getOutput();    }​    @RequestMapping("/ai/image2")    private Object image2(@RequestParam(value = "msg")String msg){        ImageResponse imageResponse = openAiImageClient.call(new ImagePrompt(msg,OpenAiImageOptions.builder()                .withQuality("hd")//高清图像                .withN(1)//生成图片的数量                .withHeight(1024)//生成图片的高度                .withWidth(1024)//生成图片的宽度                .build()));        System.out.println(imageResponse);​        String imageUrl = imageResponse.getResult().getOutput().getUrl();        //把图片进行业务处理        return imageResponse.getResult().getOutput();    }​​​}

      2.项目配置

      spring:  application:    name: spring-ai-03-transcription​  ai:    openai:      api-key: ${api-key}      base-url:${base-url}

      3.音频转文本controller

      @RestControllerpublic 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(imageResponse);​        String imageUrl = imageResponse.getResult().getOutput().getUrl();        //把图片进行业务处理        return imageResponse.getResult().getOutput();    }​    @RequestMapping("/ai/image2")    private Object image2(@RequestParam(value = "msg")String msg){        ImageResponse imageResponse = openAiImageClient.call(new ImagePrompt(msg,OpenAiImageOptions.builder()                .withQuality("hd")//高清图像                .withN(1)//生成图片的数量                .withHeight(1024)//生成图片的高度                .withWidth(1024)//生成图片的宽度                .build()));        System.out.println(imageResponse);​        String imageUrl = imageResponse.getResult().getOutput().getUrl();        //把图片进行业务处理        return imageResponse.getResult().getOutput();    }​​​}

      4.接口测试

      http://localhost:8080/ai/transcription

      AI文本转音频

      1.依赖注入

      <?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.2.4</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>com.xh</groupId>    <artifactId>ai</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>ai</name>    <description>ai</description>    <properties>        <java.version>17</java.version>    </properties>    <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>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework.ai</groupId>            <artifactId>spring-ai-openai</artifactId>        </dependency>​        <dependency>            <groupId>org.springframework.ai</groupId>            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.ai</groupId>            <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>        </dependency>    </dependencies>    <repositories>        <repository>            <id>spring-milestones</id>            <name>Spring Milestones</name>            <url>https://repo.spring.io/milestone</url>            <snapshots>                <enabled>false</enabled>            </snapshots>        </repository>        <repository>            <id>spring-snapshots</id>            <name>Spring Snapshots</name>            <url>https://repo.spring.io/snapshot</url>            <releases>                <enabled>false</enabled>            </releases>        </repository>    </repositories>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build>​</project>

      2.项目配置

      spring:  application:    name: spring-ai-04-tts​  ai:    openai:      api-key: ${api-key}      base-url: ${base-url}

      3.文本转音频controller

      @RestControllerpublic class TTSController {​    @Resource    private OpenAiAudioSpeechClient openAiAudioSpeechClient;​    @RequestMapping(value = "/ai/tts")    public Object tts() {        String text = "2023年全球汽车销量重回9000万辆大关,同比2022年增长11%。";        byte[] bytes = openAiAudioSpeechClient.call(text);        FileUtils.save2File("D:\\SpringAI\\test.mp3", bytes);        return "OK";    }​    @RequestMapping(value = "/ai/tts2")    public Object tts2() {        String text = "Spring AI is an application framework for AI engineering. Its goal is to apply to the AI domain Spring ecosystem design principles such as portability and modular design and promote using POJOs as the building blocks of an application to the AI domain.";        byte[] bytes = openAiAudioSpeechClient.call(text);        FileUtils.save2File("D:\\SpringAI\\test2.mp3", bytes);        return "OK";    }}

      2.项目配置

      spring:  application:    name: spring-ai-04-tts​  ai:    openai:      api-key: ${api-key}      base-url: ${base-url}

      3.多模特Api调用 文字+图片测试聊天controller

      @RestControllerpublic class MultiModelController {    @Resource    private ChatClient chatClient;​    @RequestMapping(value = "/ai/multi")    public Object multi(String msg,String imageUrl){        var 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()));​        System.out.println(response.getResult().getOutput());        return response.getResult().getOutput().getContent();    }}​

      4.接口测试

      http://localhost:8080/ai/multi?msg = xxxxxxx,imageUrl=xxxxxx

      分区域看,西欧(14%)、面对这样亮眼的数据,全球汽车行业却都对2024年的市场前景表示悲观,宏观数据和企业体感之前的差异并非中国独有,在汽车市场中,这是共性问题。中国(12%)两大市场均实现两位数增长。分区域看,西欧(14%)、图像、