最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot快速接入OpenAI大模型的方法(JDK8)

 更新時(shí)間:2025年02月28日 10:48:38   作者:呱 太  
本文介紹了如何使用AI4J快速接入OpenAI大模型,并展示了如何實(shí)現(xiàn)流式與非流式的輸出,以及對(duì)函數(shù)調(diào)用的使用,AI4J支持JDK8,適用于多種應(yīng)用場(chǎng)景,包括Spring Boot項(xiàng)目,感興趣的朋友一起看看吧

使用AI4J快速接入OpenAI大模型

本博文給大家介紹一下如何使用AI4J快速接入OpenAI大模型,并且如何實(shí)現(xiàn)流式與非流式的輸出,以及對(duì)函數(shù)調(diào)用的使用。

介紹

由于SpringAI需要使用JDK17和Spring Boot3,但是目前很多應(yīng)用依舊使用的JDK8版本,所以使用可以支持JDK8的AI4J來接入OpenAI大模型。

AI4J是一款JavaSDK用于快速接入AI大模型應(yīng)用,整合多平臺(tái)大模型,如OpenAi、智譜Zhipu(ChatGLM)、深度求索DeepSeek、月之暗面Moonshot(Kimi)、騰訊混元Hunyuan、零一萬物(01)等等,提供統(tǒng)一的輸入輸出(對(duì)齊OpenAi)消除差異化,優(yōu)化函數(shù)調(diào)用(Tool Call),優(yōu)化RAG調(diào)用、支持向量數(shù)據(jù)庫(kù)(Pinecone),并且支持JDK1.8,為用戶提供快速整合AI的能力。

AI4J-GitHub

快速使用

目前較多的應(yīng)用場(chǎng)景為Spring應(yīng)用,而AI4J接入SpringBoot應(yīng)用也是非常簡(jiǎn)單的,本篇博文先帶著大家為SpringBoot應(yīng)用集成OpenAI服務(wù),后續(xù)會(huì)介紹如何再非Spring項(xiàng)目中搭建。

創(chuàng)建SpringBoot項(xiàng)目

這里以JDK1.8為例創(chuàng)建SpringBoot2項(xiàng)目,當(dāng)然你也可以創(chuàng)建JDK17、SpringBoot3。

引入AI4J依賴

<!-- Spring應(yīng)用 -->
<dependency>
    <groupId>io.github.lnyo-cly</groupId>
    <artifactId>ai4j-spring-boot-stater</artifactId>
    <version>0.5.2</version>
</dependency>

如果你使用阿里源無法引入,可能是阿里云鏡像還沒有同步。

配置application.yml

給大家兩種配置方法

第一種:使用官網(wǎng)的url,自己有代理

第二種:使用中轉(zhuǎn)代理地址(或第三方中轉(zhuǎn)平臺(tái))

如:https://api.openai-proxy.com

上面任意配置一種即可。

搭建聊天服務(wù)Controller

下面是一個(gè)小的demo演示:

@RestController
public class OpenAiController {
    // 注入Ai服務(wù)
    @Autowired
    private AiService aiService;
    @GetMapping("/chat")
    public String getChatMessage(@RequestParam String question) throws Exception {
        // 獲取OpenAi的聊天服務(wù)
        IChatService chatService = aiService.getChatService(PlatformType.OPENAI);
        // 創(chuàng)建請(qǐng)求參數(shù)
        ChatCompletion chatCompletion = ChatCompletion.builder()
                .model("gpt-4o-mini")
                .message(ChatMessage.withUser(question))
                .build();
        System.out.println(chatCompletion);
        // 發(fā)送chat請(qǐng)求
        ChatCompletionResponse chatCompletionResponse = chatService.chatCompletion(chatCompletion);
        // 獲取聊天內(nèi)容和token消耗
        String content = chatCompletionResponse.getChoices().get(0).getMessage().getContent();
        long totalTokens = chatCompletionResponse.getUsage().getTotalTokens();
        System.out.println("總token消耗: " + totalTokens);
        return content;
    }
}

