Skip to content

Commit 131beec

Browse files
committed
Introduce TemplateRenderer for prompt templating
- Introduce new TemplateRenderer API providing the logic for rendering an input template. - Update the PromptTemplate API to accept a TemplateRenderer object at construction time. - Move ST logic to StTemplateRenderer implementation, used by default in PromptTemplate. Additionally, make start and end delimiter character configurable. Relates to gh-2655 Signed-off-by: Thomas Vitale <[email protected]>
1 parent 687dea5 commit 131beec

File tree

10 files changed

+929
-150
lines changed

10 files changed

+929
-150
lines changed

spring-ai-client-chat/src/test/java/org/springframework/ai/prompt/PromptTemplateTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void testRenderWithList() {
118118

119119
PromptTemplate unfilledPromptTemplate = new PromptTemplate(templateString);
120120
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(unfilledPromptTemplate::render)
121-
.withMessage("Not all template variables were replaced. Missing variable names are [items]");
121+
.withMessage("Not all variables were replaced in the template. Missing variable names are: [items].");
122122
}
123123

124124
@Test

spring-ai-client-chat/src/test/java/org/springframework/ai/prompt/PromptTests.java

+1-40
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.HashMap;
2020
import java.util.Map;
21-
import java.util.Set;
2221

2322
import org.assertj.core.api.Assertions;
2423
import org.junit.jupiter.api.Test;
@@ -45,7 +44,7 @@ void newApiPlaygroundTests() {
4544
// Try to render with missing value for template variable, expect exception
4645
Assertions.assertThatThrownBy(() -> pt.render(model))
4746
.isInstanceOf(IllegalStateException.class)
48-
.hasMessage("Not all template variables were replaced. Missing variable names are [lastName]");
47+
.hasMessage("Not all variables were replaced in the template. Missing variable names are: [lastName].");
4948

5049
pt.add("lastName", "Park"); // TODO investigate partial
5150
String promptString = pt.render(model);
@@ -93,44 +92,6 @@ void newApiPlaygroundTests() {
9392

9493
}
9594

96-
@Test
97-
void testSingleInputVariable() {
98-
String template = "This is a {foo} test";
99-
PromptTemplate promptTemplate = new PromptTemplate(template);
100-
Set<String> inputVariables = promptTemplate.getInputVariables();
101-
assertThat(inputVariables).isNotEmpty();
102-
assertThat(inputVariables).hasSize(1);
103-
assertThat(inputVariables).contains("foo");
104-
}
105-
106-
@Test
107-
void testMultipleInputVariables() {
108-
String template = "This {bar} is a {foo} test";
109-
PromptTemplate promptTemplate = new PromptTemplate(template);
110-
Set<String> inputVariables = promptTemplate.getInputVariables();
111-
assertThat(inputVariables).isNotEmpty();
112-
assertThat(inputVariables).hasSize(2);
113-
assertThat(inputVariables).contains("foo", "bar");
114-
}
115-
116-
@Test
117-
void testMultipleInputVariablesWithRepeats() {
118-
String template = "This {bar} is a {foo} test {foo}.";
119-
PromptTemplate promptTemplate = new PromptTemplate(template);
120-
Set<String> inputVariables = promptTemplate.getInputVariables();
121-
assertThat(inputVariables).isNotEmpty();
122-
assertThat(inputVariables).hasSize(2);
123-
assertThat(inputVariables).contains("foo", "bar");
124-
}
125-
126-
@Test
127-
void testBadFormatOfTemplateString() {
128-
String template = "This is a {foo test";
129-
Assertions.assertThatThrownBy(() -> new PromptTemplate(template))
130-
.isInstanceOf(IllegalArgumentException.class)
131-
.hasMessage("The template string is not valid.");
132-
}
133-
13495
@Test
13596
public void testPromptCopy() {
13697
String template = "Hello, {name}! Your age is {age}.";

0 commit comments

Comments
 (0)