|
|
@@ -0,0 +1,93 @@
|
|
|
+package com.tzld.piaoquan.api.component;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.tzld.piaoquan.api.model.dto.AIOfficialApiResponse;
|
|
|
+import com.tzld.piaoquan.api.model.dto.AIResult;
|
|
|
+import com.tzld.piaoquan.growth.common.utils.MapBuilder;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import okhttp3.*;
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.apache.http.util.TextUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class DeepSeekApiService {
|
|
|
+
|
|
|
+ private OkHttpClient client;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ client = new OkHttpClient().newBuilder()
|
|
|
+ .connectTimeout(5, TimeUnit.MINUTES)
|
|
|
+ .readTimeout(5, TimeUnit.MINUTES)
|
|
|
+ .writeTimeout(5, TimeUnit.MINUTES)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ public AIResult requestOfficialApi(String prompt, String model, Double temperature, Boolean isJSON) {
|
|
|
+ AIResult result = new AIResult();
|
|
|
+ result.setSuccess(false);
|
|
|
+ if (TextUtils.isBlank(prompt) || TextUtils.isBlank(prompt.trim())) {
|
|
|
+ result.setFailReason("prompt is empty");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ JSONArray jsonArray = new JSONArray();
|
|
|
+ JSONObject message = new JSONObject();
|
|
|
+ message.put("role", "user");
|
|
|
+ message.put("content", prompt);
|
|
|
+ jsonArray.add(message);
|
|
|
+
|
|
|
+ Map<Object, Object> bodyParam = MapBuilder
|
|
|
+ .builder()
|
|
|
+ .put("model", Optional.ofNullable(model).orElse("deepseek-chat"))
|
|
|
+ .put("temperature", Optional.ofNullable(temperature).orElse(0.3))
|
|
|
+ .put("messages", jsonArray)
|
|
|
+ .build();
|
|
|
+ if (isJSON) {
|
|
|
+ JSONObject formatJSON = new JSONObject();
|
|
|
+ formatJSON.put("type", "json_object");
|
|
|
+ bodyParam.put("response_format", formatJSON);
|
|
|
+ }
|
|
|
+
|
|
|
+ MediaType mediaType = MediaType.parse("application/json");
|
|
|
+ RequestBody body = RequestBody.create(mediaType, JSONObject.toJSONString(bodyParam));
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url("https://api.deepseek.com/chat/completions")
|
|
|
+ .method("POST", body)
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
+ .addHeader("Accept", "application/json")
|
|
|
+ .addHeader("Authorization", "Bearer sk-62d7b2c37f824735aa4985852c919c1f")
|
|
|
+ .build();
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+
|
|
|
+ String responseContent = response.body().string();
|
|
|
+ result.setResponseStr(responseContent);
|
|
|
+ log.info("deepseek api responseContent = {}", responseContent);
|
|
|
+ if (response.isSuccessful()) {
|
|
|
+ AIOfficialApiResponse obj = JSONObject.parseObject(responseContent, AIOfficialApiResponse.class);
|
|
|
+ if (CollectionUtils.isNotEmpty(obj.getChoices())) {
|
|
|
+ result.setSuccess(true);
|
|
|
+ result.setResponse(obj);
|
|
|
+ } else {
|
|
|
+ result.setFailReason("response empty");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ JSONObject json = JSONObject.parseObject(responseContent);
|
|
|
+ result.setFailReason("request error code:" + response.code() + " message:" + json.getString("error"));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("deepseek official api fail: " + e.getMessage());
|
|
|
+ result.setFailReason(e.getMessage());
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+}
|