Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
poc-api
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
poc
poc-api
Commits
cab016c0
Commit
cab016c0
authored
Jul 30, 2025
by
R10
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
法律文书ai写作
parent
2908e0d9
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
312 additions
and
89 deletions
+312
-89
AiLawDialoguesDto.java
src/main/java/cn/com/poc/law/dto/AiLawDialoguesDto.java
+26
-0
LegalRest.java
src/main/java/cn/com/poc/law/rest/LegalRest.java
+2
-1
LegalRestImpl.java
src/main/java/cn/com/poc/law/rest/impl/LegalRestImpl.java
+5
-3
AiLawService.java
src/main/java/cn/com/poc/law/service/AiLawService.java
+3
-1
AiLawServiceImpl.java
...in/java/cn/com/poc/law/service/impl/AiLawServiceImpl.java
+276
-84
No files found.
src/main/java/cn/com/poc/law/dto/AiLawDialoguesDto.java
View file @
cab016c0
...
@@ -33,6 +33,16 @@ public class AiLawDialoguesDto {
...
@@ -33,6 +33,16 @@ public class AiLawDialoguesDto {
*/
*/
private
Integer
[]
databaseIds
;
private
Integer
[]
databaseIds
;
/**
* 是否开启联网检索
*/
private
boolean
enableSearchEngine
;
/**
* 是否开启思考模式
*/
private
boolean
enableDeepThinking
;
public
String
getDialoguesId
()
{
public
String
getDialoguesId
()
{
return
dialoguesId
;
return
dialoguesId
;
}
}
...
@@ -72,4 +82,20 @@ public class AiLawDialoguesDto {
...
@@ -72,4 +82,20 @@ public class AiLawDialoguesDto {
public
void
setDatabaseIds
(
Integer
[]
databaseIds
)
{
public
void
setDatabaseIds
(
Integer
[]
databaseIds
)
{
this
.
databaseIds
=
databaseIds
;
this
.
databaseIds
=
databaseIds
;
}
}
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
;
}
}
}
src/main/java/cn/com/poc/law/rest/LegalRest.java
View file @
cab016c0
package
cn
.
com
.
poc
.
law
.
rest
;
package
cn
.
com
.
poc
.
law
.
rest
;
import
cn.com.poc.ai_finance.dto.AiFinanceDialoguesDto
;
import
cn.com.poc.ai_finance.dto.AiFinanceDialoguesDto
;
import
cn.com.poc.law.dto.AiLawDialoguesDto
;
import
cn.com.poc.law.dto.LegalFileDto
;
import
cn.com.poc.law.dto.LegalFileDto
;
import
cn.com.yict.framemax.core.rest.BaseRest
;
import
cn.com.yict.framemax.core.rest.BaseRest
;
import
cn.com.yict.framemax.web.permission.Access
;
import
cn.com.yict.framemax.web.permission.Access
;
...
@@ -25,7 +26,7 @@ public interface LegalRest extends BaseRest {
...
@@ -25,7 +26,7 @@ public interface LegalRest extends BaseRest {
/**
/**
* ai对话
* ai对话
*/
*/
void
call
(
@RequestBody
Ai
Finance
DialoguesDto
dto
)
throws
Exception
;
void
call
(
@RequestBody
Ai
Law
DialoguesDto
dto
)
throws
Exception
;
}
}
src/main/java/cn/com/poc/law/rest/impl/LegalRestImpl.java
View file @
cab016c0
...
@@ -4,8 +4,10 @@ import cn.com.poc.ai_finance.aggregate.AiFinanceService;
...
@@ -4,8 +4,10 @@ import cn.com.poc.ai_finance.aggregate.AiFinanceService;
import
cn.com.poc.ai_finance.dto.AiFinanceDialoguesDto
;
import
cn.com.poc.ai_finance.dto.AiFinanceDialoguesDto
;
import
cn.com.poc.common.utils.Assert
;
import
cn.com.poc.common.utils.Assert
;
import
cn.com.poc.common.utils.BlContext
;
import
cn.com.poc.common.utils.BlContext
;
import
cn.com.poc.law.dto.AiLawDialoguesDto
;
import
cn.com.poc.law.dto.LegalFileDto
;
import
cn.com.poc.law.dto.LegalFileDto
;
import
cn.com.poc.law.rest.LegalRest
;
import
cn.com.poc.law.rest.LegalRest
;
import
cn.com.poc.law.service.AiLawService
;
import
cn.com.poc.support.security.oauth.entity.UserBaseEntity
;
import
cn.com.poc.support.security.oauth.entity.UserBaseEntity
;
import
cn.com.poc.thirdparty.resource.demand.ai.aggregate.LegalService
;
import
cn.com.poc.thirdparty.resource.demand.ai.aggregate.LegalService
;
import
cn.com.poc.thirdparty.resource.demand.ai.entity.legal.LegalFileResponse
;
import
cn.com.poc.thirdparty.resource.demand.ai.entity.legal.LegalFileResponse
;
...
@@ -43,10 +45,10 @@ public class LegalRestImpl implements LegalRest {
...
@@ -43,10 +45,10 @@ public class LegalRestImpl implements LegalRest {
}
}
@Resource
@Resource
private
Ai
FinanceService
aiFinance
Service
;
private
Ai
LawService
aiLaw
Service
;
@Override
@Override
public
void
call
(
Ai
Finance
DialoguesDto
dto
)
throws
Exception
{
public
void
call
(
Ai
Law
DialoguesDto
dto
)
throws
Exception
{
cn
.
hutool
.
core
.
lang
.
Assert
.
notNull
(
dto
.
getDialoguesId
(),
"对话id不能为空"
);
cn
.
hutool
.
core
.
lang
.
Assert
.
notNull
(
dto
.
getDialoguesId
(),
"对话id不能为空"
);
cn
.
hutool
.
core
.
lang
.
Assert
.
notNull
(
dto
.
getInput
(),
"问题不能为空"
);
cn
.
hutool
.
core
.
lang
.
Assert
.
notNull
(
dto
.
getInput
(),
"问题不能为空"
);
UserBaseEntity
userBaseEntity
=
BlContext
.
getCurrentUserNotException
();
UserBaseEntity
userBaseEntity
=
BlContext
.
getCurrentUserNotException
();
...
@@ -54,6 +56,6 @@ public class LegalRestImpl implements LegalRest {
...
@@ -54,6 +56,6 @@ public class LegalRestImpl implements LegalRest {
throw
new
BusinessException
(
"用户未登录"
);
throw
new
BusinessException
(
"用户未登录"
);
}
}
ai
FinanceService
.
call
(
dto
.
getDialoguesId
(),
dto
.
getInput
(),
dto
.
getFileUrl
(),
dto
.
getKnowledgeIds
(),
dto
.
getDatabaseIds
(),
userBaseEntity
.
getUserId
());
ai
LawService
.
call
(
dto
.
getDialoguesId
(),
dto
.
getInput
(),
dto
.
getEnableSearchEngine
(),
dto
.
getEnableDeepThinking
(),
dto
.
getFileUrl
(),
dto
.
getKnowledgeIds
(),
dto
.
getDatabaseIds
(),
userBaseEntity
.
getUserId
());
}
}
}
}
src/main/java/cn/com/poc/law/service/AiLawService.java
View file @
cab016c0
...
@@ -14,10 +14,12 @@ public interface AiLawService {
...
@@ -14,10 +14,12 @@ public interface AiLawService {
* @param dialoguesId 对话id
* @param dialoguesId 对话id
* @param input 问题
* @param input 问题
* @param fileUrl 文件地址
* @param fileUrl 文件地址
* @param enableSearchEngine 是否开启联网检索
* @param enableDeepThinking 是否开启思考模式
* @param knowledgeIds 知识库ids
* @param knowledgeIds 知识库ids
* @param databaseIds 数据库ids
* @param databaseIds 数据库ids
* @param userId 用户id
* @param userId 用户id
*/
*/
void
call
(
String
dialoguesId
,
String
input
,
String
fileUrl
,
Integer
[]
knowledgeIds
,
Integer
[]
databaseIds
,
Long
userId
)
throws
Exception
;
void
call
(
String
dialoguesId
,
String
input
,
boolean
enableSearchEngine
,
boolean
enableDeepThinking
,
String
fileUrl
,
Integer
[]
knowledgeIds
,
Integer
[]
databaseIds
,
Long
userId
)
throws
Exception
;
}
}
src/main/java/cn/com/poc/law/service/impl/AiLawServiceImpl.java
View file @
cab016c0
...
@@ -4,22 +4,39 @@ import cn.com.poc.agent_application.aggregate.AgentApplicationService;
...
@@ -4,22 +4,39 @@ import cn.com.poc.agent_application.aggregate.AgentApplicationService;
import
cn.com.poc.agent_application.constant.AgentApplicationDialoguesRecordConstants
;
import
cn.com.poc.agent_application.constant.AgentApplicationDialoguesRecordConstants
;
import
cn.com.poc.agent_application.entity.*
;
import
cn.com.poc.agent_application.entity.*
;
import
cn.com.poc.agent_application.service.BizAgentApplicationDialoguesRecordService
;
import
cn.com.poc.agent_application.service.BizAgentApplicationDialoguesRecordService
;
import
cn.com.poc.agent_application.service.BizAgentApplicationGcConfigService
;
import
cn.com.poc.agent_application.service.BizAgentApplicationPublishService
;
import
cn.com.poc.agent_application.service.BizAgentApplicationPublishService
;
import
cn.com.poc.agent_application.utils.AgentApplicationTools
;
import
cn.com.poc.agent_application.utils.AgentApplicationTools
;
import
cn.com.poc.ai_dialogues.entity.BizAiDialoguesEntity
;
import
cn.com.poc.ai_dialogues.entity.BizAiDialoguesEntity
;
import
cn.com.poc.ai_dialogues.service.BizAiDialoguesService
;
import
cn.com.poc.ai_dialogues.service.BizAiDialoguesService
;
import
cn.com.poc.common.constant.CommonConstant
;
import
cn.com.poc.common.constant.CommonConstant
;
import
cn.com.poc.common.utils.DocumentLoad
;
import
cn.com.poc.common.utils.JsonUtils
;
import
cn.com.poc.common.utils.JsonUtils
;
import
cn.com.poc.common.utils.S
tringUtils
;
import
cn.com.poc.common.utils.S
SEUtil
;
import
cn.com.poc.knowledge.aggregate.KnowledgeService
;
import
cn.com.poc.knowledge.aggregate.KnowledgeService
;
import
cn.com.poc.knowledge.constant.KnowledgeConstant
;
import
cn.com.poc.knowledge.entity.BizKnowledgeDocumentEntity
;
import
cn.com.poc.knowledge.query.KnowledgeDocumentRelationQueryItem
;
import
cn.com.poc.law.service.AiLawService
;
import
cn.com.poc.law.service.AiLawService
;
import
cn.com.poc.long_document.domain.LongtextDialoguesResult
;
import
cn.com.poc.thirdparty.resource.demand.ai.constants.KnowledgeSearchTypeEnum
;
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.Message
;
import
cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Tool
;
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.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.core.exception.BusinessException
;
import
cn.com.yict.framemax.frame.service.FmxParamConfigService
;
import
cn.com.yict.framemax.frame.service.FmxParamConfigService
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
...
@@ -28,8 +45,12 @@ import org.springframework.web.context.request.ServletRequestAttributes;
...
@@ -28,8 +45,12 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.BufferedReader
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
java.util.stream.Collectors
;
/**
/**
* @author alex.yao
* @author alex.yao
...
@@ -56,112 +77,283 @@ public class AiLawServiceImpl implements AiLawService {
...
@@ -56,112 +77,283 @@ public class AiLawServiceImpl implements AiLawService {
private
KnowledgeService
knowledgeService
;
private
KnowledgeService
knowledgeService
;
@Resource
@Resource
private
BizAgentApplicationPublishService
bizAgentApplicationPublish
Service
;
private
LLMService
llm
Service
;
@Resource
@Resource
private
BizAgentApplicationDialoguesRecordService
bizAgentApplicationDialoguesRecordService
;
private
BizAgentApplicationDialoguesRecordService
bizAgentApplicationDialoguesRecordService
;
@Resource
private
WebSearchFunction
webSearchFunction
;
@Override
@Resource
public
void
call
(
String
dialoguesId
,
String
input
,
String
fileUrl
,
Integer
[]
knowledgeIds
,
Integer
[]
databaseIds
,
Long
userId
)
throws
Exception
{
private
BizAgentApplicationGcConfigService
bizAgentApplicationGcConfigService
;
String
agentId
=
fmxParamConfigService
.
getParam
(
FMX_PARAM_AGENT_ID_CODE
);
if
(
StringUtils
.
isBlank
(
agentId
))
{
logger
.
error
(
"获取AI财务对话agentId失败"
);
throw
new
BusinessException
(
"对话异常"
);
}
// 获取Agent基本信息
BizAgentApplicationPublishEntity
infoEntity
=
bizAgentApplicationPublishService
.
getByAgentId
(
agentId
);
if
(
infoEntity
==
null
)
{
logger
.
error
(
"获取AI财务对话agent信息失败"
);
throw
new
BusinessException
(
"对话异常"
);
}
// 保存对话基础信息
@Override
public
void
call
(
String
dialoguesId
,
String
input
,
boolean
enableSearchEngine
,
boolean
enableDeepThinking
,
String
fileUrl
,
Integer
[]
knowledgeIds
,
Integer
[]
databaseIds
,
Long
userId
)
throws
Exception
{
BizAiDialoguesEntity
bizAiDialoguesEntity
=
new
BizAiDialoguesEntity
();
BizAiDialoguesEntity
bizAiDialoguesEntity
=
new
BizAiDialoguesEntity
();
bizAiDialoguesEntity
.
setMemberId
(
userId
);
bizAiDialoguesEntity
.
setDialoguesId
(
dialoguesId
);
bizAiDialoguesEntity
.
setDialoguesId
(
dialoguesId
);
bizAiDialoguesEntity
.
setMemberId
(
userId
);
bizAiDialoguesEntity
.
setIsDeleted
(
CommonConstant
.
IsDeleted
.
N
);
bizAiDialoguesEntity
.
setIsDeleted
(
CommonConstant
.
IsDeleted
.
N
);
List
<
BizAiDialoguesEntity
>
bizAiDialoguesEntities
=
bizAiDialoguesService
.
findByExample
(
bizAiDialoguesEntity
,
null
);
List
<
BizAiDialoguesEntity
>
bizAiDialoguesEntities
=
bizAiDialoguesService
.
findByExample
(
bizAiDialoguesEntity
,
null
);
if
(
CollectionUtils
.
isEmpty
(
bizAiDialoguesEntities
))
{
if
(
CollectionUtils
.
isEmpty
(
bizAiDialoguesEntities
))
{
logger
.
error
(
"dialogues id 不存在,请重新创建"
);
throw
new
BusinessException
(
"当前对话不存在"
);
throw
new
BusinessException
(
"对话异常"
);
}
}
BizAiDialoguesEntity
aiDialoguesEntity
=
bizAiDialoguesEntities
.
get
(
0
);
// 保存标题信息
if
(
StringUtils
.
isBlank
(
aiDialoguesEntity
.
getTitle
()))
{
if
(
StringUtils
.
isBlank
(
bizAiDialoguesEntities
.
get
(
0
).
getTitle
()))
{
aiDialoguesEntity
.
setTitle
(
input
);
BizAiDialoguesEntity
portalDialoguesEntity
=
bizAiDialoguesEntities
.
get
(
0
);
bizAiDialoguesService
.
update
(
aiDialoguesEntity
);
portalDialoguesEntity
.
setTitle
(
input
);
bizAiDialoguesService
.
update
(
portalDialoguesEntity
);
}
}
// 获取对话配置
//记录提问时间
String
largeModel
=
infoEntity
.
getLargeModel
();
//获取模型
long
inputTimestamp
=
System
.
currentTimeMillis
();
String
agentSystem
=
infoEntity
.
getAgentSystem
();
//获取Agent角色词
SSEUtil
sseUtil
=
new
SSEUtil
();
List
<
Integer
>
kdIds
=
knowledgeService
.
getKdIdsByKnowledgeInfoIds
(
knowledgeIds
);
//获取知识库ids
// 知识库
List
<
Message
>
messages
=
AgentApplicationTools
.
buildMessage
(
dialoguesId
,
agentId
,
userId
,
input
);
//构建消息
//获取知识库配置
List
<
Tool
>
tools
=
AgentApplicationTools
.
buildFunctionConfig
(
infoEntity
.
getVariableStructure
(),
infoEntity
.
getIsLongMemory
(),
dialoguesId
,
agentId
,
infoEntity
.
getUnitIds
(),
infoEntity
.
getIsDocumentParsing
());
//构建函数配置
// List<Integer> kdIds = knowledgeService.getKdIdsByKnowledgeInfoIds(knowledgeIds);
// List<KnowledgeContentResult> knowledgeResult = knowledge(kdIds, input, 5, 0.5, KnowledgeSearchTypeEnum.MIX);
List
<
String
>
fileUrls
=
new
ArrayList
<>();
//构建文件链接
// if (CollectionUtils.isNotEmpty(knowledgeResult)) {
if
(
StringUtils
.
isNotBlank
(
fileUrl
))
{
// LargeModelDemandResult result = new LargeModelDemandResult();
fileUrls
.
add
(
fileUrl
);
// result.setCode("0");
// result.setFunction(null);
// result.setDbChainResult(null);
// result.setKnowledgeContentResult(knowledgeResult);
// sseUtil.send(JsonUtils.serialize(result));
// }
// 插件调用
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
));
}
}
}
}
}
}
CheckPluginUseEntity
checkPluginUseEntity
=
AgentApplicationTools
.
checkPluginUse
(
messages
,
tools
,
fileUrls
);
//检查插件使用
FunctionCallResult
functionCallResult
=
checkPluginUseEntity
.
getFunctionCallResult
();
//函数调用结果
//组装请求参数
boolean
stream
=
true
;
//是否流式输出
List
<
Message
>
messages
=
buildMessages
(
dialoguesId
,
userId
,
input
,
fileUrl
,
null
,
toolFunction
);
KnowledgeSearchTypeEnum
knowledgeSearchTypeEnum
=
KnowledgeSearchTypeEnum
.
valueOf
(
infoEntity
.
getKnowledgeSearchType
());
//知识库检索方式
LargeModelResponse
largeModelResponse
=
new
LargeModelResponse
();
KnowledgeSuperclassProblemConfig
knowledgeSuperclassProblemConfig
=
new
KnowledgeSuperclassProblemConfig
();
//知识库超纲配置
largeModelResponse
.
setModel
(
enableDeepThinking
?
"deepseek-chat"
:
"deepseek-v3"
);
//获取模型);
knowledgeSuperclassProblemConfig
.
setKnowledgeResponseType
(
infoEntity
.
getKnowledgeResponseType
());
//知识库回复类型
largeModelResponse
.
setMessages
(
messages
.
toArray
(
new
Message
[
0
]));
knowledgeSuperclassProblemConfig
.
setKnowledgeCustomResponse
(
infoEntity
.
getKnowledgeCustomResponse
());
//知识库自定义回复
largeModelResponse
.
setStream
(
true
);
BufferedReader
bufferedReader
=
llmService
.
chatChunk
(
largeModelResponse
);
// 记录用户输入时间戳
LongtextDialoguesResult
longtextDialoguesResult
=
textOutputStream
(
sseUtil
,
bufferedReader
);
Long
inputTimestamp
=
System
.
currentTimeMillis
();
// 调用对话Agent
// 保存用户输入记录
ServletRequestAttributes
servletRequestAttributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletResponse
response
=
servletRequestAttributes
.
getResponse
();
AgentResultEntity
agentResultEntity
=
agentApplicationService
.
callAgentApplication
(
agentId
,
dialoguesId
,
largeModel
,
agentSystem
,
kdIds
.
toArray
(
new
Integer
[
0
]),
databaseIds
,
infoEntity
.
getCommunicationTurn
(),
infoEntity
.
getTopP
(),
infoEntity
.
getTemperature
(),
messages
,
tools
,
functionCallResult
,
stream
,
infoEntity
.
getKnowledgeSimilarity
(),
infoEntity
.
getKnowledgeNResult
(),
knowledgeSearchTypeEnum
,
knowledgeSuperclassProblemConfig
,
response
);
//保存对话记录
//用户输入记录
BizAgentApplicationDialoguesRecordEntity
inputRecord
=
new
BizAgentApplicationDialoguesRecordEntity
();
BizAgentApplicationDialoguesRecordEntity
inputRecord
=
new
BizAgentApplicationDialoguesRecordEntity
();
inputRecord
.
setAgentId
(
agentId
);
inputRecord
.
setMemberId
(
userId
);
inputRecord
.
setMemberId
(
userId
);
inputRecord
.
setContent
(
input
);
inputRecord
.
setContent
(
input
);
inputRecord
.
setDialogsId
(
dialoguesId
);
inputRecord
.
setDialogsId
(
dialoguesId
);
inputRecord
.
setRole
(
AgentApplicationDialoguesRecordConstants
.
ROLE
.
USER
);
inputRecord
.
setRole
(
LLMRoleEnum
.
USER
.
getRole
()
);
inputRecord
.
setTimestamp
(
inputTimestamp
);
inputRecord
.
setTimestamp
(
inputTimestamp
);
// 保存AI回复记录
BizAgentApplicationDialoguesRecordEntity
assistantRecord
=
new
BizAgentApplicationDialoguesRecordEntity
();
assistantRecord
.
setMemberId
(
userId
);
assistantRecord
.
setContent
(
longtextDialoguesResult
.
getMessage
());
assistantRecord
.
setReasoningContent
(
longtextDialoguesResult
.
getReasoningContent
());
assistantRecord
.
setDialogsId
(
dialoguesId
);
assistantRecord
.
setRole
(
LLMRoleEnum
.
ASSISTANT
.
getRole
());
assistantRecord
.
setTimestamp
(
System
.
currentTimeMillis
());
bizAgentApplicationDialoguesRecordService
.
save
(
inputRecord
);
bizAgentApplicationDialoguesRecordService
.
save
(
inputRecord
);
//AI输出记录
bizAgentApplicationDialoguesRecordService
.
save
(
assistantRecord
);
BizAgentApplicationDialoguesRecordEntity
outputRecord
=
new
BizAgentApplicationDialoguesRecordEntity
();
}
outputRecord
.
setRole
(
AgentApplicationDialoguesRecordConstants
.
ROLE
.
ASSISTANT
);
outputRecord
.
setAgentId
(
infoEntity
.
getAgentId
());
private
List
<
Message
>
buildMessages
(
String
dialogsId
,
Long
userId
,
String
input
,
String
fileUrl
,
List
<
KnowledgeContentResult
>
knowledgeContentResults
,
ToolFunction
toolFunction
)
throws
Exception
{
outputRecord
.
setDialogsId
(
dialoguesId
);
// 获取对话提示词
outputRecord
.
setMemberId
(
userId
);
String
promptCode
=
"AiPortalPrompt"
;
if
(
agentResultEntity
.
getFunction
()
!=
null
)
{
BizAgentApplicationGcConfigEntity
documentDialoguePrompt
=
bizAgentApplicationGcConfigService
.
getByConfigCode
(
promptCode
);
outputRecord
.
setFunction
(
JsonUtils
.
serialize
(
agentResultEntity
.
getFunction
()));
if
(
documentDialoguePrompt
==
null
||
StringUtils
.
isBlank
(
documentDialoguePrompt
.
getConfigSystem
()))
{
logger
.
error
(
"获取对话提示词失败 , configCode:{}"
,
promptCode
);
throw
new
BusinessException
(
"获取对话提示词失败"
);
}
// 加载文件
String
fileContent
=
StringUtils
.
EMPTY
;
if
(
StringUtils
.
isNoneBlank
(
fileUrl
))
{
File
file
=
DocumentLoad
.
downloadURLDocument
(
fileUrl
);
fileContent
=
DocumentLoad
.
documentToText
(
file
);
}
// 加载对话提示词
String
prompt
=
documentDialoguePrompt
.
getConfigSystem
();
if
(
CollectionUtils
.
isNotEmpty
(
knowledgeContentResults
))
{
String
knowledgeContent
=
JsonUtils
.
serialize
(
knowledgeContentResults
);
prompt
=
prompt
.
replace
(
"${knowledgeContent}"
,
knowledgeContent
);
}
else
{
prompt
=
prompt
.
replace
(
"${knowledgeContent}"
,
StringUtils
.
EMPTY
);
}
if
(
toolFunction
!=
null
)
{
prompt
=
prompt
.
replace
(
"${toolFunction}"
,
JsonUtils
.
serialize
(
toolFunction
));
}
prompt
=
prompt
.
replace
(
"${question}"
,
input
).
replace
(
"${fileContent}"
,
fileContent
);
// 配置message
List
<
Message
>
messages
=
new
ArrayList
<>();
Message
systemMessage
=
new
Message
();
systemMessage
.
setContent
(
prompt
);
systemMessage
.
setRole
(
LLMRoleEnum
.
SYSTEM
.
getRole
());
messages
.
add
(
systemMessage
);
// 对话记录
BizAgentApplicationDialoguesRecordEntity
recordEntity
=
new
BizAgentApplicationDialoguesRecordEntity
();
recordEntity
.
setDialogsId
(
dialogsId
);
recordEntity
.
setIsDeleted
(
CommonConstant
.
IsDeleted
.
N
);
recordEntity
.
setMemberId
(
userId
);
List
<
BizAgentApplicationDialoguesRecordEntity
>
recordEntities
=
bizAgentApplicationDialoguesRecordService
.
findByExample
(
recordEntity
,
null
);
if
(
CollectionUtils
.
isNotEmpty
(
recordEntities
))
{
for
(
BizAgentApplicationDialoguesRecordEntity
entity
:
recordEntities
)
{
Message
message
=
new
Message
();
message
.
setContent
(
entity
.
getContent
());
message
.
setRole
(
entity
.
getRole
());
messages
.
add
(
message
);
}
}
//判断最后是否为User,若是则删除。
if
(
CollectionUtils
.
isNotEmpty
(
messages
))
{
if
(
LLMRoleEnum
.
USER
.
getRole
().
equals
(
messages
.
get
(
messages
.
size
()
-
1
).
getRole
()))
{
Long
recordId
=
recordEntities
.
get
(
recordEntities
.
size
()
-
1
).
getId
();
bizAgentApplicationDialoguesRecordService
.
deletedById
(
recordId
);
messages
.
remove
(
messages
.
size
()
-
1
);
}
}
// 用户输入
Message
message
=
new
Message
();
message
.
setContent
(
input
);
message
.
setRole
(
LLMRoleEnum
.
USER
.
getRole
());
messages
.
add
(
message
);
logger
.
info
(
"--------- Build Messages dialogsId:{},messages:{}--------------"
,
dialogsId
,
messages
);
return
messages
;
}
// private List<KnowledgeContentResult> knowledge(List<Integer> kdIds, String input, Integer topK, Double score, KnowledgeSearchTypeEnum searchTypeEnum) {
// List<KnowledgeContentResult> knowledgeContentResults = new ArrayList<>();
// if (CollectionUtils.isEmpty(kdIds)) {
// return knowledgeContentResults;
// }
// List<String> knowledgeIds = new ArrayList<>();
// for (Integer kdId : kdIds) {
// BizKnowledgeDocumentEntity knowledgeDocumentEntity = bizKnowledgeDocumentService.get(kdId);
// // 筛选训练完成的文档,否则跳过该文档
// if (null == knowledgeDocumentEntity && KnowledgeConstant.TrainStatus.COMPLETE.equals(knowledgeDocumentEntity.getTrainStatus())) {
// continue;
// }
// knowledgeIds.add(knowledgeDocumentEntity.getKnowledgeId());
// }
//
// SearchKnowledgeResult searchKnowledgeResult = demandKnowledgeService.searchKnowledge(input, knowledgeIds, topK, score, searchTypeEnum);
//
// if (CollectionUtils.isNotEmpty(searchKnowledgeResult.getDocuments())) {
// for (int i = 1; i <= searchKnowledgeResult.getDocuments().size(); i++) {
// KnowledgeContentResult knowledgeContentResult = new KnowledgeContentResult();
// knowledgeContentResult.setContent(searchKnowledgeResult.getDocuments().get(i - 1));
// knowledgeContentResult.setKnowledgeId(searchKnowledgeResult.getKnowledgeIds().get(i - 1));
// knowledgeContentResult.setScore(searchKnowledgeResult.getScore().get(i - 1));
// knowledgeContentResults.add(knowledgeContentResult);
// }
// // 根据knowledgeId获取知识库名和文档名
// knowledgeIds = knowledgeContentResults.stream().map(KnowledgeContentResult::getKnowledgeId).distinct().collect(Collectors.toList());
// List<KnowledgeDocumentRelationQueryItem> knowledgeDocumentRelationQueryItems = bizKnowledgeDocumentService.knowledgeDocumentRelationQuery(knowledgeIds, null);
// for (KnowledgeContentResult result : knowledgeContentResults) {
// String knowledgeId = result.getKnowledgeId();
// KnowledgeDocumentRelationQueryItem item = knowledgeDocumentRelationQueryItems.stream().filter(v -> v.getKnowledgeId().equals(knowledgeId)).findFirst().get();
// result.setKnowledgeName(item.getKnowledgeName());
// result.setKdId(item.getKdId());
// result.setDocumentName(item.getDocumentName());
// }
// }
//
// return knowledgeContentResults;
// }
/**
* 文本输出结果
*
* @param sseUtil
* @param bufferedReader
* @throws IOException
*/
private
LongtextDialoguesResult
textOutputStream
(
SSEUtil
sseUtil
,
BufferedReader
bufferedReader
)
throws
IOException
{
String
res
=
""
;
StringBuilder
output
=
new
StringBuilder
();
StringBuilder
reasoningContent
=
new
StringBuilder
();
try
{
while
((
res
=
bufferedReader
.
readLine
())
!=
null
)
{
if
(
StringUtils
.
isEmpty
(
res
))
{
continue
;
}
res
=
StringUtils
.
replace
(
res
,
"data: "
,
StringUtils
.
EMPTY
);
LargeModelDemandResult
result
=
JsonUtils
.
deSerialize
(
res
,
LargeModelDemandResult
.
class
);
if
(!
"0"
.
equals
(
result
.
getCode
()))
{
logger
.
error
(
"LLM Error,code:{}"
,
result
.
getCode
());
BusinessException
ex
=
new
BusinessException
(
"exception/call.failure"
);
result
.
setMessage
(
ex
.
getMessage
());
sseUtil
.
send
(
JsonUtils
.
serialize
(
result
));
sseUtil
.
send
(
"[DONE]"
);
sseUtil
.
complete
();
throw
ex
;
}
result
.
setFunction
(
null
);
result
.
setDbChainResult
(
null
);
result
.
setKnowledgeContentResult
(
null
);
sseUtil
.
send
(
JsonUtils
.
serialize
(
result
));
if
(
StringUtils
.
isNotEmpty
(
result
.
getMessage
()))
{
output
.
append
(
result
.
getMessage
());
}
if
(
StringUtils
.
isNotEmpty
(
result
.
getReasoningContent
()))
{
reasoningContent
.
append
(
result
.
getReasoningContent
());
}
}
// 关闭资源
sseUtil
.
send
(
"[DONE]"
);
sseUtil
.
complete
();
LongtextDialoguesResult
longtextDialoguesResult
=
new
LongtextDialoguesResult
();
longtextDialoguesResult
.
setMessage
(
output
.
toString
());
longtextDialoguesResult
.
setReasoningContent
(
reasoningContent
.
toString
());
return
longtextDialoguesResult
;
}
catch
(
IOException
e
)
{
logger
.
error
(
"连接断开,code:{}"
,
e
.
getMessage
());
sseUtil
.
completeByError
(
"连接断开"
);
throw
new
BusinessException
(
"连接断开"
);
}
finally
{
bufferedReader
.
close
();
sseUtil
.
complete
();
}
}
outputRecord
.
setTimestamp
(
System
.
currentTimeMillis
());
outputRecord
.
setContent
(
agentResultEntity
.
getMessage
());
outputRecord
.
setReasoningContent
(
agentResultEntity
.
getReasoningContent
());
bizAgentApplicationDialoguesRecordService
.
save
(
outputRecord
);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment