chat
Sends a chat request to the OpenAI and returns a non-streaming response.
Return
A ChatResponse with one or more io.kory.core.chat.choice.ChatChoice objects.
Parameters
A fully configured ChatRequest containing the chat, model parameters, and optional tools.
See also
Throws
if the request fails.
if provider returns an error.
Samples
// Send a ChatRequest to OpenAI.
// Create a chat using koryChat DSL function.
val chat: Chat = koryChat(model = modelName) {
user("Ping!")
}
val request: ChatRequest = chat.asChatRequest(
temperature = 1.4
)
// Send request to OpenAI API.
val response: ChatResponse = runCatching {
client.chat(request)
}.getOrElse { th ->
if (th is CancellationException) throw th
th.printStackTrace()
return
}
println(response.outputText)Sends a chat request to the OpenAI API and returns a non-streaming response.
Uses a DSL builder to construct messages with a clean syntax.
Return
A ChatResponse containing one or more io.kory.core.chat.choice.ChatChoice objects.
Parameters
The name of the AI model to use (e.g., "gpt-4", "gpt-3.5-turbo").
DSL builder block for constructing chat messages. Use ChatBuilder.system, ChatBuilder.user, ChatBuilder.assistant, and ChatBuilder.tool to add messages.
See also
Throws
if the request fails.
if provider returns an error.
Samples
// Send a request to OpenAI using chat() function with chat DSL.
val response: ChatResponse? = try {
client.chat(modelName) {
// Body for messages
user("Ping!")
}
// This function does not support parameters for ChatRequest.
// If you need to add `tools`, `temperature`, or other specific parameters,
// take a look at the `chat()` function with `ChatRequestBuilder`.
} 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 to the OpenAI API and returns a non-streaming response.
Return
A ChatResponse containing one or more io.kory.core.chat.choice.ChatChoice objects.
Parameters
DSL builder for configuring the full request including messages, temperature, maxTokens, tools, and reasoning.
See also
Throws
if the request fails.
if provider returns an error.
Samples
// Send a request to OpenAI using chat() function with chat request DSL.
val response: ChatResponse? = try {
client.chat {
temperature = 1.4 // Set temperature.
chat(model = modelName) { // Chat function with dsl for messages.
user("Ping!")
}
}
} 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}")
}
}
}
}