本文主要是介绍Java8对接三方流式接口,并实时输出(GPT),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java对接模型流式接口,并流式输出
核心依赖
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.33</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
Demo代码
@GetMapping(value = "/test", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)public DeferredResult<ResponseBodyEmitter> proxyModel22() throws IOException {DeferredResult<ResponseBodyEmitter> deferredResult = new DeferredResult<>();// 创建 URLURL url = new URL("Third-party API");// 打开连接HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("POST");connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");connection.setDoOutput(true); // 设置为 POST 请求connection.setDoInput(true); // 允许读取响应// 构建请求体String bodyJSON = "{\n" +" \"input\": \"Who are you?\",\n" +" \"user\": \"guest\",\n" +" \"stream\": true,\n" +" \"increase\": true,\n" +" \"sessionId\": \"9xxxxxx\",\n" +" \"history\": []\n" +"}";JSONObject jsonObject = JSON.parseObject(bodyJSON);try (OutputStream os = connection.getOutputStream()) {os.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8)); // 写入请求体os.flush();}// 检查响应状态码int responseCode = connection.getResponseCode();if (responseCode == HttpURLConnection.HTTP_OK) {ResponseBodyEmitter emitter = new ResponseBodyEmitter();InputStream inputStream = connection.getInputStream();Thread thread = new Thread(() -> {byte[] buffer = new byte[4096]; // 缓冲区大小int length;try {while ((length = inputStream.read(buffer)) != -1) {// 只发送实际读取的数据长度byte[] dataToSend = new byte[length];System.arraycopy(buffer, 0, dataToSend, 0, length);emitter.send(dataToSend);}inputStream.close();emitter.complete(); // 完成发送} catch (IOException e) {log.error("错误信息为: " + e);}});thread.start();deferredResult.setResult(emitter);} else {// 如果第三方服务返回错误状态码deferredResult.setErrorResult(new RuntimeException("Third-party API returned an error: " + responseCode));}return deferredResult;}
这篇关于Java8对接三方流式接口,并实时输出(GPT)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!