|
@@ -36,7 +36,9 @@ public class LlmController {
|
|
@PostMapping(AiConstants.LLM_CHAT_ENDPOINT_PATH)
|
|
@PostMapping(AiConstants.LLM_CHAT_ENDPOINT_PATH)
|
|
public ChatEndpoint chatEndpoint(@RequestAttribute AccessOrg accessOrg,
|
|
public ChatEndpoint chatEndpoint(@RequestAttribute AccessOrg accessOrg,
|
|
@RequestHeader(AiConstants.LLM_APP_TYPE) LlmAppType type,
|
|
@RequestHeader(AiConstants.LLM_APP_TYPE) LlmAppType type,
|
|
- @RequestBody Object param) throws Exception {
|
|
|
|
|
|
+ @RequestBody Object param,
|
|
|
|
+ @RequestParam(required = false) Boolean stream,
|
|
|
|
+ @RequestParam(required = false) Boolean enableThinking) throws Exception {
|
|
LlmOrgConfig config = llmOrgConfigService.findByOrgAndAppType(accessOrg.getOrg().getId(), type);
|
|
LlmOrgConfig config = llmOrgConfigService.findByOrgAndAppType(accessOrg.getOrg().getId(), type);
|
|
if (config == null || config.getLeftCount() <= 0) {
|
|
if (config == null || config.getLeftCount() <= 0) {
|
|
throw new ForbiddenException(
|
|
throw new ForbiddenException(
|
|
@@ -47,6 +49,7 @@ public class LlmController {
|
|
throw new NotFoundException(
|
|
throw new NotFoundException(
|
|
"Chat prompt template not found for app_type=" + type + ", modelId=" + config.getModelId());
|
|
"Chat prompt template not found for app_type=" + type + ", modelId=" + config.getModelId());
|
|
}
|
|
}
|
|
|
|
+
|
|
ChatRequest request = new ChatRequest();
|
|
ChatRequest request = new ChatRequest();
|
|
String systemMessage = FreemarkerUtil.getValue(llmPromptTemplate.getSystem(), param, null);
|
|
String systemMessage = FreemarkerUtil.getValue(llmPromptTemplate.getSystem(), param, null);
|
|
String userMessage = FreemarkerUtil.getValue(llmPromptTemplate.getUser(), param, null);
|
|
String userMessage = FreemarkerUtil.getValue(llmPromptTemplate.getUser(), param, null);
|
|
@@ -56,7 +59,15 @@ public class LlmController {
|
|
if (StringUtils.isNotBlank(userMessage)) {
|
|
if (StringUtils.isNotBlank(userMessage)) {
|
|
request.addMessage(ChatRole.user, userMessage);
|
|
request.addMessage(ChatRole.user, userMessage);
|
|
}
|
|
}
|
|
- request.setStream(true);//todo
|
|
|
|
|
|
+
|
|
|
|
+ request.setStream(stream != null ? stream : false);
|
|
|
|
+ if (enableThinking == null || !enableThinking) {
|
|
|
|
+ request.setEnableThinking(false);
|
|
|
|
+ } else {
|
|
|
|
+ request.setEnableThinking(true);
|
|
|
|
+ // 若开启思考模式,则默认流式输出
|
|
|
|
+ request.setStream(true);
|
|
|
|
+ }
|
|
|
|
|
|
return llmClientService.chatEndpoint(request, config.getModelId(), type);
|
|
return llmClientService.chatEndpoint(request, config.getModelId(), type);
|
|
}
|
|
}
|
|
@@ -71,6 +82,7 @@ public class LlmController {
|
|
throw new ForbiddenException(
|
|
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(), type);
|
|
ChatResult result = llmClientService.chat(request, config.getModelId(), type);
|
|
llmOrgConfigService.consume(config);
|
|
llmOrgConfigService.consume(config);
|
|
return result;
|
|
return result;
|
|
@@ -78,18 +90,20 @@ public class LlmController {
|
|
|
|
|
|
@PostMapping(AiConstants.LLM_CHAT_TEMPLATE_PATH)
|
|
@PostMapping(AiConstants.LLM_CHAT_TEMPLATE_PATH)
|
|
public ChatResult chatTemplate(@RequestAttribute AccessOrg accessOrg,
|
|
public ChatResult chatTemplate(@RequestAttribute AccessOrg accessOrg,
|
|
- @RequestHeader(AiConstants.LLM_APP_TYPE) LlmAppType type, @RequestBody Object param)
|
|
|
|
- throws Exception {
|
|
|
|
|
|
+ @RequestHeader(AiConstants.LLM_APP_TYPE) LlmAppType type,
|
|
|
|
+ @RequestBody Object param) throws Exception {
|
|
LlmOrgConfig config = llmOrgConfigService.findByOrgAndAppType(accessOrg.getOrg().getId(), type);
|
|
LlmOrgConfig config = llmOrgConfigService.findByOrgAndAppType(accessOrg.getOrg().getId(), type);
|
|
if (config == null || config.getLeftCount() <= 0) {
|
|
if (config == null || config.getLeftCount() <= 0) {
|
|
throw new ForbiddenException(
|
|
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);
|
|
}
|
|
}
|
|
|
|
+
|
|
LlmPromptTemplate llmPromptTemplate = llmPromptTemplateService.findById(config.getPromptId());
|
|
LlmPromptTemplate llmPromptTemplate = llmPromptTemplateService.findById(config.getPromptId());
|
|
if (llmPromptTemplate == null) {
|
|
if (llmPromptTemplate == null) {
|
|
throw new NotFoundException(
|
|
throw new NotFoundException(
|
|
"Chat prompt template not found for app_type=" + type + ", modelId=" + config.getModelId());
|
|
"Chat prompt template not found for app_type=" + type + ", modelId=" + config.getModelId());
|
|
}
|
|
}
|
|
|
|
+
|
|
ChatRequest request = new ChatRequest();
|
|
ChatRequest request = new ChatRequest();
|
|
String systemMessage = FreemarkerUtil.getValue(llmPromptTemplate.getSystem(), param, null);
|
|
String systemMessage = FreemarkerUtil.getValue(llmPromptTemplate.getSystem(), param, null);
|
|
String userMessage = FreemarkerUtil.getValue(llmPromptTemplate.getUser(), param, null);
|
|
String userMessage = FreemarkerUtil.getValue(llmPromptTemplate.getUser(), param, null);
|
|
@@ -99,6 +113,7 @@ public class LlmController {
|
|
if (StringUtils.isNotBlank(userMessage)) {
|
|
if (StringUtils.isNotBlank(userMessage)) {
|
|
request.addMessage(ChatRole.user, userMessage);
|
|
request.addMessage(ChatRole.user, userMessage);
|
|
}
|
|
}
|
|
|
|
+
|
|
ChatResult result = llmClientService.chat(request, config.getModelId(), type);
|
|
ChatResult result = llmClientService.chat(request, config.getModelId(), type);
|
|
llmOrgConfigService.consume(config);
|
|
llmOrgConfigService.consume(config);
|
|
return result;
|
|
return result;
|