OpenAI Java SDK——chatgpt-java-v1.0.3更新支援GPT-3.5-Turbo,支援語音轉文字,語音翻譯。

程式設計師的黑洞發表於2023-03-03

簡介

chatgpt-java是一個OpenAI的Java版SDK,支援開箱即用。目前以支援官網全部Api。支援最新版本GPT-3.5-Turbo模型以及whisper-1模型。增加chat聊天對話以及語音檔案轉文字,語音翻譯。
開源地址:https://github.com/Grt1228/chatgpt-java

快速開始

匯入pom依賴

<dependency>
    <groupId>com.unfbx</groupId>
    <artifactId>chatgpt-java</artifactId>
    <version>1.0.3</version>
</dependency>
package com.unfbx.eventTest.test;
import com.unfbx.chatgpt.OpenAiClient;
import com.unfbx.chatgpt.entity.completions.CompletionResponse;
import java.util.Arrays;

public class TestB {
    public static void main(String[] args) {
        //代理可以為null
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.1.111", 7890));
        OpenAiClient openAiClient = OpenAiClient.builder()
                .apiKey("sk-**************")
                .proxy(proxy)
                .build();
        //簡單模型
        //CompletionResponse completions = //openAiClient.completions("我想申請轉專業,從計算機專業轉到會計學專業,幫我完成一份兩百字左右的申請書");
        //最新GPT-3.5-Turbo模型
        Message message = Message.builder().role(Message.Role.USER).content("你好啊我的夥伴!").build();
        ChatCompletion chatCompletion = ChatCompletion.builder().messages(Arrays.asList(message)).build();
        ChatCompletionResponse chatCompletionResponse = openAiClient.chatCompletion(chatCompletion);
        chatCompletionResponse.getChoices().forEach(e -> {
            System.out.println(e.getMessage());
        });
    }
}

支援流式輸出

官方對於解決請求緩慢的情況推薦使用流式輸出模式。
主要是基於SSE 實現的(可以百度下這個技術)。也是最近在瞭解到SSE。OpenAI官網在接受Completions介面的時候,有提到過這個技術。 Completion物件本身有一個stream屬性,當stream為true時候Api的Response返回就會變成Http長連結。 具體可以看下文件:https://platform.openai.com/docs/api-reference/completions/create

package com.unfbx.chatgpt;

********************

/**
 * @author https:www.unfbx.com
 * 2023-02-28
 */
public class OpenAiStreamClientTest {

    private OpenAiStreamClient client;
    @Before
    public void before() {
        //建立流式輸出客戶端
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.1.111", 7890));
        client = OpenAiStreamClient.builder()
                .connectTimeout(50)
                .readTimeout(50)
                .writeTimeout(50)
                .apiKey("sk-******************************")
                .proxy(proxy)
                .build();
    }
    //GPT-3.5-Turbo模型
    @Test
    public void chatCompletions() {
        ConsoleEventSourceListener eventSourceListener = new ConsoleEventSourceListener();
        Message message = Message.builder().role(Message.Role.USER).content("你好啊我的夥伴!").build();
        ChatCompletion chatCompletion = ChatCompletion.builder().messages(Arrays.asList(message)).build();
        client.streamChatCompletion(chatCompletion, eventSourceListener);
        CountDownLatch countDownLatch = new CountDownLatch(1);
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //常規對話模型
    @Test
    public void completions() {
        ConsoleEventSourceListener eventSourceListener = new ConsoleEventSourceListener();
        Completion q = Completion.builder()
                .prompt("我想申請轉專業,從計算機專業轉到會計學專業,幫我完成一份兩百字左右的申請書")
                .stream(true)
                .build();
        client.streamCompletions(q, eventSourceListener);
        CountDownLatch countDownLatch = new CountDownLatch(1);
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

輸出的是sse流式資料:


22:51:23.620 [OkHttp - OpenAI建立sse連線...
22:51:23.623 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"role":"assistant"},"index":0,"finish_reason":null}]}
22:51:23.625 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"你"},"index":0,"finish_reason":null}]}
22:51:23.636 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"好"},"index":0,"finish_reason":null}]}
22:51:23.911 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"!"},"index":0,"finish_reason":null}]}
22:51:23.911 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"有"},"index":0,"finish_reason":null}]}
22:51:23.911 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"什"},"index":0,"finish_reason":null}]}
22:51:23.911 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"麼"},"index":0,"finish_reason":null}]}
22:51:23.911 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"我"},"index":0,"finish_reason":null}]}
22:51:23.911 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"可以"},"index":0,"finish_reason":null}]}
22:51:23.911 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"幫"},"index":0,"finish_reason":null}]}
22:51:23.912 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"助"},"index":0,"finish_reason":null}]}
22:51:23.934 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"你"},"index":0,"finish_reason":null}]}
22:51:24.203 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"的"},"index":0,"finish_reason":null}]}
22:51:24.203 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"嗎"},"index":0,"finish_reason":null}]}
22:51:24.203 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"?"},"index":0,"finish_reason":null}]}
22:51:24.276 [OkHttp - OpenAI返回資料:{****省略無效資料******"model":"gpt-3.5-turbo-0301","choices":[{"delta":{},"index":0,"finish_reason":"stop"}]}
22:51:24.276 [OkHttp - OpenAI返回資料:[DONE]
22:51:24.277 [OkHttp - OpenAI返回資料結束了
22:51:24.277 [OkHttp - OpenAI關閉sse連線...

流式輸出如何整合Spring Boot實現 api介面?

可以參考專案:https://github.com/Grt1228/chatgpt-steam-output

實現自定義的EventSourceListener,例如:OpenAIEventSourceListener並持有一個SseEmitter,透過SseEmitter進行資料的通訊

postman測試

傳送請求:Get http://localhost:8080/test/sse?uid=123

看下response (需要新版本postman)
image

重點關注下header:Content-Type:text/event-stream

如果想結合前端顯示自行百度sse前端相關實現

說明

支援最新版的語音轉文字,語音翻譯api請參考測試程式碼:OpenAiClientTest.java

相關文章