2020import  java .util .function .BiFunction ;
2121import  java .util .function .Function ;
2222
23+ import  com .fasterxml .jackson .core .JsonProcessingException ;
2324import  com .fasterxml .jackson .databind .DeserializationFeature ;
2425import  com .fasterxml .jackson .databind .ObjectMapper ;
2526import  com .fasterxml .jackson .databind .SerializationFeature ;
3031import  org .springframework .ai .chat .model .ToolContext ;
3132import  org .springframework .ai .model .ModelOptionsUtils ;
3233import  org .springframework .ai .model .function .FunctionCallback .Builder ;
33- import  org .springframework .ai .model .function .FunctionCallback .FunctionInvokerBuilder ;
34- import  org .springframework .ai .model .function .FunctionCallback .MethodInvokerBuilder ;
34+ import  org .springframework .ai .model .function .FunctionCallback .FunctionInvokingSpec ;
35+ import  org .springframework .ai .model .function .FunctionCallback .MethodInvokingSpec ;
3536import  org .springframework .ai .model .function .FunctionCallbackContext .SchemaType ;
3637import  org .springframework .ai .util .JacksonUtils ;
3738import  org .springframework .ai .util .ParsingUtils ;
@@ -64,8 +65,8 @@ public class DefaultFunctionCallbackBuilder implements FunctionCallback.Builder
6465	 * The function to convert the response object to a string. The default is to convert 
6566	 * the response to a JSON string. 
6667	 */ 
67- 	private  Function <Object , String > responseConverter  = input  -> (input  instanceof  String ) ? ""  + input 
68- 			: ModelOptionsUtils .toJsonString (input );
68+ 	private  Function <Object , String > responseConverter  = response  -> (response  instanceof  String ) ? ""  + response 
69+ 			: this .toJsonString (response );
6970
7071	/** 
7172	 * (Optional) Instead of generating the input type schema from the input type or 
@@ -80,6 +81,15 @@ public class DefaultFunctionCallbackBuilder implements FunctionCallback.Builder
8081		.disable (SerializationFeature .FAIL_ON_EMPTY_BEANS )
8182		.build ();
8283
84+ 	private  String  toJsonString (Object  object ) {
85+ 		try  {
86+ 			return  this .objectMapper .writeValueAsString (object );
87+ 		}
88+ 		catch  (JsonProcessingException  e ) {
89+ 			throw  new  RuntimeException (e );
90+ 		}
91+ 	}
92+ 
8393	@ Override 
8494	public  Builder  description (String  description ) {
8595		Assert .hasText (description , "Description must not be empty" );
@@ -116,21 +126,21 @@ public Builder objectMapper(ObjectMapper objectMapper) {
116126	}
117127
118128	@ Override 
119- 	public  <I , O > FunctionInvokerBuilder <I , O > function (String  name , Function <I , O > function ) {
120- 		return  new  FunctionInvokerBuilderImpl <>(name , function );
129+ 	public  <I , O > FunctionInvokingSpec <I , O > function (String  name , Function <I , O > function ) {
130+ 		return  new  DefaultFunctionInvokingSpec <>(name , function );
121131	}
122132
123133	@ Override 
124- 	public  <I , O > FunctionInvokerBuilder <I , O > function (String  name , BiFunction <I , ToolContext , O > biFunction ) {
125- 		return  new  FunctionInvokerBuilderImpl <>(name , biFunction );
134+ 	public  <I , O > FunctionInvokingSpec <I , O > function (String  name , BiFunction <I , ToolContext , O > biFunction ) {
135+ 		return  new  DefaultFunctionInvokingSpec <>(name , biFunction );
126136	}
127137
128138	@ Override 
129- 	public  MethodInvokerBuilder  method (String  methodName , Class <?>... argumentTypes ) {
130- 		return  new  MethodInvokerBuilderImpl (methodName , argumentTypes );
139+ 	public  MethodInvokingSpec  method (String  methodName , Class <?>... argumentTypes ) {
140+ 		return  new  DefaultMethodInvokingSpec (methodName , argumentTypes );
131141	}
132142
133- 	public   class  FunctionInvokerBuilderImpl <I , O > implements  FunctionInvokerBuilder <I , O > {
143+ 	class  DefaultFunctionInvokingSpec <I , O > implements  FunctionInvokingSpec <I , O > {
134144
135145		private  final  String  name ;
136146
@@ -140,15 +150,15 @@ public class FunctionInvokerBuilderImpl<I, O> implements FunctionInvokerBuilder<
140150
141151		private  final  Function <I , O > function ;
142152
143- 		private  FunctionInvokerBuilderImpl (String  name , BiFunction <I , ToolContext , O > biFunction ) {
153+ 		private  DefaultFunctionInvokingSpec (String  name , BiFunction <I , ToolContext , O > biFunction ) {
144154			Assert .hasText (name , "Name must not be empty" );
145155			Assert .notNull (biFunction , "BiFunction must not be null" );
146156			this .name  = name ;
147157			this .biFunction  = biFunction ;
148158			this .function  = null ;
149159		}
150160
151- 		private  FunctionInvokerBuilderImpl (String  name , Function <I , O > function ) {
161+ 		private  DefaultFunctionInvokingSpec (String  name , Function <I , O > function ) {
152162			Assert .hasText (name , "Name must not be empty" );
153163			Assert .notNull (function , "Function must not be null" );
154164			this .name  = name ;
@@ -157,14 +167,14 @@ private FunctionInvokerBuilderImpl(String name, Function<I, O> function) {
157167		}
158168
159169		@ Override 
160- 		public  FunctionInvokerBuilder <I , O > inputType (Class <?> inputType ) {
170+ 		public  FunctionInvokingSpec <I , O > inputType (Class <?> inputType ) {
161171			Assert .notNull (inputType , "InputType must not be null" );
162172			this .inputType  = inputType ;
163173			return  this ;
164174		}
165175
166176		@ Override 
167- 		public  FunctionInvokerBuilder <I , O > inputType (ParameterizedTypeReference <?> inputType ) {
177+ 		public  FunctionInvokingSpec <I , O > inputType (ParameterizedTypeReference <?> inputType ) {
168178			Assert .notNull (inputType , "InputType must not be null" );
169179			this .inputType  = inputType .getType ();
170180			;
@@ -187,8 +197,8 @@ public FunctionCallback build() {
187197			BiFunction <I , ToolContext , O > finalBiFunction  = (this .biFunction  != null ) ? this .biFunction 
188198					: (request , context ) -> this .function .apply (request );
189199
190- 			return  new  FunctionCallbackWrapper (this .name , this .getDescription (), inputTypeSchema ,  this . inputType ,
191- 					(Function <I , String >) responseConverter , objectMapper , finalBiFunction );
200+ 			return  new  FunctionInvokingFunctionCallback (this .name , this .getDescription (), inputTypeSchema ,
201+ 					this . inputType ,  (Function <I , String >) responseConverter , objectMapper , finalBiFunction );
192202		}
193203
194204		private  String  getDescription () {
@@ -200,7 +210,7 @@ private String getDescription() {
200210
201211	}
202212
203- 	public   class  MethodInvokerBuilderImpl  implements  FunctionCallback .MethodInvokerBuilder  {
213+ 	class  DefaultMethodInvokingSpec  implements  FunctionCallback .MethodInvokingSpec  {
204214
205215		private  String  name ;
206216
@@ -212,27 +222,27 @@ public class MethodInvokerBuilderImpl implements FunctionCallback.MethodInvokerB
212222
213223		private  final  Class <?>[] argumentTypes ;
214224
215- 		private  MethodInvokerBuilderImpl (String  methodName , Class <?>... argumentTypes ) {
225+ 		private  DefaultMethodInvokingSpec (String  methodName , Class <?>... argumentTypes ) {
216226			Assert .hasText (methodName , "Method name must not be null" );
217227			Assert .notNull (argumentTypes , "Argument types must not be null" );
218228			this .methodName  = methodName ;
219229			this .argumentTypes  = argumentTypes ;
220230		}
221231
222- 		public  MethodInvokerBuilder  name (String  name ) {
232+ 		public  MethodInvokingSpec  name (String  name ) {
223233			Assert .hasText (name , "Name must not be empty" );
224234			this .name  = name ;
225235			return  this ;
226236		}
227237
228- 		public  MethodInvokerBuilder  targetClass (Class <?> targetClass ) {
238+ 		public  MethodInvokingSpec  targetClass (Class <?> targetClass ) {
229239			Assert .notNull (targetClass , "Target class must not be null" );
230240			this .targetClass  = targetClass ;
231241			return  this ;
232242		}
233243
234244		@ Override 
235- 		public  MethodInvokerBuilder  targetObject (Object  methodObject ) {
245+ 		public  MethodInvokingSpec  targetObject (Object  methodObject ) {
236246			Assert .notNull (methodObject , "Method object must not be null" );
237247			this .targetObject  = methodObject ;
238248			this .targetClass  = methodObject .getClass ();
@@ -246,8 +256,8 @@ public FunctionCallback build() {
246256			var  method  = ReflectionUtils .findMethod (targetClass , methodName , argumentTypes );
247257			Assert .notNull (method ,
248258					"Method: '"  + methodName  + "' with arguments:"  + Arrays .toString (argumentTypes ) + " not found!" );
249- 			return  new  MethodFunctionCallback (this .targetObject , method , this .getDescription (), objectMapper ,  this . name ,
250- 					responseConverter );
259+ 			return  new  MethodInvokingFunctionCallback (this .targetObject , method , this .getDescription (), objectMapper ,
260+ 					this . name ,  responseConverter );
251261		}
252262
253263		private  String  getDescription () {
@@ -264,9 +274,9 @@ private String generateDescription(String fromName) {
264274
265275		String  generatedDescription  = ParsingUtils .reConcatenateCamelCase (fromName , " " );
266276
267- 		logger .warn ("Description is not set! A best effort attempt to generate a description:'{}' from the:'{}'" ,
277+ 		logger .info ("Description is not set! A best effort attempt to generate a description:'{}' from the:'{}'" ,
268278				generatedDescription , fromName );
269- 		logger .warn ("It is recommended to set the Description explicitly! Use the 'description()' method!" );
279+ 		logger .info ("It is recommended to set the Description explicitly! Use the 'description()' method!" );
270280
271281		return  generatedDescription ;
272282	}
0 commit comments