Commit fa46d72a authored by alex yao's avatar alex yao

feat:长文/写作 添加思考模式/联网模式

parent 4ce373e7
......@@ -12,7 +12,7 @@ import java.util.List;
public interface LongTextDialoguesService {
void call(String dialoguesId, String fileUrl, String input, Integer[] knowledgeIds, Long userId) throws Exception;
void call(String dialoguesId, String fileUrl, String input, Integer[] knowledgeIds, Long userId, Boolean enableSearchEngine, Boolean enableDeepThinking) throws Exception;
LongTextSummaryDto summary(String dialoguesId, Long userId) throws Exception;
......
......@@ -2,13 +2,14 @@ package cn.com.poc.long_document.aggregate.impl;
import cn.com.poc.agent_application.entity.BizAgentApplicationDialoguesRecordEntity;
import cn.com.poc.agent_application.entity.BizAgentApplicationGcConfigEntity;
import cn.com.poc.agent_application.entity.CheckPluginUseEntity;
import cn.com.poc.agent_application.entity.KnowledgeContentResult;
import cn.com.poc.agent_application.service.BizAgentApplicationDialoguesRecordService;
import cn.com.poc.agent_application.service.BizAgentApplicationGcConfigService;
import cn.com.poc.agent_application.utils.AgentApplicationTools;
import cn.com.poc.ai_dialogues.entity.BizAiDialoguesEntity;
import cn.com.poc.ai_dialogues.service.BizAiDialoguesService;
import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.common.constant.FmxParamConfigConstant;
import cn.com.poc.common.model.BizFileUploadRecordModel;
import cn.com.poc.common.service.BizFileUploadRecordService;
import cn.com.poc.common.utils.DocumentLoad;
......@@ -30,10 +31,17 @@ import cn.com.poc.long_document.service.BizLongTextSummaryCacheService;
import cn.com.poc.thirdparty.resource.demand.ai.aggregate.DemandKnowledgeService;
import cn.com.poc.thirdparty.resource.demand.ai.constants.KnowledgeSearchTypeEnum;
import cn.com.poc.thirdparty.resource.demand.ai.constants.LLMRoleEnum;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.FunctionCall;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Message;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Tool;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.ToolFunction;
import cn.com.poc.thirdparty.resource.demand.ai.entity.function.FunctionCallResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.knowledge.SearchKnowledgeResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelDemandResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelResponse;
import cn.com.poc.thirdparty.resource.demand.ai.function.AbstractFunctionResult;
import cn.com.poc.thirdparty.resource.demand.ai.function.web_seach.WebSearchFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.web_seach.WebSearchFunctionResult;
import cn.com.poc.thirdparty.service.LLMService;
import cn.com.yict.framemax.core.exception.BusinessException;
import cn.com.yict.framemax.frame.service.FmxParamConfigService;
......@@ -97,9 +105,12 @@ public class LongTextDialoguesServiceImpl implements LongTextDialoguesService {
@Resource
private FmxParamConfigService fmxParamConfigService;
@Resource
private WebSearchFunction webSearchFunction;
@Override
public void call(String dialoguesId, String fileUrl, String input, Integer[] knowledgeIds, Long userId) throws Exception {
public void call(String dialoguesId, String fileUrl, String input, Integer[] knowledgeIds, Long userId, Boolean enableSearchEngine, Boolean enableDeepThinking) throws Exception {
BizAiDialoguesEntity bizAiDialoguesEntity = new BizAiDialoguesEntity();
bizAiDialoguesEntity.setDialoguesId(dialoguesId);
bizAiDialoguesEntity.setMemberId(userId);
......@@ -165,9 +176,15 @@ public class LongTextDialoguesServiceImpl implements LongTextDialoguesService {
sseUtil.send(JsonUtils.serialize(result));
}
String longTextDialoguesModel = fmxParamConfigService.getParam("longtext.dialogues_model");
String longTextDialoguesModel = enableDeepThinking ? fmxParamConfigService.getParam("longtext.dialogues_model_think")
: fmxParamConfigService.getParam("longtext.dialogues_model");
// 插件调用
ToolFunction toolFunction = getToolFunction(dialoguesId, input, enableSearchEngine, sseUtil);
//组装请求参数
List<Message> messages = buildMessages(dialoguesId, userId, input, fileUrl, knowledgeResult);
List<Message> messages = buildMessages(dialoguesId, userId, input, fileUrl, knowledgeResult, toolFunction);
LargeModelResponse largeModelResponse = new LargeModelResponse();
largeModelResponse.setModel(longTextDialoguesModel);
largeModelResponse.setMessages(messages.toArray(new Message[0]));
......@@ -292,7 +309,7 @@ public class LongTextDialoguesServiceImpl implements LongTextDialoguesService {
}
private List<Message> buildMessages(String dialogsId, Long userId, String input, String fileUrl, List<KnowledgeContentResult> knowledgeContentResults) throws Exception {
private List<Message> buildMessages(String dialogsId, Long userId, String input, String fileUrl, List<KnowledgeContentResult> knowledgeContentResults, ToolFunction toolFunction) throws Exception {
// 获取对话提示词
String promptCode = "DocumentDialoguePrompt";
BizAgentApplicationGcConfigEntity documentDialoguePrompt = bizAgentApplicationGcConfigService.getByConfigCode(promptCode);
......@@ -315,6 +332,10 @@ public class LongTextDialoguesServiceImpl implements LongTextDialoguesService {
prompt = prompt.replace("${knowledgeContent}", StringUtils.EMPTY);
}
if (toolFunction != null) {
prompt = prompt.replace("${toolFunction}", JsonUtils.serialize(toolFunction));
}
// 配置message
List<Message> messages = new ArrayList<>();
Message systemMessage = new Message();
......@@ -577,4 +598,44 @@ public class LongTextDialoguesServiceImpl implements LongTextDialoguesService {
throw new BusinessException("获取核心观点失败");
}
}
private ToolFunction getToolFunction(String dialoguesId, String input, Boolean enableSearchEngine, SSEUtil sseUtil) throws IOException {
ToolFunction toolFunction = null;
if (enableSearchEngine) {
List<Message> messages = new ArrayList<>();
Message message = new Message();
message.setRole("user");
message.setContent(input);
messages.add(message);
String[] unitIds = new String[1];
unitIds[0] = "web_search";
List<Tool> tools = AgentApplicationTools.buildFunctionConfig(null, "N", dialoguesId, dialoguesId, unitIds, "N");
CheckPluginUseEntity checkPluginUseEntity = AgentApplicationTools.checkPluginUse(messages, tools, null);
FunctionCallResult functionCallResult = checkPluginUseEntity.getFunctionCallResult();
if (functionCallResult != null) {
FunctionCall functionCall = functionCallResult.getFunctionCall();
if ("web_search".equals(functionCall.getName())) {
AbstractFunctionResult<List<WebSearchFunctionResult>> functionResult = webSearchFunction.doFunction(functionCall.getArguments(), dialoguesId, null, null);
if (functionResult != null && CollectionUtils.isNotEmpty(functionResult.getFunctionResult())) {
List<WebSearchFunctionResult> webSearchFunctionResults = functionResult.getFunctionResult();
if (CollectionUtils.isNotEmpty(webSearchFunctionResults)) {
toolFunction = new ToolFunction();
toolFunction.setResult(JsonUtils.serialize(webSearchFunctionResults));
toolFunction.setName("web_search");
toolFunction.setArguments(functionCall.getArguments());
toolFunction.setDisplayFormat("json");
LargeModelDemandResult result = new LargeModelDemandResult();
result.setCode("0");
result.setFunction(toolFunction);
result.setDbChainResult(null);
result.setKnowledgeContentResult(null);
sseUtil.send(JsonUtils.serialize(result));
}
}
}
}
}
return toolFunction;
}
}
package cn.com.poc.long_document.dto;
import java.util.List;
/**
* @author alex.yao
* @date 2025/6/3
......@@ -28,6 +26,33 @@ public class LongTextDialoguesCallDto {
*/
private Integer[] knowledgeIds;
/**
* 是否启用搜索引擎
*/
private Boolean enableSearchEngine;
/**
* 是否开启深度思考
*/
private Boolean enableDeepThinking;
public Boolean getEnableSearchEngine() {
return enableSearchEngine;
}
public void setEnableSearchEngine(Boolean enableSearchEngine) {
this.enableSearchEngine = enableSearchEngine;
}
public Boolean getEnableDeepThinking() {
return enableDeepThinking;
}
public void setEnableDeepThinking(Boolean enableDeepThinking) {
this.enableDeepThinking = enableDeepThinking;
}
public String getDialoguesId() {
return dialoguesId;
}
......
......@@ -40,7 +40,7 @@ public class LongTextDialoguesRestImpl implements LongTextDialoguesRest {
dto.getFileUrl(),
dto.getInput(),
dto.getKnowledgeIds(),
userBaseEntity.getUserId());
userBaseEntity.getUserId(), dto.getEnableSearchEngine(), dto.getEnableDeepThinking());
}
@Override
......
......@@ -22,8 +22,10 @@ public interface AiWritingService {
* @param input
* @param knowledgeIds
* @param userId
* @param enableSearchEngine
* @param enableDeepThinking
*/
void call(String dialoguesId, String fileUrl, String input, Integer[] knowledgeIds, Long userId) throws Exception;
void call(String dialoguesId, String fileUrl, String input, Integer[] knowledgeIds, Long userId, Boolean enableSearchEngine, Boolean enableDeepThinking) throws Exception;
/**
......
package cn.com.poc.writing.aggregate.impl;
import cn.com.poc.agent_application.entity.CheckPluginUseEntity;
import cn.com.poc.agent_application.utils.AgentApplicationTools;
import cn.com.poc.expose.dto.DialoguesContextDto;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.FunctionCall;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Tool;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.ToolFunction;
import cn.com.poc.thirdparty.resource.demand.ai.entity.function.FunctionCallResult;
import cn.com.poc.thirdparty.resource.demand.ai.function.AbstractFunctionResult;
import cn.com.poc.thirdparty.resource.demand.ai.function.web_seach.WebSearchFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.web_seach.WebSearchFunctionResult;
import cn.com.poc.writing.dto.AiWritingDialoguesContextDto;
import cn.com.poc.writing.entity.BizAiWritingDialoguesRecordEntity;
import cn.com.poc.writing.query.AiWritingDialoguesRecordQueryItem;
......@@ -42,6 +51,7 @@ import cn.com.poc.writing.service.BizAiWritingExampleTypeService;
import cn.com.yict.framemax.core.exception.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
......@@ -92,9 +102,12 @@ public class AiWritingServiceImpl implements AiWritingService {
@Resource
private BizAiWritingExampleTypeService bizAiWritingExampleTypeService;
@Resource
private WebSearchFunction webSearchFunction;
@Override
public void call(String dialoguesId, String fileUrl, String input, Integer[] knowledgeIds, Long userId) throws Exception {
public void call(String dialoguesId, String fileUrl, String input, Integer[] knowledgeIds, Long userId, Boolean enableSearchEngine, Boolean enableDeepThinking) throws Exception {
BizAiDialoguesEntity bizAiDialoguesEntity = new BizAiDialoguesEntity();
bizAiDialoguesEntity.setDialoguesId(dialoguesId);
bizAiDialoguesEntity.setMemberId(userId);
......@@ -168,10 +181,14 @@ public class AiWritingServiceImpl implements AiWritingService {
sseUtil.send(JsonUtils.serialize(aiWritingTitleGenerationDto));
}
// 插件调用
ToolFunction toolFunction = getToolFunction(dialoguesId, input, enableSearchEngine, sseUtil);
//组装请求参数
List<Message> messages = buildMessages(dialoguesId, userId, input, fileUrl, knowledgeResult, aiWritingTitleGenerationDto);
List<Message> messages = buildMessages(dialoguesId, userId, input, fileUrl, knowledgeResult, aiWritingTitleGenerationDto, toolFunction);
LargeModelResponse largeModelResponse = new LargeModelResponse();
largeModelResponse.setModel("deepseek-chat");
largeModelResponse.setModel(enableDeepThinking ? "deepseek-chat" : "deepseek-v3");
largeModelResponse.setMessages(messages.toArray(new Message[0]));
largeModelResponse.setStream(true);
BufferedReader bufferedReader = llmService.chatChunk(largeModelResponse);
......@@ -205,6 +222,46 @@ public class AiWritingServiceImpl implements AiWritingService {
bizAiWritingDialoguesRecordService.save(assistantRecord);
}
private ToolFunction getToolFunction(String dialoguesId, String input, Boolean enableSearchEngine, SSEUtil sseUtil) throws IOException {
ToolFunction toolFunction = null;
if (enableSearchEngine) {
List<Message> messages = new ArrayList<>();
Message message = new Message();
message.setRole("user");
message.setContent(input);
messages.add(message);
String[] unitIds = new String[1];
unitIds[0] = "web_search";
List<Tool> tools = AgentApplicationTools.buildFunctionConfig(null, "N", dialoguesId, dialoguesId, unitIds, "N");
CheckPluginUseEntity checkPluginUseEntity = AgentApplicationTools.checkPluginUse(messages, tools, null);
FunctionCallResult functionCallResult = checkPluginUseEntity.getFunctionCallResult();
if (functionCallResult != null) {
FunctionCall functionCall = functionCallResult.getFunctionCall();
if ("web_search".equals(functionCall.getName())) {
AbstractFunctionResult<List<WebSearchFunctionResult>> functionResult = webSearchFunction.doFunction(functionCall.getArguments(), dialoguesId, null, null);
if (functionResult != null && CollectionUtils.isNotEmpty(functionResult.getFunctionResult())) {
List<WebSearchFunctionResult> webSearchFunctionResults = functionResult.getFunctionResult();
if (CollectionUtils.isNotEmpty(webSearchFunctionResults)) {
toolFunction = new ToolFunction();
toolFunction.setResult(JsonUtils.serialize(webSearchFunctionResults));
toolFunction.setName("web_search");
toolFunction.setArguments(functionCall.getArguments());
toolFunction.setDisplayFormat("json");
LargeModelDemandResult result = new LargeModelDemandResult();
result.setCode("0");
result.setFunction(toolFunction);
result.setDbChainResult(null);
result.setKnowledgeContentResult(null);
sseUtil.send(JsonUtils.serialize(result));
}
}
}
}
}
return toolFunction;
}
@Override
public List<AiWritingExampleDto> example(String type) throws Exception {
......@@ -295,7 +352,7 @@ public class AiWritingServiceImpl implements AiWritingService {
return aiWritingTitleGenerationDto;
}
private List<Message> buildMessages(String dialogsId, Long userId, String input, String fileUrl, List<KnowledgeContentResult> knowledgeContentResults, AiWritingTitleGenerationDto aiWritingTitleGenerationDto) throws Exception {
private List<Message> buildMessages(String dialogsId, Long userId, String input, String fileUrl, List<KnowledgeContentResult> knowledgeContentResults, AiWritingTitleGenerationDto aiWritingTitleGenerationDto, ToolFunction toolFunction) throws Exception {
// 获取对话提示词
String promptCode = "AiWritingPrompt";
BizAgentApplicationGcConfigEntity documentDialoguePrompt = bizAgentApplicationGcConfigService.getByConfigCode(promptCode);
......@@ -325,6 +382,9 @@ public class AiWritingServiceImpl implements AiWritingService {
} else {
prompt = prompt.replace("${title}", StringUtils.EMPTY);
}
if (toolFunction != null) {
prompt = prompt.replace("${toolFunction}", JsonUtils.serialize(toolFunction));
}
prompt = prompt.replace("${fileContent}", fileContent);
......
......@@ -26,6 +26,34 @@ public class AiWritingDto {
*/
private Integer[] knowledgeIds;
/**
* 是否启用搜索引擎
*/
private Boolean enableSearchEngine;
/**
* 是否开启深度思考
*/
private Boolean enableDeepThinking;
public Boolean getEnableSearchEngine() {
return enableSearchEngine;
}
public void setEnableSearchEngine(Boolean enableSearchEngine) {
this.enableSearchEngine = enableSearchEngine;
}
public Boolean getEnableDeepThinking() {
return enableDeepThinking;
}
public void setEnableDeepThinking(Boolean enableDeepThinking) {
this.enableDeepThinking = enableDeepThinking;
}
public String getDialoguesId() {
return dialoguesId;
}
......
......@@ -33,7 +33,8 @@ public class AiWritingRestImpl implements AiWritingRest {
throw new BusinessException("用户未登录");
}
aiWritingService.call(aiWritingDto.getDialoguesId(), aiWritingDto.getFileUrl(),
aiWritingDto.getInput(), aiWritingDto.getKnowledgeIds(), userBaseEntity.getUserId());
aiWritingDto.getInput(), aiWritingDto.getKnowledgeIds(), userBaseEntity.getUserId(),
aiWritingDto.getEnableSearchEngine(), aiWritingDto.getEnableDeepThinking());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment