JAVA流式调用大模型

流式调用文心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();
        }
    }
  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值