實(shí)現(xiàn)stream流式輸出(打字機(jī)效果)

編寫stream.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Stream Example</title>
</head>
<body>
<input id="question" type="text" placeholder="輸入需要提問的問題"/>
<button id="startButton">開始</button>
<div id="output"></div>
<script>
    const input = document.getElementById("question");
    const outputDiv = document.getElementById('output');
    const startButton = document.getElementById('startButton');
    async function getResponse(){
        const question = input.value;
        const resp = await fetch("/chatStream" + "?question=" + question,{
            method: 'GET'
        })
        const reader = resp.body.getReader();
        const textDecoder = new TextDecoder();
        while (1){
            const { done , value } = await reader.read()
            if(done) break;
            const str = textDecoder.decode(value);
            outputDiv.innerText += str;
            console.log(str)
        }
    }
    startButton.addEventListener("click", getResponse)
</script>
</body>
</html>

向controller中添加stream接口:

    @GetMapping("/chatStream")
    public void getChatMessageStream(@RequestParam String question, HttpServletResponse response) throws Exception {
        // 中文亂碼問題
        response.setCharacterEncoding("UTF-8");
        // 獲取OpenAi的聊天服務(wù)
        IChatService chatService = aiService.getChatService(PlatformType.OPENAI);
        // 創(chuàng)建請(qǐng)求參數(shù)
        ChatCompletion chatCompletion = ChatCompletion.builder()
                .model("gpt-4o-mini")
                .message(ChatMessage.withUser(question))
                .build();
        PrintWriter writer = response.getWriter();
        // 發(fā)送chat請(qǐng)求
        SseListener sseListener = new SseListener() {
            @Override
            protected void send() {
                writer.write(this.getCurrStr());
                writer.flush();
                System.out.println(this.getCurrStr());
            }
        };
        chatService.chatCompletionStream(chatCompletion, sseListener);
        writer.close();
        System.out.println(sseListener.getOutput());
    }

注意:上面只是一個(gè)簡(jiǎn)單的示例,你也可以使用其它方法,比如:

    @GetMapping("/chatStream")
    public ResponseBodyEmitter getChatMessageStream(@RequestParam String question) {
        ResponseBodyEmitter emitter = new ResponseBodyEmitter();
        // 獲取OpenAi的聊天服務(wù)
        IChatService chatService = aiService.getChatService(PlatformType.OPENAI);
        // 創(chuàng)建請(qǐng)求參數(shù)
        ChatCompletion chatCompletion = ChatCompletion.builder()
                .model("gpt-4o-mini")
                .message(ChatMessage.withUser(question))
                .build();
        Executors.newSingleThreadExecutor().submit(() -> {
            try {
                SseListener sseListener = new SseListener() {
                    @Override
                    protected void send() {
                        try {
                            emitter.send(this.getCurrStr());
                            System.out.println(this.getCurrStr());  // 打印當(dāng)前發(fā)送的內(nèi)容
                        } catch (IOException e) {
                            emitter.completeWithError(e);
                        }
                    }
                };
                // 發(fā)送流式數(shù)據(jù)
                chatService.chatCompletionStream(chatCompletion, sseListener);
                // 完成后關(guān)閉連接
                emitter.complete();
            } catch (Exception e) {
                emitter.completeWithError(e);
            }
        });
        return emitter;
    }

實(shí)現(xiàn)函數(shù)調(diào)用

首先我們需要編寫一個(gè)函數(shù),以天氣預(yù)報(bào)為例子:

