|
@@ -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)
|