Remove deprecated PromptTemplate constructors

- Remove the deprecated constructors from PromptTemplate and replace the references using the builder methods

Signed-off-by: Ilayaperumal Gopinathan <ilayaperumal.gopinathan@broadcom.com>
This commit is contained in:
Ilayaperumal Gopinathan
2025-05-05 00:57:39 +01:00
parent 46be8987d6
commit 722c77e812
23 changed files with 326 additions and 175 deletions

View File

@@ -218,7 +218,7 @@ public class QuestionAnswerAdvisorTests {
var qaAdvisor = new QuestionAnswerAdvisor(this.vectorStore, SearchRequest.builder().build());
var userTextTemplate = "Please answer my question {question}";
var userPromptTemplate = new PromptTemplate(userTextTemplate, Map.of("question", "XYZ"));
var userPromptTemplate = PromptTemplate.builder().template(userTextTemplate).variables(Map.of("question", "XYZ")).build();
var userMessage = userPromptTemplate.createMessage();
// @formatter:off
chatClient.prompt(new Prompt(userMessage))

View File

@@ -154,8 +154,10 @@ class AnthropicChatModelIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -172,8 +174,11 @@ class AnthropicChatModelIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format",
format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -192,7 +197,10 @@ class AnthropicChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -212,7 +220,10 @@ class AnthropicChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.streamingChatModel.stream(prompt)

View File

@@ -137,8 +137,10 @@ class AzureOpenAiChatModelIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -156,8 +158,11 @@ class AzureOpenAiChatModelIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format",
format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -176,7 +181,10 @@ class AzureOpenAiChatModelIT {
Generate the filmography for a random actor.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -194,7 +202,10 @@ class AzureOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -214,7 +225,10 @@ class AzureOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.chatModel.stream(prompt)

View File

@@ -152,8 +152,10 @@ class BedrockProxyChatModelIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -170,8 +172,11 @@ class BedrockProxyChatModelIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format",
format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -190,7 +195,10 @@ class BedrockProxyChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -210,7 +218,10 @@ class BedrockProxyChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.streamingChatModel.stream(prompt)

View File

@@ -127,8 +127,10 @@ class MistralAiChatModelIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -145,8 +147,11 @@ class MistralAiChatModelIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format",
format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -165,7 +170,10 @@ class MistralAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -185,7 +193,10 @@ class MistralAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.streamingChatModel.stream(prompt)

View File

@@ -163,8 +163,10 @@ class OllamaChatModelIT extends BaseOllamaIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors.", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors.", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -182,7 +184,10 @@ class OllamaChatModelIT extends BaseOllamaIT {
Example: R -> Red.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -203,7 +208,10 @@ class OllamaChatModelIT extends BaseOllamaIT {
Consider the filmography of Tom Hanks and tell me 5 of his movies.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -221,7 +229,10 @@ class OllamaChatModelIT extends BaseOllamaIT {
Consider the filmography of Tom Hanks and tell me 5 of his movies.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.chatModel.stream(prompt)

View File

@@ -246,8 +246,10 @@ public class OpenAiChatModelIT extends AbstractIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -265,8 +267,10 @@ public class OpenAiChatModelIT extends AbstractIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -285,7 +289,10 @@ public class OpenAiChatModelIT extends AbstractIT {
Generate the filmography for a random actor.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -302,7 +309,10 @@ public class OpenAiChatModelIT extends AbstractIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -322,7 +332,10 @@ public class OpenAiChatModelIT extends AbstractIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.streamingChatModel.stream(prompt)

View File

@@ -58,7 +58,10 @@ class OpenAiChatModelTypeReferenceBeanOutputConverterIT extends AbstractIT {
Generate the filmography of 5 movies for Tom Hanks and Bill Murray.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -84,7 +87,10 @@ class OpenAiChatModelTypeReferenceBeanOutputConverterIT extends AbstractIT {
Generate the filmography of 5 movies for Tom Hanks and Bill Murray.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.streamingChatModel.stream(prompt)

View File

@@ -148,8 +148,10 @@ class DeepSeekWithOpenAiChatModelIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -167,8 +169,10 @@ class DeepSeekWithOpenAiChatModelIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -187,7 +191,10 @@ class DeepSeekWithOpenAiChatModelIT {
Generate the filmography for a random actor.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -206,7 +213,10 @@ class DeepSeekWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -228,7 +238,10 @@ class DeepSeekWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.chatModel.stream(prompt)

View File

@@ -163,8 +163,10 @@ class DockerModelRunnerWithOpenAiChatModelIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -182,8 +184,10 @@ class DockerModelRunnerWithOpenAiChatModelIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -202,7 +206,10 @@ class DockerModelRunnerWithOpenAiChatModelIT {
Generate the filmography for a random actor.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -220,7 +227,10 @@ class DockerModelRunnerWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -240,7 +250,10 @@ class DockerModelRunnerWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.chatModel.stream(prompt)

View File

@@ -145,8 +145,10 @@ class GroqWithOpenAiChatModelIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -164,8 +166,10 @@ class GroqWithOpenAiChatModelIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -184,7 +188,10 @@ class GroqWithOpenAiChatModelIT {
Generate the filmography for a random actor.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -202,7 +209,10 @@ class GroqWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -222,7 +232,10 @@ class GroqWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.chatModel.stream(prompt)

View File

@@ -146,8 +146,10 @@ class MistralWithOpenAiChatModelIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -165,8 +167,10 @@ class MistralWithOpenAiChatModelIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -185,7 +189,10 @@ class MistralWithOpenAiChatModelIT {
Generate the filmography for a random actor.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -203,7 +210,10 @@ class MistralWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -223,7 +233,10 @@ class MistralWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.chatModel.stream(prompt)

View File

@@ -141,8 +141,10 @@ class NvidiaWithOpenAiChatModelIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -160,8 +162,10 @@ class NvidiaWithOpenAiChatModelIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -180,7 +184,10 @@ class NvidiaWithOpenAiChatModelIT {
Generate the filmography for a random actor.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -198,7 +205,10 @@ class NvidiaWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -218,7 +228,10 @@ class NvidiaWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.chatModel.stream(prompt)

View File

@@ -164,8 +164,10 @@ class OllamaWithOpenAiChatModelIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -183,8 +185,10 @@ class OllamaWithOpenAiChatModelIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -203,7 +207,10 @@ class OllamaWithOpenAiChatModelIT {
Generate the filmography for a random actor.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -221,7 +228,10 @@ class OllamaWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -241,7 +251,10 @@ class OllamaWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.chatModel.stream(prompt)

View File

@@ -155,8 +155,10 @@ class PerplexityWithOpenAiChatModelIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -173,8 +175,10 @@ class PerplexityWithOpenAiChatModelIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "numbers from 1 to 9 under the key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "numbers from 1 to 9 under the key name 'numbers'", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -192,7 +196,10 @@ class PerplexityWithOpenAiChatModelIT {
Generate the filmography for a random actor.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -209,7 +216,10 @@ class PerplexityWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -228,7 +238,10 @@ class PerplexityWithOpenAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.chatModel.stream(prompt)

View File

@@ -87,8 +87,10 @@ public abstract class AbstractIT {
String answer = response.getResult().getOutput().getText();
logger.info("Question: " + question);
logger.info("Answer:" + answer);
PromptTemplate userPromptTemplate = new PromptTemplate(this.userEvaluatorResource,
Map.of("question", question, "answer", answer));
PromptTemplate userPromptTemplate = PromptTemplate.builder()
.resource(this.userEvaluatorResource)
.variables(Map.of("question", question, "answer", answer))
.build();
SystemMessage systemMessage;
if (factBased) {
systemMessage = new SystemMessage(this.qaEvaluatorFactBasedAnswerResource);

View File

@@ -149,8 +149,10 @@ class VertexAiGeminiChatModelIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors.", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors.", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -167,8 +169,11 @@ class VertexAiGeminiChatModelIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format",
format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -188,7 +193,10 @@ class VertexAiGeminiChatModelIT {
Remove the ```json outer brackets.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -226,7 +234,10 @@ class VertexAiGeminiChatModelIT {
Remove the ```json outer brackets.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = this.chatModel.stream(prompt)

View File

@@ -127,8 +127,10 @@ class ZhiPuAiChatModelIT {
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "ice cream flavors", "format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage(), ChatOptions.builder().build());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -146,8 +148,11 @@ class ZhiPuAiChatModelIT {
Provide me a List of {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format",
format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage(), ChatOptions.builder().build());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -166,7 +171,10 @@ class ZhiPuAiChatModelIT {
Generate the filmography for a random actor.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage(), ChatOptions.builder().build());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -183,7 +191,10 @@ class ZhiPuAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage(), ChatOptions.builder().build());
Generation generation = this.chatModel.call(prompt).getResult();
@@ -203,7 +214,10 @@ class ZhiPuAiChatModelIT {
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
PromptTemplate promptTemplate = PromptTemplate.builder()
.template(template)
.variables(Map.of("format", format))
.build();
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = Objects

View File

@@ -72,7 +72,7 @@ public class PromptTemplateTest {
Map<String, Object> model = new HashMap<>();
model.put("name", "Alice");
model.put("age", 30);
PromptTemplate promptTemplate = new PromptTemplate(template, model);
PromptTemplate promptTemplate = PromptTemplate.builder().template(template).variables(model).build();
ChatOptions chatOptions = ChatOptions.builder().temperature(0.5).maxTokens(100).build();
Prompt prompt = promptTemplate.create(model, chatOptions);
@@ -88,7 +88,7 @@ public class PromptTemplateTest {
Map<String, Object> initialModel = new HashMap<>();
initialModel.put("name", "Bob");
initialModel.put("color", "blue");
PromptTemplate promptTemplate = new PromptTemplate(template, initialModel);
PromptTemplate promptTemplate = PromptTemplate.builder().template(template).variables(initialModel).build();
Map<String, Object> overriddenModel = new HashMap<>();
overriddenModel.put("color", "red");
@@ -128,7 +128,7 @@ public class PromptTemplateTest {
// Create a simple template with placeholders for keys in the generative
String template = "This is a {key1}, it is {key2}, and it costs {key3}";
PromptTemplate promptTemplate = new PromptTemplate(template, model);
PromptTemplate promptTemplate = PromptTemplate.builder().template(template).variables(model).build();
// The expected result after rendering the template with the generative
String expected = "This is a value1, it is true, and it costs 100";
@@ -147,7 +147,7 @@ public class PromptTemplateTest {
public void testRenderWithHyphen() {
Map<String, Object> model = Map.of("key-1", "value1");
String template = "This is a {key-1}";
PromptTemplate promptTemplate = new PromptTemplate(template, model);
PromptTemplate promptTemplate = PromptTemplate.builder().template(template).variables(model).build();
String expected = "This is a value1";
String result = promptTemplate.render();
@@ -161,7 +161,7 @@ public class PromptTemplateTest {
InputStream inputStream = new ByteArrayInputStream(
"key1's value is {key1} and key2's value is {key2}".getBytes(Charset.defaultCharset()));
Resource resource = new InputStreamResource(inputStream);
PromptTemplate promptTemplate = new PromptTemplate(resource, model);
PromptTemplate promptTemplate = PromptTemplate.builder().resource(resource).variables(model).build();
String expected = "key1's value is value1 and key2's value is true";
String result = promptTemplate.render();
assertEquals(expected, result);
@@ -180,7 +180,7 @@ public class PromptTemplateTest {
// Create a simple template with placeholders for keys in the generative
String template = "{key1}, {key2}, {key3}";
PromptTemplate promptTemplate = new PromptTemplate(template, model);
PromptTemplate promptTemplate = PromptTemplate.builder().resource(resource).variables(model).build();
// The expected result after rendering the template with the generative
String expected = "value1, true, it costs 100";
@@ -199,7 +199,7 @@ public class PromptTemplateTest {
// Create a simple template that includes a key not present in the generative
String template = "This is a {key2}!";
PromptTemplate promptTemplate = new PromptTemplate(template, model);
PromptTemplate promptTemplate = PromptTemplate.builder().template(template).variables(model).build();
// Rendering the template with a missing key should throw an exception
assertThrows(IllegalStateException.class, promptTemplate::render);

View File

@@ -98,7 +98,7 @@ class PromptTests {
Map<String, Object> model = new HashMap<>();
model.put("name", "Alice");
model.put("age", 30);
PromptTemplate promptTemplate = new PromptTemplate(template, model);
PromptTemplate promptTemplate = PromptTemplate.builder().template(template).variables(model).build();
ChatOptions chatOptions = ChatOptions.builder().temperature(0.5).maxTokens(100).build();
Prompt prompt = promptTemplate.create(model, chatOptions);
@@ -115,7 +115,7 @@ class PromptTests {
Map<String, Object> model = new HashMap<>();
model.put("name", "Alice");
model.put("age", 30);
PromptTemplate promptTemplate = new PromptTemplate(template, model);
PromptTemplate promptTemplate = PromptTemplate.builder().template(template).variables(model).build();
ChatOptions chatOptions = ChatOptions.builder().temperature(0.5).maxTokens(100).build();
Prompt prompt = promptTemplate.create(model, chatOptions);

View File

@@ -52,19 +52,11 @@ public class PromptTemplate implements PromptTemplateActions, PromptTemplateMess
private static final TemplateRenderer DEFAULT_TEMPLATE_RENDERER = StTemplateRenderer.builder().build();
/**
* @deprecated will become private in the next release. If you're subclassing this
* class, re-consider using the built-in implementation together with the new
* PromptTemplateRenderer interface, designed to give you more flexibility and control
* over the rendering process.
* If you're subclassing this class, re-consider using the built-in implementation
* together with the new PromptTemplateRenderer interface, designed to give you more
* flexibility and control over the rendering process.
*/
@Deprecated
protected String template;
/**
* @deprecated in favor of {@link TemplateRenderer}
*/
@Deprecated
protected TemplateFormat templateFormat = TemplateFormat.ST;
private String template;
private final Map<String, Object> variables = new HashMap<>();
@@ -78,22 +70,6 @@ public class PromptTemplate implements PromptTemplateActions, PromptTemplateMess
this(template, new HashMap<>(), DEFAULT_TEMPLATE_RENDERER);
}
/**
* @deprecated in favor of {@link PromptTemplate#builder()}.
*/
@Deprecated
public PromptTemplate(String template, Map<String, Object> variables) {
this(template, variables, DEFAULT_TEMPLATE_RENDERER);
}
/**
* @deprecated in fahvor of {@link PromptTemplate#builder()}.
*/
@Deprecated
public PromptTemplate(Resource resource, Map<String, Object> variables) {
this(resource, variables, DEFAULT_TEMPLATE_RENDERER);
}
PromptTemplate(String template, Map<String, Object> variables, TemplateRenderer renderer) {
Assert.hasText(template, "template cannot be null or empty");
Assert.notNull(variables, "variables cannot be null");
@@ -130,14 +106,6 @@ public class PromptTemplate implements PromptTemplateActions, PromptTemplateMess
return this.template;
}
/**
* @deprecated in favor of {@link TemplateRenderer}
*/
@Deprecated
public TemplateFormat getTemplateFormat() {
return this.templateFormat;
}
// From PromptTemplateStringActions.
@Override
@@ -233,25 +201,6 @@ public class PromptTemplate implements PromptTemplateActions, PromptTemplateMess
return Prompt.builder().content(render(additionalVariables)).chatOptions(modelOptions).build();
}
// Compatibility
/**
* @deprecated in favor of {@link TemplateRenderer}.
*/
@Deprecated
public Set<String> getInputVariables() {
throw new UnsupportedOperationException(
"The template rendering logic is now provided by PromptTemplateRenderer");
}
/**
* @deprecated in favor of {@link TemplateRenderer}.
*/
@Deprecated
protected void validate(Map<String, Object> model) {
throw new UnsupportedOperationException("Validation is now provided by the PromptTemplateRenderer");
}
public Builder mutate() {
return new Builder().template(this.template).variables(this.variables).renderer(this.renderer);
}

View File

@@ -77,7 +77,8 @@ class PromptTemplateTests {
void createWithNullVariables() {
String template = "Hello!";
Map<String, Object> variables = null;
assertThatThrownBy(() -> new PromptTemplate(template, variables)).isInstanceOf(IllegalArgumentException.class)
assertThatThrownBy(() -> PromptTemplate.builder().template(template).variables(variables).build())
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("variables cannot be null");
}
@@ -86,7 +87,8 @@ class PromptTemplateTests {
String template = "Hello!";
Map<String, Object> variables = new HashMap<>();
variables.put(null, "value");
assertThatThrownBy(() -> new PromptTemplate(template, variables)).isInstanceOf(IllegalArgumentException.class)
assertThatThrownBy(() -> PromptTemplate.builder().template(template).variables(variables).build())
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("variables keys cannot be null");
}
@@ -107,7 +109,7 @@ class PromptTemplateTests {
void renderWithVariables() {
Map<String, Object> variables = new HashMap<>();
variables.put("name", "Spring AI");
PromptTemplate promptTemplate = new PromptTemplate("Hello {name}!", variables);
PromptTemplate promptTemplate = PromptTemplate.builder().template("Hello {name}!").variables(variables).build();
assertThat(promptTemplate.render()).isEqualTo("Hello Spring AI!");
}
@@ -115,7 +117,10 @@ class PromptTemplateTests {
void renderWithAdditionalVariables() {
Map<String, Object> variables = new HashMap<>();
variables.put("greeting", "Hello");
PromptTemplate promptTemplate = new PromptTemplate("{greeting} {name}!", variables);
PromptTemplate promptTemplate = PromptTemplate.builder()
.template("{greeting} {name}!")
.variables(variables)
.build();
Map<String, Object> additionalVariables = new HashMap<>();
additionalVariables.put("name", "Spring AI");
@@ -162,7 +167,7 @@ class PromptTemplateTests {
void createPromptWithVariables() {
Map<String, Object> variables = new HashMap<>();
variables.put("name", "Spring AI");
PromptTemplate promptTemplate = new PromptTemplate("Hello {name}!", variables);
PromptTemplate promptTemplate = PromptTemplate.builder().template("Hello {name}!").variables(variables).build();
Prompt prompt = promptTemplate.create(variables);
assertThat(prompt.getContents()).isEqualTo("Hello Spring AI!");
}

View File

@@ -58,8 +58,10 @@ public class BasicEvaluationTest {
assertThat(answer).isNotNull();
logger.info("Question: " + question);
logger.info("Answer:" + answer);
PromptTemplate userPromptTemplate = new PromptTemplate(this.userEvaluatorResource,
Map.of("question", question, "answer", answer));
PromptTemplate userPromptTemplate = PromptTemplate.builder()
.resource(this.userEvaluatorResource)
.variables(Map.of("question", question, "answer", answer))
.build();
SystemMessage systemMessage;
if (factBased) {
systemMessage = new SystemMessage(this.qaEvaluatorFactBasedAnswerResource);