|
@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.tzld.longarticle.recommend.server.model.dto.kimi.KimiOfficialApiResponse;
|
|
import com.tzld.longarticle.recommend.server.model.dto.kimi.KimiOfficialApiResponse;
|
|
|
|
+import com.tzld.longarticle.recommend.server.model.dto.kimi.KimiResponse;
|
|
import com.tzld.longarticle.recommend.server.model.dto.kimi.KimiResult;
|
|
import com.tzld.longarticle.recommend.server.model.dto.kimi.KimiResult;
|
|
import com.tzld.longarticle.recommend.server.util.MapBuilder;
|
|
import com.tzld.longarticle.recommend.server.util.MapBuilder;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -29,9 +30,16 @@ public class KimiApiService {
|
|
.readTimeout(15, TimeUnit.MINUTES)
|
|
.readTimeout(15, TimeUnit.MINUTES)
|
|
.writeTimeout(15, TimeUnit.MINUTES)
|
|
.writeTimeout(15, TimeUnit.MINUTES)
|
|
.build();
|
|
.build();
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public KimiResult request(String prompt, String model, Double temperature) {
|
|
|
|
+ try {
|
|
|
|
+ return requestWeb(prompt);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("kimi web fail: " + e.getMessage());
|
|
|
|
+ return requestOfficialApi(prompt, model, temperature);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
public KimiResult requestOfficialApi(String prompt, String model, Double temperature) {
|
|
public KimiResult requestOfficialApi(String prompt, String model, Double temperature) {
|
|
KimiResult result = new KimiResult();
|
|
KimiResult result = new KimiResult();
|
|
@@ -72,13 +80,13 @@ public class KimiApiService {
|
|
KimiOfficialApiResponse obj = JSONObject.parseObject(responseContent, KimiOfficialApiResponse.class);
|
|
KimiOfficialApiResponse obj = JSONObject.parseObject(responseContent, KimiOfficialApiResponse.class);
|
|
if (CollectionUtil.isNotEmpty(obj.getChoices())) {
|
|
if (CollectionUtil.isNotEmpty(obj.getChoices())) {
|
|
result.setSuccess(true);
|
|
result.setSuccess(true);
|
|
- result.setResponse(obj);
|
|
|
|
|
|
+ result.setResponse(obj.getChoices().get(0).getMessage().getContent());
|
|
} else {
|
|
} else {
|
|
result.setFailReason("response empty");
|
|
result.setFailReason("response empty");
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
JSONObject json = JSONObject.parseObject(responseContent);
|
|
JSONObject json = JSONObject.parseObject(responseContent);
|
|
- result.setFailReason("request error code:" + response.code() + " message:" + json.getString("error"));
|
|
|
|
|
|
+ result.setFailReason("request official api error code:" + response.code() + " message:" + json.getString("error"));
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("kimi official api fail: " + e.getMessage());
|
|
log.error("kimi official api fail: " + e.getMessage());
|
|
@@ -86,4 +94,56 @@ public class KimiApiService {
|
|
}
|
|
}
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public KimiResult requestWeb(String prompt) {
|
|
|
|
+ KimiResult result = new KimiResult();
|
|
|
|
+ result.setSuccess(false);
|
|
|
|
+ if (TextUtils.isBlank(prompt) || TextUtils.isBlank(prompt.trim())) {
|
|
|
|
+ result.setFailReason("prompt is empty");
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ Map<Object, Object> bodyParam = MapBuilder
|
|
|
|
+ .builder()
|
|
|
|
+ .put("prompt", prompt)
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ MediaType mediaType = MediaType.parse("application/json");
|
|
|
|
+ RequestBody body = RequestBody.create(mediaType, JSONObject.toJSONString(bodyParam));
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
+ .url("http://crawler.aiddit.com/crawler/moonshot/kimi")
|
|
|
|
+ .method("POST", body)
|
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
|
+ .build();
|
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
|
+
|
|
|
|
+ String responseContent = response.body().string();
|
|
|
|
+ result.setResponseStr(responseContent);
|
|
|
|
+ log.info("kimi web responseContent = {}", responseContent);
|
|
|
|
+ if (response.isSuccessful()) {
|
|
|
|
+ KimiResponse obj = JSONObject.parseObject(responseContent, KimiResponse.class);
|
|
|
|
+ if (obj == null) {
|
|
|
|
+ result.setFailReason("远端业务接口处理出错,kimiResponse为空");
|
|
|
|
+ } else if (obj.getCode() != 0) {
|
|
|
|
+ result.setFailReason("远端业务接口处理出错,code不为0,code=" + obj.getCode() + ",msg=" + obj.getMsg());
|
|
|
|
+ } else if (obj.getData() == null) {
|
|
|
|
+ result.setFailReason("远端业务接口处理出错,data为空");
|
|
|
|
+ } else if (obj.getData().getData() == null) {
|
|
|
|
+ result.setFailReason("远端业务接口处理出错,data.data为空");
|
|
|
|
+ } else if (obj.getData().getData().getMsg() == null) {
|
|
|
|
+ result.setFailReason("远端业务接口处理出错,data.data.msg为空");
|
|
|
|
+ } else {
|
|
|
|
+ result.setSuccess(true);
|
|
|
|
+ result.setResponse(obj.getData().getData().getMsg());
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ JSONObject json = JSONObject.parseObject(responseContent);
|
|
|
|
+ result.setFailReason("request web error code:" + response.code() + " message:" + json.getString("error"));
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("kimi web fail: " + e.getMessage());
|
|
|
|
+ result.setFailReason(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
}
|
|
}
|