From 83c640d90548e5115eab884a7db39106edb0090d Mon Sep 17 00:00:00 2001 From: Sun Yuhan <1085481446@qq.com> Date: Thu, 13 Mar 2025 17:41:42 +0800 Subject: [PATCH 1/3] fix: Fixed the issue where using built-in functions in PromptTemplate caused validation failures, and added a method:skipValidate() to skip validation for PromptTemplate. Signed-off-by: Sun Yuhan <1085481446@qq.com> --- .../ai/chat/prompt/PromptTemplate.java | 22 ++++++- .../ai/chat/prompt/PromptTemplateTests.java | 63 +++++++++++++++++++ 2 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 spring-ai-core/src/test/java/org/springframework/ai/chat/prompt/PromptTemplateTests.java diff --git a/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java b/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java index 852089e2b23..b7be15951d4 100644 --- a/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java +++ b/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java @@ -29,6 +29,7 @@ import org.antlr.runtime.Token; import org.antlr.runtime.TokenStream; import org.stringtemplate.v4.ST; +import org.stringtemplate.v4.compiler.Compiler; import org.stringtemplate.v4.compiler.STLexer; import org.springframework.ai.chat.messages.Message; @@ -47,6 +48,8 @@ public class PromptTemplate implements PromptTemplateActions, PromptTemplateMess private Map dynamicModel = new HashMap<>(); + private boolean skipRenderValidate = false; + public PromptTemplate(Resource resource) { try (InputStream inputStream = resource.getInputStream()) { this.template = StreamUtils.copyToString(inputStream, Charset.defaultCharset()); @@ -111,6 +114,10 @@ public void add(String name, Object value) { this.dynamicModel.put(name, value); } + public void skipValidate() { + this.skipRenderValidate = true; + } + public String getTemplate() { return this.template; } @@ -199,15 +206,21 @@ public Set getInputVariables() { if (token.getType() == STLexer.LDELIM && i + 1 < tokens.size() && tokens.get(i + 1).getType() == STLexer.ID) { if (i + 2 < tokens.size() && tokens.get(i + 2).getType() == STLexer.COLON) { - inputVariables.add(tokens.get(i + 1).getText()); - isInsideList = true; + String text = tokens.get(i + 1).getText(); + if (!Compiler.funcs.containsKey(text)) { + inputVariables.add(text); + isInsideList = true; + } } } else if (token.getType() == STLexer.RDELIM) { isInsideList = false; } else if (!isInsideList && token.getType() == STLexer.ID) { - inputVariables.add(token.getText()); + if (!Compiler.funcs.containsKey(token.getText())) { + inputVariables.add(token.getText()); + } + } } @@ -222,6 +235,9 @@ private Set getModelKeys(Map model) { } protected void validate(Map model) { + if (skipRenderValidate) { + return; + } Set templateTokens = getInputVariables(); Set modelKeys = getModelKeys(model); diff --git a/spring-ai-core/src/test/java/org/springframework/ai/chat/prompt/PromptTemplateTests.java b/spring-ai-core/src/test/java/org/springframework/ai/chat/prompt/PromptTemplateTests.java new file mode 100644 index 00000000000..55898424239 --- /dev/null +++ b/spring-ai-core/src/test/java/org/springframework/ai/chat/prompt/PromptTemplateTests.java @@ -0,0 +1,63 @@ +/* + * Copyright 2023-2024 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.chat.prompt; + +import org.junit.jupiter.api.Test; + +/** + * Unit Tests for {@link PromptTemplateTests}. + * + * @author Sun Yuhan + */ +public class PromptTemplateTests { + @Test + void buildPromptTemplateWithBuiltInFunctions() { + String chatMemoryPrompt = """ + {if(strlen(memory))} + + Hello World! + + --------------------- + {memory} + --------------------- + {endif} + """; + + PromptTemplate promptTemplate = new PromptTemplate(chatMemoryPrompt); + promptTemplate.add("memory", "you are a helpful assistant"); + System.out.println(promptTemplate.render()); + } + + @Test + void buildPromptTemplateSkipRenderValidate() { + String chatMemoryPrompt = """ + {if(strlen(memory))} + + Hello World! + + --------------------- + {memory} + --------------------- + {endif} + """; + + PromptTemplate promptTemplate = new PromptTemplate(chatMemoryPrompt); + promptTemplate.add("memory", "you are a helpful assistant"); + promptTemplate.skipValidate(); + System.out.println(promptTemplate.render()); + } +} From 6b970005375e14bf1e526b3bb28cf69afc05f851 Mon Sep 17 00:00:00 2001 From: Sun Yuhan <1085481446@qq.com> Date: Thu, 24 Apr 2025 10:50:16 +0800 Subject: [PATCH 2/3] fix: Adjust the skipRenderValidate parameter to the value specified in the constructor. Signed-off-by: Sun Yuhan <1085481446@qq.com> --- .../ai/chat/prompt/PromptTemplate.java | 24 +++++++++++++++---- .../ai/chat/prompt/PromptTemplateTests.java | 3 +-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java b/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java index b7be15951d4..fa1b0446e89 100644 --- a/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java +++ b/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java @@ -109,15 +109,31 @@ public PromptTemplate(Resource resource, Map model) { } } + public PromptTemplate(Resource resource, boolean skipRenderValidate) { + this(resource); + this.skipRenderValidate = skipRenderValidate; + } + + public PromptTemplate(String template, boolean skipRenderValidate) { + this(template); + this.skipRenderValidate = skipRenderValidate; + } + + public PromptTemplate(String template, Map model, boolean skipRenderValidate) { + this(template, model); + this.skipRenderValidate = skipRenderValidate; + } + + public PromptTemplate(Resource resource, Map model, boolean skipRenderValidate) { + this(resource, model); + this.skipRenderValidate = skipRenderValidate; + } + public void add(String name, Object value) { this.st.add(name, value); this.dynamicModel.put(name, value); } - public void skipValidate() { - this.skipRenderValidate = true; - } - public String getTemplate() { return this.template; } diff --git a/spring-ai-core/src/test/java/org/springframework/ai/chat/prompt/PromptTemplateTests.java b/spring-ai-core/src/test/java/org/springframework/ai/chat/prompt/PromptTemplateTests.java index 55898424239..7eb5edec465 100644 --- a/spring-ai-core/src/test/java/org/springframework/ai/chat/prompt/PromptTemplateTests.java +++ b/spring-ai-core/src/test/java/org/springframework/ai/chat/prompt/PromptTemplateTests.java @@ -55,9 +55,8 @@ void buildPromptTemplateSkipRenderValidate() { {endif} """; - PromptTemplate promptTemplate = new PromptTemplate(chatMemoryPrompt); + PromptTemplate promptTemplate = new PromptTemplate(chatMemoryPrompt, true); promptTemplate.add("memory", "you are a helpful assistant"); - promptTemplate.skipValidate(); System.out.println(promptTemplate.render()); } } From 6336fbb9b4f92d57810af60fce6b60a9f14c35e1 Mon Sep 17 00:00:00 2001 From: Sun Yuhan <1085481446@qq.com> Date: Thu, 24 Apr 2025 11:17:09 +0800 Subject: [PATCH 3/3] fix: Skip validation by default when executing render(). Signed-off-by: Sun Yuhan <1085481446@qq.com> --- .../java/org/springframework/ai/chat/prompt/PromptTemplate.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java b/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java index fa1b0446e89..07bfefbe41b 100644 --- a/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java +++ b/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java @@ -48,7 +48,7 @@ public class PromptTemplate implements PromptTemplateActions, PromptTemplateMess private Map dynamicModel = new HashMap<>(); - private boolean skipRenderValidate = false; + private boolean skipRenderValidate = true; public PromptTemplate(Resource resource) { try (InputStream inputStream = resource.getInputStream()) {