ソースを参照

core-ai consumeBalance api

deason 1 ヶ月 前
コミット
ca6741e276

+ 20 - 0
src/main/java/com/qmth/ops/api/controller/ai/LlmController.java

@@ -15,6 +15,8 @@ import com.qmth.ops.biz.service.LlmClientService;
 import com.qmth.ops.biz.service.LlmOrgConfigService;
 import com.qmth.ops.biz.service.LlmPromptTemplateService;
 import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
@@ -24,6 +26,8 @@ import javax.annotation.Resource;
 @Aac(auth = true, signType = SignatureType.SECRET)
 public class LlmController {
 
+    private static final Logger log = LoggerFactory.getLogger(LlmController.class);
+
     @Resource
     private LlmOrgConfigService llmOrgConfigService;
 
@@ -131,6 +135,22 @@ public class LlmController {
         return balance;
     }
 
+    @PostMapping(AiConstants.LLM_BALANCE_CONSUME_PATH)
+    public LlmAppBalance consumeBalance(@RequestAttribute AccessOrg accessOrg,
+                                        @RequestHeader(AiConstants.LLM_APP_TYPE) LlmAppType type) {
+        LlmAppBalance balance = new LlmAppBalance();
+        LlmOrgConfig config = llmOrgConfigService.findByOrgAndAppType(accessOrg.getOrg().getId(), type);
+        if (config != null && config.getLeftCount() > 0) {
+            log.warn("Consume balance for org:{} appType:{} permitCount:{} leftCount:{}",
+                    accessOrg.getOrg().getCode(), type, config.getPermitCount(), config.getLeftCount());
+            llmOrgConfigService.consume(config);
+
+            balance.setPermitCount(config.getPermitCount());
+            balance.setLeftCount(config.getLeftCount() - 1);
+        }
+        return balance;
+    }
+
     @PostMapping(AiConstants.LLM_PROMPT_TEMPLATE_PATH)
     public PromptTemplate getPromptTemplate(@RequestAttribute AccessOrg accessOrg,
                                             @RequestHeader(AiConstants.LLM_APP_TYPE) LlmAppType type) {