From 13e7f1880c2a2af64e5e08ba84ddb8cdcb80fbff Mon Sep 17 00:00:00 2001 From: 1993heqiang <531364804@qq.com> Date: Sat, 9 Nov 2024 14:53:06 +0800 Subject: [PATCH 1/5] Optimize PromptTemplate constructor. --- .../ai/chat/prompt/PromptTemplate.java | 35 ++++++------------- 1 file changed, 10 insertions(+), 25 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 852089e2b23..2736ec95012 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 @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -54,37 +55,17 @@ public PromptTemplate(Resource resource) { catch (IOException ex) { throw new RuntimeException("Failed to read resource", ex); } - try { - this.st = new ST(this.template, '{', '}'); - } - catch (Exception ex) { - throw new IllegalArgumentException("The template string is not valid.", ex); - } + initST(this.template, Collections.emptyMap()); } 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, '{', '}'); - } - catch (Exception ex) { - throw new IllegalArgumentException("The template string is not valid.", ex); - } + initST(this.template, Collections.emptyMap()); } public PromptTemplate(String template, Map model) { this.template = template; - // If the template string is not valid, an exception will be thrown - try { - this.st = new ST(this.template, '{', '}'); - for (Entry entry : model.entrySet()) { - add(entry.getKey(), entry.getValue()); - } - } - catch (Exception ex) { - throw new IllegalArgumentException("The template string is not valid.", ex); - } + initST(this.template, model); } public PromptTemplate(Resource resource, Map model) { @@ -94,11 +75,15 @@ public PromptTemplate(Resource resource, Map model) { catch (IOException ex) { throw new RuntimeException("Failed to read resource", ex); } + initST(this.template, model); + } + + private void initST(String template, Map model) { // If the template string is not valid, an exception will be thrown try { - this.st = new ST(this.template, '{', '}'); + this.st = new ST(template, '{', '}'); for (Entry entry : model.entrySet()) { - this.add(entry.getKey(), entry.getValue()); + add(entry.getKey(), entry.getValue()); } } catch (Exception ex) { From 9588711b945950ed409b086ae6c42149fc2b0e2a Mon Sep 17 00:00:00 2001 From: 1993heqiang <531364804@qq.com> Date: Tue, 12 Nov 2024 23:57:15 +0800 Subject: [PATCH 2/5] Use chained constructors instead. --- .../ai/chat/prompt/PromptTemplate.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 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 2736ec95012..b1db6fdee92 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 @@ -49,18 +49,21 @@ public class PromptTemplate implements PromptTemplateActions, PromptTemplateMess private Map dynamicModel = new HashMap<>(); public PromptTemplate(Resource resource) { + this(resource, Collections.emptyMap()); + } + + public PromptTemplate(Resource resource, Map model) { try (InputStream inputStream = resource.getInputStream()) { this.template = StreamUtils.copyToString(inputStream, Charset.defaultCharset()); } catch (IOException ex) { throw new RuntimeException("Failed to read resource", ex); } - initST(this.template, Collections.emptyMap()); + initST(this.template, model); } public PromptTemplate(String template) { - this.template = template; - initST(this.template, Collections.emptyMap()); + this(template, Collections.emptyMap()); } public PromptTemplate(String template, Map model) { @@ -68,16 +71,6 @@ public PromptTemplate(String template, Map model) { initST(this.template, model); } - public PromptTemplate(Resource resource, Map model) { - try (InputStream inputStream = resource.getInputStream()) { - this.template = StreamUtils.copyToString(inputStream, Charset.defaultCharset()); - } - catch (IOException ex) { - throw new RuntimeException("Failed to read resource", ex); - } - initST(this.template, model); - } - private void initST(String template, Map model) { // If the template string is not valid, an exception will be thrown try { From 955a081e6f7cfc6b4afd6b751db8559788d1a69a Mon Sep 17 00:00:00 2001 From: heqiang <18710386490@163.com> Date: Wed, 13 Nov 2024 09:21:44 +0800 Subject: [PATCH 3/5] Polishing --- .../ai/chat/prompt/PromptTemplate.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 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 b1db6fdee92..7ad6f329608 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.springframework.util.Assert; import org.stringtemplate.v4.ST; import org.stringtemplate.v4.compiler.STLexer; @@ -53,13 +54,7 @@ public PromptTemplate(Resource resource) { } public PromptTemplate(Resource resource, Map model) { - try (InputStream inputStream = resource.getInputStream()) { - this.template = StreamUtils.copyToString(inputStream, Charset.defaultCharset()); - } - catch (IOException ex) { - throw new RuntimeException("Failed to read resource", ex); - } - initST(this.template, model); + this(readTemplateFromResource(resource), model); } public PromptTemplate(String template) { @@ -67,23 +62,29 @@ public PromptTemplate(String template) { } public PromptTemplate(String template, Map model) { + Assert.notNull(template, "template must not be null"); + Assert.notNull(model, "model must not be null"); this.template = template; - initST(this.template, model); - } - - private void initST(String template, Map model) { // If the template string is not valid, an exception will be thrown try { this.st = new ST(template, '{', '}'); for (Entry entry : model.entrySet()) { add(entry.getKey(), entry.getValue()); } - } - catch (Exception ex) { + } catch (Exception ex) { throw new IllegalArgumentException("The template string is not valid.", ex); } } + private static String readTemplateFromResource(Resource resource) { + Assert.notNull(resource, "resource must not be null"); + try (InputStream inputStream = resource.getInputStream()) { + return StreamUtils.copyToString(inputStream, Charset.defaultCharset()); + } catch (IOException ex) { + throw new RuntimeException("Failed to read resource", ex); + } + } + public void add(String name, Object value) { this.st.add(name, value); this.dynamicModel.put(name, value); From 81b7ac9986ed937edb74dee0e33db5555e1107c4 Mon Sep 17 00:00:00 2001 From: heqiang <18710386490@163.com> Date: Wed, 13 Nov 2024 09:32:30 +0800 Subject: [PATCH 4/5] format --- .../org/springframework/ai/chat/prompt/PromptTemplate.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 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 7ad6f329608..cacc9eff13a 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 @@ -71,7 +71,8 @@ public PromptTemplate(String template, Map model) { for (Entry entry : model.entrySet()) { add(entry.getKey(), entry.getValue()); } - } catch (Exception ex) { + } + catch (Exception ex) { throw new IllegalArgumentException("The template string is not valid.", ex); } } @@ -80,7 +81,8 @@ private static String readTemplateFromResource(Resource resource) { Assert.notNull(resource, "resource must not be null"); try (InputStream inputStream = resource.getInputStream()) { return StreamUtils.copyToString(inputStream, Charset.defaultCharset()); - } catch (IOException ex) { + } + catch (IOException ex) { throw new RuntimeException("Failed to read resource", ex); } } From b8e812d19bf964f52a87f0cfd54458ea9ae84075 Mon Sep 17 00:00:00 2001 From: heqiang <18710386490@163.com> Date: Wed, 13 Nov 2024 09:49:59 +0800 Subject: [PATCH 5/5] Polishing --- .../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 cacc9eff13a..43a1b373efb 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 @@ -67,7 +67,7 @@ public PromptTemplate(String template, Map model) { this.template = template; // If the template string is not valid, an exception will be thrown try { - this.st = new ST(template, '{', '}'); + this.st = new ST(this.template, '{', '}'); for (Entry entry : model.entrySet()) { add(entry.getKey(), entry.getValue()); }