Преглед изворни кода

fix 记录对话次数接口更换

chenbiao пре 2 година
родитељ
комит
9cb0c1c877

+ 16 - 6
netflix-service/src/main/java/com/cyksj/service/chatgpt/impl/ChatGptAccountServiceImpl.java

@@ -431,11 +431,10 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
     @Override
     public void saveConversationRecord(String userToken, ConversationRequest conversationRequest) {
         LambdaQueryWrapper<ChatgptSession> wrapper = Wrappers.lambdaQuery(ChatgptSession.class);
-
+        ChatgptUser chatgptUser = chatgptUserMapper.selectOne(Wrappers.lambdaQuery(ChatgptUser.class).eq(ChatgptUser::getUserToken, userToken));
         if (StringUtils.isNotBlank(conversationRequest.getCarId())) {
             wrapper.eq(ChatgptSession::getCarId, conversationRequest.getCarId());
         } else {
-            ChatgptUser chatgptUser = chatgptUserMapper.selectOne(Wrappers.lambdaQuery(ChatgptUser.class).eq(ChatgptUser::getUserToken, userToken));
             if (chatgptUser.getSessionId() != null) {
                 wrapper.eq(ChatgptSession::getId, chatgptUser.getSessionId());
             }
@@ -454,7 +453,22 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
         chatgptUserConversationRecord.setModel(conversationRequest.getModel());
         chatgptUserConversationRecordMapper.insert(chatgptUserConversationRecord);
 
+        String key = RedisService.key.CHATGPT_CONVERSATION_LIMIT.getName() + ":" + userToken;
+        long currentTimeMillis = System.currentTimeMillis();
+
+        // 如果未超过限制,记录当前请求的时间戳
+        redisService.zAdd(key, currentTimeMillis, currentTimeMillis);
+        // 设置ZSet的过期时间,窗口大小加上一段冗余时间
+        redisService.expire(key, (chatgptUser.getLimitTime() * 60 * 60) + 20);
+
         if (StringUtils.isNotBlank(chatgptSession.getCarId())) {
+            redisService.del();
+            if(redisService.hasKey("chatgpt:team:clears_in:" + chatgptSession.getCarId())){
+                redisService.del("chatgpt:team:clears_in:" + chatgptSession.getCarId());
+            }
+            if (redisService.hasKey("chatgpt:clears_in:" + chatgptSession.getCarId())) {
+                redisService.del("chatgpt:clears_in:" + chatgptSession.getCarId());
+            }
             updateExperienceAndScore(chatgptSession.getCarId(), !"text-davinci-002-render-sha".equals(conversationRequest.getModel()), System.currentTimeMillis());
         }
 
@@ -508,10 +522,6 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
             chatgptUserCarUsedRecordMapper.insert(chatgptUserCarUsedRecord);
             return conversationLimitResponse;
         } else {
-            // 如果未超过限制,记录当前请求的时间戳
-            redisService.zAdd(key, currentTimeMillis, currentTimeMillis);
-            // 设置ZSet的过期时间,窗口大小加上一段冗余时间
-            redisService.expire(key, (limitTime * 60 * 60) + 20);
             conversationLimitResponse.setLimited(false);
             return conversationLimitResponse;
         }

+ 23 - 4
netflix-web/src/main/java/com/cyksj/web/controller/mirror/MirrorController.java

@@ -171,10 +171,6 @@ public class MirrorController {
                 return chatgptConversionLimitView;
             } else {
                 response.setStatus(200);
-                //记录提问时间,车次,标题和模型
-                TASK_EXECUTOR.execute(() -> {
-                    chatGptAccountService.saveConversationRecord(userToken, conversationRequest);
-                });
             }
             //5.获取 conversationRequest 中的model 字段,判断是否为 gpt-4 如果为gpt-4 执行 查看是否达到限制方法
             //6.如果达到限制,返回状态码 429 并返回文本 xxx
@@ -183,6 +179,29 @@ public class MirrorController {
         return new ChatgptConversionLimitView();
     }
 
+    /**
+     * 对话成功后记录次数
+     */
+    @RequestMapping("/gpt/conversation/notifyUrl")
+    public void conversationNotifyUrl(@RequestParam(value = "isCar", defaultValue = "false") Boolean isCar, @RequestBody ConversationRequest conversationRequest) {
+        //1.从request获取请求头Authorization 并取 值内 'Bearer ' 后的值为usertoken
+        String authorization = request.getHeader("Authorization");
+        String carid = request.getHeader("Carid");
+        conversationRequest.setCarId(carid);
+        String userToken = authorization.substring(7);
+        //2.根据usertoken 查询 chatgpt_user 表中的记录
+        ChatgptUser user = chatGptAccountService.findByUserTokenAndExpireTimeAfter(userToken, LocalDateTime.now());
+        //3.如果记录不存在,返回状态码 401
+        if (user == null) {
+            return;
+        }
+        if(StringUtils.isNotBlank(conversationRequest.getCarId())){
+            TASK_EXECUTOR.execute(() -> {
+                chatGptAccountService.saveConversationRecord(userToken, conversationRequest);
+            });
+        }
+    }
+
 
     /**
      * 获取车队列表