This commit is contained in:
zhaoyuhang
2023-06-30 00:03:21 +08:00
parent fb90e98a4e
commit c06934bc89

View File

@@ -5,11 +5,13 @@ import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray; import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import com.abin.mallchat.common.common.exception.BusinessException; import com.abin.mallchat.common.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@Slf4j
public class OpenAIUtils { public class OpenAIUtils {
private static final String URL = "https://api.openai.com/v1/completions"; private static final String URL = "https://api.openai.com/v1/completions";
@@ -73,7 +75,13 @@ public class OpenAIUtils {
} }
public static String parseText(String body) { public static String parseText(String body) {
log.info("body >>> " + body);
JSONObject jsonObj = new JSONObject(body); JSONObject jsonObj = new JSONObject(body);
JSONObject error = jsonObj.getJSONObject("error");
if (error != null) {
log.error("error >>> " + error);
return "闹脾气了,等会再试试吧~";
}
JSONArray choicesArr = jsonObj.getJSONArray("choices"); JSONArray choicesArr = jsonObj.getJSONArray("choices");
JSONObject choiceObj = choicesArr.getJSONObject(0); JSONObject choiceObj = choicesArr.getJSONObject(0);
return choiceObj.getStr("text"); return choiceObj.getStr("text");