Jelajahi Sumber

写入日志

yzx 1 hari lalu
induk
melakukan
0085d7a0d4
1 mengubah file dengan 47 tambahan dan 9 penghapusan
  1. 47 9
      src/main/java/com/telerobot/fs/robot/impl/DeepSeekChat.java

+ 47 - 9
src/main/java/com/telerobot/fs/robot/impl/DeepSeekChat.java

@@ -30,7 +30,16 @@ public class DeepSeekChat extends AbstractChatRobot {
         if (headers == null) {
             return "";
         }
-        String requestId = headers.get("x-request-id");
+        String requestId = headers.get("x-dashscope-request-id");
+        if (StringUtils.isBlank(requestId)) {
+            requestId = headers.get("x-acs-request-id");
+        }
+        if (StringUtils.isBlank(requestId)) {
+            requestId = headers.get("x-bailian-request-id");
+        }
+        if (StringUtils.isBlank(requestId)) {
+            requestId = headers.get("x-request-id");
+        }
         if (StringUtils.isBlank(requestId)) {
             requestId = headers.get("request-id");
         }
@@ -43,6 +52,25 @@ public class DeepSeekChat extends AbstractChatRobot {
         return StringUtils.defaultString(requestId);
     }
 
+    private String findLlmRequestIdFromBody(String responseBody) {
+        if (StringUtils.isBlank(responseBody)) {
+            return "";
+        }
+        try {
+            JSONObject json = JSON.parseObject(responseBody);
+            String requestId = json.getString("request_id");
+            if (StringUtils.isBlank(requestId)) {
+                requestId = json.getString("requestId");
+            }
+            if (StringUtils.isBlank(requestId)) {
+                requestId = json.getString("id");
+            }
+            return StringUtils.defaultString(requestId);
+        } catch (Throwable ignore) {
+            return "";
+        }
+    }
+
     @Override
     public LlmAiphoneRes  talkWithAiAgent(String question, Boolean... withKbResponse) {
         LlmAiphoneRes aiphoneRes = new LlmAiphoneRes();
@@ -127,7 +155,8 @@ public class DeepSeekChat extends AbstractChatRobot {
 
     private  JSONObject sendStreamingRequest(LlmAiphoneRes aiphoneRes, List<JSONObject> messages) throws IOException {
         JSONObject requestBody = new JSONObject();
-        requestBody.put("model", ((LlmAccount)getAccount()).getModelName());
+        String modelName = ((LlmAccount)getAccount()).getModelName();
+        requestBody.put("model", modelName);
         requestBody.put("stream", true);
         // enable stream output
 
@@ -152,15 +181,23 @@ public class DeepSeekChat extends AbstractChatRobot {
 
         try (Response response = CLIENT.newCall(request).execute()) {
             String headerRequestId = findLlmRequestId(response.headers());
-            if (StringUtils.isNotBlank(headerRequestId)) {
-                logger.info("{} llm response header requestId={}", getTraceId(), headerRequestId);
-            }
+            logger.info("{} llm http response. model={}, httpCode={}, headerRequestId={}",
+                    this.uuid, modelName, response.code(), StringUtils.defaultString(headerRequestId));
             if (!response.isSuccessful()) {
-                logger.error("Model api error: http-code={}, msg={}, url={}, requestId={}",
+                String responseBody = "";
+                if (response.body() != null) {
+                    responseBody = response.body().string();
+                }
+                String bodyRequestId = findLlmRequestIdFromBody(responseBody);
+                String finalRequestId = StringUtils.isNotBlank(headerRequestId) ? headerRequestId : bodyRequestId;
+                logger.error("{} llm api error. model={}, httpCode={}, msg={}, url={}, requestId={}, responseBody={}",
+                        this.uuid,
+                        modelName,
                         response.code(),
                         response.message(),
                         getAccount().serverUrl,
-                        headerRequestId
+                        StringUtils.defaultString(finalRequestId),
+                        StringUtils.left(responseBody, 800)
                 );
                 if(response.code() == HttpStatus.SC_UNAUTHORIZED) {
                    CommonUtils.setHangupCauseDetail(
@@ -204,7 +241,7 @@ public class DeepSeekChat extends AbstractChatRobot {
                     if (StringUtils.isBlank(bodyRequestId)) {
                         bodyRequestId = jsonResponse.getString("id");
                         if (StringUtils.isNotBlank(bodyRequestId)) {
-                            logger.info("{} llm stream requestId={}", getTraceId(), bodyRequestId);
+                            logger.info("{} llm stream requestId={}", this.uuid, bodyRequestId);
                         }
                     }
                     JSONObject message = jsonResponse.getJSONArray("choices")
@@ -258,8 +295,9 @@ public class DeepSeekChat extends AbstractChatRobot {
             }
 
             String answer = responseBuilder.toString();
+            String finalRequestId = StringUtils.isNotBlank(headerRequestId) ? headerRequestId : bodyRequestId;
             logger.info("{} recv llm response end flag. requestId={}, headerRequestId={}, answer={}",
-                    this.uuid, bodyRequestId, headerRequestId, answer);
+                    this.uuid, StringUtils.defaultString(finalRequestId), StringUtils.defaultString(headerRequestId), answer);
             if(ttsTextLength > 0){
                 sendToTts();
             }