@@ -134,6 +134,8 @@ public static class DefaultPromptUserSpec implements PromptUserSpec {
134
134
135
135
private final Map <String , Object > params = new HashMap <>();
136
136
137
+ private final Map <String , Object > metadata = new HashMap <>();
138
+
137
139
private final List <Media > media = new ArrayList <>();
138
140
139
141
@ Nullable
@@ -212,6 +214,23 @@ public PromptUserSpec params(Map<String, Object> params) {
212
214
return this ;
213
215
}
214
216
217
+ @ Override
218
+ public PromptUserSpec metadata (Map <String , Object > metadata ) {
219
+ Assert .notNull (metadata , "metadata cannot be null" );
220
+ Assert .noNullElements (metadata .keySet (), "metadata keys cannot contain null elements" );
221
+ Assert .noNullElements (metadata .values (), "metadata values cannot contain null elements" );
222
+ this .metadata .putAll (metadata );
223
+ return this ;
224
+ }
225
+
226
+ @ Override
227
+ public PromptUserSpec metadata (String key , Object value ) {
228
+ Assert .hasText (key , "metadata key cannot be null or empty" );
229
+ Assert .notNull (value , "metadata value cannot be null" );
230
+ this .metadata .put (key , value );
231
+ return this ;
232
+ }
233
+
215
234
@ Nullable
216
235
protected String text () {
217
236
return this .text ;
@@ -225,12 +244,18 @@ protected List<Media> media() {
225
244
return this .media ;
226
245
}
227
246
247
+ protected Map <String , Object > metadata () {
248
+ return this .metadata ;
249
+ }
250
+
228
251
}
229
252
230
253
public static class DefaultPromptSystemSpec implements PromptSystemSpec {
231
254
232
255
private final Map <String , Object > params = new HashMap <>();
233
256
257
+ private final Map <String , Object > metadata = new HashMap <>();
258
+
234
259
@ Nullable
235
260
private String text ;
236
261
@@ -278,6 +303,23 @@ public PromptSystemSpec params(Map<String, Object> params) {
278
303
return this ;
279
304
}
280
305
306
+ @ Override
307
+ public PromptSystemSpec metadata (Map <String , Object > metadata ) {
308
+ Assert .notNull (metadata , "metadata cannot be null" );
309
+ Assert .noNullElements (metadata .keySet (), "metadata keys cannot contain null elements" );
310
+ Assert .noNullElements (metadata .values (), "metadata values cannot contain null elements" );
311
+ this .metadata .putAll (metadata );
312
+ return this ;
313
+ }
314
+
315
+ @ Override
316
+ public PromptSystemSpec metadata (String key , Object value ) {
317
+ Assert .hasText (key , "metadata key cannot be null or empty" );
318
+ Assert .notNull (value , "metadata value cannot be null" );
319
+ this .metadata .put (key , value );
320
+ return this ;
321
+ }
322
+
281
323
@ Nullable
282
324
protected String text () {
283
325
return this .text ;
@@ -287,6 +329,10 @@ protected Map<String, Object> params() {
287
329
return this .params ;
288
330
}
289
331
332
+ protected Map <String , Object > metadata () {
333
+ return this .metadata ;
334
+ }
335
+
290
336
}
291
337
292
338
public static class DefaultAdvisorSpec implements AdvisorSpec {
@@ -576,8 +622,12 @@ public static class DefaultChatClientRequestSpec implements ChatClientRequestSpe
576
622
577
623
private final Map <String , Object > userParams = new HashMap <>();
578
624
625
+ private final Map <String , Object > userMetadata = new HashMap <>();
626
+
579
627
private final Map <String , Object > systemParams = new HashMap <>();
580
628
629
+ private final Map <String , Object > systemMetadata = new HashMap <>();
630
+
581
631
private final List <Advisor > advisors = new ArrayList <>();
582
632
583
633
private final Map <String , Object > advisorParams = new HashMap <>();
@@ -597,22 +647,25 @@ public static class DefaultChatClientRequestSpec implements ChatClientRequestSpe
597
647
598
648
/* copy constructor */
599
649
DefaultChatClientRequestSpec (DefaultChatClientRequestSpec ccr ) {
600
- this (ccr .chatModel , ccr .userText , ccr .userParams , ccr .systemText , ccr .systemParams , ccr .toolCallbacks ,
601
- ccr .messages , ccr .toolNames , ccr .media , ccr .chatOptions , ccr .advisors , ccr .advisorParams ,
602
- ccr .observationRegistry , ccr .observationConvention , ccr .toolContext , ccr .templateRenderer );
650
+ this (ccr .chatModel , ccr .userText , ccr .userParams , ccr .userMetadata , ccr .systemText , ccr .systemParams ,
651
+ ccr .systemMetadata , ccr .toolCallbacks , ccr .messages , ccr .toolNames , ccr .media , ccr .chatOptions ,
652
+ ccr .advisors , ccr .advisorParams , ccr .observationRegistry , ccr .observationConvention ,
653
+ ccr .toolContext , ccr .templateRenderer );
603
654
}
604
655
605
656
public DefaultChatClientRequestSpec (ChatModel chatModel , @ Nullable String userText ,
606
- Map <String , Object > userParams , @ Nullable String systemText , Map <String , Object > systemParams ,
607
- List < ToolCallback > toolCallbacks , List < Message > messages , List <String > toolNames , List <Media > media ,
608
- @ Nullable ChatOptions chatOptions , List <Advisor > advisors , Map < String , Object > advisorParams ,
609
- ObservationRegistry observationRegistry ,
657
+ Map <String , Object > userParams , Map <String , Object > userMetadata , @ Nullable String systemText ,
658
+ Map < String , Object > systemParams , Map <String , Object > systemMetadata , List <ToolCallback > toolCallbacks ,
659
+ List < Message > messages , List <String > toolNames , List < Media > media , @ Nullable ChatOptions chatOptions ,
660
+ List < Advisor > advisors , Map < String , Object > advisorParams , ObservationRegistry observationRegistry ,
610
661
@ Nullable ChatClientObservationConvention observationConvention , Map <String , Object > toolContext ,
611
662
@ Nullable TemplateRenderer templateRenderer ) {
612
663
613
664
Assert .notNull (chatModel , "chatModel cannot be null" );
614
665
Assert .notNull (userParams , "userParams cannot be null" );
666
+ Assert .notNull (userMetadata , "userMetadata cannot be null" );
615
667
Assert .notNull (systemParams , "systemParams cannot be null" );
668
+ Assert .notNull (systemMetadata , "systemMetadata cannot be null" );
616
669
Assert .notNull (toolCallbacks , "toolCallbacks cannot be null" );
617
670
Assert .notNull (messages , "messages cannot be null" );
618
671
Assert .notNull (toolNames , "toolNames cannot be null" );
@@ -628,8 +681,11 @@ public DefaultChatClientRequestSpec(ChatModel chatModel, @Nullable String userTe
628
681
629
682
this .userText = userText ;
630
683
this .userParams .putAll (userParams );
684
+ this .userMetadata .putAll (userMetadata );
685
+
631
686
this .systemText = systemText ;
632
687
this .systemParams .putAll (systemParams );
688
+ this .systemMetadata .putAll (systemMetadata );
633
689
634
690
this .toolNames .addAll (toolNames );
635
691
this .toolCallbacks .addAll (toolCallbacks );
@@ -653,6 +709,10 @@ public Map<String, Object> getUserParams() {
653
709
return this .userParams ;
654
710
}
655
711
712
+ public Map <String , Object > getUserMetadata () {
713
+ return this .userMetadata ;
714
+ }
715
+
656
716
@ Nullable
657
717
public String getSystemText () {
658
718
return this .systemText ;
@@ -662,6 +722,10 @@ public Map<String, Object> getSystemParams() {
662
722
return this .systemParams ;
663
723
}
664
724
725
+ public Map <String , Object > getSystemMetadata () {
726
+ return this .systemMetadata ;
727
+ }
728
+
665
729
@ Nullable
666
730
public ChatOptions getChatOptions () {
667
731
return this .chatOptions ;
@@ -717,12 +781,15 @@ public Builder mutate() {
717
781
}
718
782
719
783
if (StringUtils .hasText (this .userText )) {
720
- builder .defaultUser (
721
- u -> u .text (this .userText ).params (this .userParams ).media (this .media .toArray (new Media [0 ])));
784
+ builder .defaultUser (u -> u .text (this .userText )
785
+ .params (this .userParams )
786
+ .media (this .media .toArray (new Media [0 ]))
787
+ .metadata (this .userMetadata ));
722
788
}
723
789
724
790
if (StringUtils .hasText (this .systemText )) {
725
- builder .defaultSystem (s -> s .text (this .systemText ).params (this .systemParams ));
791
+ builder .defaultSystem (
792
+ s -> s .text (this .systemText ).params (this .systemParams ).metadata (this .systemMetadata ));
726
793
}
727
794
728
795
if (this .chatOptions != null ) {
@@ -734,6 +801,7 @@ public Builder mutate() {
734
801
return builder ;
735
802
}
736
803
804
+ @ Override
737
805
public ChatClientRequestSpec advisors (Consumer <ChatClient .AdvisorSpec > consumer ) {
738
806
Assert .notNull (consumer , "consumer cannot be null" );
739
807
var advisorSpec = new DefaultAdvisorSpec ();
@@ -743,27 +811,31 @@ public ChatClientRequestSpec advisors(Consumer<ChatClient.AdvisorSpec> consumer)
743
811
return this ;
744
812
}
745
813
814
+ @ Override
746
815
public ChatClientRequestSpec advisors (Advisor ... advisors ) {
747
816
Assert .notNull (advisors , "advisors cannot be null" );
748
817
Assert .noNullElements (advisors , "advisors cannot contain null elements" );
749
818
this .advisors .addAll (Arrays .asList (advisors ));
750
819
return this ;
751
820
}
752
821
822
+ @ Override
753
823
public ChatClientRequestSpec advisors (List <Advisor > advisors ) {
754
824
Assert .notNull (advisors , "advisors cannot be null" );
755
825
Assert .noNullElements (advisors , "advisors cannot contain null elements" );
756
826
this .advisors .addAll (advisors );
757
827
return this ;
758
828
}
759
829
830
+ @ Override
760
831
public ChatClientRequestSpec messages (Message ... messages ) {
761
832
Assert .notNull (messages , "messages cannot be null" );
762
833
Assert .noNullElements (messages , "messages cannot contain null elements" );
763
834
this .messages .addAll (List .of (messages ));
764
835
return this ;
765
836
}
766
837
838
+ @ Override
767
839
public ChatClientRequestSpec messages (List <Message > messages ) {
768
840
Assert .notNull (messages , "messages cannot be null" );
769
841
Assert .noNullElements (messages , "messages cannot contain null elements" );
@@ -819,6 +891,7 @@ public ChatClientRequestSpec toolCallbacks(ToolCallbackProvider... toolCallbackP
819
891
return this ;
820
892
}
821
893
894
+ @ Override
822
895
public ChatClientRequestSpec toolContext (Map <String , Object > toolContext ) {
823
896
Assert .notNull (toolContext , "toolContext cannot be null" );
824
897
Assert .noNullElements (toolContext .keySet (), "toolContext keys cannot contain null elements" );
@@ -827,12 +900,14 @@ public ChatClientRequestSpec toolContext(Map<String, Object> toolContext) {
827
900
return this ;
828
901
}
829
902
903
+ @ Override
830
904
public ChatClientRequestSpec system (String text ) {
831
905
Assert .hasText (text , "text cannot be null or empty" );
832
906
this .systemText = text ;
833
907
return this ;
834
908
}
835
909
910
+ @ Override
836
911
public ChatClientRequestSpec system (Resource text , Charset charset ) {
837
912
Assert .notNull (text , "text cannot be null" );
838
913
Assert .notNull (charset , "charset cannot be null" );
@@ -846,28 +921,32 @@ public ChatClientRequestSpec system(Resource text, Charset charset) {
846
921
return this ;
847
922
}
848
923
924
+ @ Override
849
925
public ChatClientRequestSpec system (Resource text ) {
850
926
Assert .notNull (text , "text cannot be null" );
851
927
return this .system (text , Charset .defaultCharset ());
852
928
}
853
929
930
+ @ Override
854
931
public ChatClientRequestSpec system (Consumer <PromptSystemSpec > consumer ) {
855
932
Assert .notNull (consumer , "consumer cannot be null" );
856
933
857
934
var systemSpec = new DefaultPromptSystemSpec ();
858
935
consumer .accept (systemSpec );
859
936
this .systemText = StringUtils .hasText (systemSpec .text ()) ? systemSpec .text () : this .systemText ;
860
937
this .systemParams .putAll (systemSpec .params ());
861
-
938
+ this . systemMetadata . putAll ( systemSpec . metadata ());
862
939
return this ;
863
940
}
864
941
942
+ @ Override
865
943
public ChatClientRequestSpec user (String text ) {
866
944
Assert .hasText (text , "text cannot be null or empty" );
867
945
this .userText = text ;
868
946
return this ;
869
947
}
870
948
949
+ @ Override
871
950
public ChatClientRequestSpec user (Resource text , Charset charset ) {
872
951
Assert .notNull (text , "text cannot be null" );
873
952
Assert .notNull (charset , "charset cannot be null" );
@@ -881,11 +960,13 @@ public ChatClientRequestSpec user(Resource text, Charset charset) {
881
960
return this ;
882
961
}
883
962
963
+ @ Override
884
964
public ChatClientRequestSpec user (Resource text ) {
885
965
Assert .notNull (text , "text cannot be null" );
886
966
return this .user (text , Charset .defaultCharset ());
887
967
}
888
968
969
+ @ Override
889
970
public ChatClientRequestSpec user (Consumer <PromptUserSpec > consumer ) {
890
971
Assert .notNull (consumer , "consumer cannot be null" );
891
972
@@ -894,21 +975,25 @@ public ChatClientRequestSpec user(Consumer<PromptUserSpec> consumer) {
894
975
this .userText = StringUtils .hasText (us .text ()) ? us .text () : this .userText ;
895
976
this .userParams .putAll (us .params ());
896
977
this .media .addAll (us .media ());
978
+ this .userMetadata .putAll (us .metadata ());
897
979
return this ;
898
980
}
899
981
982
+ @ Override
900
983
public ChatClientRequestSpec templateRenderer (TemplateRenderer templateRenderer ) {
901
984
Assert .notNull (templateRenderer , "templateRenderer cannot be null" );
902
985
this .templateRenderer = templateRenderer ;
903
986
return this ;
904
987
}
905
988
989
+ @ Override
906
990
public CallResponseSpec call () {
907
991
BaseAdvisorChain advisorChain = buildAdvisorChain ();
908
992
return new DefaultCallResponseSpec (DefaultChatClientUtils .toChatClientRequest (this ), advisorChain ,
909
993
this .observationRegistry , this .observationConvention );
910
994
}
911
995
996
+ @ Override
912
997
public StreamResponseSpec stream () {
913
998
BaseAdvisorChain advisorChain = buildAdvisorChain ();
914
999
return new DefaultStreamResponseSpec (DefaultChatClientUtils .toChatClientRequest (this ), advisorChain ,
0 commit comments