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
876dd01a
Commit
876dd01a
authored
Nov 13, 2024
by
alex yao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 优化变量记忆方法,支持多属性同时配置
parent
3e64d624
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
236 additions
and
23 deletions
+236
-23
AgentApplicationInfoServiceImpl.java
...ation/aggregate/impl/AgentApplicationInfoServiceImpl.java
+3
-2
AgentApplicationInfoRestImpl.java
...t_application/rest/impl/AgentApplicationInfoRestImpl.java
+6
-5
AgentApplicationServiceImpl.java
...oc/expose/aggregate/impl/AgentApplicationServiceImpl.java
+5
-4
LargeModelFunctionEnum.java
...y/resource/demand/ai/function/LargeModelFunctionEnum.java
+2
-1
GetMemoryVariable.java
...ai/function/memory_variable_writer/GetMemoryVariable.java
+20
-0
MemoryVariableWriterConstants.java
...memory_variable_writer/MemoryVariableWriterConstants.java
+7
-0
MemoryVariableWriterFunction.java
.../memory_variable_writer/MemoryVariableWriterFunction.java
+122
-0
AgentApplicationInfoTest.java
src/test/java/cn/com/poc/AgentApplicationInfoTest.java
+24
-11
AiDialogueTest.java
src/test/java/cn/com/poc/demand/AiDialogueTest.java
+47
-0
No files found.
src/main/java/cn/com/poc/agent_application/aggregate/impl/AgentApplicationInfoServiceImpl.java
View file @
876dd01a
...
...
@@ -30,6 +30,7 @@ import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelResp
import
cn.com.poc.thirdparty.resource.demand.ai.function.LargeModelFunctionEnum
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.GetLongMemory
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.LongMemoryEntity
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.memory_variable_writer.GetMemoryVariable
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.value_memory.GetValueMemory
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.value_memory.SetValueMemoryConstants
;
import
cn.com.poc.thirdparty.service.LLMService
;
...
...
@@ -608,8 +609,8 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
}
// 变量记忆
if
(
LargeModelFunctionEnum
.
set_value_memory
.
name
().
equals
(
name
))
{
Map
<
Object
,
Object
>
map
=
Get
ValueMemory
.
get
(
identifier
+
":"
+
agentId
);
if
(
LargeModelFunctionEnum
.
memory_variable_writer
.
name
().
equals
(
name
))
{
Map
<
Object
,
Object
>
map
=
Get
MemoryVariable
.
get
(
identifier
+
":"
+
agentId
);
StringBuilder
stringBuilder
=
new
StringBuilder
(
""
);
if
(
MapUtils
.
isNotEmpty
(
map
))
{
Set
<
Object
>
keySet
=
map
.
keySet
();
...
...
src/main/java/cn/com/poc/agent_application/rest/impl/AgentApplicationInfoRestImpl.java
View file @
876dd01a
...
...
@@ -17,6 +17,7 @@ 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.LargeModelFunctionEnum
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.SetLongMemoryConstants
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.memory_variable_writer.GetMemoryVariable
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.value_memory.GetValueMemory
;
import
cn.com.yict.framemax.core.i18n.I18nMessageException
;
import
cn.com.yict.framemax.data.model.PagingInfo
;
...
...
@@ -218,21 +219,21 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
List
<
Tool
>
tools
=
new
ArrayList
<>();
//开启对话变量
if
(
CollectionUtils
.
isNotEmpty
(
infoEntity
.
getVariableStructure
()))
{
String
functionName
=
LargeModelFunctionEnum
.
set_value_memory
.
name
();
String
functionName
=
LargeModelFunctionEnum
.
memory_variable_writer
.
name
();
String
llmConfig
=
LargeModelFunctionEnum
.
valueOf
(
functionName
).
getFunction
().
getVariableStructureLLMConfig
(
infoEntity
.
getVariableStructure
()).
get
(
0
);
Tool
tool
=
JsonUtils
.
deSerialize
(
llmConfig
,
Tool
.
class
);
tools
.
add
(
tool
);
//初始化变量函数
Map
<
Object
,
Object
>
map
=
Get
ValueMemory
.
get
(
agentId
+
":"
+
agentId
);
Map
<
Object
,
Object
>
map
=
Get
MemoryVariable
.
get
(
agentId
+
":"
+
agentId
);
List
<
Variable
>
variableStructure
=
infoEntity
.
getVariableStructure
();
if
(
MapUtils
.
isEmpty
(
map
))
{
for
(
Variable
variable
:
variableStructure
)
{
String
key
=
variable
.
getKey
();
String
variableDefault
=
variable
.
getVariableDefault
();
JSONObject
jsonObject
=
new
JSONObject
();
jsonObject
.
put
(
"
contentName
"
,
key
);
jsonObject
.
put
(
"
contentV
alue"
,
variableDefault
);
jsonObject
.
put
(
"
key
"
,
key
);
jsonObject
.
put
(
"
v
alue"
,
variableDefault
);
LargeModelFunctionEnum
.
valueOf
(
functionName
).
getFunction
().
doFunction
(
jsonObject
.
toJSONString
(),
agentId
+
":"
+
agentId
);
}
}
...
...
@@ -347,7 +348,7 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
public
List
<
AgentApplicationValueMemoryDto
>
getVariableList
(
String
agentId
)
{
List
<
AgentApplicationValueMemoryDto
>
result
=
new
ArrayList
<>();
BizAgentApplicationInfoEntity
infoEntity
=
bizAgentApplicationInfoService
.
getByAgentId
(
agentId
);
Map
<
Object
,
Object
>
map
=
Get
ValueMemory
.
get
(
agentId
+
":"
+
agentId
);
Map
<
Object
,
Object
>
map
=
Get
MemoryVariable
.
get
(
agentId
+
":"
+
agentId
);
List
<
Variable
>
variableStructure
=
infoEntity
.
getVariableStructure
();
if
(
MapUtils
.
isEmpty
(
map
))
{
if
(
CollectionUtils
.
isEmpty
(
variableStructure
))
{
...
...
src/main/java/cn/com/poc/expose/aggregate/impl/AgentApplicationServiceImpl.java
View file @
876dd01a
...
...
@@ -28,6 +28,7 @@ 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.GetMemoryVariable
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.value_memory.GetValueMemory
;
import
cn.com.poc.thirdparty.service.LLMService
;
import
cn.com.yict.framemax.core.i18n.I18nMessageException
;
...
...
@@ -381,21 +382,21 @@ public class AgentApplicationServiceImpl implements AgentApplicationService {
List
<
Tool
>
tools
=
new
ArrayList
<>();
//开启对话变量
if
(
CollectionUtils
.
isNotEmpty
(
infoEntity
.
getVariableStructure
()))
{
String
functionName
=
LargeModelFunctionEnum
.
set_value_memory
.
name
();
String
functionName
=
LargeModelFunctionEnum
.
memory_variable_writer
.
name
();
String
llmConfig
=
LargeModelFunctionEnum
.
valueOf
(
functionName
).
getFunction
().
getVariableStructureLLMConfig
(
infoEntity
.
getVariableStructure
()).
get
(
0
);
Tool
tool
=
JsonUtils
.
deSerialize
(
llmConfig
,
Tool
.
class
);
tools
.
add
(
tool
);
//初始化变量函数
Map
<
Object
,
Object
>
map
=
Get
ValueMemory
.
get
(
identifier
+
":"
+
infoEntity
.
getAgentId
());
Map
<
Object
,
Object
>
map
=
Get
MemoryVariable
.
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
(
"
contentName
"
,
key
);
jsonObject
.
put
(
"
contentV
alue"
,
variableDefault
);
jsonObject
.
put
(
"
key
"
,
key
);
jsonObject
.
put
(
"
v
alue"
,
variableDefault
);
LargeModelFunctionEnum
.
valueOf
(
functionName
).
getFunction
().
doFunction
(
jsonObject
.
toJSONString
(),
identifier
+
":"
+
infoEntity
.
getAgentId
());
}
}
...
...
src/main/java/cn/com/poc/thirdparty/resource/demand/ai/function/LargeModelFunctionEnum.java
View file @
876dd01a
...
...
@@ -2,12 +2,13 @@ package cn.com.poc.thirdparty.resource.demand.ai.function;
import
cn.com.poc.common.utils.SpringUtils
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.SetLongMemoryFunction
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.memory_variable_writer.MemoryVariableWriterFunction
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.value_memory.SetValueMemoryFunction
;
public
enum
LargeModelFunctionEnum
{
set_long_memory
(
SetLongMemoryFunction
.
class
),
set_value_memory
(
SetValueMemoryFunction
.
class
),
memory_variable_writer
(
MemoryVariableWriterFunction
.
class
),
;
private
Class
<?
extends
AbstractLargeModelFunction
>
function
;
...
...
src/main/java/cn/com/poc/thirdparty/resource/demand/ai/function/memory_variable_writer/GetMemoryVariable.java
0 → 100644
View file @
876dd01a
package
cn
.
com
.
poc
.
thirdparty
.
resource
.
demand
.
ai
.
function
.
memory_variable_writer
;
import
cn.com.poc.common.service.RedisService
;
import
cn.com.poc.common.utils.BlContext
;
import
cn.com.poc.common.utils.SpringUtils
;
import
java.util.Map
;
public
class
GetMemoryVariable
{
public
static
Map
<
Object
,
Object
>
get
(
String
key
)
{
String
redisKey
=
MemoryVariableWriterConstants
.
REDIS_PREFIX
+
key
+
":"
+
BlContext
.
getCurrentUserNotException
().
getUserId
().
toString
();
RedisService
redisService
=
SpringUtils
.
getBean
(
RedisService
.
class
);
if
(!
redisService
.
hasKey
(
redisKey
))
{
return
null
;
}
Map
<
Object
,
Object
>
result
=
redisService
.
hmget
(
redisKey
);
return
result
;
}
}
src/main/java/cn/com/poc/thirdparty/resource/demand/ai/function/memory_variable_writer/MemoryVariableWriterConstants.java
0 → 100644
View file @
876dd01a
package
cn
.
com
.
poc
.
thirdparty
.
resource
.
demand
.
ai
.
function
.
memory_variable_writer
;
public
interface
MemoryVariableWriterConstants
{
String
REDIS_PREFIX
=
"AGENT_APP_FUNCTION:MEMORY_VARIABLE:"
;
}
src/main/java/cn/com/poc/thirdparty/resource/demand/ai/function/memory_variable_writer/MemoryVariableWriterFunction.java
0 → 100644
View file @
876dd01a
package
cn
.
com
.
poc
.
thirdparty
.
resource
.
demand
.
ai
.
function
.
memory_variable_writer
;
import
cn.com.poc.agent_application.entity.Variable
;
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.StringUtils
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.AbstractLargeModelFunction
;
import
cn.com.yict.framemax.core.i18n.I18nMessageException
;
import
cn.hutool.json.JSONArray
;
import
cn.hutool.json.JSONException
;
import
cn.hutool.json.JSONObject
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
@Service
public
class
MemoryVariableWriterFunction
extends
AbstractLargeModelFunction
{
private
final
String
DESC
=
"该方法仅用enum给定的内容名来保存用户想记录的内容值,不可使用该方法进行查询"
;
private
final
String
LLM_JSON_SCHEMA
=
"{\n"
+
" \"type\": \"function\",\n"
+
" \"function\": {\n"
+
" \"name\": \"memory_variable_writer\",\n"
+
" \"parameters\": {\n"
+
" \"type\": \"array\",\n"
+
" \"properties\": {\n"
+
" \"key\": {\n"
+
" \"type\": \"string\",\n"
+
" \"enum\": ${variableStructure},\n"
+
" \"description\": \"内容键\"\n"
+
" },\n"
+
" \"value\": {\n"
+
" \"type\": \"string\",\n"
+
" \"description\": \"内容值\"\n"
+
" }\n"
+
" }\n"
+
" }\n"
+
" },\n"
+
" \"description\": \""
+
DESC
+
"\"\n"
+
" }"
;
private
final
Long
expireTime
=
30
*
60
*
24L
;
// 30天有效期
@Resource
private
RedisService
redisService
;
@Override
public
String
doFunction
(
String
content
,
String
identifier
)
{
if
(
StringUtils
.
isBlank
(
content
)
||
StringUtils
.
isBlank
(
identifier
))
{
return
"FAIL"
;
}
String
contentKey
=
MemoryVariableWriterConstants
.
REDIS_PREFIX
+
identifier
+
":"
+
BlContext
.
getCurrentUserNotException
().
getUserId
().
toString
();
// 创建 JSONObject 对象
Map
<
String
,
Object
>
result
=
new
HashMap
<>();
if
(
redisService
.
hasKey
(
contentKey
))
{
Map
<
Object
,
Object
>
hmget
=
redisService
.
hmget
(
contentKey
);
for
(
Map
.
Entry
<
Object
,
Object
>
entry
:
hmget
.
entrySet
())
{
if
(
entry
.
getKey
()
instanceof
String
)
{
String
tempKey
=
(
String
)
entry
.
getKey
();
result
.
put
(
tempKey
,
entry
.
getValue
());
}
}
}
if
(
isJsonArray
(
content
))
{
JSONArray
jsonArray
=
new
JSONArray
(
content
);
for
(
int
i
=
0
;
i
<
jsonArray
.
size
();
i
++)
{
setMap
(
jsonArray
.
getJSONObject
(
i
),
result
);
}
}
else
{
setMap
(
new
JSONObject
(
content
),
result
);
}
redisService
.
hmset
(
contentKey
,
result
,
expireTime
);
return
"SUCCESS"
;
}
private
static
void
setMap
(
JSONObject
jsonObject
,
Map
<
String
,
Object
>
result
)
{
String
key
=
jsonObject
.
getStr
(
"key"
);
String
value
=
jsonObject
.
getStr
(
"value"
);
result
.
put
(
key
,
value
);
}
@Override
public
String
getDesc
()
{
return
DESC
;
}
@Override
public
List
<
String
>
getLLMConfig
()
{
throw
new
I18nMessageException
(
"exception/this.method.is.not.supported"
);
}
@Override
public
List
<
String
>
getVariableStructureLLMConfig
(
List
<
Variable
>
variableStructure
)
{
List
<
String
>
enumList
=
new
ArrayList
<>();
for
(
Variable
variable
:
variableStructure
)
{
enumList
.
add
(
variable
.
getKey
());
}
String
enums
=
JsonUtils
.
serialize
(
enumList
);
String
configStr
=
LLM_JSON_SCHEMA
.
replace
(
"${variableStructure}"
,
enums
);
List
<
String
>
resultList
=
new
ArrayList
<>();
resultList
.
add
(
configStr
);
return
resultList
;
}
private
boolean
isJsonArray
(
String
json
)
{
try
{
new
JSONArray
(
json
);
return
true
;
}
catch
(
JSONException
e
)
{
return
false
;
}
}
}
src/test/java/cn/com/poc/AgentApplicationInfoTest.java
View file @
876dd01a
...
...
@@ -7,20 +7,23 @@ import cn.com.poc.common.utils.BlContext;
import
cn.com.poc.expose.aggregate.AgentApplicationService
;
import
cn.com.poc.thirdparty.resource.demand.ai.aggregate.DemandKnowledgeService
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.SetLongMemoryConstants
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.value_memory.GetValueMemory
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.value_memory.SetValueMemoryConstants
;
import
cn.com.yict.framemax.core.spring.SingleContextInitializer
;
import
cn.com.yict.framemax.security.oauth.support.UsernameOauthTokenAuthenticationToken
;
import
com.google.common.collect.Lists
;
import
org.apache.commons.collections4.ListUtils
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.security.core.context.SecurityContext
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.security.core.context.SecurityContextImpl
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
javax.annotation.Resource
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.*
;
import
java.util.stream.Collectors
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
...
...
@@ -33,14 +36,14 @@ public class AgentApplicationInfoTest {
private
AgentApplicationInfoService
applicationInfoService
;
@Resource
private
DemandKnowledgeService
demandKnowledge
Service
;
private
RedisService
redis
Service
;
@Resource
private
RedisService
redis
Service
;
private
AgentApplicationService
agentApplication
Service
;
@Test
public
void
del
(){
String
contentKey
=
SetValueMemoryConstants
.
REDIS_PREFIX
;
public
void
del
()
{
String
contentKey
=
SetValueMemoryConstants
.
REDIS_PREFIX
;
redisService
.
del
(
contentKey
);
}
...
...
@@ -64,17 +67,27 @@ public class AgentApplicationInfoTest {
@Test
public
void
test
()
{
List
<
Object
>
list
=
Lists
.
newArrayList
(
"1"
,
"2"
,
"3"
,
"4"
,
"5"
,
"6"
,
"7"
,
"8"
,
"9"
,
"1"
);
redisService
.
lSet
(
"key"
,
list
);
redisService
.
lSet
(
"key"
,
list
);
// redisService.sSet("key",1);
}
@Resource
private
AgentApplicationService
agentApplicationService
;
@Test
public
void
test2
()
throws
InterruptedException
{
public
void
createRecommendQuestion
()
throws
InterruptedException
{
agentApplicationService
.
createRecommendQuestion
();
}
@Test
public
void
getValueMemory
()
{
String
agentId
=
"d56d9a6a90ed435d831f84bc1af50547"
;
String
contentKey
=
SetValueMemoryConstants
.
REDIS_PREFIX
+
agentId
+
":"
+
"204"
;
System
.
out
.
println
(
redisService
.
hmget
(
contentKey
));
// Set<Object> keySet = result.keySet();
// for (Object key : keySet) {
// Object value = result.get(key);
// System.out.println("key:" + key + ",value:" + value);
// }
}
}
src/test/java/cn/com/poc/demand/AiDialogueTest.java
View file @
876dd01a
package
cn
.
com
.
poc
.
demand
;
import
cn.com.poc.agent_application.entity.Variable
;
import
cn.com.poc.thirdparty.resource.demand.ai.aggregate.AIDialogueService
;
import
cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Function
;
import
cn.com.poc.thirdparty.resource.demand.ai.entity.function.FunctionCallResponse
;
import
cn.com.poc.thirdparty.resource.demand.ai.entity.function.FunctionCallResult
;
import
cn.com.poc.thirdparty.resource.demand.ai.function.memory_variable_writer.MemoryVariableWriterFunction
;
import
cn.com.yict.framemax.core.spring.SingleContextInitializer
;
import
cn.hutool.json.JSONArray
;
import
cn.hutool.json.JSONException
;
import
com.alibaba.fastjson.JSONObject
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.test.context.ContextConfiguration
;
...
...
@@ -59,4 +64,46 @@ public class AiDialogueTest {
System
.
out
.
println
(
functionCallResult
);
}
@Resource
private
MemoryVariableWriterFunction
memoryVariableWriterFunction
;
@Test
public
void
getMemoryVariableWriterFunctionConfig
()
{
Variable
name
=
new
Variable
();
name
.
setKey
(
"name"
);
name
.
setVariableDefault
(
""
);
Variable
age
=
new
Variable
();
age
.
setKey
(
"age"
);
age
.
setVariableDefault
(
""
);
List
<
Variable
>
variableStructure
=
new
ArrayList
<>();
variableStructure
.
add
(
name
);
variableStructure
.
add
(
age
);
List
<
String
>
variableStructureLLMConfig
=
memoryVariableWriterFunction
.
getVariableStructureLLMConfig
(
variableStructure
);
System
.
out
.
println
(
variableStructureLLMConfig
);
}
@Test
public
void
testJsonArray
()
{
String
json1
=
"[{\"key\": \"name\", \"value\": \"roger\"}, {\"key\": \"age\", \"value\": 12}]"
;
String
json2
=
"{\"key\": \"name\", \"value\": \"roger\"}"
;
System
.
out
.
println
(
isJsonArray
(
json1
));
System
.
out
.
println
(
isJsonArray
(
json2
));
}
public
static
boolean
isJsonArray
(
String
json
)
{
try
{
new
JSONArray
(
json
);
return
true
;
}
catch
(
JSONException
e
)
{
return
false
;
}
}
}
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