oo refactoring

This commit is contained in:
Mark Pollack
2023-07-27 01:50:46 -04:00
parent 6325fd361c
commit 62704e0bd5
6 changed files with 39 additions and 79 deletions

View File

@@ -23,8 +23,12 @@ public abstract class AbstractPromptTemplate implements PromptInput {
private Optional<OutputParser> outputParser = Optional.empty();
public AbstractPromptTemplate(Optional<OutputParser> outputParser) {
this.outputParser = outputParser;
public AbstractPromptTemplate() {
this.outputParser = Optional.empty();
}
public AbstractPromptTemplate(OutputParser outputParser) {
this.outputParser = Optional.of(outputParser);
}
@Override
@@ -32,8 +36,9 @@ public abstract class AbstractPromptTemplate implements PromptInput {
return this.outputParser;
}
public abstract String formatAsString(Map<String, Object> inputVariables);
public abstract PromptValue formatAsPrompt(Map<String, Object> inputVariables);
public PromptValue formatAsPrompt(Map<String, Object> inputVariables) {
String formattedPrompt = formatAsString(inputVariables);
return new StringPromptValue(formattedPrompt);
}
}

View File

@@ -1,34 +0,0 @@
/*
* Copyright 2023 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.core.prompts;
import java.util.Map;
import java.util.Optional;
public abstract class AbstractStringPromptTemplate extends AbstractPromptTemplate implements PromptTemplateInput {
public AbstractStringPromptTemplate(Optional<OutputParser> outputParser) {
super(outputParser);
}
@Override
public PromptValue formatAsPrompt(Map<String, Object> inputVariables) {
String formattedPrompt = formatAsString(inputVariables);
return new StringPromptValue(formattedPrompt);
}
}

View File

@@ -16,15 +16,27 @@
package org.springframework.ai.core.prompts;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
public interface PromptInput {
Set<String> getInputVariables();
// *Output*
Optional<OutputParser> getOutputParser();
// *Input*
// This is the handoff point. These methods provide the "input", then the
// "Template" is "rendered" and the output is then used to construct a "Message" that gets sent to the "LLM" model.
// Maybe should be called String renderAsString() and renderAsPrompt
// View in spring mvc has render(Map<String,?> model, HttpServletRequest request, HttpServletResponse response) method.
String formatAsString(Map<String, Object> inputVariables);
PromptValue formatAsPrompt(Map<String, Object> inputVariables);
// Leave out Partial Input Variables for now
}

View File

@@ -28,63 +28,38 @@ import org.antlr.runtime.TokenStream;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.compiler.STLexer;
public class PromptTemplate extends AbstractStringPromptTemplate implements PromptTemplateInput {
public class PromptTemplate extends AbstractPromptTemplate implements PromptTemplateInput {
private String template;
private TemplateFormat templateFormat = TemplateFormat.FSTRING;
public PromptTemplate(String template) {
super(Optional.empty());
this.template = template;
}
public PromptTemplate(String template, boolean validate) {
super(Optional.empty());
validateTemplate(validate);
super();
this.template = template;
}
public PromptTemplate(String template, TemplateFormat templateFormat) {
super(Optional.empty());
super();
this.template = template;
this.templateFormat = templateFormat;
}
public PromptTemplate(String template, TemplateFormat templateFormat, boolean validate) {
super(Optional.empty());
validateTemplate(validate);
this.template = template;
this.templateFormat = templateFormat;
}
public PromptTemplate(String template, Optional<OutputParser> outputParser) {
public PromptTemplate(String template, OutputParser outputParser) {
super(outputParser);
this.template = template;
}
public PromptTemplate(String template, Optional<OutputParser> outputParser, boolean validate) {
public PromptTemplate(String template, OutputParser outputParser, TemplateFormat templateFormat) {
super(outputParser);
validateTemplate(validate);
this.template = template;
}
public PromptTemplate(String template, Optional<OutputParser> outputParser, TemplateFormat templateFormat) {
super(outputParser);
this.template = template;
this.templateFormat = templateFormat;
}
public PromptTemplate(String template, Optional<OutputParser> outputParser, TemplateFormat templateFormat,
boolean validate) {
super(outputParser);
validateTemplate(validate);
this.template = template;
this.templateFormat = templateFormat;
}
@Override
public String formatAsString(Map<String, Object> inputVariables) {
validate();
// Only "F-String" for now
ST st = new ST(this.template, '{', '}');
for (Entry<String, Object> stringObjectEntry : inputVariables.entrySet()) {
@@ -103,8 +78,7 @@ public class PromptTemplate extends AbstractStringPromptTemplate implements Prom
return this.templateFormat;
}
@Override
public Set<String> getInputVariables() {
protected Set<String> getInputVariables() {
ST st = new ST(this.template, '{', '}');
TokenStream tokens = st.impl.tokens;
return IntStream.range(0, tokens.range())
@@ -114,12 +88,10 @@ public class PromptTemplate extends AbstractStringPromptTemplate implements Prom
.collect(Collectors.toSet());
}
private void validateTemplate(boolean validate) {
if (!validate) {
return;
}
public void validate() {
try {
ST st = new ST(this.template, '{', '}');
// TODO is doing this test even necessary, if it parsed correctly in the ctor, there should be no issues.
Set<String> inputVariables = getInputVariables();
for (String inputVariable : inputVariables) {
st.add(inputVariable, "foo");

View File

@@ -22,4 +22,6 @@ public interface PromptTemplateInput extends PromptInput {
TemplateFormat getTemplateFormat();
// *Validation*
void validate();
}

View File

@@ -16,6 +16,8 @@
package org.springframework.ai.core.prompts;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.assertj.core.api.Assertions;
@@ -59,7 +61,8 @@ class PromptTests {
void testBadTemplateString() {
String template = "This is a {foo test";
Assertions.assertThatThrownBy(() -> {
PromptTemplate promptTemplate = new PromptTemplate(template, true);
PromptTemplate promptTemplate = new PromptTemplate(template);
promptTemplate.validate();
}).isInstanceOf(IllegalArgumentException.class).hasMessage("The template string is not valid.");
}