chatWithTools

open suspend override fun chatWithTools(request: ChatRequest, autoExecute: Boolean, maxSteps: Int?, currentStep: Int, onToolCall: ToolCapable.ToolCallCallback, onToolExecutionFailed: ToolCapable.ToolExecutionFailedCallback): ChatResponse

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

request

The fully configured ChatRequest with tools.

autoExecute

If true, automatically executes tools and continues the conversation.

onToolCall

Callback invoked when a tool is called (name, argumentsJson).

See also

Throws

if the request fails.

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}")
            }
        }
    }
}

open suspend override fun chatWithTools(autoExecute: Boolean, maxSteps: Int?, currentStep: Int, onToolCall: ToolCapable.ToolCallCallback, onToolExecutionFailed: ToolCapable.ToolExecutionFailedCallback, block: ChatRequestBuilder.() -> Unit): ChatResponse

Sends a chat request with tool support using full DSL configuration.

Return

A ChatResponse with the final response.

Parameters

autoExecute

If true, automatically executes tools and continues the conversation.

onToolCall

Callback invoked when a tool is called.

block

DSL builder for configuring messages, temperature, maxTokens, tools, etc.

See also

Throws

if the request fails.

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}")
            }
        }
    }
}