chatStreamWithTools
Sends a chat request with tool support and returns a streaming response.
Tool calls are accumulated from deltas. If autoExecute is true, executes tools and continues streaming with the results.
Return
A Flow of ChatChunk objects.
Parameters
The fully configured ChatRequest with tools.
If true, automatically executes tools and continues the conversation.
Callback invoked when a complete tool call is collected.
Callback invoked when a tool is executed.
See also
Throws
if the API returns a provider-specific error.
if the HTTP request fails (timeout, network, JSON parsing, etc.).
if there are errors in running the tools.
Samples
// Send a stream request to OpenAI with tools using chat() function with chat request.
// Init TestWeatherTool.
val getWeatherTool = TestWeatherTool()
// Create a chat using koryChat DSL function.
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,
tools = {
tool(getWeatherTool)
}
)
// ChatStream uses a flow to stream chunks
// The tools will execute automatically.
client.chatStreamWithTools(
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")
},
onFullToolCollected = { chunkIndex, name, argsJson ->
// This callback will be triggered when the library has fully assembled the message regarding the tool call.
println("Full tool call in $chunkIndex chunk: $name with args: $argsJson")
}
).collect { chunk ->
// Iterate all choices in chunk.
for (choice in chunk.choices) {
// Print information about content.
when (val content = choice.content) { // Content is Content.StreamResponse.
is Content.Reasoning -> println("Reasoning: ${content.value}")
is Content.Text -> println("Text: ${content.text}")
is Content.ToolCallDelta -> println("Tool call: ${content.name} with ${content.argumentsChunk ?: "no arguments"}")
}
}
}Sends a chat request with tool support and returns a streaming response using full DSL configuration.
Return
A Flow of ChatChunk objects.
Parameters
If true, automatically executes tools and continues the conversation.
Callback invoked when a complete tool call is collected.
Callback invoked when a tool is executed.
DSL builder for configuring messages, temperature, maxTokens, tools, etc.
See also
Throws
if the API returns a provider-specific error.
if the HTTP request fails (timeout, network, JSON parsing, etc.).
Samples
// Send a stream request to OpenAI with tools using chat() function with chat request DSL.
// Init TestWeatherTool.
val getWeatherTool = TestWeatherTool()
// The tools will execute automatically.
client.chatStreamWithTools(
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")
},
onFullToolCollected = { chunkIndex, name, argsJson ->
// This callback will be triggered when the library has fully assembled the message regarding the tool call.
println("Full tool call 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.
// Register getWeatherTool.
registerTools(getWeatherTool)
chat(model = modelName) { // Chat function with dsl for messages.
user("Ping!")
}
}.collect { chunk ->
// Iterate all choices in chunk.
for (choice in chunk.choices) {
// Print information about content.
when (val content = choice.content) { // Content is Content.StreamResponse.
is Content.Reasoning -> println("Reasoning: ${content.value}")
is Content.Text -> println("Text: ${content.text}")
is Content.ToolCallDelta -> println("Tool call: ${content.name} with ${content.argumentsChunk ?: "no arguments"}")
}
}
}