流式调用文心3.5,文心4.0以及Chatgpt大模型
1.需要在参数中增加“stream”参数,代表流式调用
Map<String,Object> map = new HashMap<>();
map.put("role","user");
map.put("content",question);
jsonArray.add(new JSONObject(map));
builder.put("messages", jsonArray);
builder.put("stream", true);
2.创建并发送请求(每个大模型的api调用方式不尽相同,需要查看官方文档)
//文心3.5
// 创建HTTP请求
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(tongyiqianwen35 + "?access_token=" + token))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(new JSONObject(builder).toString()))
.build();
// 发送请求npm
HttpClient client = HttpClient.newHttpClient();
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
//文心4.0
// 创建HTTP请求
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(tongyiqianwen40 + "?access_token=" + token))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(new JSONObject(builder).toString()))
.build();
// 发送请求npm
HttpClient client = HttpClient.newHttpClient();
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
//Chatgpt
// 创建HTTP请求
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(chatgpt35))
.header("Authorization", "Bearer " + apikey)
.header("Content-Type", "application/json")
.timeout(Duration.ofSeconds(60))
.POST(HttpRequest.BodyPublishers.ofString(bodymap.toString()))
.build();
// 发送请求npm
HttpClient client = HttpClient.newHttpClient();
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
3.解析数据流,实现流式请求调用中每解析出一句就实时做出处理的效果
public void doTongYiStream(HttpResponse<InputStream> response) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.trim().isEmpty()) {
line = line.replace("data: ", "");
try {
JSONObject reqJson = JSON.parseObject(line);
processPartialResponse(reqJson.getString("result"));
} catch (Exception e) {
System.err.println("Failed to parse JSON: " + line);
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void doChatGPTStream(HttpResponse<InputStream> response) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.trim().isEmpty() && !"data: [DONE]".equals(line)) {
line = line.replace("data: ", "");
try {
JSONObject reqJson = JSON.parseObject(line);
JSONArray choicesArray = reqJson.getJSONArray("choices");
JSONObject firstChoice = choicesArray.getJSONObject(0);
JSONObject delta = firstChoice.getJSONObject("delta");
if (delta.size()!=0){
String content = delta.getString("content");
processPartialResponse(content);
}
} catch (Exception e) {
System.err.println("Failed to parse JSON: " + line);
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}