chatWithTools
Sends a chat request with tool support and returns a non-streaming response.
If autoExecute is true, automatically executes tools and continues the conversation until no more tool calls are requested.
Return
A ChatResponse with the final response.
Parameters
The fully configured ChatRequest with tools.
If true, automatically executes tools and continues the conversation.
Callback invoked when a tool is called (name, argumentsJson).
See also
Throws
if the request fails.
if provider returns an error.
if there are errors in running the tools.
Samples
// Send a request to OpenAI with tools using ChatRequest.
// Init TestWeatherTool.
val getWeatherTool = TestWeatherTool()
val chat: Chat = koryChat(model = modelName) {
user("What's the weather like in London?")
}
val request: ChatRequest = chat.asChatRequest(
reasoning = ReasoningConfig.Enabled(ReasoningConfig.Level.LOW), // Low reasoning so that the model can think.
temperature = 1.4,
// Add getWeatherTool.
tools = {
tool(getWeatherTool)
}
)
// Send request to OpenAI.
val response = try {
// The tools will execute automatically.
client.chatWithTools(
request = request,
onToolCall = { chunkIndex, name, argsJson ->
// This callback will be triggered when the AI calls the tool.
println("AI called tool in $chunkIndex chunk: $name with args: $argsJson")
}
)
} catch (e: Exception) {
println("Error: ${e.message}")
null
}
response?.let { response ->
// Iterate all choices.
for (choice in response.choices) {
// Iterate through all the content from choice
for (content in choice.contents) {
// Print information about content.
when (content) { // Content is Content.Response.
is Content.Parts -> {
println("Parts: ${content.parts.joinToString(separator = "\n")}")
}
is Content.Reasoning -> println("Reasoning: ${content.value}")
is Content.Text -> println("Text: ${content.text}")
is Content.ToolCall -> println("Tool call: ${content.name} with ${content.argumentsJson}")
}
}
}
}Sends a chat request with tool support using full DSL configuration.
Return
A ChatResponse with the final response.
Parameters
If true, automatically executes tools and continues the conversation.
Callback invoked when a tool is called.
DSL builder for configuring messages, temperature, maxTokens, tools, etc.
See also
Throws
if the request fails.
if provider returns an error.
if there are errors in running the tools.
Samples
// Send a request to OpenAI with tools using chat() function with chat request DSL.
// Init TestWeatherTool.
val getWeatherTool = TestWeatherTool()
val response: ChatResponse? = try {
// The tools will execute automatically.
client.chatWithTools(
onToolCall = { chunkIndex, name, argsJson ->
// This callback will be triggered when the AI calls the tool.
println("AI called tool in $chunkIndex chunk: $name with args: $argsJson")
}
) {
reasoning = ReasoningConfig.Enabled(ReasoningConfig.Level.LOW) // Low reasoning so that the model can think.
temperature = 1.4 // Set temperature.
chat(model = modelName) { // Chat function with dsl for messages.
user("What's the weather like in London?")
}
// Register getWeatherTool.
registerTools(getWeatherTool)
}
} catch (e: Exception) {
println("Error: ${e.message}")
null
}
response?.let { response ->
// Iterate all choices.
for (choice in response.choices) {
// Iterate through all the content from choice
for (content in choice.contents) {
// Print information about content.
when (content) { // Content is Content.Response.
is Content.Parts -> {
println("Parts: ${content.parts.joinToString(separator = "\n")}")
}
is Content.Reasoning -> println("Reasoning: ${content.value}")
is Content.Text -> println("Text: ${content.text}")
is Content.ToolCall -> println("Tool call: ${content.name} with ${content.argumentsJson}")
}
}
}
}