Skip to content

Commit 0d276cb

Browse files
authored
fix(agent): replace systemprompt (#4067)
1 parent 5aba000 commit 0d276cb

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/ReactAgent.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,10 @@ public void setInstruction(String instruction) {
796796
llmNode.setInstruction(instruction);
797797
}
798798

799+
public void setSystemPrompt(String systemPrompt) {
800+
llmNode.setSystemPrompt(systemPrompt);
801+
}
802+
799803
public Map<String, Object> getThreadState(String threadId) {
800804
return threadIdStateMap.get(threadId);
801805
}

spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/node/AgentLlmNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ public void setInstruction(String instruction) {
125125
this.instruction = instruction;
126126
}
127127

128+
public void setSystemPrompt(String systemPrompt) {
129+
this.systemPrompt = systemPrompt;
130+
}
131+
128132
@Override
129133
public Map<String, Object> apply(OverAllState state, RunnableConfig config) throws Exception {
130134
if (enableReasoningLog && logger.isDebugEnabled()) {

spring-ai-alibaba-agent-framework/src/test/java/com/alibaba/cloud/ai/graph/agent/ReactAgentTest.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,5 +969,45 @@ public void testInstructionSentToLLM() throws Exception {
969969
assertTrue(response.getText().length() > 0, "响应应该有内容");
970970
}
971971

972-
}
973972

973+
@Test
974+
public void testDynamicSystemPromptUpdate() throws Exception {
975+
String initialSystemPrompt = "你是一个专业的技术助手,回答要简洁明了。";
976+
String updatedSystemPrompt = "你是一个诗歌创作专家,用优美的语言回答问题。";
977+
String finalSystemPrompt = "你是一个数学专家,用精确的数字回答问题。";
978+
979+
ReactAgent agent = ReactAgent.builder()
980+
.name("dynamic_system_prompt_agent")
981+
.model(chatModel)
982+
.systemPrompt(initialSystemPrompt)
983+
.saver(new MemorySaver())
984+
.enableLogging(true)
985+
.build();
986+
987+
assertNotNull(agent, "Agent 不应为空");
988+
989+
AssistantMessage response1 = agent.call("什么是 Java?");
990+
assertNotNull(response1, "第一次响应不应为空");
991+
assertFalse(response1.getText().isEmpty(), "第一次响应不应为空字符串");
992+
System.out.println(response1.getText());
993+
994+
agent.setSystemPrompt(updatedSystemPrompt);
995+
996+
AssistantMessage response2 = agent.call("什么是 Spring?");
997+
assertNotNull(response2, "第二次响应不应为空");
998+
assertFalse(response2.getText().isEmpty(), "第二次响应不应为空字符串");
999+
System.out.println(response2.getText());
1000+
1001+
agent.setSystemPrompt(finalSystemPrompt);
1002+
1003+
AssistantMessage response3 = agent.call("1+1等于多少?");
1004+
assertNotNull(response3, "第三次响应不应为空");
1005+
assertFalse(response3.getText().isEmpty(), "第三次响应不应为空字符串");
1006+
System.out.println(response3.getText());
1007+
1008+
assertTrue(response1.getText().length() > 0, "第一次响应应该有内容");
1009+
assertTrue(response2.getText().length() > 0, "第二次响应应该有内容");
1010+
assertTrue(response3.getText().length() > 0, "第三次响应应该有内容");
1011+
}
1012+
1013+
}

spring-boot-starters/spring-ai-alibaba-starter-config-nacos/src/main/java/com/alibaba/cloud/ai/agent/nacos/NacosReactAgentBuilder.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public ReactAgent build() {
7272
//2.load prompt vo
7373
PromptVO promptVO = getPromptByKey(nacosOptions, agentVO.getPromptKey());
7474
agentVOHolder.setPromptVO(promptVO);
75-
this.instruction = promptVO.getTemplate();
75+
String systemPrompt = promptVO.getTemplate();
7676

7777
//3.load model vo
7878
ModelVO modelVO = NacosModelInjector.getModelByAgentName(nacosOptions);
@@ -96,7 +96,6 @@ public ReactAgent build() {
9696
}
9797

9898
clientBuilder.defaultOptions(chatOptions);
99-
clientBuilder.defaultSystem(instruction);
10099
chatClient = clientBuilder.build();
101100

102101
//6.load mcp servers
@@ -106,7 +105,9 @@ public ReactAgent build() {
106105
this.tools = convert(nacosOptions, mcpServersVO);
107106

108107
//7. build tools
109-
AgentLlmNode.Builder llmNodeBuilder = AgentLlmNode.builder().chatClient(chatClient);
108+
AgentLlmNode.Builder llmNodeBuilder = AgentLlmNode.builder()
109+
.chatClient(chatClient)
110+
.systemPrompt(systemPrompt);
110111
if (outputKey != null && !outputKey.isEmpty()) {
111112
llmNodeBuilder.outputKey(outputKey);
112113
}
@@ -304,7 +305,7 @@ public void receiveConfigInfo(String configInfo) {
304305
if (promptVO != null && promptVO.getTemplate() != null) {
305306
nacosContextHolder.getObservationMetadataAwareOptions().getObservationMetadata()
306307
.putAll(getMetadata(promptVO));
307-
reactAgent.setInstruction(promptVO.getTemplate());
308+
reactAgent.setSystemPrompt(promptVO.getTemplate());
308309
}
309310

310311
}
@@ -341,7 +342,7 @@ public void receiveConfigInfo(String configInfo) {
341342
PromptVO newPromptVO = getPromptByKey(nacosOptions, newPromptKey);
342343
nacosContextHolder.getObservationMetadataAwareOptions().getObservationMetadata()
343344
.putAll(getMetadata(newPromptVO));
344-
nacosContextHolder.getReactAgent().setInstruction(newPromptVO.getTemplate());
345+
nacosContextHolder.getReactAgent().setSystemPrompt(newPromptVO.getTemplate());
345346

346347
NacosReactAgentBuilder.registerPromptListener(nacosOptions, nacosContextHolder, newPromptKey, nacosContextHolder.getReactAgent());
347348
}

0 commit comments

Comments
 (0)