Skip to content

Fixed GH-3526, Fixed the error in using the Builder in SystemPromptTemplate. #3529

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

Closed
Closed
Show file tree
Hide file tree
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 @@ -209,17 +209,17 @@ public static Builder builder() {
return new Builder();
}

public static final class Builder {
public static class Builder {

private String template;
protected String template;

private Resource resource;
protected Resource resource;

private Map<String, Object> variables = new HashMap<>();
protected Map<String, Object> variables = new HashMap<>();

private TemplateRenderer renderer = DEFAULT_TEMPLATE_RENDERER;
protected TemplateRenderer renderer = DEFAULT_TEMPLATE_RENDERER;

private Builder() {
protected Builder() {
}

public Builder template(String template) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.template.TemplateRenderer;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

public class SystemPromptTemplate extends PromptTemplate {

Expand All @@ -32,6 +34,14 @@ public SystemPromptTemplate(Resource resource) {
super(resource);
}

private SystemPromptTemplate(String template, Map<String, Object> variables, TemplateRenderer renderer) {
super(template, variables, renderer);
}

private SystemPromptTemplate(Resource resource, Map<String, Object> variables, TemplateRenderer renderer) {
super(resource, variables, renderer);
}

@Override
public Message createMessage() {
return new SystemMessage(render());
Expand All @@ -52,4 +62,50 @@ public Prompt create(Map<String, Object> model) {
return new Prompt(new SystemMessage(render(model)));
}

public static Builder builder() {
return new Builder();
}

public static class Builder extends PromptTemplate.Builder {

public Builder template(String template) {
Assert.hasText(template, "template cannot be null or empty");
this.template = template;
return this;
}

public Builder resource(Resource resource) {
Assert.notNull(resource, "resource cannot be null");
this.resource = resource;
return this;
}

public Builder variables(Map<String, Object> variables) {
Assert.notNull(variables, "variables cannot be null");
Assert.noNullElements(variables.keySet(), "variables keys cannot be null");
this.variables = variables;
return this;
}

public Builder renderer(TemplateRenderer renderer) {
Assert.notNull(renderer, "renderer cannot be null");
this.renderer = renderer;
return this;
}

@Override
public SystemPromptTemplate build() {
if (this.template != null && this.resource != null) {
throw new IllegalArgumentException("Only one of template or resource can be set");
}
else if (this.resource != null) {
return new SystemPromptTemplate(this.resource, this.variables, this.renderer);
}
else {
return new SystemPromptTemplate(this.template, this.variables, this.renderer);
}
}

}

}
Loading