|
|
@@ -18,7 +18,9 @@ import okhttp3.Response;
|
|
|
public class AiApi {
|
|
|
public static final String API_KEY = "JDAC4Uf4JpVoKnP64do0YRWH";
|
|
|
public static final String SECRET_KEY = "d2Tx3sG1LVvW2p8SFkJYqfjfV5hpv4E8";
|
|
|
-
|
|
|
+
|
|
|
+ private static final int MAX_RETRY_COUNT = 4;
|
|
|
+
|
|
|
static String getAccessToken() throws IOException {
|
|
|
OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
|
|
|
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
|
|
|
@@ -44,33 +46,40 @@ public class AiApi {
|
|
|
try {
|
|
|
switch (modelNum) {
|
|
|
case 1: // Deepseek
|
|
|
+ ask = deepseek3(str);
|
|
|
+ askJson = net.sf.json.JSONObject.fromObject(ask);
|
|
|
+ if (!askJson.has("error")) { // 调用成功
|
|
|
+ message.setLlm(askJson.getString("model"));
|
|
|
+ result = extractContentFromDeepseek(askJson);
|
|
|
+ } else { // 调用失败
|
|
|
+ throw new IOException(ask);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 2: // Deepseek
|
|
|
ask = deepseek(str);
|
|
|
askJson = net.sf.json.JSONObject.fromObject(ask);
|
|
|
- if (!askJson.has("error")) { // Deepseek 调用成功
|
|
|
+ if (!askJson.has("error")) { // 调用成功
|
|
|
message.setLlm(askJson.getString("model"));
|
|
|
- net.sf.json.JSONArray choicesJson = askJson.getJSONArray("choices");
|
|
|
- net.sf.json.JSONObject choiceJson = choicesJson.getJSONObject(0);
|
|
|
- net.sf.json.JSONObject messageJson = choiceJson.getJSONObject("message");
|
|
|
- result = messageJson.getString("content");
|
|
|
- } else { // Deepseek 调用失败
|
|
|
+ result = extractContentFromDeepseek(askJson);
|
|
|
+ } else { // 调用失败
|
|
|
throw new IOException(ask);
|
|
|
}
|
|
|
break;
|
|
|
- case 2: // 文心一言 默认模型
|
|
|
+ default: // 文心一言 默认模型
|
|
|
ask = yiyan(str);
|
|
|
message.setLlm("yiyan");
|
|
|
askJson = net.sf.json.JSONObject.fromObject(ask);
|
|
|
result = askJson.getString("result");
|
|
|
break;
|
|
|
- default:
|
|
|
- result = "系统出错,请稍后重试";
|
|
|
- break;
|
|
|
}
|
|
|
message.setReceiveContent(result);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
- modelNum++;
|
|
|
- message = getResult(str, modelNum);
|
|
|
+ if (modelNum < MAX_RETRY_COUNT ) {
|
|
|
+ message = getResult(str, modelNum + 1);
|
|
|
+ } else {
|
|
|
+ message.setReceiveContent("系统出错,请稍后重试");
|
|
|
+ }
|
|
|
}
|
|
|
return message;
|
|
|
}
|
|
|
@@ -124,7 +133,14 @@ public class AiApi {
|
|
|
.build();
|
|
|
Response response = HTTP_CLIENT.newCall(request).execute();
|
|
|
return response.body().string();
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String extractContentFromDeepseek(net.sf.json.JSONObject askJson) {
|
|
|
+ net.sf.json.JSONArray choicesJson = askJson.getJSONArray("choices");
|
|
|
+ net.sf.json.JSONObject choiceJson = choicesJson.getJSONObject(0);
|
|
|
+ net.sf.json.JSONObject messageJson = choiceJson.getJSONObject("message");
|
|
|
+ return messageJson.getString("content");
|
|
|
+ }
|
|
|
|
|
|
public static void main(String []args) throws IOException{
|
|
|
System.out.println(sendAsk("请说出你的名字"));
|