天天看點

OpenAI Java SDK——GPT-3.5-Turbo,支援語音轉文字,語音翻譯

作者:要寵你上天

簡介

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.4</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長連結。

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)

OpenAI Java SDK——GPT-3.5-Turbo,支援語音轉文字,語音翻譯

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

繼續閱讀