3636import org .springframework .util .ReflectionUtils ;
3737
3838import static org .assertj .core .api .Assertions .assertThat ;
39- import static org .junit . Assert . assertThrows ;
39+ import static org .assertj . core . api . AssertionsForClassTypes . assertThatThrownBy ;
4040
4141@ SpringBootTest (classes = AnthropicTestConfiguration .class , properties = "spring.ai.retry.on-http-codes=429" )
4242@ EnabledIfEnvironmentVariable (named = "ANTHROPIC_API_KEY" , matches = ".+" )
@@ -47,66 +47,6 @@ class AnthropicChatClientMethodFunctionCallbackIT {
4747
4848 public static Map <String , Object > arguments = new ConcurrentHashMap <>();
4949
50- @ Autowired
51- ChatModel chatModel ;
52-
53- record MyRecord (String foo , String bar ) {
54- }
55-
56- public enum Unit {
57-
58- CELSIUS , FAHRENHEIT
59-
60- }
61-
62- public static class TestFunctionClass {
63-
64- public static void argumentLessReturnVoid () {
65- arguments .put ("method called" , "argumentLessReturnVoid" );
66- }
67-
68- public static String getWeatherStatic (String city , Unit unit ) {
69-
70- logger .info ("City: " + city + " Unit: " + unit );
71-
72- arguments .put ("city" , city );
73- arguments .put ("unit" , unit );
74-
75- double temperature = 0 ;
76- if (city .contains ("Paris" )) {
77- temperature = 15 ;
78- }
79- else if (city .contains ("Tokyo" )) {
80- temperature = 10 ;
81- }
82- else if (city .contains ("San Francisco" )) {
83- temperature = 30 ;
84- }
85-
86- return "temperature: " + temperature + " unit: " + unit ;
87- }
88-
89- public String getWeatherNonStatic (String city , Unit unit ) {
90- return getWeatherStatic (city , unit );
91- }
92-
93- public String getWeatherWithContext (String city , Unit unit , ToolContext context ) {
94- arguments .put ("tool" , context .getContext ().get ("tool" ));
95- return getWeatherStatic (city , unit );
96- }
97-
98- public void turnLight (String roomName , boolean on ) {
99- arguments .put ("roomName" , roomName );
100- arguments .put ("on" , on );
101- logger .info ("Turn light in room: {} to: {}" , roomName , on );
102- }
103-
104- public void turnLivingRoomLightOn () {
105- arguments .put ("turnLivingRoomLightOn" , true );
106- }
107-
108- }
109-
11050 @ BeforeEach
11151 void beforeEach () {
11252 arguments .clear ();
@@ -120,7 +60,7 @@ void methodGetWeatherStatic() {
12060 String response = ChatClient .create (this .chatModel ).prompt ()
12161 .user ("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius." )
12262 .functions (MethodFunctionCallback .builder ()
123- .method (method )
63+ .method (method )
12464 .description ("Get the weather in location" )
12565 .build ())
12666 .call ()
@@ -144,7 +84,7 @@ void methodTurnLightNoResponse() {
14484 .user ("Turn light on in the living room." )
14585 .functions (MethodFunctionCallback .builder ()
14686 .functionObject (targetObject )
147- .method (method )
87+ .method (method )
14888 .description ("Can turn lights on or off by room name" )
14989 .build ())
15090 .call ()
@@ -170,7 +110,7 @@ void methodGetWeatherNonStatic() {
170110 .user ("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius." )
171111 .functions (MethodFunctionCallback .builder ()
172112 .functionObject (targetObject )
173- .method (method )
113+ .method (method )
174114 .description ("Get the weather in location" )
175115 .build ())
176116 .call ()
@@ -195,7 +135,7 @@ void methodGetWeatherToolContext() {
195135 .user ("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius." )
196136 .functions (MethodFunctionCallback .builder ()
197137 .functionObject (targetObject )
198- .method (method )
138+ .method (method )
199139 .description ("Get the weather in location" )
200140 .build ())
201141 .toolContext (Map .of ("tool" , "value" ))
@@ -218,18 +158,18 @@ void methodGetWeatherToolContextButNonContextMethod() {
218158 Unit .class );
219159
220160 // @formatter:off
221- assertThrows ("Configured method does not accept ToolContext as input parameter!" ,IllegalArgumentException .class , () -> {
222- ChatClient .create (this .chatModel ).prompt ()
161+ assertThatThrownBy (() -> ChatClient .create (this .chatModel ).prompt ()
223162 .user ("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius." )
224163 .functions (MethodFunctionCallback .builder ()
225- .functionObject (targetObject )
226- .method (method )
227- .description ("Get the weather in location" )
228- .build ())
164+ .functionObject (targetObject )
165+ .method (method )
166+ .description ("Get the weather in location" )
167+ .build ())
229168 .toolContext (Map .of ("tool" , "value" ))
230169 .call ()
231- .content ();
232- });
170+ .content ())
171+ .isInstanceOf (IllegalArgumentException .class )
172+ .hasMessage ("Configured method does not accept ToolContext as input parameter!" );
233173 // @formatter:on
234174 }
235175
@@ -245,7 +185,7 @@ void methodNoParameters() {
245185 .user ("Turn light on in the living room." )
246186 .functions (MethodFunctionCallback .builder ()
247187 .functionObject (targetObject )
248- .method (method )
188+ .method (method )
249189 .description ("Can turn lights on in the Living Room" )
250190 .build ())
251191 .call ()
@@ -257,4 +197,64 @@ void methodNoParameters() {
257197 assertThat (arguments ).containsEntry ("turnLivingRoomLightOn" , true );
258198 }
259199
200+ @ Autowired
201+ ChatModel chatModel ;
202+
203+ record MyRecord (String foo , String bar ) {
204+ }
205+
206+ public enum Unit {
207+
208+ CELSIUS , FAHRENHEIT
209+
210+ }
211+
212+ public static class TestFunctionClass {
213+
214+ public static void argumentLessReturnVoid () {
215+ arguments .put ("method called" , "argumentLessReturnVoid" );
216+ }
217+
218+ public static String getWeatherStatic (String city , Unit unit ) {
219+
220+ logger .info ("City: " + city + " Unit: " + unit );
221+
222+ arguments .put ("city" , city );
223+ arguments .put ("unit" , unit );
224+
225+ double temperature = 0 ;
226+ if (city .contains ("Paris" )) {
227+ temperature = 15 ;
228+ }
229+ else if (city .contains ("Tokyo" )) {
230+ temperature = 10 ;
231+ }
232+ else if (city .contains ("San Francisco" )) {
233+ temperature = 30 ;
234+ }
235+
236+ return "temperature: " + temperature + " unit: " + unit ;
237+ }
238+
239+ public String getWeatherNonStatic (String city , Unit unit ) {
240+ return getWeatherStatic (city , unit );
241+ }
242+
243+ public String getWeatherWithContext (String city , Unit unit , ToolContext context ) {
244+ arguments .put ("tool" , context .getContext ().get ("tool" ));
245+ return getWeatherStatic (city , unit );
246+ }
247+
248+ public void turnLight (String roomName , boolean on ) {
249+ arguments .put ("roomName" , roomName );
250+ arguments .put ("on" , on );
251+ logger .info ("Turn light in room: {} to: {}" , roomName , on );
252+ }
253+
254+ public void turnLivingRoomLightOn () {
255+ arguments .put ("turnLivingRoomLightOn" , true );
256+ }
257+
258+ }
259+
260260}
0 commit comments