-
Notifications
You must be signed in to change notification settings - Fork 2k
Provides a Jinja2 implementation for TemplateRenderer #3259
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
Open
sunyuhan1998
wants to merge
3
commits into
spring-projects:main
Choose a base branch
from
sunyuhan1998:feat-template-jinja
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| ~ 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. | ||
| --> | ||
|
|
||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>org.springframework.ai</groupId> | ||
| <artifactId>spring-ai-parent</artifactId> | ||
| <version>1.1.0-SNAPSHOT</version> | ||
| </parent> | ||
| <artifactId>spring-ai-template-jinja</artifactId> | ||
| <packaging>jar</packaging> | ||
| <name>Spring AI Template Jinja</name> | ||
| <description>Jinja implementation for Spring AI templating</description> | ||
| <url>https://github.com/spring-projects/spring-ai</url> | ||
|
|
||
| <scm> | ||
| <url>https://github.com/spring-projects/spring-ai</url> | ||
| <connection>git://github.com/spring-projects/spring-ai.git</connection> | ||
| <developerConnection>[email protected]:spring-projects/spring-ai.git</developerConnection> | ||
| </scm> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>17</maven.compiler.source> | ||
| <maven.compiler.target>17</maven.compiler.target> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.springframework.ai</groupId> | ||
| <artifactId>spring-ai-commons</artifactId> | ||
| <version>${project.parent.version}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.hubspot.jinjava</groupId> | ||
| <artifactId>jinjava</artifactId> | ||
| <version>2.8.0</version> | ||
| </dependency> | ||
|
|
||
| <!-- Logging --> | ||
| <dependency> | ||
| <groupId>org.slf4j</groupId> | ||
| <artifactId>slf4j-api</artifactId> | ||
| </dependency> | ||
|
|
||
| <!-- test dependencies --> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-test</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> |
172 changes: 172 additions & 0 deletions
172
...late-jinja/src/main/java/org/springframework/ai/template/jinja/JinjaTemplateRenderer.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,172 @@ | ||
| /* | ||
| * 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.jinja; | ||
|
|
||
| import com.hubspot.jinjava.Jinjava; | ||
| import com.hubspot.jinjava.JinjavaConfig; | ||
| import com.hubspot.jinjava.tree.parse.ExpressionToken; | ||
| import com.hubspot.jinjava.tree.parse.Token; | ||
| import com.hubspot.jinjava.tree.parse.TokenScanner; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.ai.template.TemplateRenderer; | ||
| import org.springframework.ai.template.ValidationMode; | ||
| import org.springframework.util.Assert; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Renders a template using the Jin-java library. | ||
| * | ||
| * <p> | ||
| * This renderer allows customization of validation behavior. | ||
| * | ||
| * <p> | ||
| * Use the {@link #builder()} to create and configure instances. | ||
| * | ||
| * <p> | ||
| * <b>Thread safety:</b> This class is safe for concurrent use. Each call to | ||
| * {@link #apply(String, Map)} creates a new Jin-java instance, and no mutable state is | ||
| * shared between threads. | ||
| * | ||
| * @author Sun Yuhan | ||
| * @since 1.1.0 | ||
| */ | ||
| public class JinjaTemplateRenderer implements TemplateRenderer { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(JinjaTemplateRenderer.class); | ||
|
|
||
| private static final String VALIDATION_MESSAGE = "Not all variables were replaced in the template. Missing variable names are: %s."; | ||
|
|
||
| private static final ValidationMode DEFAULT_VALIDATION_MODE = ValidationMode.THROW; | ||
|
|
||
| private final ValidationMode validationMode; | ||
|
|
||
| /** | ||
| * Constructs a new {@code JinjaTemplateRenderer} with the specified validation mode. | ||
| * @param validationMode the mode to use for template variable validation; must not be | ||
| * null | ||
| */ | ||
| public JinjaTemplateRenderer(ValidationMode validationMode) { | ||
| Assert.notNull(validationMode, "validationMode cannot be null"); | ||
| this.validationMode = validationMode; | ||
| } | ||
|
|
||
| @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"); | ||
|
|
||
| if (this.validationMode != ValidationMode.NONE) { | ||
| validate(template, variables); | ||
| } | ||
| Jinjava jinjava = new Jinjava(); | ||
| String rendered; | ||
| try { | ||
| rendered = jinjava.render(template, variables); | ||
| } | ||
| catch (Exception ex) { | ||
| throw new IllegalArgumentException("The template string is not valid.", ex); | ||
| } | ||
| return rendered; | ||
| } | ||
|
|
||
| /** | ||
| * Validates that all required template variables are provided in the model. Returns | ||
| * the set of missing variables for further handling or logging. | ||
| * @param template the template to be rendered | ||
| * @param templateVariables the provided variables | ||
| * @return set of missing variable names, or empty set if none are missing | ||
| */ | ||
| private Set<String> validate(String template, Map<String, Object> templateVariables) { | ||
| Set<String> templateTokens = getInputVariables(template); | ||
| Set<String> modelKeys = templateVariables.keySet(); | ||
| Set<String> missingVariables = new HashSet<>(templateTokens); | ||
| missingVariables.removeAll(modelKeys); | ||
|
|
||
| if (!missingVariables.isEmpty()) { | ||
| if (this.validationMode == ValidationMode.WARN) { | ||
| logger.warn(VALIDATION_MESSAGE.formatted(missingVariables)); | ||
| } | ||
| else if (this.validationMode == ValidationMode.THROW) { | ||
| throw new IllegalStateException(VALIDATION_MESSAGE.formatted(missingVariables)); | ||
| } | ||
| } | ||
| return missingVariables; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieve all variables in the template | ||
| * @param template the template to be rendered | ||
| * @return set of variable names | ||
| */ | ||
| private Set<String> getInputVariables(String template) { | ||
| Set<String> variables = new HashSet<>(); | ||
| JinjavaConfig config = JinjavaConfig.newBuilder().build(); | ||
| TokenScanner scanner = new TokenScanner(template, config); | ||
|
|
||
| while (scanner.hasNext()) { | ||
| Token token = scanner.next(); | ||
| if (token instanceof ExpressionToken expressionToken) { | ||
| String varName = expressionToken.getExpr().trim(); | ||
| variables.add(varName); | ||
| } | ||
| } | ||
| return variables; | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| /** | ||
| * Builder for configuring and creating {@link JinjaTemplateRenderer} instances. | ||
| */ | ||
| public static final class Builder { | ||
|
|
||
| private ValidationMode validationMode = DEFAULT_VALIDATION_MODE; | ||
|
|
||
| private Builder() { | ||
| } | ||
|
|
||
| /** | ||
| * Sets the validation mode to control behavior when the provided variables do not | ||
| * match the variables required by the template. Default is | ||
| * {@link ValidationMode#THROW}. | ||
| * @param validationMode The desired validation mode. | ||
| * @return This builder instance for chaining. | ||
| */ | ||
| public Builder validationMode(ValidationMode validationMode) { | ||
| this.validationMode = validationMode; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Builds and returns a new {@link JinjaTemplateRenderer} instance with the | ||
| * configured settings. | ||
| * @return A configured {@link JinjaTemplateRenderer}. | ||
| */ | ||
| public JinjaTemplateRenderer build() { | ||
| return new JinjaTemplateRenderer(this.validationMode); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
22 changes: 22 additions & 0 deletions
22
...g-ai-template-jinja/src/main/java/org/springframework/ai/template/jinja/package-info.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,22 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| @NonNullApi | ||
| @NonNullFields | ||
| package org.springframework.ai.template.jinja; | ||
|
|
||
| import org.springframework.lang.NonNullApi; | ||
| import org.springframework.lang.NonNullFields; |
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.
Uh oh!
There was an error while loading. Please reload this page.