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

增加异常返回类型,增加按模版chat调用接口实现

luoshi пре 1 година
родитељ
комит
235b54d34c

+ 9 - 0
pom.xml

@@ -54,6 +54,10 @@
             <groupId>com.qmth.boot</groupId>
             <artifactId>core-solar</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.qmth.boot</groupId>
+            <artifactId>tools-freemarker</artifactId>
+        </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
@@ -113,6 +117,11 @@
                 <artifactId>core-sms</artifactId>
                 <version>${qmth.boot.version}</version>
             </dependency>
+            <dependency>
+                <groupId>com.qmth.boot</groupId>
+                <artifactId>tools-freemarker</artifactId>
+                <version>${qmth.boot.version}</version>
+            </dependency>
             <dependency>
                 <groupId>junit</groupId>
                 <artifactId>junit</artifactId>

+ 34 - 6
src/main/java/com/qmth/ops/api/controller/ai/LlmController.java

@@ -4,6 +4,8 @@ import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.core.ai.model.AiConstants;
 import com.qmth.boot.core.ai.model.llm.*;
 import com.qmth.boot.core.exception.ForbiddenException;
+import com.qmth.boot.core.exception.NotFoundException;
+import com.qmth.boot.tools.freemarker.FreemarkerUtil;
 import com.qmth.boot.tools.signature.SignatureType;
 import com.qmth.ops.api.security.AccessOrg;
 import com.qmth.ops.biz.domain.LlmOrgConfig;
@@ -15,6 +17,7 @@ import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
+import java.util.Map;
 
 @RestController
 @Aac(auth = true, signType = SignatureType.SECRET)
@@ -34,14 +37,39 @@ public class LlmController {
             @RequestHeader(AiConstants.LLM_APP_TYPE_HEADER) LlmAppType type,
             @RequestBody @Validated ChatRequest request) throws Exception {
         LlmOrgConfig config = llmOrgConfigService.findByOrgAndAppType(accessOrg.getOrg(), type);
-        if (config != null && config.getLeftCount() > 0) {
-            ChatResult result = llmClientService.chat(request, config.getModelId());
-            llmOrgConfigService.consume(config);
-            return result;
-        } else {
+        if (config == null || config.getLeftCount() <= 0) {
             throw new ForbiddenException(
-                    "Chat api is disabled or exhausted for org=" + accessOrg.getOrg().getCode() + ", app type=" + type);
+                    "Chat api is disabled or exhausted for org=" + accessOrg.getOrg().getCode() + ", app_type=" + type);
         }
+        ChatResult result = llmClientService.chat(request, config.getModelId());
+        llmOrgConfigService.consume(config);
+        return result;
+    }
+
+    @PostMapping(AiConstants.LLM_CHAT_TEMPLATE_PATH)
+    public ChatResult chatTemplate(@RequestAttribute AccessOrg accessOrg,
+            @RequestHeader(AiConstants.LLM_APP_TYPE_HEADER) LlmAppType type, @RequestBody Map<String, Object> param)
+            throws Exception {
+        LlmOrgConfig config = llmOrgConfigService.findByOrgAndAppType(accessOrg.getOrg(), type);
+        if (config == null || config.getLeftCount() <= 0) {
+            throw new ForbiddenException(
+                    "Chat api is disabled or exhausted for org=" + accessOrg.getOrg().getCode() + ", app_type=" + type);
+        }
+        LlmPromptTemplate llmPromptTemplate = llmPromptTemplateService.findByAppType(type, config.getModelId());
+        if (llmPromptTemplate == null) {
+            throw new NotFoundException(
+                    "Chat prompt template not found for app_type=" + type + ", modelId=" + config.getModelId());
+        }
+        ChatRequest request = new ChatRequest();
+        if (llmPromptTemplate.getSystem() != null) {
+            request.addMessage(ChatRole.system, FreemarkerUtil.getValue(llmPromptTemplate.getSystem(), param, ""));
+        }
+        if (llmPromptTemplate.getUser() != null) {
+            request.addMessage(ChatRole.user, FreemarkerUtil.getValue(llmPromptTemplate.getUser(), param, ""));
+        }
+        ChatResult result = llmClientService.chat(request, config.getModelId());
+        llmOrgConfigService.consume(config);
+        return result;
     }
 
     @PostMapping(AiConstants.LLM_BALANCE_PATH)

+ 2 - 2
src/main/java/com/qmth/ops/biz/ai/client/aliyun/AliyunChatClient.java

@@ -51,9 +51,9 @@ public class AliyunChatClient extends ChatApiClient {
         case 400:
             throw new ChatRequestError(error != null ? error.getMessage() : "chat request error");
         case 429:
-            throw new ReentrantException(error != null ? error.getMessage() : "chat api rate limit exceeded");
+            throw new ReentrantException(error != null ? error.getMessage() : "chat model rate limit exceeded");
         default:
-            throw new StatusException(error != null ? error.getMessage() : "chat api error");
+            throw new StatusException(error != null ? error.getMessage() : "chat model error");
         }
     }
 

+ 1 - 1
src/main/java/com/qmth/ops/biz/ai/client/azure/AzureChatClient.java

@@ -49,7 +49,7 @@ public class AzureChatClient extends ChatApiClient {
         case 404:
             throw new NotFoundException(message != null ? message : "chat resource not found");
         default:
-            throw new StatusException(message != null ? message : "chat api error");
+            throw new StatusException(message != null ? message : "chat model error");
         }
     }
 

+ 1 - 2
src/main/java/com/qmth/ops/biz/service/InitService.java

@@ -28,8 +28,7 @@ public class InitService implements SqlProvider, CommandLineRunner {
     @Override
     public String get() {
         try {
-            return ByteArray.fromInputStream(this.getClass().getClassLoader().getResourceAsStream("script/init.sql"))
-                    .toString();
+            return ByteArray.fromResource("script/init.sql").toString();
         } catch (IOException e) {
             throw new RuntimeException("数据库初始化异常", e);
         }