-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Introduce TemplateRenderer for prompt templating #2780
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
Merged
markpollack
merged 1 commit into
spring-projects:main
from
ThomasVitale:prompt-template-strategy-part-1
Apr 24, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
spring-ai-commons/src/main/java/org/springframework/ai/template/NoOpTemplateRenderer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2023-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.ai.template; | ||
|
||
import java.util.Map; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* No-op implementation of {@link TemplateRenderer} that returns the template unchanged. | ||
* | ||
* @author Thomas Vitale | ||
* @since 1.0.0 | ||
*/ | ||
public class NoOpTemplateRenderer implements TemplateRenderer { | ||
|
||
@Override | ||
public String apply(String template, Map<String, Object> variables) { | ||
Assert.hasText(template, "template cannot be null or empty"); | ||
Assert.notNull(variables, "variables cannot be null"); | ||
Assert.noNullElements(variables.keySet(), "variables keys cannot be null"); | ||
return template; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
spring-ai-commons/src/main/java/org/springframework/ai/template/TemplateRenderer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2023-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.ai.template; | ||
|
||
import java.util.Map; | ||
import java.util.function.BiFunction; | ||
|
||
/** | ||
* Renders a template using a given strategy. | ||
* | ||
* @author Thomas Vitale | ||
* @since 1.0.0 | ||
*/ | ||
public interface TemplateRenderer extends BiFunction<String, Map<String, Object>, String> { | ||
|
||
@Override | ||
String apply(String template, Map<String, Object> variables); | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
spring-ai-commons/src/main/java/org/springframework/ai/template/ValidationMode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2023-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.ai.template; | ||
|
||
/** | ||
* Validation modes for template renderers. | ||
* | ||
* @author Thomas Vitale | ||
* @since 1.0.0 | ||
*/ | ||
public enum ValidationMode { | ||
|
||
/** | ||
* If the validation fails, an exception is thrown. This is the default mode. | ||
*/ | ||
THROW, | ||
|
||
/** | ||
* If the validation fails, a warning is logged. The template is rendered with the | ||
* missing placeholders/variables. This mode is not recommended for production use. | ||
*/ | ||
WARN, | ||
|
||
/** | ||
* No validation is performed. | ||
*/ | ||
NONE; | ||
|
||
} |
117 changes: 117 additions & 0 deletions
117
...g-ai-commons/src/test/java/org/springframework/ai/template/NoOpTemplateRendererTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright 2023-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.ai.template; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Unit tests for {@link NoOpTemplateRenderer}. | ||
* | ||
* @author Thomas Vitale | ||
*/ | ||
class NoOpTemplateRendererTests { | ||
|
||
@Test | ||
void shouldReturnUnchangedTemplate() { | ||
NoOpTemplateRenderer renderer = new NoOpTemplateRenderer(); | ||
Map<String, Object> variables = new HashMap<>(); | ||
variables.put("name", "Spring AI"); | ||
|
||
String result = renderer.apply("Hello {name}!", variables); | ||
|
||
assertThat(result).isEqualTo("Hello {name}!"); | ||
} | ||
|
||
@Test | ||
void shouldReturnUnchangedTemplateWithMultipleVariables() { | ||
NoOpTemplateRenderer renderer = new NoOpTemplateRenderer(); | ||
Map<String, Object> variables = new HashMap<>(); | ||
variables.put("greeting", "Hello"); | ||
variables.put("name", "Spring AI"); | ||
variables.put("punctuation", "!"); | ||
|
||
String result = renderer.apply("{greeting} {name}{punctuation}", variables); | ||
|
||
assertThat(result).isEqualTo("{greeting} {name}{punctuation}"); | ||
} | ||
|
||
@Test | ||
void shouldNotAcceptEmptyTemplate() { | ||
NoOpTemplateRenderer renderer = new NoOpTemplateRenderer(); | ||
Map<String, Object> variables = new HashMap<>(); | ||
|
||
assertThatThrownBy(() -> renderer.apply("", variables)).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("template cannot be null or empty"); | ||
} | ||
|
||
@Test | ||
void shouldNotAcceptNullTemplate() { | ||
NoOpTemplateRenderer renderer = new NoOpTemplateRenderer(); | ||
Map<String, Object> variables = new HashMap<>(); | ||
|
||
assertThatThrownBy(() -> renderer.apply(null, variables)).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("template cannot be null or empty"); | ||
} | ||
|
||
@Test | ||
void shouldNotAcceptNullVariables() { | ||
NoOpTemplateRenderer renderer = new NoOpTemplateRenderer(); | ||
String template = "Hello!"; | ||
|
||
assertThatThrownBy(() -> renderer.apply(template, null)).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("variables cannot be null"); | ||
} | ||
|
||
@Test | ||
void shouldNotAcceptVariablesWithNullKeySet() { | ||
NoOpTemplateRenderer renderer = new NoOpTemplateRenderer(); | ||
String template = "Hello!"; | ||
Map<String, Object> variables = new HashMap<String, Object>(); | ||
variables.put(null, "Spring AI"); | ||
|
||
assertThatThrownBy(() -> renderer.apply(template, variables)).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("variables keys cannot be null"); | ||
} | ||
|
||
@Test | ||
void shouldReturnUnchangedComplexTemplate() { | ||
NoOpTemplateRenderer renderer = new NoOpTemplateRenderer(); | ||
Map<String, Object> variables = new HashMap<>(); | ||
variables.put("header", "Welcome"); | ||
variables.put("user", "Spring AI"); | ||
variables.put("items", "one, two, three"); | ||
variables.put("footer", "Goodbye"); | ||
|
||
String template = """ | ||
{header} | ||
User: {user} | ||
Items: {items} | ||
{footer} | ||
"""; | ||
|
||
String result = renderer.apply(template, variables); | ||
|
||
assertThat(result).isEqualToNormalizingNewlines(template); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ST specific templating logic is now tested in StTemplateRendererTests.