1- # Using Prompt Templates with AiServices
1+ # Customizing Prompt Templates
22
3- This guide demonstrates how to configure and use prompt templates with the AiServices module. This setup involves
4- configuring prompt templates, creating prompt template sources, and rendering templates with specific variables.
5- Additionally, a test class validates the functionality of these templates.
6-
7- ## Configuration
8-
9- First, make sure you have the necessary configuration in your ` langchain4j-kotlin.properties ` file. This is default
10- configuration:
11-
12- ``` properties
13- prompt.template.source =me.kpavlov.langchain4j.kotlin.prompt.ClasspathPromptTemplateSource
14- prompt.template.renderer =me.kpavlov.langchain4j.kotlin.prompt.SimpleTemplateRenderer
15- ```
16-
17- This configuration specifies the source and renderer for the prompt templates.
3+ This guide demonstrates how to configure and use prompt templates with
4+ the [ LangChain4J's AiServices] ( https://docs.langchain4j.dev/tutorials/ai-services ) . This setup involves
5+ configuring prompt templates, defining and extending prompt template sources and template rendering.
186
197## Creating and Using Prompt Template
208
21- Include the necessary classes. Your prompt templates will be sourced from the classpath and rendered using a simple
22- template renderer .
9+ Let's start with built-in mechanizm of loading prompt template from classpath. Your prompt templates should be located
10+ in the classpath, e.g .
2311
2412File: ` prompts/default-system-prompt.mustache `
2513
@@ -74,13 +62,14 @@ System and user prompts will be:
7462- ** System prompt:** "You are helpful assistant using chatMemoryID=default"
7563- ** User Prompt:** "Hello, My friend! How are you?"
7664
77- In this example, ` TemplateSystemMessageProvider ` handles the system prompt template and ` AiServices ` uses the templates
78- to generate responses.
65+ ## How does it work
7966
80- ` PromptTemplateFactory ` is registered automatically via Java ServiceLoaders mechanism and is used to provide
81- ` PromptTemplate ` . This class is responsible for obtaining prompt templates from a ` PromptTemplateSource ` and rendering
82- them using a ` TemplateRenderer ` . If the specified template cannot be found, it will fallback to using the input template
83- content.
67+ In the default implementation, ` TemplateSystemMessageProvider ` handles the system prompt template and ` AiServices ` uses
68+ the templates to generate responses.
69+
70+ ` PromptTemplateFactory ` provides ` PromptTemplateFactory.Template ` for ` AiServices ` . It is registered automatically via
71+ Java ServiceLoaders mechanism. This class is responsible for obtaining prompt templates from a ` PromptTemplateSource ` .
72+ If the specified template cannot be found, it will fallback to using default LC4J's the input template content.
8473
8574` ClasspathPromptTemplateSource ` is implementing ` PromptTemplateSource ` interface and provides a mechanism to load prompt
8675templates from the classpath using the template name as the resource identifier. It attempts to locate the template file
@@ -90,12 +79,89 @@ overridden.
9079Implementers of the ` TemplateRenderer ` interface will typically replace placeholders in the template with corresponding
9180values from the variables map.
9281
93- ` SimpleTemplateRenderer ` finds and replaces placeholders in the template in the format ` {{key}} ` , where ` key `
82+ ` SimpleTemplateRenderer ` finds and replaces placeholders in the template in the Mustache-like format ` {{key}} ` , where
83+ ` key `
9484corresponds to an entry in the variables map. If any placeholders in the template are not defined in the variables map,
9585an ` IllegalArgumentException ` will be thrown.
9686
9787` RenderablePromptTemplate ` implements both ` PromptTemplate ` and LangChain4j's ` PromptTemplateFactory.Template `
9888interfaces. It uses a ` TemplateRenderer ` to render the template content using provided variables.
9989
90+ ## Customization
91+
92+ You may customize templates via configuration file ` langchain4j-kotlin.properties ` , located in the classpath.
93+
94+ | Key | Description | Default Value |
95+ | ----------------------------| -------------------| ----------------------------------------------------------------------|
96+ | ` prompt.template.source ` | Template source | ` me.kpavlov.langchain4j.kotlin.prompt.ClasspathPromptTemplateSource ` |
97+ | ` prompt.template.renderer ` | Template renderer | ` me.kpavlov.langchain4j.kotlin.prompt.SimpleTemplateRenderer ` |
98+
99+ ### Extending PromptTemplateSource
100+
101+ To create a custom template source, implement the PromptTemplateSource interface:
102+
103+ ``` kotlin
104+ interface PromptTemplateSource {
105+ fun getTemplate (name : TemplateName ): PromptTemplate ?
106+ }
107+ ```
108+
109+ Example implementation for Redis and Jedis:
110+
111+ ``` kotlin
112+ package com.example
113+
114+ // Redis/Jedis-backed template source
115+
116+ class RedisPromptTemplateSource (private val jedis : Jedis ) : PromptTemplateSource {
117+ override fun getTemplate (name : TemplateName ): PromptTemplate ? {
118+ return jedis.get(name)?.let {
119+ SimplePromptTemplate (it)
120+ }
121+ }
122+ }
123+ ```
124+
125+ Register your implementation in the ` langchain4j-kotlin.properties ` configuration file:
126+
127+ ``` properties
128+ prompt.template.source =com.example.RedisPromptTemplateSource
129+ ```
130+
131+ ### Extending TemplateRenderer
132+
133+ To create a custom template renderer, implement the TemplateRenderer interface:
134+
135+ ``` kotlin
136+ interface TemplateRenderer {
137+ fun render (
138+ template : TemplateContent ,
139+ variables : Map <String , Any ?>
140+ ): String
141+ }
142+ ```
143+
144+ Example implementation:
145+
146+ ``` kotlin
147+ package com.example
148+
149+ // Freemarker-based renderer
150+ class MyTemplateRenderer : TemplateRenderer {
151+
152+ override fun render (template : TemplateContent , variables : Map <String , Any ?>): String {
153+ TODO (" Add implementation here" )
154+ }
155+ }
156+ ```
157+
158+ Register your implementation in the ` langchain4j-kotlin.properties ` configuration file:
159+
160+ ``` properties
161+ prompt.template.renderer =com.example.MyTemplateRenderer
162+ ```
163+
164+ ## Examples
165+
100166You may find the unit test with the
101167example [ here] ( ../langchain4j-kotlin/src/test/kotlin/me/kpavlov/langchain4j/kotlin/service/ServiceWithPromptTemplatesTest.kt )
0 commit comments