@FunctionCall(name = "queryWeather", description = "查詢目標(biāo)地點(diǎn)的天氣預(yù)報(bào)")
public class QueryWeatherFunction implements Function<QueryWeatherFunction.Request, String> {
    @Override
    public String apply(Request request) {
        final String key = "abcdefg";
        // https://api.seniverse.com/v3/weather/hourly.json?key=your_api_key&location=beijing&start=0&hours=24
        // https://api.seniverse.com/v3/weather/daily.json?key=your_api_key&location=beijing&start=0&days=5
        String url = String.format("https://api.seniverse.com/v3/weather/%s.json?key=%s&location=%s&days=%d",
                request.type.name(),
                key,
                request.location,
                request.days);
        OkHttpClient client = new OkHttpClient();
        okhttp3.Request http = new okhttp3.Request.Builder()
                .url(url)
                .get()
                .build();
        try (Response response = client.newCall(http).execute()) {
            if (response.isSuccessful()) {
                // 解析響應(yīng)體
                return response.body() != null ? response.body().string() : "";
            } else {
                return "獲取天氣失敗 當(dāng)前天氣未知";
            }
        } catch (Exception e) {
            // 處理異常
            e.printStackTrace();
            return "獲取天氣失敗 當(dāng)前天氣未知";
        }
    }
    @Data
    @FunctionRequest
    public static class Request{
        @FunctionParameter(description = "需要查詢天氣的目標(biāo)位置, 可以是城市中文名、城市拼音/英文名、省市名稱組合、IP 地址、經(jīng)緯度")
        private String location;
        @FunctionParameter(description = "需要查詢未來天氣的天數(shù), 最多15日")
        private int days = 15;
        @FunctionParameter(description = "預(yù)報(bào)的天氣類型,daily表示預(yù)報(bào)多天天氣、hourly表示預(yù)測(cè)當(dāng)天24天氣、now為當(dāng)前天氣實(shí)況")
        private Type type;
    }
    public enum Type{
        daily,
        hourly,
        now
    }
}

其中有三個(gè)核心注解:

  • @FunctionCall:標(biāo)識(shí)這個(gè)類為一個(gè)函數(shù)
  • @FunctionRequest:標(biāo)識(shí)為該類為函數(shù)的請(qǐng)求類
  • @FunctionParameter:標(biāo)識(shí)為函數(shù)的具體參數(shù)

接下來稍微修改下剛剛編寫的Stream實(shí)現(xiàn)函數(shù):

    @GetMapping("/chatStream")
    public void getChatMessageStream(@RequestParam String question, HttpServletResponse response) throws Exception {
        // ......
        // 創(chuàng)建請(qǐng)求參數(shù)
        ChatCompletion chatCompletion = ChatCompletion.builder()
                .model("gpt-4o-mini")
                .message(ChatMessage.withUser(question))
                .functions("queryWeather") // 這里傳入剛剛我們定義的函數(shù)名稱即可
                .build();
        // ......
    }

如果想要知道函數(shù)調(diào)用的參數(shù),只需要在剛剛的代碼中,添加下面這一行即可:

sseListener.setShowToolArgs(true);

更換其它平臺(tái)

細(xì)心的你可能已經(jīng)發(fā)現(xiàn),我們?cè)趧?chuàng)建chatService的時(shí)候傳入了PlatformType.OPENAI,如果我們想要更換其它平臺(tái),只需要更換PlatformType即可。如果你不懂,那下篇博文再詳細(xì)的說說

IChatService chatService = aiService.getChatService(PlatformType.OPENAI);

其它功能

篇幅有限,其它功能使用,下篇再講

到此這篇關(guān)于SpringBoot快速接入OpenAI大模型(JDK8)的文章就介紹到這了,更多相關(guān)SpringBoot接入OpenAI大模型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

太仓市| 常山县| 乐至县| 武冈市| 贡山| 营口市| 龙陵县| 青铜峡市| 龙口市| 阿克苏市| 玉树县| 永城市| 贵州省| 黄梅县| 盐城市| 昆明市| 磐安县| 阿克苏市| 茂名市| 溧阳市| 壤塘县| 吉林市| 永城市| 剑河县| 西乌珠穆沁旗| 荣昌县| 苗栗市| 惠安县| 汉寿县| 安西县| 永康市| 来安县| 茂名市| 巴青县| 永仁县| 尖扎县| 葵青区| 都兰县| 石屏县| 惠来县| 望城县|