Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,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;

Expand All @@ -48,32 +50,20 @@ public class PromptTemplate implements PromptTemplateActions, PromptTemplateMess
private Map<String, Object> dynamicModel = new HashMap<>();

public PromptTemplate(Resource resource) {
try (InputStream inputStream = resource.getInputStream()) {
this.template = StreamUtils.copyToString(inputStream, Charset.defaultCharset());
}
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);
}
this(resource, Collections.emptyMap());
}

public PromptTemplate(Resource resource, Map<String, Object> model) {
this(readTemplateFromResource(resource), model);
}

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);
}
this(template, Collections.emptyMap());
}

public PromptTemplate(String template, Map<String, Object> model) {
Assert.notNull(template, "template must not be null");
Assert.notNull(model, "model must not be null");
this.template = template;
// If the template string is not valid, an exception will be thrown
try {
Expand All @@ -87,23 +77,14 @@ public PromptTemplate(String template, Map<String, Object> model) {
}
}

public PromptTemplate(Resource resource, Map<String, Object> model) {
private static String readTemplateFromResource(Resource resource) {
Assert.notNull(resource, "resource must not be null");
try (InputStream inputStream = resource.getInputStream()) {
this.template = StreamUtils.copyToString(inputStream, Charset.defaultCharset());
return StreamUtils.copyToString(inputStream, Charset.defaultCharset());
}
catch (IOException ex) {
throw new RuntimeException("Failed to read resource", ex);
}
// If the template string is not valid, an exception will be thrown
try {
this.st = new ST(this.template, '{', '}');
for (Entry<String, Object> entry : model.entrySet()) {
this.add(entry.getKey(), entry.getValue());
}
}
catch (Exception ex) {
throw new IllegalArgumentException("The template string is not valid.", ex);
}
}

public void add(String name, Object value) {
Expand Down