当前位置: 首页 > news >正文

Spring AI简单使用

https://github.com/chatanywhere/GPT_API_free

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

超级详细Spring AI+ChatGPT(java接入OpenAI大模型)

Spring Cloud Alibaba AI 速通版!保姆级教程!

Java大模型应用开发,微服务整合DeepSeek,LangChain大型语言模型LLM实战 - 尚硅谷

文章目录

    • pom.xml
    • OpenAiApp
    • AiController
    • application.yml

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>spring-boot-dependencies</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>3.2.4</version>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>demo-openai</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
            <version>1.0.0-M6</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.10</version>
        </dependency>

    </dependencies>

</project>

OpenAiApp

@SpringBootApplication
public class OpenAiApp {
    public static void main(String[] args) {
		
		/*
		// 设置代理
		String proxy ="127.0.0.1"; //100.100.101.235 8811 示例,里面填具体的代理ip
		int port = 7890; //代理的端口,
		System.setProperty("proxyType","4");
		System.setProperty("proxyPort"Integer.toString(port));
		System.setProperty("proxyHost",proxy);
		System.setProperty("proxySet","true");
		*/

       SpringApplication.run(OpenAiApp.class, args);
    }
}

AiController

import org.springframework.ai.audio.transcription.AudioTranscriptionPrompt;
import org.springframework.ai.audio.transcription.AudioTranscriptionResponse;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.model.Media;
import org.springframework.ai.openai.*;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.openai.api.OpenAiAudioApi;
import org.springframework.ai.openai.api.OpenAiImageApi;
import org.springframework.ai.openai.audio.speech.SpeechPrompt;
import org.springframework.ai.openai.audio.speech.SpeechResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Controller;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Flux;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
https://github.com/chatanywhere/GPT_API_free
转发Host1: https://api.chatanywhere.tech (国内中转,延时更低)
转发Host2: https://api.chatanywhere.org (国外使用)

AiCore API    https://api.xty.app/token
OpenAI购买平台 https://eylink.cn/

spring ai 使用官方示例代码 https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/test

*/
@SuppressWarnings("all")
@Controller
public class AiController {

    @Autowired(required = false)
    private ChatClient.Builder builder;

    // 一次返回
    @RequestMapping("test01")
    @ResponseBody
    public String test01(@RequestParam("prompt") String prompt) {
        ChatClient chatClient = builder.defaultSystem("You are a helpful assistant.").build();
        String response = chatClient.prompt(prompt).system("You are also talented").call().content();
        // String response = chatClient.prompt().user(prompt).call().content();
        return response;
    }


    // 逐词返回
    @RequestMapping(value = "test02", produces = "text/html;charset=utf-8")
    @ResponseBody
    public Flux<String> test02(@RequestParam("prompt") String prompt) {
        ChatClient chatClient = builder.build();
        Flux<String> flux = chatClient.prompt(prompt).stream().content();
        // flux.subscribe(System.out::println);
        return flux;
    }


    // @see OpenAiAutoConfiguration
    @Autowired
    private ChatModel chatModel;

    @RequestMapping(value = "test03")
    @ResponseBody
    public String test03(@RequestParam("prompt") String prompt) {
        ChatResponse chatResponse = chatModel.call(
                new Prompt(
                        new UserMessage(prompt),
                        OpenAiChatOptions.builder()
                                .model("deepseek-reasoner")
                                .temperature(0.8)
                                .build()
                )
        );
        return chatResponse.getResult().getOutput().getText();
    }

    @RequestMapping(value = "test04", produces = "text/html;charset=utf-8")
    @ResponseBody
    public Flux<String> test04(@RequestParam("prompt") String prompt) {
        return chatModel.stream(
                new Prompt(
                        new UserMessage(prompt),
                        OpenAiChatOptions.builder()
                                .model("deepseek-chat")
                                .temperature(0.8)
                                .build()
                ))
                .flatMap(chatResponse -> Flux.just(chatResponse.getResult().getOutput().getText()));

    }

    @Autowired
    private OpenAiImageModel openaiImageModel;

    // 文生图
    @RequestMapping(value = "test05")
    @ResponseBody
    public String test05(@RequestParam("prompt") String prompt) {
        ImageResponse response = openaiImageModel.call(
                new ImagePrompt("draw a cat",
                        OpenAiImageOptions.builder()
                                .quality("hd")
                                .withModel(OpenAiImageApi.DEFAULT_IMAGE_MODEL)
                                .N(1)
                                .height(1024)
                                .width(1024).build())

        );

        return response.getResult().getOutput().getUrl();
    }

    @Autowired
    private OpenAiAudioSpeechModel openAiAudioSpeechModel;

