Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ must not hide Nacos Config configuration or connection failures.

The regression tests cover the Prompt auto-configuration offline. They do not
verify connections to a live Nacos server.

### Dynamic prompt update logs

Queries for `OnPromptTemplateConfigChange` can still filter on `templateName`.
Use `result:applied` (INFO) or `result:failed` (WARN) instead of the former
`template` and `model` fields. Neither prompt text nor variable values are logged
by the factory. A failure identifies the exception type, without forwarding its
message or cause. Each result refers to one template: if a later update fails,
processing stops and earlier updates remain applied. An applied entry does not
indicate that the whole batch succeeded.
6 changes: 6 additions & 0 deletions prompt/argi-prompt-nacos/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,21 @@ protected void onConfigChange(List<ConfigurablePromptTemplateModel> configList)
if (!StringUtils.hasText(configuration.name()) || !StringUtils.hasText(configuration.template())) {
continue;
}
PromptTemplate.Builder promptTemplateBuilder = promptTemplateBuilderConfigure
.configure(PromptTemplate.builder()
.template(configuration.template())
.variables(configuration.model() == null ? new HashMap<>() : configuration.model()));

templates.put(configuration.name(),
new ConfigurablePromptTemplate(configuration.name(), promptTemplateBuilder.build()));

logger.info("OnPromptTemplateConfigChange,templateName:{},template:{},model:{}", configuration.name(),
configuration.template(), configuration.model());
String name = configuration.name();
try {
PromptTemplate.Builder promptTemplateBuilder = promptTemplateBuilderConfigure
.configure(PromptTemplate.builder()
.template(configuration.template())
.variables(configuration.model() == null ? new HashMap<>() : configuration.model()));
templates.put(name, new ConfigurablePromptTemplate(name, promptTemplateBuilder.build()));
}
catch (RuntimeException ex) {
// Builders and customizers can include prompt content in exception messages or causes.
String errorType = ex.getClass().getSimpleName();
logger.warn("OnPromptTemplateConfigChange,templateName:{},result:failed,errorType:{}", name, errorType);
throw new IllegalStateException("Failed to update prompt template " + name + " (" + errorType + ")");
}
logger.info("OnPromptTemplateConfigChange,templateName:{},result:applied", name);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.template.st.StTemplateRenderer;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

/**
* Test cases for ConfigurablePromptTemplateFactory. Tests template creation, retrieval,
Expand Down Expand Up @@ -183,6 +189,53 @@ void testOnConfigChangeWithInvalidConfigs() {
assertThat(factory.getTemplate("")).isNull();
}

@Test
void updateDiagnosticsDoNotExposeConfigOnSuccessOrPartialFailure() {
String body = "template-marker {value}";
var variables = Map.<String, Object>of("value", "variable-marker");
var configure = new PromptTemplateBuilderConfigure();
var count = new AtomicInteger();
configure.setPromptTemplateBuilderCustomizers(List.of(builder -> {
if (count.incrementAndGet() == 2) {
throw new IllegalArgumentException("exception-marker", new RuntimeException("cause-marker"));
}
}));
factory = new ConfigurablePromptTemplateFactory(configure);
var first = new ConfigurablePromptTemplateFactory.ConfigurablePromptTemplateModel("first", body, variables);
var second = new ConfigurablePromptTemplateFactory.ConfigurablePromptTemplateModel("second", body, variables);
var third = new ConfigurablePromptTemplateFactory.ConfigurablePromptTemplateModel("third", body, variables);

Logger logger = (Logger) LoggerFactory.getLogger(ConfigurablePromptTemplateFactory.class);
ListAppender<ILoggingEvent> logs = new ListAppender<>();
logs.start();
logger.addAppender(logs);
Throwable failure;
try {
failure = catchThrowable(() -> factory.onConfigChange(List.of(first, second, third)));
}
finally {
logger.detachAppender(logs);
logs.stop();
}
assertThat(logs.list).isNotEmpty().allSatisfy(event -> {
assertThat(event.getFormattedMessage() + Arrays.deepToString(event.getArgumentArray()))
.doesNotContain("template-marker", "variable-marker", "exception-marker", "cause-marker");
assertThat(event.getThrowableProxy()).isNull();
});
assertThat(logs.list).extracting(ILoggingEvent::getFormattedMessage)
.anySatisfy(line -> assertThat(line).contains("templateName:first,result:applied"))
.anySatisfy(line -> assertThat(line).contains("templateName:second,result:failed",
"IllegalArgumentException"));
assertThat(failure).isInstanceOf(IllegalStateException.class)
.hasMessageContaining("second")
.hasMessageContaining("IllegalArgumentException")
.hasMessageNotContaining("exception-marker")
.hasNoCause();
assertThat(factory.getTemplate("first").render()).isEqualTo("template-marker variable-marker");
assertThat(factory.getTemplate("second")).isNull();
assertThat(factory.getTemplate("third")).isNull();
}

@Test
void testTemplateReuse() {
// Test that same name returns same template instance
Expand Down
Loading