From 8424b4153c82fae78fb6cd590fd464b1dd16a9b4 Mon Sep 17 00:00:00 2001 From: Fu Cheng Date: Thu, 9 May 2024 16:16:09 +0800 Subject: [PATCH] refactor planner properties --- .../chatagent/ChatAgentAutoConfiguration.java | 31 ++++++++- .../chatagent/ChatAgentProperties.java | 63 +++++++++++++++++-- 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/spring/spring-boot-autoconfigure/src/main/java/io/github/llmagentbuilder/spring/autoconfigure/chatagent/ChatAgentAutoConfiguration.java b/spring/spring-boot-autoconfigure/src/main/java/io/github/llmagentbuilder/spring/autoconfigure/chatagent/ChatAgentAutoConfiguration.java index 4bd7fcb..f32cec1 100644 --- a/spring/spring-boot-autoconfigure/src/main/java/io/github/llmagentbuilder/spring/autoconfigure/chatagent/ChatAgentAutoConfiguration.java +++ b/spring/spring-boot-autoconfigure/src/main/java/io/github/llmagentbuilder/spring/autoconfigure/chatagent/ChatAgentAutoConfiguration.java @@ -8,6 +8,7 @@ import io.github.llmagentbuilder.core.chatmemory.ChatMemoryStore; import io.github.llmagentbuilder.core.chatmemory.InMemoryChatMemoryStore; import io.github.llmagentbuilder.core.planner.reactjson.ReActJsonPlannerFactory; +import io.github.llmagentbuilder.core.planner.simple.SimplePlannerFactory; import io.github.llmagentbuilder.core.tool.AgentToolFunctionCallbackContext; import io.github.llmagentbuilder.core.tool.AgentToolsProvider; import io.github.llmagentbuilder.core.tool.AgentToolsProviderFactory; @@ -66,8 +67,11 @@ public ChatMemoryStore chatMemoryStore() { } @Bean + @Primary + @ConditionalOnProperty(prefix = ChatAgentProperties.CONFIG_PREFIX + + ".planner.reActJson", name = "enabled", matchIfMissing = true) @ConditionalOnMissingBean - public Planner planner(ChatClient chatClient, + public Planner reActJsonPlanner(ChatClient chatClient, ChatOptions chatOptions, Optional chatMemoryStore, AgentToolsProvider agentToolsProvider, @@ -77,7 +81,30 @@ public Planner planner(ChatClient chatClient, chatClient, chatOptions, agentToolsProvider, - properties.getReActJson().getSystemInstructions(), + properties.getPlanner().getSystemInstructions(), + chatMemoryStore.orElse(null), + properties.tracingEnabled() ? observationRegistry.orElse(null) + : null, + properties.metricsEnabled() ? meterRegistry.orElse(null) + : null + ); + } + + @Bean + @ConditionalOnProperty(prefix = ChatAgentProperties.CONFIG_PREFIX + + ".planner.simple", name = "enabled") + @ConditionalOnMissingBean + public Planner simplePlanner(ChatClient chatClient, + ChatOptions chatOptions, + Optional chatMemoryStore, + AgentToolsProvider agentToolsProvider, + Optional observationRegistry, + Optional meterRegistry) { + return SimplePlannerFactory.INSTANCE.create( + chatClient, + chatOptions, + agentToolsProvider, + properties.getPlanner().getSystemInstructions(), chatMemoryStore.orElse(null), properties.tracingEnabled() ? observationRegistry.orElse(null) : null, diff --git a/spring/spring-boot-autoconfigure/src/main/java/io/github/llmagentbuilder/spring/autoconfigure/chatagent/ChatAgentProperties.java b/spring/spring-boot-autoconfigure/src/main/java/io/github/llmagentbuilder/spring/autoconfigure/chatagent/ChatAgentProperties.java index 159357f..9a7db65 100644 --- a/spring/spring-boot-autoconfigure/src/main/java/io/github/llmagentbuilder/spring/autoconfigure/chatagent/ChatAgentProperties.java +++ b/spring/spring-boot-autoconfigure/src/main/java/io/github/llmagentbuilder/spring/autoconfigure/chatagent/ChatAgentProperties.java @@ -20,7 +20,7 @@ public class ChatAgentProperties { private String usageInstruction = "Ask me anything"; @NestedConfigurationProperty - private ReActJson reActJson = new ReActJson(); + private Planner planner = new Planner(); @NestedConfigurationProperty private Memory memory = new Memory(); @@ -74,12 +74,13 @@ public void setUsageInstruction(String usageInstruction) { this.usageInstruction = usageInstruction; } - public ReActJson getReActJson() { - return reActJson; + public Planner getPlanner() { + return planner; } - public void setReActJson(ReActJson reActJson) { - this.reActJson = reActJson; + public void setPlanner( + Planner planner) { + this.planner = planner; } public Memory getMemory() { @@ -126,10 +127,16 @@ public boolean metricsEnabled() { return metrics == null || metrics.isEnabled(); } - public static class ReActJson { + public static class Planner { private String systemInstructions; + @NestedConfigurationProperty + private ReActJson reActJson = new ReActJson(); + + @NestedConfigurationProperty + private Simple simple = new Simple(); + public String getSystemInstructions() { return systemInstructions; } @@ -137,6 +144,50 @@ public String getSystemInstructions() { public void setSystemInstructions(String systemInstructions) { this.systemInstructions = systemInstructions; } + + public ReActJson getReActJson() { + return reActJson; + } + + public void setReActJson(ReActJson reActJson) { + this.reActJson = reActJson; + } + + public Simple getSimple() { + return simple; + } + + public void setSimple( + Simple simple) { + this.simple = simple; + } + } + + public static class ReActJson { + + private boolean enabled = true; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + } + + public static class Simple { + + private boolean enabled = false; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } } public static class Memory {