75
75
* @author Soby Chacko
76
76
* @author Dariusz Jedrzejczyk
77
77
* @author Thomas Vitale
78
+ * @author Andres da Silva Santos
78
79
* @since 1.0.0
79
80
*/
80
81
public class DefaultChatClient implements ChatClient {
@@ -288,6 +289,68 @@ protected Map<String, Object> params() {
288
289
289
290
}
290
291
292
+ public static class DefaultPromptDeveloperSpec implements PromptDeveloperSpec {
293
+
294
+ private final Map <String , Object > params = new HashMap <>();
295
+
296
+ @ Nullable
297
+ private String text ;
298
+
299
+ @ Override
300
+ public PromptDeveloperSpec text (String text ) {
301
+ Assert .hasText (text , "text cannot be null or empty" );
302
+ this .text = text ;
303
+ return this ;
304
+ }
305
+
306
+ @ Override
307
+ public PromptDeveloperSpec text (Resource text , Charset charset ) {
308
+ Assert .notNull (text , "text cannot be null" );
309
+ Assert .notNull (charset , "charset cannot be null" );
310
+ try {
311
+ this .text (text .getContentAsString (charset ));
312
+ }
313
+ catch (IOException e ) {
314
+ throw new RuntimeException (e );
315
+ }
316
+ return this ;
317
+ }
318
+
319
+ @ Override
320
+ public PromptDeveloperSpec text (Resource text ) {
321
+ Assert .notNull (text , "text cannot be null" );
322
+ this .text (text , Charset .defaultCharset ());
323
+ return this ;
324
+ }
325
+
326
+ @ Override
327
+ public PromptDeveloperSpec param (String key , Object value ) {
328
+ Assert .hasText (key , "key cannot be null or empty" );
329
+ Assert .notNull (value , "value cannot be null" );
330
+ this .params .put (key , value );
331
+ return this ;
332
+ }
333
+
334
+ @ Override
335
+ public PromptDeveloperSpec params (Map <String , Object > params ) {
336
+ Assert .notNull (params , "params cannot be null" );
337
+ Assert .noNullElements (params .keySet (), "param keys cannot contain null elements" );
338
+ Assert .noNullElements (params .values (), "param values cannot contain null elements" );
339
+ this .params .putAll (params );
340
+ return this ;
341
+ }
342
+
343
+ @ Nullable
344
+ protected String text () {
345
+ return this .text ;
346
+ }
347
+
348
+ protected Map <String , Object > params () {
349
+ return this .params ;
350
+ }
351
+
352
+ }
353
+
291
354
public static class DefaultAdvisorSpec implements AdvisorSpec {
292
355
293
356
private final List <Advisor > advisors = new ArrayList <>();
@@ -577,6 +640,8 @@ public static class DefaultChatClientRequestSpec implements ChatClientRequestSpe
577
640
578
641
private final Map <String , Object > systemParams = new HashMap <>();
579
642
643
+ private final Map <String , Object > developerParams = new HashMap <>();
644
+
580
645
private final List <Advisor > advisors = new ArrayList <>();
581
646
582
647
private final Map <String , Object > advisorParams = new HashMap <>();
@@ -591,27 +656,32 @@ public static class DefaultChatClientRequestSpec implements ChatClientRequestSpe
591
656
@ Nullable
592
657
private String systemText ;
593
658
659
+ @ Nullable
660
+ private String developerText ;
661
+
594
662
@ Nullable
595
663
private ChatOptions chatOptions ;
596
664
597
665
/* copy constructor */
598
666
DefaultChatClientRequestSpec (DefaultChatClientRequestSpec ccr ) {
599
- this (ccr .chatModel , ccr .userText , ccr .userParams , ccr .systemText , ccr .systemParams , ccr .toolCallbacks ,
600
- ccr .messages , ccr .toolNames , ccr .media , ccr .chatOptions , ccr .advisors , ccr .advisorParams ,
601
- ccr .observationRegistry , ccr .observationConvention , ccr .toolContext , ccr .templateRenderer );
667
+ this (ccr .chatModel , ccr .userText , ccr .userParams , ccr .systemText , ccr .systemParams , ccr .developerText ,
668
+ ccr .developerParams , ccr .toolCallbacks , ccr .messages , ccr .toolNames , ccr .media , ccr .chatOptions ,
669
+ ccr .advisors , ccr .advisorParams , ccr .observationRegistry , ccr .observationConvention ,
670
+ ccr .toolContext , ccr .templateRenderer );
602
671
}
603
672
604
673
public DefaultChatClientRequestSpec (ChatModel chatModel , @ Nullable String userText ,
605
674
Map <String , Object > userParams , @ Nullable String systemText , Map <String , Object > systemParams ,
606
- List < ToolCallback > toolCallbacks , List < Message > messages , List <String > toolNames , List <Media > media ,
607
- @ Nullable ChatOptions chatOptions , List <Advisor > advisors , Map < String , Object > advisorParams ,
608
- ObservationRegistry observationRegistry ,
675
+ @ Nullable String developerText , Map <String , Object > developerParams , List <ToolCallback > toolCallbacks ,
676
+ List < Message > messages , List <String > toolNames , List < Media > media , @ Nullable ChatOptions chatOptions ,
677
+ List < Advisor > advisors , Map < String , Object > advisorParams , ObservationRegistry observationRegistry ,
609
678
@ Nullable ChatClientObservationConvention observationConvention , Map <String , Object > toolContext ,
610
679
@ Nullable TemplateRenderer templateRenderer ) {
611
680
612
681
Assert .notNull (chatModel , "chatModel cannot be null" );
613
682
Assert .notNull (userParams , "userParams cannot be null" );
614
683
Assert .notNull (systemParams , "systemParams cannot be null" );
684
+ Assert .notNull (developerParams , "developerParams cannot be null" );
615
685
Assert .notNull (toolCallbacks , "toolCallbacks cannot be null" );
616
686
Assert .notNull (messages , "messages cannot be null" );
617
687
Assert .notNull (toolNames , "toolNames cannot be null" );
@@ -629,6 +699,8 @@ public DefaultChatClientRequestSpec(ChatModel chatModel, @Nullable String userTe
629
699
this .userParams .putAll (userParams );
630
700
this .systemText = systemText ;
631
701
this .systemParams .putAll (systemParams );
702
+ this .developerText = developerText ;
703
+ this .developerParams .putAll (developerParams );
632
704
633
705
this .toolNames .addAll (toolNames );
634
706
this .toolCallbacks .addAll (toolCallbacks );
@@ -661,6 +733,15 @@ public Map<String, Object> getSystemParams() {
661
733
return this .systemParams ;
662
734
}
663
735
736
+ @ Nullable
737
+ public String getDeveloperText () {
738
+ return this .developerText ;
739
+ }
740
+
741
+ public Map <String , Object > getDeveloperParams () {
742
+ return this .developerParams ;
743
+ }
744
+
664
745
@ Nullable
665
746
public ChatOptions getChatOptions () {
666
747
return this .chatOptions ;
@@ -719,6 +800,10 @@ public Builder mutate() {
719
800
builder .defaultSystem (s -> s .text (this .systemText ).params (this .systemParams ));
720
801
}
721
802
803
+ if (StringUtils .hasText (this .developerText )) {
804
+ builder .defaultDeveloper (s -> s .text (this .developerText ).params (this .developerParams ));
805
+ }
806
+
722
807
if (this .chatOptions != null ) {
723
808
builder .defaultOptions (this .chatOptions );
724
809
}
@@ -821,6 +906,41 @@ public ChatClientRequestSpec toolContext(Map<String, Object> toolContext) {
821
906
return this ;
822
907
}
823
908
909
+ public ChatClientRequestSpec developer (String text ) {
910
+ Assert .hasText (text , "text cannot be null or empty" );
911
+ this .developerText = text ;
912
+ return this ;
913
+ }
914
+
915
+ public ChatClientRequestSpec developer (Resource text , Charset charset ) {
916
+ Assert .notNull (text , "text cannot be null" );
917
+ Assert .notNull (charset , "charset cannot be null" );
918
+
919
+ try {
920
+ this .developerText = text .getContentAsString (charset );
921
+ }
922
+ catch (IOException e ) {
923
+ throw new RuntimeException (e );
924
+ }
925
+ return this ;
926
+ }
927
+
928
+ public ChatClientRequestSpec developer (Resource text ) {
929
+ Assert .notNull (text , "text cannot be null" );
930
+ return this .developer (text , Charset .defaultCharset ());
931
+ }
932
+
933
+ public ChatClientRequestSpec developer (Consumer <PromptDeveloperSpec > consumer ) {
934
+ Assert .notNull (consumer , "consumer cannot be null" );
935
+
936
+ var developerSpec = new DefaultPromptDeveloperSpec ();
937
+ consumer .accept (developerSpec );
938
+ this .developerText = StringUtils .hasText (developerSpec .text ()) ? developerSpec .text () : this .developerText ;
939
+ this .developerParams .putAll (developerSpec .params ());
940
+
941
+ return this ;
942
+ }
943
+
824
944
public ChatClientRequestSpec system (String text ) {
825
945
Assert .hasText (text , "text cannot be null or empty" );
826
946
this .systemText = text ;
0 commit comments