File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed
main/java/org/springframework/ai/chat/prompt
test/java/org/springframework/ai/chat/prompt Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -123,9 +123,14 @@ public String render() {
123123
124124 @ Override
125125 public String render (Map <String , Object > additionalVariables ) {
126- Map <String , Object > combinedVariables = new HashMap <>(this .variables );
126+ Map <String , Object > combinedVariables = new HashMap <>();
127+ Map <String , Object > mergedVariables = new HashMap <>(this .variables );
128+ // variables + additionalVariables => mergedVariables
129+ if (additionalVariables != null && !additionalVariables .isEmpty ()) {
130+ mergedVariables .putAll (additionalVariables );
131+ }
127132
128- for (Entry <String , Object > entry : additionalVariables .entrySet ()) {
133+ for (Entry <String , Object > entry : mergedVariables .entrySet ()) {
129134 if (entry .getValue () instanceof Resource resource ) {
130135 combinedVariables .put (entry .getKey (), renderResource (resource ));
131136 }
Original file line number Diff line number Diff line change 2626import org .springframework .ai .template .NoOpTemplateRenderer ;
2727import org .springframework .ai .template .TemplateRenderer ;
2828import org .springframework .core .io .ByteArrayResource ;
29+ import org .springframework .core .io .ClassPathResource ;
2930import org .springframework .core .io .Resource ;
3031
3132import static org .assertj .core .api .Assertions .assertThat ;
@@ -306,6 +307,19 @@ void resource_Builder() {
306307 assertThat (promptTemplate .render ()).isEqualTo ("Hello Builder from Resource!" );
307308 }
308309
310+ @ Test
311+ void renderWithResourceFile () {
312+ Resource resource = new ClassPathResource ("prompt-user.txt" );
313+
314+ // Build PromptTemplate: bind the Resource to "name" in this.variables
315+ PromptTemplate promptTemplate = PromptTemplate .builder ()
316+ .template ("How {name}" )
317+ .variables (Map .of ("name" , resource ))
318+ .build ();
319+
320+ assertThat (promptTemplate .render (Map .of ())).isEqualTo ("How Hello, world!" );
321+ }
322+
309323 // Helper Custom Renderer for testing
310324 private static class CustomTestRenderer implements TemplateRenderer {
311325
You can’t perform that action at this time.
0 commit comments