我的要求是通过post发送一个请求来调用API接口,然后逐个打印服务器返回的文本/事件流事件流,不管我使用的是HttpURLConnection、WebFlux、或者RestTemplate,客户端等待服务器的完整响应,然后将数据合并成一块打印出来,无法实现一块一块返回的效果。有什么解决方案可以满足我的需求吗?这是我通过crul发送请求时得到的结果:
{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "1", "index": 0, "logprobs": null, "finish_reason": null}]}
{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "2", "index": 0, "logprobs": null, "finish_reason": null}]}
{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "3", "index": 0, "logprobs": null, "finish_reason": null}]}
{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "4", "index": 0, "logprobs": null, "finish_reason": null}]}
{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "5", "index": 0, "logprobs": null, "finish_reason": null}]}
{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "6", "index": 0, "logprobs": null, "finish_reason": null}]}
{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "7", "index": 0, "logprobs": null, "finish_reason": null}]}
这是我使用HttpURLConnection编写的代码:
HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Charset", "UTF-8");
urlConnection.setRequestProperty("Authorization", "Bearer " + token);
urlConnection.setRequestProperty("Accept", "text/event-stream");
urlConnection.getOutputStream().write(JSON.toJSONString(completionRequest).getBytes(StandardCharsets.UTF_8));
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println("lien:"+line);
}
这是我用webflux写的代码:
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.parseMediaType("application/json; charset=UTF-8"));
httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
httpHeaders.add("Authorization", "Bearer " + token);
HttpEntity<String> formEntity = new HttpEntity<String>(completionJSON, httpHeaders);
Flux.create(emitter -> {
try {
List<String> resultList = Arrays.stream(restTemplate.postForObject(url, formEntity, String.class).split("\n")).
filter(line -> !line.trim().equals("")).collect(Collectors.toList());
for (String result : resultList) {
String message = result.replace("data: ", "");
if (message.equals("[DONE]")) {
return;
} else {
JSONArray choicesJSON = new JSONObject(message).getJSONArray("choices");
Object text = new JSONObject(choicesJSON.get(0).toString()).get("text");
emitter.next(text);
}
}
} catch (Exception ex) {
emitter.error(ex);
}
}).subscribe(System.out::println);
但在这两种情况下,输出如下所示:
{"id": "cmpl-6onbJlmNNZBShLvHeJhdOMRAhR2x6", "object": "text_completion", "created": 1677565085, "choices": [{"text":1 2 3 4 5 6 7", "index": 0, "logprobs": null, "finish_reason": null}]}
很明显,结果已经被合并,而不是被逐条返回打印,有没有办法让客户端一条条接收数据并打印出来?
1条答案
按热度按时间siv3szwd1#
假设服务器使用
text/event-stream
媒体类型返回数据,您可以使用WebClient
使用流,然后作为Flux
发出。您可以通过模拟来自同一控制器的服务器事件来测试它
下面是要验证的
curl
命令