Skip to content

Commit b7207a0

Browse files
committed
Move template renderers to dedicated modules
- Move NoOpTemplateRenderer to spring-ai-commons - Create spring-ai-template-st module for StringTemplate implementation - Update dependencies in spring-ai-model
1 parent 5628f05 commit b7207a0

File tree

10 files changed

+259
-36
lines changed

10 files changed

+259
-36
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<module>spring-ai-docs</module>
3434
<module>spring-ai-bom</module>
3535
<module>spring-ai-commons</module>
36+
<module>spring-ai-template-st</module>
3637
<module>spring-ai-client-chat</module>
3738
<module>spring-ai-model</module>
3839
<module>spring-ai-test</module>

spring-ai-model/src/main/java/org/springframework/ai/template/NoOpTemplateRenderer.java renamed to spring-ai-commons/src/main/java/org/springframework/ai/template/NoOpTemplateRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ public String apply(String template, Map<String, Object> variables) {
3636
return template;
3737
}
3838

39-
}
39+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2023-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.template;
18+
19+
import java.util.Map;
20+
import java.util.function.BiFunction;
21+
22+
/**
23+
* Renders a template using a given strategy.
24+
*
25+
* @author Thomas Vitale
26+
* @since 1.0.0
27+
*/
28+
public interface TemplateRenderer extends BiFunction<String, Map<String, Object>, String> {
29+
30+
@Override
31+
String apply(String template, Map<String, Object> variables);
32+
33+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2023-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.template;
18+
19+
/**
20+
* Validation modes for template renderers.
21+
*
22+
* @author Thomas Vitale
23+
* @since 1.0.0
24+
*/
25+
public enum ValidationMode {
26+
27+
/**
28+
* If the validation fails, an exception is thrown. This is the default mode.
29+
*/
30+
THROW,
31+
32+
/**
33+
* If the validation fails, a warning is logged. The template is rendered with the
34+
* missing placeholders/variables. This mode is not recommended for production use.
35+
*/
36+
WARN,
37+
38+
/**
39+
* No validation is performed.
40+
*/
41+
NONE;
42+
43+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
* @author Thomas Vitale
3131
*/
32-
class NoOpPromptTemplateRendererTests {
32+
class NoOpTemplateRendererTests {
3333

3434
@Test
3535
void shouldReturnUnchangedTemplate() {
@@ -114,4 +114,4 @@ void shouldReturnUnchangedComplexTemplate() {
114114
assertThat(result).isEqualToNormalizingNewlines(template);
115115
}
116116

117-
}
117+
}

spring-ai-model/pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
<version>${project.parent.version}</version>
4848
</dependency>
4949

50+
<dependency>
51+
<groupId>org.springframework.ai</groupId>
52+
<artifactId>spring-ai-template-st</artifactId>
53+
<version>${project.parent.version}</version>
54+
</dependency>
55+
5056
<dependency>
5157
<groupId>io.micrometer</groupId>
5258
<artifactId>micrometer-observation</artifactId>
@@ -63,12 +69,6 @@
6369
<artifactId>reactor-core</artifactId>
6470
</dependency>
6571

66-
<dependency>
67-
<groupId>org.antlr</groupId>
68-
<artifactId>ST4</artifactId>
69-
<version>${ST4.version}</version>
70-
</dependency>
71-
7272
<!-- ANTLR for Filter Expression Parsing -->
7373
<dependency>
7474
<groupId>org.antlr</groupId>

spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Map.Entry;
2626
import java.util.Set;
2727

28+
import org.springframework.ai.template.NoOpTemplateRenderer;
2829
import org.springframework.ai.template.TemplateRenderer;
2930
import org.springframework.ai.template.st.StTemplateRenderer;
3031
import org.springframework.util.Assert;

spring-ai-template-st/pom.xml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2023-2025 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
<parent>
23+
<groupId>org.springframework.ai</groupId>
24+
<artifactId>spring-ai-parent</artifactId>
25+
<version>1.0.0-SNAPSHOT</version>
26+
</parent>
27+
<artifactId>spring-ai-template-st</artifactId>
28+
<packaging>jar</packaging>
29+
<name>Spring AI Template StringTemplate</name>
30+
<description>StringTemplate implementation for Spring AI templating</description>
31+
<url>https://github.com/spring-projects/spring-ai</url>
32+
33+
<scm>
34+
<url>https://github.com/spring-projects/spring-ai</url>
35+
<connection>git://github.com/spring-projects/spring-ai.git</connection>
36+
<developerConnection>[email protected]:spring-projects/spring-ai.git</developerConnection>
37+
</scm>
38+
39+
<properties>
40+
<maven.compiler.target>17</maven.compiler.target>
41+
<maven.compiler.source>17</maven.compiler.source>
42+
</properties>
43+
44+
<dependencies>
45+
<dependency>
46+
<groupId>org.springframework.ai</groupId>
47+
<artifactId>spring-ai-commons</artifactId>
48+
<version>${project.parent.version}</version>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.antlr</groupId>
53+
<artifactId>ST4</artifactId>
54+
<version>${ST4.version}</version>
55+
</dependency>
56+
57+
<!-- ANTLR for token parsing -->
58+
<dependency>
59+
<groupId>org.antlr</groupId>
60+
<artifactId>antlr4-runtime</artifactId>
61+
<version>${antlr.version}</version>
62+
</dependency>
63+
64+
<!-- Logging -->
65+
<dependency>
66+
<groupId>org.slf4j</groupId>
67+
<artifactId>slf4j-api</artifactId>
68+
</dependency>
69+
70+
<!-- test dependencies -->
71+
<dependency>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-starter-test</artifactId>
74+
<scope>test</scope>
75+
</dependency>
76+
</dependencies>
77+
</project>

spring-ai-model/src/main/java/org/springframework/ai/template/st/StTemplateRenderer.java renamed to spring-ai-template-st/src/main/java/org/springframework/ai/template/st/StTemplateRenderer.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323
import org.springframework.ai.template.TemplateRenderer;
24+
import org.springframework.ai.template.ValidationMode;
2425
import org.springframework.util.Assert;
2526
import org.stringtemplate.v4.ST;
2627
import org.stringtemplate.v4.compiler.STLexer;
@@ -127,27 +128,6 @@ else if (!isInsideList && token.getType() == STLexer.ID) {
127128
return inputVariables;
128129
}
129130

130-
public enum ValidationMode {
131-
132-
/**
133-
* If the validation fails, an exception is thrown. This is the default mode.
134-
*/
135-
THROW,
136-
137-
/**
138-
* If the validation fails, a warning is logged. The template is rendered with the
139-
* missing placeholders/variables. This mode is not recommended for production
140-
* use.
141-
*/
142-
WARN,
143-
144-
/**
145-
* No validation is performed.
146-
*/
147-
NONE;
148-
149-
}
150-
151131
public static Builder builder() {
152132
return new Builder();
153133
}
@@ -184,4 +164,4 @@ public StTemplateRenderer build() {
184164

185165
}
186166

187-
}
167+
}

0 commit comments

Comments
 (0)