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
d9e13ce9
Commit
d9e13ce9
authored
Nov 19, 2024
by
alex yao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
style
parent
51d16f4b
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
122 additions
and
182 deletions
+122
-182
AgentApplicationInfoService.java
...nt_application/aggregate/AgentApplicationInfoService.java
+7
-3
AgentApplicationInfoServiceImpl.java
...ation/aggregate/impl/AgentApplicationInfoServiceImpl.java
+23
-63
CreateAgentTitleAndDescEntity.java
...ent_application/entity/CreateAgentTitleAndDescEntity.java
+6
-0
AgentApplicationInfoRestImpl.java
...t_application/rest/impl/AgentApplicationInfoRestImpl.java
+17
-27
AgentApplicationTools.java
...om/poc/agent_application/utils/AgentApplicationTools.java
+13
-3
RedisLimit.java
src/main/java/cn/com/poc/common/annotation/RedisLimit.java
+3
-2
AgentApplicationServiceImpl.java
...oc/expose/aggregate/impl/AgentApplicationServiceImpl.java
+23
-78
AgentLongMemoryEntity.java
...demand/ai/function/long_memory/AgentLongMemoryEntity.java
+1
-2
LongMemory.java
...y/resource/demand/ai/function/long_memory/LongMemory.java
+29
-4
No files found.
src/main/java/cn/com/poc/agent_application/aggregate/AgentApplicationInfoService.java
View file @
d9e13ce9
...
...
@@ -12,12 +12,16 @@ public interface AgentApplicationInfoService {
/**
* 创建/更新应用
*
* @param entity 应用信息
*/
BizAgentApplicationInfoEntity
saveOrUpdate
(
BizAgentApplicationInfoEntity
entity
)
throws
Exception
;
/**
* 更新并发布应用
*
* @param entity 应用信息
*/
boolean
updateAndPublish
(
BizAgentApplicationInfoEntity
entity
)
throws
Exception
;
...
...
@@ -29,7 +33,7 @@ public interface AgentApplicationInfoService {
boolean
deletedAgentApplication
(
String
agentId
)
throws
Exception
;
/**
* 应用对话
*
Agent
应用对话
*
* @param agentId 应用ID
* @param identifier 对话唯一标识
...
...
@@ -50,7 +54,7 @@ public interface AgentApplicationInfoService {
/**
* 应用下架
*
* @param agentId
* @param agentId
应用ID
* @return
*/
boolean
unPublish
(
String
agentId
)
throws
Exception
;
...
...
@@ -58,7 +62,7 @@ public interface AgentApplicationInfoService {
/**
* 角色指令AI生成
*
* @param input
* @param input
用户输入内容
* @param httpServletResponse
* @return
*/
...
...
src/main/java/cn/com/poc/agent_application/aggregate/impl/AgentApplicationInfoServiceImpl.java
View file @
d9e13ce9
...
...
@@ -8,18 +8,16 @@ import cn.com.poc.agent_application.domain.FunctionResult;
import
cn.com.poc.agent_application.entity.*
;
import
cn.com.poc.agent_application.query.DialogsIdsQueryByAgentIdQueryItem
;
import
cn.com.poc.agent_application.service.*
;
import
cn.com.poc.agent_application.utils.AgentApplicationTools
;
import
cn.com.poc.common.constant.CommonConstant
;
import
cn.com.poc.common.service.RedisService
;
import
cn.com.poc.common.utils.BlContext
;
import
cn.com.poc.common.utils.JsonUtils
;
import
cn.com.poc.common.utils.ListUtils
;
import
cn.com.poc.knowledge.entity.BizKnowledgeDocumentEntity
;
import
cn.com.poc.knowledge.service.BizKnowledgeDocumentService
;
import
cn.com.poc.support.security.oauth.entity.UserBaseEntity
;
import
cn.com.poc.thirdparty.resource.demand.ai.aggregate.AICreateImageService
;
import
cn.com.poc.thirdparty.resource.demand.ai.aggregate.DemandKnowledgeService
;
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.MultiContent
;
import
cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Tool
;
...
...
@@ -30,7 +28,7 @@ import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelDema
import
cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelResponse
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.LargeModelFunctionEnum
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.LongMemory
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.LongMemoryEntity
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.
Agent
LongMemoryEntity
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.memory_variable_writer.MemoryVariableWriter
;
import
cn.com.poc.thirdparty.service.LLMService
;
import
cn.com.yict.framemax.core.i18n.I18nMessageException
;
...
...
@@ -95,9 +93,6 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
@Resource
private
BizAgentApplicationDialoguesRecordService
bizAgentApplicationDialoguesRecordService
;
@Resource
private
RedisService
redisService
;
@Override
public
BizAgentApplicationInfoEntity
saveOrUpdate
(
BizAgentApplicationInfoEntity
entity
)
throws
Exception
{
// 如果存在agentId,则判断变量结构是否有变化,如果有变化,则删除redis中的数据
...
...
@@ -151,7 +146,7 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
if
(
CollectionUtils
.
isNotEmpty
(
items
))
{
logger
.
info
(
"variables structure cache remove,items:{}"
,
items
);
for
(
DialogsIdsQueryByAgentIdQueryItem
item
:
items
)
{
MemoryVariableWriter
.
clearByPre
(
item
.
getDialogsId
()
+
":"
+
bizAgentApplicationInfoEntity
.
getAgentId
(
));
MemoryVariableWriter
.
clearByPre
(
AgentApplicationTools
.
identifier
(
item
.
getDialogsId
(),
bizAgentApplicationInfoEntity
.
getAgentId
()
));
}
}
...
...
@@ -174,19 +169,19 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
}
@Override
public
String
callAgentApplication
(
String
agentId
,
String
identifier
,
String
largeModel
,
public
String
callAgentApplication
(
String
agentId
,
String
dialogueId
,
String
largeModel
,
String
agentSystem
,
Integer
[]
kdIds
,
Integer
communicationTurn
,
Float
topP
,
Float
temperature
,
List
<
Message
>
messages
,
List
<
Tool
>
tools
,
List
<
String
>
fileUrls
,
HttpServletResponse
httpServletResponse
)
throws
Exception
{
logger
.
info
(
"Call Agent Application, agentId:{},
identifier
:{},largeModel:{},agentSystem:{},kdIds:{},communicationTurn:{},topP:{},temperature:{},messages:{}, tools:{}"
,
agentId
,
identifier
,
largeModel
,
agentSystem
,
kdIds
,
communicationTurn
,
topP
,
temperature
,
messages
,
tools
);
logger
.
info
(
"Call Agent Application, agentId:{},
dialogueId
:{},largeModel:{},agentSystem:{},kdIds:{},communicationTurn:{},topP:{},temperature:{},messages:{}, tools:{}"
,
agentId
,
dialogueId
,
largeModel
,
agentSystem
,
kdIds
,
communicationTurn
,
topP
,
temperature
,
messages
,
tools
);
String
model
=
modelConvert
(
largeModel
);
Tool
[]
toolArray
=
tools
.
toArray
(
new
Tool
[
0
]);
FunctionResult
functionResult
=
functionCall
(
identifier
,
messages
,
toolArray
,
agentId
,
fileUrls
);
FunctionResult
functionResult
=
functionCall
(
dialogueId
,
messages
,
toolArray
,
agentId
,
fileUrls
);
String
promptTemplate
=
buildDialogsPrompt
(
functionResult
,
messages
,
agentSystem
,
kdIds
,
toolArray
,
identifier
,
agentId
);
String
promptTemplate
=
buildDialogsPrompt
(
functionResult
,
messages
,
agentSystem
,
kdIds
,
toolArray
,
dialogueId
,
agentId
);
Message
[]
messageArray
=
buildMessages
(
messages
,
communicationTurn
,
promptTemplate
);
...
...
@@ -534,17 +529,17 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
* @param agentSystem 应用角色指令
* @param kdIds 知识库id
* @param tools 组件
* @param
identifier
对话标识符
* @param
dialogueId
对话标识符
* @return
*/
private
String
buildDialogsPrompt
(
FunctionResult
functionResult
,
List
<
Message
>
messages
,
String
agentSystem
,
Integer
[]
kdIds
,
Tool
[]
tools
,
String
identifier
,
String
agentId
)
{
private
String
buildDialogsPrompt
(
FunctionResult
functionResult
,
List
<
Message
>
messages
,
String
agentSystem
,
Integer
[]
kdIds
,
Tool
[]
tools
,
String
dialogueId
,
String
agentId
)
{
String
promptTemplate
=
bizAgentApplicationGcConfigService
.
getByConfigCode
(
AgentApplicationGCConfigConstants
.
AGENT_BASE_SYSTEM
).
getConfigSystem
();
// 应用角色指令
promptTemplate
=
promptTemplate
.
replace
(
"${agentSystem}"
,
StringUtils
.
isNotBlank
(
agentSystem
)
?
agentSystem
:
StringUtils
.
EMPTY
);
// 调用知识库
promptTemplate
=
buildKnowledgePrompt
(
messages
,
kdIds
,
promptTemplate
);
// 记忆
promptTemplate
=
buildMemoryPrompt
(
promptTemplate
,
tools
,
identifier
,
agentId
);
promptTemplate
=
buildMemoryPrompt
(
promptTemplate
,
tools
,
dialogueId
,
agentId
);
// 函数调用
promptTemplate
=
buildFunctionPrompt
(
functionResult
,
promptTemplate
);
return
promptTemplate
;
...
...
@@ -586,10 +581,11 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
*
* @param promptTemplate
* @param tools
* @param identifier
* @param dialogueId
* @param agentId
* @return
*/
private
String
buildMemoryPrompt
(
String
promptTemplate
,
Tool
[]
tools
,
String
identifier
,
String
agentId
)
{
private
String
buildMemoryPrompt
(
String
promptTemplate
,
Tool
[]
tools
,
String
dialogueId
,
String
agentId
)
{
String
longMemoryContent
=
StringUtils
.
EMPTY
;
String
valueMemoryContent
=
StringUtils
.
EMPTY
;
if
(
ArrayUtils
.
isNotEmpty
(
tools
))
{
...
...
@@ -597,14 +593,14 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
String
name
=
tool
.
getFunction
().
getName
();
// 长期记忆
if
(
LargeModelFunctionEnum
.
set_long_memory
.
name
().
equals
(
name
))
{
List
<
LongMemoryEntity
>
longMemoryEntities
=
LongMemory
.
get
(
identifier
+
":"
+
agentId
);
List
<
AgentLongMemoryEntity
>
longMemoryEntities
=
LongMemory
.
get
(
AgentApplicationTools
.
identifier
(
dialogueId
,
agentId
)
);
if
(
CollectionUtils
.
isNotEmpty
(
longMemoryEntities
))
{
StringBuilder
stringBuilder
=
new
StringBuilder
(
""
);
for
(
LongMemoryEntity
l
ongMemoryEntity
:
longMemoryEntities
)
{
for
(
AgentLongMemoryEntity
agentL
ongMemoryEntity
:
longMemoryEntities
)
{
stringBuilder
.
append
(
"Time"
).
append
(
":"
).
append
(
l
ongMemoryEntity
.
getTimestamp
())
.
append
(
"Time"
).
append
(
":"
).
append
(
agentL
ongMemoryEntity
.
getTimestamp
())
.
append
(
StringUtils
.
SPACE
)
.
append
(
"Content"
).
append
(
":"
).
append
(
l
ongMemoryEntity
.
getContent
())
.
append
(
"Content"
).
append
(
":"
).
append
(
agentL
ongMemoryEntity
.
getContent
())
.
append
(
StringUtils
.
LF
);
}
longMemoryContent
=
stringBuilder
.
toString
();
...
...
@@ -613,7 +609,7 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
// 变量记忆
if
(
LargeModelFunctionEnum
.
memory_variable_writer
.
name
().
equals
(
name
))
{
Map
<
Object
,
Object
>
map
=
MemoryVariableWriter
.
get
(
identifier
+
":"
+
agentId
);
Map
<
Object
,
Object
>
map
=
MemoryVariableWriter
.
get
(
AgentApplicationTools
.
identifier
(
dialogueId
,
agentId
)
);
StringBuilder
stringBuilder
=
new
StringBuilder
(
""
);
if
(
MapUtils
.
isNotEmpty
(
map
))
{
Set
<
Object
>
keySet
=
map
.
keySet
();
...
...
@@ -664,43 +660,6 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
return
llmService
.
chatChunk
(
largeModelResponse
);
}
/**
* 构建function_call消息体
*
* @param messageArray
* @param functionName
* @param functionArguments
* @param functionResult
* @return
*/
private
Message
[]
buildFunctionMessage
(
Message
[]
messageArray
,
String
functionName
,
String
functionArguments
,
String
functionResult
,
String
functionRole
)
{
List
<
FunctionCall
>
functionCalls
=
new
ArrayList
<>();
FunctionCall
functionCall
=
new
FunctionCall
();
functionCall
.
setName
(
functionName
);
functionCall
.
setArguments
(
functionArguments
);
functionCalls
.
add
(
functionCall
);
Message
assistantMessage
=
new
Message
();
assistantMessage
.
setRole
(
LLMRoleEnum
.
ASSISTANT
.
getRole
());
assistantMessage
.
setContent
(
null
);
assistantMessage
.
setFunction_call
(
functionCalls
);
Message
functionMessage
=
new
Message
();
functionMessage
.
setRole
(
functionRole
);
functionMessage
.
setName
(
functionName
);
functionMessage
.
setContent
(
functionResult
);
Message
[]
sendMessage
=
new
Message
[
messageArray
.
length
+
2
];
for
(
int
i
=
0
;
i
<
messageArray
.
length
;
i
++)
{
sendMessage
[
i
]
=
messageArray
[
i
];
}
sendMessage
[
messageArray
.
length
]
=
assistantMessage
;
sendMessage
[
messageArray
.
length
+
1
]
=
functionMessage
;
return
sendMessage
;
}
/**
* 文本输出结果
...
...
@@ -793,11 +752,12 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
/**
* 判断是否需要FunctionCall
*
* @param
identifier
* @param
dialogueId
* @param messages
* @param tools
* @param agentId
*/
private
FunctionResult
functionCall
(
String
identifier
,
List
<
Message
>
messages
,
Tool
[]
tools
,
String
agentId
,
List
<
String
>
fileUrls
)
{
private
FunctionResult
functionCall
(
String
dialogueId
,
List
<
Message
>
messages
,
Tool
[]
tools
,
String
agentId
,
List
<
String
>
fileUrls
)
{
FunctionResult
result
=
new
FunctionResult
();
if
(
ArrayUtils
.
isEmpty
(
tools
))
{
return
result
;
...
...
@@ -818,7 +778,7 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
if
(
functionCallResult
!=
null
&&
functionCallResult
.
isNeed
())
{
// 执行函数返回结果
LargeModelFunctionEnum
functionEnum
=
LargeModelFunctionEnum
.
valueOf
(
functionCallResult
.
getFunctionCall
().
getName
());
String
functionResult
=
functionEnum
.
getFunction
().
doFunction
(
functionCallResult
.
getFunctionCall
().
getArguments
(),
identifier
+
":"
+
agentId
);
String
functionResult
=
functionEnum
.
getFunction
().
doFunction
(
functionCallResult
.
getFunctionCall
().
getArguments
(),
AgentApplicationTools
.
identifier
(
dialogueId
,
agentId
)
);
//构造返回结果
result
.
setFunctionName
(
functionCallResult
.
getFunctionCall
().
getName
());
...
...
src/main/java/cn/com/poc/agent_application/entity/CreateAgentTitleAndDescEntity.java
View file @
d9e13ce9
...
...
@@ -2,8 +2,14 @@ package cn.com.poc.agent_application.entity;
public
class
CreateAgentTitleAndDescEntity
{
/**
* 应用名称
*/
private
String
agentTitle
;
/**
* 应用描述
*/
private
String
agentDesc
;
public
String
getAgentTitle
()
{
...
...
src/main/java/cn/com/poc/agent_application/rest/impl/AgentApplicationInfoRestImpl.java
View file @
d9e13ce9
...
...
@@ -7,18 +7,19 @@ import cn.com.poc.agent_application.entity.*;
import
cn.com.poc.agent_application.query.AgentApplicationInfoQueryCondition
;
import
cn.com.poc.agent_application.rest.AgentApplicationInfoRest
;
import
cn.com.poc.agent_application.service.*
;
import
cn.com.poc.agent_application.utils.AgentApplicationTools
Builder
;
import
cn.com.poc.agent_application.utils.AgentApplicationTools
;
import
cn.com.poc.common.annotation.RedisLimit
;
import
cn.com.poc.common.constant.CommonConstant
;
import
cn.com.poc.common.service.RedisService
;
import
cn.com.poc.common.utils.BlContext
;
import
cn.com.poc.knowledge.aggregate.KnowledgeService
;
import
cn.com.poc.support.security.oauth.entity.UserBaseEntity
;
import
cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Tool
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.SetLongMemoryConstants
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.AgentLongMemoryEntity
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.LongMemory
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.memory_variable_writer.MemoryVariableWriter
;
import
cn.com.yict.framemax.core.i18n.I18nMessageException
;
import
cn.com.yict.framemax.data.model.PagingInfo
;
import
cn.hutool.core.bean.BeanUtil
;
import
cn.hutool.core.collection.ListUtil
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.MapUtils
;
...
...
@@ -30,7 +31,10 @@ import javax.annotation.Resource;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.util.*
;
import
java.util.ArrayList
;
import
java.util.Comparator
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
@Component
...
...
@@ -51,9 +55,6 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
@Resource
private
KnowledgeService
knowledgeService
;
@Resource
private
RedisService
redisService
;
@Resource
private
BizMemberAgentApplicationCollectService
bizMemberAgentApplicationCollectService
;
...
...
@@ -212,7 +213,7 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
List
<
Integer
>
kdIds
=
knowledgeService
.
getKdIdsByKnowledgeInfoIds
(
infoEntity
.
getKnowledgeIds
());
//配置对话function
List
<
Tool
>
tools
=
AgentApplicationTools
Builder
.
buildFunctionConfig
(
infoEntity
.
getVariableStructure
(),
infoEntity
.
getIsLongMemory
(),
dialogueId
,
agentId
,
infoEntity
.
getUnitIds
(),
infoEntity
.
getIsDocumentParsing
());
List
<
Tool
>
tools
=
AgentApplicationTools
.
buildFunctionConfig
(
infoEntity
.
getVariableStructure
(),
infoEntity
.
getIsLongMemory
(),
dialogueId
,
agentId
,
infoEntity
.
getUnitIds
(),
infoEntity
.
getIsDocumentParsing
());
//对话大模型配置
String
model
=
StringUtils
.
isNotBlank
(
dto
.
getModelNickName
())
?
dto
.
getModelNickName
()
:
infoEntity
.
getLargeModel
();
...
...
@@ -340,35 +341,24 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
@Override
public
List
<
AgentLongMemoryDto
>
getLongMemoryList
(
String
agentId
)
{
Assert
.
notNull
(
agentId
);
List
<
AgentLongMemoryDto
>
result
=
new
ArrayList
<>();
String
key
=
agentId
+
":"
+
agentId
;
String
contentKey
=
SetLongMemoryConstants
.
REDIS_PREFIX
+
key
+
":"
+
BlContext
.
getCurrentUserNotException
().
getUserId
().
toString
();
Map
<
Object
,
Object
>
map
=
redisService
.
hmget
(
contentKey
);
Set
<
Object
>
keySet
=
map
.
keySet
();
for
(
Object
mapKey
:
keySet
)
{
AgentLongMemoryDto
entity
=
new
AgentLongMemoryDto
();
entity
.
setContent
(
map
.
get
(
mapKey
).
toString
());
entity
.
setTimestamp
(
mapKey
.
toString
());
result
.
add
(
entity
);
}
return
result
.
stream
().
sorted
(
Comparator
.
comparing
(
AgentLongMemoryDto:
:
getTimestamp
).
reversed
()).
collect
(
Collectors
.
toList
());
List
<
AgentLongMemoryEntity
>
longMemoryEntities
=
LongMemory
.
get
(
AgentApplicationTools
.
identifier
(
agentId
,
agentId
));
return
longMemoryEntities
.
stream
().
map
(
value
->
{
AgentLongMemoryDto
agentLongMemoryDto
=
new
AgentLongMemoryDto
();
BeanUtil
.
copyProperties
(
value
,
agentLongMemoryDto
);
return
agentLongMemoryDto
;
}).
sorted
(
Comparator
.
comparing
(
AgentLongMemoryDto:
:
getTimestamp
).
reversed
()).
collect
(
Collectors
.
toList
());
}
@Override
public
void
deleteLongMemoryByKey
(
String
agentId
,
String
timestamp
)
{
Assert
.
notNull
(
agentId
);
Assert
.
notNull
(
timestamp
);
String
key
=
agentId
+
":"
+
agentId
;
String
contentKey
=
SetLongMemoryConstants
.
REDIS_PREFIX
+
key
+
":"
+
BlContext
.
getCurrentUserNotException
().
getUserId
().
toString
();
redisService
.
hdel
(
contentKey
,
timestamp
);
LongMemory
.
delByKey
(
agentId
,
timestamp
);
}
@Override
public
void
deleteLongMemory
(
String
agentId
)
{
Assert
.
notNull
(
agentId
);
String
key
=
agentId
+
":"
+
agentId
;
String
contentKey
=
SetLongMemoryConstants
.
REDIS_PREFIX
+
key
+
":"
+
BlContext
.
getCurrentUserNotException
().
getUserId
().
toString
();
redisService
.
del
(
contentKey
);
LongMemory
.
clean
(
agentId
);
}
}
\ No newline at end of file
src/main/java/cn/com/poc/agent_application/utils/AgentApplicationTools
Builder
.java
→
src/main/java/cn/com/poc/agent_application/utils/AgentApplicationTools.java
View file @
d9e13ce9
...
...
@@ -15,7 +15,7 @@ import java.util.ArrayList;
import
java.util.List
;
import
java.util.Map
;
public
class
AgentApplicationTools
Builder
{
public
class
AgentApplicationTools
{
/**
...
...
@@ -39,7 +39,7 @@ public class AgentApplicationToolsBuilder {
tools
.
add
(
tool
);
//初始化变量函数
Map
<
Object
,
Object
>
map
=
MemoryVariableWriter
.
get
(
identifier
+
":"
+
agentId
);
Map
<
Object
,
Object
>
map
=
MemoryVariableWriter
.
get
(
identifier
(
identifier
,
agentId
)
);
if
(
MapUtils
.
isEmpty
(
map
))
{
List
<
Variable
>
variableStructure
=
variableStructures
;
for
(
Variable
variable
:
variableStructure
)
{
...
...
@@ -48,7 +48,7 @@ public class AgentApplicationToolsBuilder {
JSONObject
jsonObject
=
new
JSONObject
();
jsonObject
.
put
(
"key"
,
key
);
jsonObject
.
put
(
"value"
,
variableDefault
);
LargeModelFunctionEnum
.
valueOf
(
functionName
).
getFunction
().
doFunction
(
jsonObject
.
toJSONString
(),
identifier
+
":"
+
agentId
);
LargeModelFunctionEnum
.
valueOf
(
functionName
).
getFunction
().
doFunction
(
jsonObject
.
toJSONString
(),
identifier
(
identifier
,
agentId
)
);
}
}
}
...
...
@@ -83,5 +83,15 @@ public class AgentApplicationToolsBuilder {
return
tools
;
}
/**
* 创建会话唯一标识符
*
* @param dialogueId 对话ID
* @param agentId 应用ID
* @return
*/
public
static
String
identifier
(
String
dialogueId
,
String
agentId
)
{
return
dialogueId
+
":"
+
agentId
;
}
}
src/main/java/cn/com/poc/common/annotation/RedisLimit.java
View file @
d9e13ce9
...
...
@@ -7,7 +7,7 @@ import java.lang.annotation.*;
/**
* 限流
*/
@Target
(
ElementType
.
METHOD
)
@Target
(
{
ElementType
.
METHOD
,
ElementType
.
TYPE
}
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Component
@Documented
...
...
@@ -77,7 +77,8 @@ public @interface RedisLimit {
*/
MONTH_OF_YEAR
;
LimitTimeUnit
(){}
LimitTimeUnit
()
{
}
}
}
src/main/java/cn/com/poc/expose/aggregate/impl/AgentApplicationServiceImpl.java
View file @
d9e13ce9
...
...
@@ -6,14 +6,13 @@ import cn.com.poc.agent_application.constant.AgentApplicationGCConfigConstants;
import
cn.com.poc.agent_application.entity.BizAgentApplicationDialoguesRecordEntity
;
import
cn.com.poc.agent_application.entity.BizAgentApplicationGcConfigEntity
;
import
cn.com.poc.agent_application.entity.BizAgentApplicationPublishEntity
;
import
cn.com.poc.agent_application.entity.Variable
;
import
cn.com.poc.agent_application.query.MemberCollectQueryCondition
;
import
cn.com.poc.agent_application.query.MemberCollectQueryItem
;
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.BizMemberAgentApplicationCollectService
;
import
cn.com.poc.agent_application.utils.AgentApplicationTools
Builder
;
import
cn.com.poc.agent_application.utils.AgentApplicationTools
;
import
cn.com.poc.common.constant.CommonConstant
;
import
cn.com.poc.common.constant.XLangConstant
;
import
cn.com.poc.common.service.RedisService
;
...
...
@@ -22,22 +21,18 @@ import cn.com.poc.common.utils.JsonUtils;
import
cn.com.poc.expose.aggregate.AgentApplicationService
;
import
cn.com.poc.knowledge.aggregate.KnowledgeService
;
import
cn.com.poc.support.security.oauth.entity.UserBaseEntity
;
import
cn.com.poc.thirdparty.resource.demand.ai.constants.LLMRoleEnum
;
import
cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Message
;
import
cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.MultiContent
;
import
cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Tool
;
import
cn.com.poc.thirdparty.resource.demand.ai.constants.LLMRoleEnum
;
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.LargeModelFunctionEnum
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.memory_variable_writer.MemoryVariableWriter
;
import
cn.com.poc.thirdparty.service.LLMService
;
import
cn.com.yict.framemax.core.i18n.I18nMessageException
;
import
cn.com.yict.framemax.data.model.PagingInfo
;
import
com.alibaba.fastjson.JSONObject
;
import
com.fasterxml.jackson.core.type.TypeReference
;
import
com.github.houbb.opencc4j.util.ZhConverterUtil
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.MapUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -46,7 +41,10 @@ import org.springframework.stereotype.Service;
import
javax.annotation.Resource
;
import
javax.servlet.http.HttpServletResponse
;
import
java.security.SecureRandom
;
import
java.util.*
;
import
java.util.ArrayList
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.stream.Collectors
;
@Service
...
...
@@ -54,6 +52,9 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
final
private
Logger
logger
=
LoggerFactory
.
getLogger
(
AgentApplicationService
.
class
);
/**
* 推荐问Key
*/
final
private
String
AGENT_APPLICATION_RECOMMEND_QUESTIONS
=
"AGENT_APPLICATION_RECOMMEND_QUESTIONS:"
;
/**
...
...
@@ -103,7 +104,7 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
if
(
StringUtils
.
isBlank
(
dialogsId
))
{
// 用于针对只有单Agent应用分享使用的场景,dialogsId为空
dialogsId
=
agentId
+
"
_
"
+
userBaseEntity
.
getUserId
();
dialogsId
=
agentId
+
"
-
"
+
userBaseEntity
.
getUserId
();
}
//获取知识库配置
...
...
@@ -124,10 +125,10 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
bizAgentApplicationDialoguesRecordService
.
save
(
inputRecord
);
//配置对话function
List
<
Tool
>
tools
=
AgentApplicationTools
Builder
.
buildFunctionConfig
(
infoEntity
.
getVariableStructure
(),
infoEntity
.
getIsLongMemory
(),
dialogsId
,
agentId
,
infoEntity
.
getUnitIds
(),
infoEntity
.
getIsDocumentParsing
());
List
<
Tool
>
tools
=
AgentApplicationTools
.
buildFunctionConfig
(
infoEntity
.
getVariableStructure
(),
infoEntity
.
getIsLongMemory
(),
dialogsId
,
agentId
,
infoEntity
.
getUnitIds
(),
infoEntity
.
getIsDocumentParsing
());
//
记录输出时间戳
//记录输出时间戳
BizAgentApplicationDialoguesRecordEntity
outputRecord
=
new
BizAgentApplicationDialoguesRecordEntity
();
outputRecord
.
setRole
(
AgentApplicationDialoguesRecordConstants
.
ROLE
.
ASSISTANT
);
outputRecord
.
setAgentId
(
infoEntity
.
getAgentId
());
...
...
@@ -255,6 +256,17 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
createENQuestion
();
}
@Override
public
List
<
MemberCollectQueryItem
>
getCollectedApplications
(
Long
memberId
,
PagingInfo
pagingInfo
)
{
MemberCollectQueryCondition
condition
=
new
MemberCollectQueryCondition
();
condition
.
setMemberId
(
memberId
);
condition
.
setIsCollect
(
CommonConstant
.
YOrN
.
Y
);
List
<
MemberCollectQueryItem
>
memberCollectQueryItems
=
bizMemberAgentApplicationCollectService
.
queryMemberCollect
(
condition
,
pagingInfo
);
return
memberCollectQueryItems
;
}
private
void
createCNQuestion
()
{
Message
message
=
new
Message
();
message
.
setRole
(
LLMRoleEnum
.
USER
.
getRole
());
...
...
@@ -308,15 +320,6 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
redisService
.
lSet
(
AGENT_APPLICATION_RECOMMEND_QUESTIONS
+
"en"
,
questions
);
}
@Override
public
List
<
MemberCollectQueryItem
>
getCollectedApplications
(
Long
memberId
,
PagingInfo
pagingInfo
)
{
MemberCollectQueryCondition
condition
=
new
MemberCollectQueryCondition
();
condition
.
setMemberId
(
memberId
);
condition
.
setIsCollect
(
CommonConstant
.
YOrN
.
Y
);
List
<
MemberCollectQueryItem
>
memberCollectQueryItems
=
bizMemberAgentApplicationCollectService
.
queryMemberCollect
(
condition
,
pagingInfo
);
return
memberCollectQueryItems
;
}
private
List
<
Message
>
buildMessages
(
String
dialogsId
,
String
agentId
,
Long
userId
,
String
input
)
throws
Exception
{
List
<
Message
>
messages
=
new
ArrayList
<>();
BizAgentApplicationDialoguesRecordEntity
recordEntity
=
new
BizAgentApplicationDialoguesRecordEntity
();
...
...
@@ -351,62 +354,4 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
logger
.
info
(
"--------- Build Messages dialogsId:{},agentId:{},messages:{}--------------"
,
dialogsId
,
agentId
,
messages
);
return
messages
;
}
@Deprecated
private
void
saveDialoguesRecord
(
String
dialogsId
,
String
input
,
BizAgentApplicationPublishEntity
infoEntity
,
UserBaseEntity
userBaseEntity
,
Long
inputTimestamp
,
String
output
)
throws
Exception
{
// 回答时间戳
Long
outputTimestamp
=
System
.
currentTimeMillis
();
BizAgentApplicationDialoguesRecordEntity
inputRecord
=
new
BizAgentApplicationDialoguesRecordEntity
();
inputRecord
.
setAgentId
(
infoEntity
.
getAgentId
());
inputRecord
.
setMemberId
(
userBaseEntity
.
getUserId
());
inputRecord
.
setContent
(
input
);
inputRecord
.
setDialogsId
(
dialogsId
);
inputRecord
.
setRole
(
AgentApplicationDialoguesRecordConstants
.
ROLE
.
USER
);
inputRecord
.
setTimestamp
(
inputTimestamp
);
BizAgentApplicationDialoguesRecordEntity
outputRecord
=
new
BizAgentApplicationDialoguesRecordEntity
();
outputRecord
.
setRole
(
AgentApplicationDialoguesRecordConstants
.
ROLE
.
ASSISTANT
);
outputRecord
.
setAgentId
(
infoEntity
.
getAgentId
());
outputRecord
.
setDialogsId
(
dialogsId
);
outputRecord
.
setMemberId
(
userBaseEntity
.
getUserId
());
outputRecord
.
setContent
(
output
);
outputRecord
.
setTimestamp
(
outputTimestamp
);
bizAgentApplicationDialoguesRecordService
.
save
(
inputRecord
);
bizAgentApplicationDialoguesRecordService
.
save
(
outputRecord
);
}
private
List
<
Tool
>
buildMemoryConfig
(
BizAgentApplicationPublishEntity
infoEntity
,
String
identifier
)
{
List
<
Tool
>
tools
=
new
ArrayList
<>();
//开启对话变量
if
(
CollectionUtils
.
isNotEmpty
(
infoEntity
.
getVariableStructure
()))
{
String
functionName
=
LargeModelFunctionEnum
.
memory_variable_writer
.
name
();
String
llmConfig
=
LargeModelFunctionEnum
.
valueOf
(
functionName
).
getFunction
().
getLLMConfig
(
infoEntity
.
getVariableStructure
()).
get
(
0
);
Tool
tool
=
JsonUtils
.
deSerialize
(
llmConfig
,
Tool
.
class
);
tools
.
add
(
tool
);
//初始化变量函数
Map
<
Object
,
Object
>
map
=
MemoryVariableWriter
.
get
(
identifier
+
":"
+
infoEntity
.
getAgentId
());
if
(
MapUtils
.
isEmpty
(
map
))
{
List
<
Variable
>
variableStructure
=
infoEntity
.
getVariableStructure
();
for
(
Variable
variable
:
variableStructure
)
{
String
key
=
variable
.
getKey
();
String
variableDefault
=
variable
.
getVariableDefault
();
JSONObject
jsonObject
=
new
JSONObject
();
jsonObject
.
put
(
"key"
,
key
);
jsonObject
.
put
(
"value"
,
variableDefault
);
LargeModelFunctionEnum
.
valueOf
(
functionName
).
getFunction
().
doFunction
(
jsonObject
.
toJSONString
(),
identifier
+
":"
+
infoEntity
.
getAgentId
());
}
}
}
//开启长期记忆
if
(
CommonConstant
.
YOrN
.
Y
.
equals
(
infoEntity
.
getIsLongMemory
()))
{
String
functionName
=
LargeModelFunctionEnum
.
set_long_memory
.
name
();
String
llmConfig
=
LargeModelFunctionEnum
.
valueOf
(
functionName
).
getFunction
().
getLLMConfig
().
get
(
0
);
Tool
tool
=
JsonUtils
.
deSerialize
(
llmConfig
,
Tool
.
class
);
tools
.
add
(
tool
);
}
return
tools
;
}
}
src/main/java/cn/com/poc/thirdparty/resource/demand/ai/function/long_memory/LongMemoryEntity.java
→
src/main/java/cn/com/poc/thirdparty/resource/demand/ai/function/long_memory/
Agent
LongMemoryEntity.java
View file @
d9e13ce9
package
cn
.
com
.
poc
.
thirdparty
.
resource
.
demand
.
ai
.
function
.
long_memory
;
import
java.io.Serializable
;
import
java.util.Date
;
public
class
LongMemoryEntity
implements
Serializable
{
public
class
Agent
LongMemoryEntity
implements
Serializable
{
private
String
content
;
...
...
src/main/java/cn/com/poc/thirdparty/resource/demand/ai/function/long_memory/LongMemory.java
View file @
d9e13ce9
package
cn
.
com
.
poc
.
thirdparty
.
resource
.
demand
.
ai
.
function
.
long_memory
;
import
cn.com.poc.agent_application.utils.AgentApplicationTools
;
import
cn.com.poc.common.service.RedisService
;
import
cn.com.poc.common.utils.BlContext
;
import
cn.com.poc.common.utils.SpringUtils
;
...
...
@@ -11,16 +12,16 @@ import java.util.Set;
public
class
LongMemory
{
public
static
List
<
LongMemoryEntity
>
get
(
String
key
)
{
public
static
List
<
AgentLongMemoryEntity
>
get
(
String
identifier
)
{
RedisService
redisService
=
SpringUtils
.
getBean
(
RedisService
.
class
);
List
<
LongMemoryEntity
>
result
=
new
ArrayList
<>();
List
<
Agent
LongMemoryEntity
>
result
=
new
ArrayList
<>();
// 查询用户相关信息(什么内容都可以)
String
contentKey
=
SetLongMemoryConstants
.
REDIS_PREFIX
+
key
+
":"
+
BlContext
.
getCurrentUserNotException
().
getUserId
().
toString
();
String
contentKey
=
SetLongMemoryConstants
.
REDIS_PREFIX
+
identifier
+
":"
+
BlContext
.
getCurrentUserNotException
().
getUserId
().
toString
();
Map
<
Object
,
Object
>
map
=
redisService
.
hmget
(
contentKey
);
Set
<
Object
>
keySet
=
map
.
keySet
();
for
(
Object
mapKey
:
keySet
)
{
LongMemoryEntity
entity
=
new
LongMemoryEntity
();
AgentLongMemoryEntity
entity
=
new
Agent
LongMemoryEntity
();
entity
.
setContent
(
map
.
get
(
mapKey
).
toString
());
entity
.
setTimestamp
(
mapKey
.
toString
());
result
.
add
(
entity
);
...
...
@@ -28,4 +29,28 @@ public class LongMemory {
return
result
;
}
/**
* 清除用户在在应用的长期记忆内容
*
* @param agentId
*/
public
static
void
clean
(
String
agentId
)
{
RedisService
redisService
=
SpringUtils
.
getBean
(
RedisService
.
class
);
String
key
=
AgentApplicationTools
.
identifier
(
agentId
,
agentId
);
String
contentKey
=
SetLongMemoryConstants
.
REDIS_PREFIX
+
key
+
":"
+
BlContext
.
getCurrentUserNotException
().
getUserId
().
toString
();
redisService
.
del
(
contentKey
);
}
/**
* 根据时间戳 删除用户在应用的长期记忆 某个时间保存的内容
*
* @param agentId 应用id
* @param item 时间戳
*/
public
static
void
delByKey
(
String
agentId
,
String
item
)
{
RedisService
redisService
=
SpringUtils
.
getBean
(
RedisService
.
class
);
String
key
=
AgentApplicationTools
.
identifier
(
agentId
,
agentId
);
String
contentKey
=
SetLongMemoryConstants
.
REDIS_PREFIX
+
key
+
":"
+
BlContext
.
getCurrentUserNotException
().
getUserId
().
toString
();
redisService
.
hdel
(
contentKey
,
item
);
}
}
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