看效果

代码如下

package com.hi.hailiaowenan.thirdpart.service.impl;

import org.apache.http.HttpStatus;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.HashMap;
import com.alibaba.fastjson.JSONObject;
import com.hi.hailiaowenan.thirdpart.service.ChatService;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;

@Service
public class ChatServiceImpl implements ChatService {

    @Override
    public JSONObject completions(String content, String appId) {

        // ============ 接口url ================
        String url = "xxx";

        // ============ 请求body ================
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model", "gpt-3.5-turbo");
        jsonObject.put("stream", true);
        JSONObject messages = new JSONObject();
        JSONArray array = new JSONArray();
        messages.put("role", "user");
        messages.put("content", content);
        array.add(messages);
        jsonObject.put("messages", array);

        // ============ 添加请求头信息 ================
        Map<String, String> heads = new HashMap<>();
        // 使用json发送请求,下面的是必须的
        heads.put("Content-Type", "application/json");
        heads.put("Authorization", "xxx");

        // ============ 发送请求 ================
        HttpResponse response = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(String.valueOf(jsonObject))
                .timeout(5 * 60 * 1000)
                .execute();
        // ============ 打印结果 ================
        System.out.println("============ \u6253\u5370\u7ED3\u679C ================");
        System.out.println(response);
        // String strResult = EntityUtils.toString(response.body());
        return jsonObject;
    }

}

或者

package com.hi.hailiaowenan.thirdpart.service.impl;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hi.hailiaowenan.thirdpart.service.ChatService;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;

@Service
public class ChatServiceImpl implements ChatService{

    @Override
    public JSONObject completions(String content, String appId) {

        // ============ 接口url ================
        String url = "xx";

        // ============ 请求body ================
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model", "gpt-3.5-turbo");
        jsonObject.put("stream", true);
        JSONObject messages = new JSONObject();
        JSONArray array = new JSONArray();
        messages.put("role", "user");
        messages.put("content", content);
        array.add(messages);
        jsonObject.put("messages", array);

        // ============ 发送请求 ================
        String result2 = HttpRequest.post(url)
                .header("Content-Type", "application/json")
                .header("Authorization", "xxx")
                .body(String.valueOf(jsonObject))
                .timeout(5 * 60 * 1000)
                .execute().body();
        // ============ 打印结果 ================
        System.out.println("============ \u6253\u5370\u7ED3\u679C ================");
        System.out.println(result2);
        //11.读取输入流中的返回值
        return JSON.parseObject(result2);
    }

}

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