@@ -87,17 +87,17 @@ public static Builder builder() {
87
87
88
88
@ Override
89
89
public Map <String , Object > apply (OverAllState state ) throws Exception {
90
- initNodeWithState (state );
90
+ ExecutionContext context = initNodeWithState (state );
91
91
92
92
// add streaming support
93
93
if (Boolean .TRUE .equals (stream )) {
94
- Flux <ChatResponse > chatResponseFlux = stream (state );
94
+ Flux <ChatResponse > chatResponseFlux = stream (context );
95
95
return Map .of (StringUtils .hasLength (this .outputKey ) ? this .outputKey : "messages" , chatResponseFlux );
96
96
}
97
97
else {
98
98
AssistantMessage responseOutput ;
99
99
try {
100
- ChatResponse response = call (state );
100
+ ChatResponse response = call (context );
101
101
responseOutput = response .getResult ().getOutput ();
102
102
}
103
103
catch (Exception e ) {
@@ -113,19 +113,24 @@ public Map<String, Object> apply(OverAllState state) throws Exception {
113
113
}
114
114
}
115
115
116
- private void initNodeWithState (OverAllState state ) {
116
+ private ExecutionContext initNodeWithState (OverAllState state ) {
117
+ String localUserPrompt = this .userPrompt ;
118
+ String localSystemPrompt = this .systemPrompt ;
119
+ Map <String , Object > localParams = new HashMap <>(this .params );
120
+ List <Message > localMessages = new ArrayList <>(this .messages );
121
+
117
122
if (StringUtils .hasLength (userPromptKey )) {
118
- this . userPrompt = (String ) state .value (userPromptKey ).orElse (this . userPrompt );
123
+ localUserPrompt = (String ) state .value (userPromptKey ).orElse (localUserPrompt );
119
124
}
120
125
if (StringUtils .hasLength (systemPromptKey )) {
121
- this . systemPrompt = (String ) state .value (systemPromptKey ).orElse (this . systemPrompt );
126
+ localSystemPrompt = (String ) state .value (systemPromptKey ).orElse (localSystemPrompt );
122
127
}
123
128
if (StringUtils .hasLength (paramsKey )) {
124
- this . params = (Map <String , Object >) state .value (paramsKey ).orElse (this . params );
129
+ localParams = (Map <String , Object >) state .value (paramsKey ).orElse (localParams );
125
130
}
126
131
// Used for adapting the dify's DSL conversion
127
- if (!this . params .isEmpty ()) {
128
- Map <String , Object > rawParams = this . params ;
132
+ if (!localParams .isEmpty ()) {
133
+ Map <String , Object > rawParams = localParams ;
129
134
Map <String , Object > filledParams = new HashMap <>();
130
135
for (Map .Entry <String , Object > entry : rawParams .entrySet ()) {
131
136
if (entry .getValue ().equals ("null" )) {
@@ -136,19 +141,32 @@ private void initNodeWithState(OverAllState state) {
136
141
filledParams .put (entry .getKey (), entry .getValue ());
137
142
}
138
143
}
139
-
140
- this .params = filledParams ;
144
+ localParams = filledParams ;
141
145
}
142
146
if (StringUtils .hasLength (messagesKey )) {
143
147
Object messagesValue = state .value (messagesKey ).orElse (null );
144
148
if (messagesValue != null ) {
145
149
List <Message > convertedMessages = convertToMessages (messagesValue );
146
- this . messages = convertedMessages .isEmpty () ? this . messages : convertedMessages ;
150
+ localMessages = convertedMessages .isEmpty () ? localMessages : convertedMessages ;
147
151
}
148
152
}
149
- if (StringUtils .hasLength (userPrompt ) && !params .isEmpty ()) {
150
- this .userPrompt = renderPromptTemplate (userPrompt , params );
153
+
154
+ String renderedUserPrompt = localUserPrompt ;
155
+ String renderedSystemPrompt = localSystemPrompt ;
156
+
157
+ if (StringUtils .hasLength (localUserPrompt ) && !localParams .isEmpty ()) {
158
+ renderedUserPrompt = renderPromptTemplate (localUserPrompt , localParams );
159
+ }
160
+
161
+ if (StringUtils .hasLength (localSystemPrompt )) {
162
+ if (!localParams .isEmpty ()) {
163
+ renderedSystemPrompt = renderPromptTemplate (localSystemPrompt , localParams );
164
+ } else {
165
+ renderedSystemPrompt = renderPromptTemplate (localSystemPrompt , state .data ());
166
+ }
151
167
}
168
+
169
+ return new ExecutionContext (renderedSystemPrompt , renderedUserPrompt , localParams , localMessages , state );
152
170
}
153
171
154
172
public void setToolCallbacks (List <ToolCallback > toolCallbacks ) {
@@ -160,35 +178,26 @@ private String renderPromptTemplate(String prompt, Map<String, Object> params) {
160
178
return promptTemplate .render (params );
161
179
}
162
180
163
- public Flux <ChatResponse > stream (OverAllState state ) {
164
- return buildChatClientRequestSpec (state ).stream ().chatResponse ();
181
+ public Flux <ChatResponse > stream (ExecutionContext context ) {
182
+ return buildChatClientRequestSpec (context ).stream ().chatResponse ();
165
183
}
166
184
167
- public ChatResponse call (OverAllState state ) {
168
- return buildChatClientRequestSpec (state ).call ().chatResponse ();
185
+ public ChatResponse call (ExecutionContext context ) {
186
+ return buildChatClientRequestSpec (context ).call ().chatResponse ();
169
187
}
170
188
171
- private ChatClient .ChatClientRequestSpec buildChatClientRequestSpec (OverAllState state ) {
189
+ private ChatClient .ChatClientRequestSpec buildChatClientRequestSpec (ExecutionContext context ) {
172
190
ChatClient .ChatClientRequestSpec chatClientRequestSpec = chatClient .prompt ()
173
191
.toolCallbacks (toolCallbacks )
174
- .messages (messages )
192
+ .messages (context . messages )
175
193
.advisors (advisors );
176
194
177
- if (StringUtils .hasLength (systemPrompt )) {
178
- if (!params .isEmpty ()) {
179
- systemPrompt = renderPromptTemplate (systemPrompt , params );
180
- } else {
181
- // try render with state
182
- systemPrompt = renderPromptTemplate (systemPrompt , state .data ());
183
- }
184
- chatClientRequestSpec .system (systemPrompt );
195
+ if (StringUtils .hasLength (context .systemPrompt )) {
196
+ chatClientRequestSpec .system (context .systemPrompt );
185
197
}
186
198
187
- if (StringUtils .hasLength (userPrompt )) {
188
- if (!params .isEmpty ()) {
189
- userPrompt = renderPromptTemplate (userPrompt , params );
190
- }
191
- chatClientRequestSpec .user (userPrompt );
199
+ if (StringUtils .hasLength (context .userPrompt )) {
200
+ chatClientRequestSpec .user (context .userPrompt );
192
201
}
193
202
194
203
return chatClientRequestSpec ;
@@ -315,4 +324,21 @@ public LlmNode build() {
315
324
316
325
}
317
326
327
+ private static class ExecutionContext {
328
+ final String systemPrompt ;
329
+ final String userPrompt ;
330
+ final Map <String , Object > params ;
331
+ final List <Message > messages ;
332
+ final OverAllState state ;
333
+
334
+ ExecutionContext (String systemPrompt , String userPrompt , Map <String , Object > params ,
335
+ List <Message > messages , OverAllState state ) {
336
+ this .systemPrompt = systemPrompt ;
337
+ this .userPrompt = userPrompt ;
338
+ this .params = params ;
339
+ this .messages = messages ;
340
+ this .state = state ;
341
+ }
342
+ }
343
+
318
344
}
0 commit comments