2121import java .util .function .Function ;
2222import java .util .stream .Collectors ;
2323
24+ import io .micrometer .observation .ObservationRegistry ;
2425import org .junit .jupiter .api .condition .EnabledIfEnvironmentVariable ;
2526import org .junit .jupiter .params .ParameterizedTest ;
2627import org .junit .jupiter .params .provider .ValueSource ;
3435import org .springframework .ai .chat .client .advisor .api .CallAroundAdvisor ;
3536import org .springframework .ai .chat .client .advisor .api .CallAroundAdvisorChain ;
3637import org .springframework .ai .converter .BeanOutputConverter ;
38+ import org .springframework .ai .model .function .FunctionCallback ;
39+ import org .springframework .ai .model .tool .ToolCallingManager ;
3740import org .springframework .ai .openai .OpenAiChatModel ;
3841import org .springframework .ai .openai .OpenAiChatOptions ;
3942import org .springframework .ai .openai .api .OpenAiApi ;
4043import org .springframework .ai .openai .api .OpenAiApi .ChatModel ;
4144import org .springframework .ai .retry .RetryUtils ;
45+ import org .springframework .ai .tool .execution .DefaultToolExecutionExceptionProcessor ;
46+ import org .springframework .ai .tool .resolution .DelegatingToolCallbackResolver ;
47+ import org .springframework .ai .tool .resolution .SpringBeanToolCallbackResolver ;
48+ import org .springframework .ai .tool .resolution .StaticToolCallbackResolver ;
49+ import org .springframework .ai .tool .resolution .ToolCallbackResolver ;
50+ import org .springframework .beans .factory .ObjectProvider ;
4251import org .springframework .beans .factory .annotation .Autowired ;
4352import org .springframework .boot .SpringBootConfiguration ;
4453import org .springframework .boot .test .context .SpringBootTest ;
4554import org .springframework .context .annotation .Bean ;
4655import org .springframework .context .annotation .Description ;
56+ import org .springframework .context .support .GenericApplicationContext ;
4757import org .springframework .core .ParameterizedTypeReference ;
4858
4959import static org .assertj .core .api .Assertions .assertThat ;
@@ -68,7 +78,7 @@ public class OpenAiPaymentTransactionIT {
6878 public void transactionPaymentStatuses (String functionName ) {
6979 List <TransactionStatusResponse > content = this .chatClient .prompt ()
7080 .advisors (new LoggingAdvisor ())
71- .functions (functionName )
81+ .tools (functionName )
7282 .user ("""
7383 What is the status of my payment transactions 001, 002 and 003?
7484 """ )
@@ -99,7 +109,7 @@ public void streamingPaymentStatuses(String functionName) {
99109
100110 Flux <String > flux = this .chatClient .prompt ()
101111 .advisors (new LoggingAdvisor ())
102- .functions (functionName )
112+ .tools (functionName )
103113 .user (u -> u .text ("""
104114 What is the status of my payment transactions 001, 002 and 003?
105115
@@ -218,15 +228,35 @@ public OpenAiApi chatCompletionApi() {
218228 }
219229
220230 @ Bean
221- public OpenAiChatModel openAiClient (OpenAiApi openAiApi ) {
231+ public OpenAiChatModel openAiClient (OpenAiApi openAiApi , ToolCallingManager toolCallingManager ) {
222232 return OpenAiChatModel .builder ()
223233 .openAiApi (openAiApi )
224234 .defaultOptions (
225235 OpenAiChatOptions .builder ().model (ChatModel .GPT_4_O_MINI .getName ()).temperature (0.1 ).build ())
236+ .toolCallingManager (toolCallingManager )
226237 .retryTemplate (RetryUtils .DEFAULT_RETRY_TEMPLATE )
227238 .build ();
228239 }
229240
241+ @ Bean
242+ ToolCallingManager toolCallingManager (GenericApplicationContext applicationContext ,
243+ List <FunctionCallback > toolCallbacks , ObjectProvider <ObservationRegistry > observationRegistry ) {
244+
245+ var staticToolCallbackResolver = new StaticToolCallbackResolver (toolCallbacks );
246+ var springBeanToolCallbackResolver = SpringBeanToolCallbackResolver .builder ()
247+ .applicationContext (applicationContext )
248+ .build ();
249+
250+ ToolCallbackResolver toolCallbackResolver = new DelegatingToolCallbackResolver (
251+ List .of (staticToolCallbackResolver , springBeanToolCallbackResolver ));
252+
253+ return ToolCallingManager .builder ()
254+ .observationRegistry (observationRegistry .getIfUnique (() -> ObservationRegistry .NOOP ))
255+ .toolCallbackResolver (toolCallbackResolver )
256+ .toolExecutionExceptionProcessor (new DefaultToolExecutionExceptionProcessor (false ))
257+ .build ();
258+ }
259+
230260 }
231261
232262}
0 commit comments