Skip to content

Use default stringtemplate delimiters for prompt templates #444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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 @@ -53,7 +53,7 @@ public PromptTemplate(Resource resource) {
throw new RuntimeException("Failed to read resource", ex);
}
try {
this.st = new ST(this.template, '{', '}');
this.st = new ST(this.template);
}
catch (Exception ex) {
throw new IllegalArgumentException("The template string is not valid.", ex);
Expand All @@ -64,7 +64,7 @@ public PromptTemplate(String template) {
this.template = template;
// If the template string is not valid, an exception will be thrown
try {
this.st = new ST(this.template, '{', '}');
this.st = new ST(this.template);
}
catch (Exception ex) {
throw new IllegalArgumentException("The template string is not valid.", ex);
Expand All @@ -75,7 +75,7 @@ public PromptTemplate(String template, Map<String, Object> model) {
this.template = template;
// If the template string is not valid, an exception will be thrown
try {
this.st = new ST(this.template, '{', '}');
this.st = new ST(this.template);
for (Entry<String, Object> entry : model.entrySet()) {
add(entry.getKey(), entry.getValue());
dynamicModel.put(entry.getKey(), entry.getValue());
Expand All @@ -95,7 +95,7 @@ public PromptTemplate(Resource resource, Map<String, Object> model) {
}
// If the template string is not valid, an exception will be thrown
try {
this.st = new ST(this.template, '{', '}');
this.st = new ST(this.template);
for (Entry<String, Object> entry : model.entrySet()) {
add(entry.getKey(), entry.getValue());
dynamicModel.put(entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void testRender() {
model.put("key3", 100);

// Create a simple template with placeholders for keys in the generative
String template = "This is a {key1}, it is {key2}, and it costs {key3}";
String template = "This is a <key1>, it is <key2>, and it costs <key3>";
PromptTemplate promptTemplate = new PromptTemplate(template, model);

// The expected result after rendering the template with the generative
Expand All @@ -57,6 +57,33 @@ public void testRender() {
result = promptTemplate.render(model);
assertEquals(expected, result);
}
@Test
public void testRenderJson() {
Map<String, Object> model = new HashMap<>();
model.put("jsonValue", "bar");

String template = "{'foo': '<jsonValue>'}";
PromptTemplate promptTemplate = new PromptTemplate(template, model);

String expected = "{'foo': 'bar'}";
String result = promptTemplate.render();

assertEquals(expected, result);
}

@Test
public void testRenderEscapedString() {
Map<String, Object> model = new HashMap<>();
model.put("xmlValue", "bar");

String template = "\\<foo><xmlValue>\\</foo>";
PromptTemplate promptTemplate = new PromptTemplate(template, model);

String expected = "<foo>bar</foo>";
String result = promptTemplate.render();

assertEquals(expected, result);
}

@Disabled("Need to improve PromptTemplate to better handle Resource toString and tracking with 'dynamicModel' for underlying StringTemplate")
@Test
Expand All @@ -74,7 +101,7 @@ public void testRenderResource() throws Exception {
model.put("key3", resource);

// Create a simple template with placeholders for keys in the generative
String template = "{key1}, {key2}, {key3}";
String template = "<key1>, <key2>, <key3>";
PromptTemplate promptTemplate = new PromptTemplate(template, model);

// The expected result after rendering the template with the generative
Expand All @@ -93,7 +120,7 @@ public void testRenderFailure() {
model.put("key1", "value1");

// Create a simple template that includes a key not present in the generative
String template = "This is a {key2}!";
String template = "This is a <key2>!";
PromptTemplate promptTemplate = new PromptTemplate(template, model);

// Rendering the template with a missing key should throw an exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PromptTests {
@Test
void newApiPlaygroundTests() {
// Create a String, a PromptValue or Messages
String templateText = "Hello '{firstName}' '{lastName}' from Unix";
String templateText = "Hello '<firstName>' '<lastName>' from Unix";
PromptTemplate pt = new PromptTemplate(templateText);

final Map<String, Object> model = new HashMap<>();
Expand Down Expand Up @@ -94,7 +94,7 @@ void newApiPlaygroundTests() {

@Test
void testSingleInputVariable() {
String template = "This is a {foo} test";
String template = "This is a <foo> test";
PromptTemplate promptTemplate = new PromptTemplate(template);
Set<String> inputVariables = promptTemplate.getInputVariables();
assertThat(inputVariables).isNotEmpty();
Expand All @@ -104,7 +104,7 @@ void testSingleInputVariable() {

@Test
void testMultipleInputVariables() {
String template = "This {bar} is a {foo} test";
String template = "This <bar> is a <foo> test";
PromptTemplate promptTemplate = new PromptTemplate(template);
Set<String> inputVariables = promptTemplate.getInputVariables();
assertThat(inputVariables).isNotEmpty();
Expand All @@ -114,7 +114,7 @@ void testMultipleInputVariables() {

@Test
void testMultipleInputVariablesWithRepeats() {
String template = "This {bar} is a {foo} test {foo}.";
String template = "This <bar> is a <foo> test <foo>.";
PromptTemplate promptTemplate = new PromptTemplate(template);
Set<String> inputVariables = promptTemplate.getInputVariables();
assertThat(inputVariables).isNotEmpty();
Expand All @@ -124,7 +124,7 @@ void testMultipleInputVariablesWithRepeats() {

@Test
void testBadFormatOfTemplateString() {
String template = "This is a {foo test";
String template = "This is a <foo test";
Assertions.assertThatThrownBy(() -> {
new PromptTemplate(template);
}).isInstanceOf(IllegalArgumentException.class).hasMessage("The template string is not valid.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ The method `Prompt create(Map<String, Object> model)`: Expands prompt creation c

== Example Usage

A simple example taken from the https://github.com/Azure-Samples/spring-ai-azure-workshop/blob/main/2-README-prompt-templating.md[AI Workshop on PromptTemplates] is shown below.
A simple example taken from the https://github.com/Azure-Samples/spring-ai-azure-workshop/blob/main/2-README-prompt-templating.md[AI Workshop on PromptTemplates] is shown below. Context variable will be delimited by `<` and `>`. The delimiters can be escaped with `\` as stated in the https://github.com/antlr/stringtemplate4/blob/master/doc/cheatsheet.md#stringtemplate-cheat-sheet[StringTemplate documentation].


```java

PromptTemplate promptTemplate = new PromptTemplate("Tell me a {adjective} joke about {topic}");
PromptTemplate promptTemplate = new PromptTemplate("Tell me a <adjective> joke about <topic>");

Prompt prompt = promptTemplate.create(Map.of("adjective", adjective, "topic", topic));

Expand All @@ -218,8 +218,8 @@ Message userMessage = new UserMessage(userText);

String systemText = """
You are a helpful AI assistant that helps people find information.
Your name is {name}
You should reply to the user's request with your name and also in the style of a {voice}.
Your name is <name>
You should reply to the user's request with your name and also in the style of a <voice>.
""";

SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemText);
Expand Down