编写java版本的http流式返回访问GPT

  • 既然测试python可以实现http流式返回,那么java也可以实现。
  • 使用okHttp3库,实现。

gradle依赖

 // okhttp3
    implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.9.1'
    // org.json
    implementation group: 'org.json', name: 'json', version: '20210307'

代码

package org.jow.http.stream;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class OKStream {
    public static void main(String[] args) throws IOException {
        // 循环聊天,直到输入“再见”
        while (true) {
            // 控制台输入
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("输入问题:");
            String str = reader.readLine();
            if ("bye".equals(str)) {
                break;
            }
            OKStream okStream = new OKStream();
            okStream.chatWithGPT(str);
        }
    }

    private void chatWithGPT(String message) throws IOException {
        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("application/json");
        JSONObject data = new JSONObject();
        data.put("messages", new JSONArray()
                .put(new JSONObject()
                        .put("role", "system")
                        .put("content", "You are an AI assistant that helps people find information."))
                .put(new JSONObject()
                        .put("role", "user")
                        .put("content", message)));
        data.put("temperature", 0.5);
        data.put("top_p", 0.95);
        data.put("frequency_penalty", 0);
        data.put("presence_penalty", 0);
        data.put("max_tokens", 800);
        data.put("stop", "null");
        data.put("stream", true);
        RequestBody body = RequestBody.create(mediaType, data.toString());
        Request request = new Request.Builder()
                .url("https://xxxx.openai.azure.com/openai/deployments/gpt-35-turbo-xxx/chat/completions?api-version=2023-03-15-preview")
                .post(body)
                .addHeader("Content-Type", "application/json")
                .addHeader("api-key", "************************")
                .build();
        Response response = client.newCall(request).execute();
        String line;
        while ((line = response.body().source().readUtf8Line()) != null) {
            if (line.equals("data: [DONE]")) {
                System.out.println("\n[DONE]");
                break;
            } else if (line.startsWith("data: ")) {
                line = line.substring(6);
                JSONObject responseJson = new JSONObject(line);
                if (responseJson.getJSONArray("choices").getJSONObject(0).getJSONObject("delta").has("content")) {
                    System.out.print(responseJson.getJSONArray("choices").getJSONObject(0).getJSONObject("delta").getString("content"));
                }
            }
        }
    }
}
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