Skip to content

Commit 52ce565

Browse files
committed
Add Groq tool calling docs
1 parent 0aaab02 commit 52ce565

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed
392 KB
Loading

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/groq-chat.adoc

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,70 @@ TIP: In addition to the model specific https://github.com/spring-projects/spring
154154

155155
== Function Calling
156156

157-
Groq API endpoints support https://console.groq.com/docs/tool-use[function calling].
158-
You can register custom Java functions with the OpenAiChatModel and have the OpenAI model intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
159-
This is a powerful technique to connect the LLM capabilities with external tools and APIs.
160-
Read more about xref:api/chat/functions/openai-chat-functions.adoc[OpenAI Function Calling].
157+
Groq API endpoints support https://console.groq.com/docs/tool-use[tool/function calling] when selecting one of the Tool/Function supporting models.
161158

162159
TIP: Check the Tool https://console.groq.com/docs/tool-use[Supported Models].
163160

161+
image::spring-ai-groq-functions-2.jpg[w=800,align="center"]
162+
163+
You can register custom Java functions with your ChatModel and have the provided Groq model intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
164+
This is a powerful technique to connect the LLM capabilities with external tools and APIs.
165+
166+
=== Tool Example
167+
168+
Here's a simple example of how to use Groq function calling with Spring AI:
169+
170+
[source,java]
171+
----
172+
@SpringBootApplication
173+
public class GroqApplication {
174+
175+
public static void main(String[] args) {
176+
SpringApplication.run(GroqApplication.class, args);
177+
}
178+
179+
@Bean
180+
CommandLineRunner runner(ChatClient.Builder chatClientBuilder) {
181+
return args -> {
182+
var chatClient = chatClientBuilder.build();
183+
184+
var response = chatClient.prompt()
185+
.user("What is the weather in Amsterdam and Paris?")
186+
.functions("weatherFunction") // reference by bean name.
187+
.call()
188+
.content();
189+
190+
System.out.println(response);
191+
};
192+
}
193+
194+
@Bean
195+
@Description("Get the weather in location")
196+
public Function<WeatherRequest, WeatherResponse> weatherFunction() {
197+
return new MockWeatherService();
198+
}
199+
200+
public static class MockWeatherService implements Function<WeatherRequest, WeatherResponse> {
201+
202+
public record WeatherRequest(String location, String unit) {}
203+
public record WeatherResponse(double temp, String unit) {}
204+
205+
@Override
206+
public WeatherResponse apply(WeatherRequest request) {
207+
double temperature = request.location().contains("Amsterdam") ? 20 : 25;
208+
return new WeatherResponse(temperature, request.unit);
209+
}
210+
}
211+
}
212+
----
213+
214+
In this example, when the model needs weather information, it will automatically call the `weatherFunction` bean, which can then fetch real-time weather data.
215+
The expected response looks like this: "The weather in Amsterdam is currently 20 degrees Celsius, and the weather in Paris is currently 25 degrees Celsius."
216+
217+
Read more about OpenAI link:https://docs.spring.io/spring-ai/reference/api/chat/functions/openai-chat-functions.html[Function Calling].
218+
219+
220+
164221
== Multimodal
165222

166223
NOTE: Currently the Groq API doesn't support media content.

0 commit comments

Comments
 (0)