From 435cf13647b4a5be4eede871cb3d378f77b08c64 Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Tue, 29 Apr 2025 16:35:09 -0400 Subject: [PATCH] Update docs to mention NoOpTemplateRenderer --- .../modules/ROOT/pages/api/chatclient.adoc | 10 ++++++++-- .../antora/modules/ROOT/pages/api/prompt.adoc | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chatclient.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chatclient.adoc index 07e27a2f2..e5776c287 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chatclient.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chatclient.adoc @@ -179,9 +179,15 @@ String answer = ChatClient.create(chatModel).prompt() .content(); ---- -Internally, the ChatClient uses the `PromptTemplate` class to handle the user and system text and replace the variables with the values provided at runtime relying on a given `TemplateRenderer` implementation. By default, Spring AI uses the `StTemplateRenderer` implementation, which is based on the open-source https://www.stringtemplate.org/[StringTemplate] engine developed by Terence Parr. +Internally, the ChatClient uses the `PromptTemplate` class to handle the user and system text and replace the variables with the values provided at runtime relying on a given `TemplateRenderer` implementation. +By default, Spring AI uses the `StTemplateRenderer` implementation, which is based on the open-source https://www.stringtemplate.org/[StringTemplate] engine developed by Terence Parr. -NOTE: The `TemplateRenderer` configured directly on the `ChatClient` (via `.templateRenderer()`) applies only to the prompt content defined directly in the `ChatClient` builder chain (e.g., via `.user()`, `.system()`). It does *not* affect templates used internally by xref:api/retrieval-augmented-generation.adoc#_questionansweradvisor[Advisors] like `QuestionAnswerAdvisor`, which have their own template customization mechanisms (see xref:api/retrieval-augmented-generation.adoc#_custom_template[Custom Advisor Templates]). +Spring AI also provides a `NoOpTemplateRenderer` for cases where no template processing is desired. + +Spring AI also provides a `NoOpTemplateRenderer`. + +NOTE: The `TemplateRenderer` configured directly on the `ChatClient` (via `.templateRenderer()`) applies only to the prompt content defined directly in the `ChatClient` builder chain (e.g., via `.user()`, `.system()`). +It does *not* affect templates used internally by xref:api/retrieval-augmented-generation.adoc#_questionansweradvisor[Advisors] like `QuestionAnswerAdvisor`, which have their own template customization mechanisms (see xref:api/retrieval-augmented-generation.adoc#_custom_template[Custom Advisor Templates]). If you'd rather use a different template engine, you can provide a custom implementation of the `TemplateRenderer` interface directly to the ChatClient. You can also keep using the default `StTemplateRenderer`, but with a custom configuration. diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/prompt.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/prompt.adoc index fc7de8d5c..53cd2fbfa 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/prompt.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/prompt.adoc @@ -141,6 +141,24 @@ public interface TemplateRenderer extends BiFunction } ---- +Spring AI uses the `TemplateRenderer` interface to handle the actual substitution of variables into the template string. +The default implementation uses <>. +You can provide your own implementation of `TemplateRenderer` if you need custom logic. +For scenarios where no template rendering is required (e.g., the template string is already complete), you can use the provided `NoOpTemplateRenderer`. + +.Example using a custom StringTemplate renderer with '<' and '>' delimiters +[source,java] +---- +PromptTemplate promptTemplate = PromptTemplate.builder() + .renderer(StTemplateRenderer.builder().startDelimiterToken('<').endDelimiterToken('>').build()) + .template(""" + Tell me the names of 5 movies whose soundtrack was composed by . + """) + .build(); + +String prompt = promptTemplate.render(Map.of("composer", "John Williams")); +---- + The interfaces implemented by this class support different aspects of prompt creation: `PromptTemplateStringActions` focuses on creating and rendering prompt strings, representing the most basic form of prompt generation.