    // 文生语音
    @RequestMapping(value = "test06")
    @ResponseBody
    public void test06(@RequestParam("prompt") String prompt) throws IOException {
        OpenAiAudioSpeechOptions speechOptions = OpenAiAudioSpeechOptions.builder()
                .model(OpenAiAudioApi.TtsModel.TTS_1.value)
                .voice(OpenAiAudioApi.SpeechRequest.Voice.ALLOY)
                .responseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3)
                .speed(1.0f)
                .build();

        SpeechPrompt speechPrompt = new SpeechPrompt("大家好,欢迎来到这里,希望大家开心!", speechOptions);
        SpeechResponse response = openAiAudioSpeechModel.call(speechPrompt);

        StreamUtils.copy(response.getResult().getOutput(), new FileOutputStream(new File(System.getProperty("user.dir") + "\\test06.mp3")));
    }

    @Autowired
    private OpenAiAudioTranscriptionModel openAiTranscriptionModel;

    // 语音转文本
    @RequestMapping(value = "test07")
    @ResponseBody
    public void test07(@RequestParam("prompt") String prompt) throws IOException {

        OpenAiAudioTranscriptionOptions transcriptionOptions = OpenAiAudioTranscriptionOptions.builder()
                .responseFormat(OpenAiAudioApi.TranscriptResponseFormat.TEXT)
                .temperature(0f)
                .build();

        FileSystemResource audioFile = new FileSystemResource(new File(System.getProperty("user.dir") + "\\test06.mp3"));

        AudioTranscriptionPrompt transcriptionRequest = new AudioTranscriptionPrompt(audioFile, transcriptionOptions);
        AudioTranscriptionResponse response = openAiTranscriptionModel.call(transcriptionRequest);

        System.out.println(response.getResult().getOutput());
    }

    // 多模态
    @RequestMapping(value = "test08")
    @ResponseBody
    public void test08(@RequestParam("prompt") String prompt) throws IOException {
        ClassPathResource imageResource = new ClassPathResource("multimodal.test.png");

        UserMessage userMessage = new UserMessage(
                "Explain what do you see in this picture?", // content
                new Media(MimeTypeUtils.IMAGE_PNG, imageResource)); // media

        ChatResponse response = chatModel.call(
                new Prompt(
                        userMessage,
                        OpenAiChatOptions.builder()
                                // 指定模型
                                .model(OpenAiApi.ChatModel.GPT_4_TURBO_PREVIEW)
                                .build()
                )
        );
        System.out.println(response.getResult().getOutput().getText());
    }

}

application.yml

spring:
  ai:
    openai:
      # base-url: https://api.chatanywhere.tech
      # base-url: https://api.chatanywhere.org
      base-url: https://api.xty.app # 中转站
      api-key: sk-xxx
      chat:
        options:
          # model: gpt-3.5-turbo
          # model: deepseek-reasoner
          # model: deepseek-chat
          model: gpt-4

相关文章:

  • 【JAVA架构师成长之路】【Spring生态】第1集:Spring生态核心
  • 使用QT + 文件IO + 鼠标拖拽事件 + 线程 ,实现大文件的传输
  • 在 macOS 上使用 CLion 进行 Google Test 单元测试
  • Van Uploader解决Android11及以下系统上传图片无反应问题
  • 【机械视觉】C#+visionPro联合编程———【一、C# + VisionPro 联合编程详解以及如何将visionPro工具加载到winform】
  • Web3 与跨链技术:如何实现不同区块链的互操作性
  • Language Agent Tree Search (1)
  • 春招中护网面试题库
  • Github 2025-03-06 Go开源项目日报 Top10
  • C语言:怎样将一个结构体数据全部清零
  • 【AI深度学习基础】Pandas完全指南进阶篇:解锁高效数据处理高阶技能 (含完整代码)
  • 【VBA】WPS/PPT设置标题字体
  • 50.日常算法
  • 算法进阶——枚举
  • Java-servlet(三)Java-servlet-Web环境搭建(下)详细讲解利用maven和tomcat搭建Java-servlet环境
  • Python 错误和异常处理:守护程序的稳定运行
  • 无耳 Solon v3.1.0 全新发布(可全面替换 Java Spring 生态)
  • 18k star,取代Navicat!一款集成了AI功能的数据库管理工具!
  • 带触屏笔记本关闭屏幕触控方法
  • redis测评
  • 中国建筑教育网官网证书查询/国内做seo最好的公司
  • 无极网络是什么意思/湖南seo优化报价
  • wordpress与dede哪个好用/好口碑关键词优化地址
  • 在哪个网站上做预收款报告/百度推广软件
  • 政府网站开发 扬州/网络广告的发布方式包括
  • 免费b2b网站要怎么做/seoul national university