
Type-safe DSL to build chats, requests and tools; multi-provider OpenAI-compatible clients; automatic tool-call execution loop, token-by-token streaming, configurable reasoning depth, and serializable models.
Kotlin-first multi-module library for working with AI providers. Provides idiomatic DSL and clients for LLM interactions.
Flow<ChatChunk>.@Serializable with kotlinx.serialization.| Module | Description |
|---|---|
core |
Provider-agnostic abstractions: Chat, Message, Content, KoryTool, DSL builders, extensions. |
openai |
OpenAI-compatible client (OpenAIClient), request/response models, converters from core types. |
kory-ktor |
HTTP transport layer built on Ktor: KoryHttpClient, auth strategies, error handling. |
kory-ktor-cio |
CIO engine factory for kory-ktor (auto-discovered via ServiceLoader). |
kory-ktor-okhttp |
OkHttp engine factory for kory-ktor (auto-discovered via ServiceLoader). |
Add dependencies in:
Gradle kotlin:
// build.gradle.kts
val koryVersion = "0.0.1"
dependencies {
implementation("io.github.kychnoo:kory-core:$koryVersion")
implementation("io.github.kychnoo:kory-openai:$koryVersion")
implementation("io.github.kychnoo:kory-ktor:$koryVersion")
// Add any network engine you want (for the JVM, not required for native code).
implementation("io.github.kychnoo:kory-ktor-cio:$koryVersion")
implementation("io.github.kychnoo:kory-ktor-okhttp:$koryVersion")
}Gradle groovy:
// build.gradle
def koryVersion = "0.0.1"
dependencies {
implementation "io.github.kychnoo:kory-core:${koryVersion}"
implementation "io.github.kychnoo:kory-openai:${koryVersion}"
implementation "io.github.kychnoo:kory-ktor:${koryVersion}"
// Add any network engine you want (for the JVM, not required for native code).
implementation "io.github.kychnoo:kory-ktor-cio:${koryVersion}"
implementation "io.github.kychnoo:kory-ktor-okhttp:${koryVersion}"
}Maven:
<project>
...
<properties>
<kory.version>0.0.1</kory.version>
</properties>
<dependencies>
<dependency>
<groupId>io.github.kychnoo</groupId>
<artifactId>kory-core</artifactId>
<version>${kory.version}</version>
</dependency>
<dependency>
<groupId>io.github.kychnoo</groupId>
<artifactId>kory-openai</artifactId>
<version>${kory.version}</version>
</dependency>
<dependency>
<groupId>io.github.kychnoo</groupId>
<artifactId>kory-ktor</artifactId>
<version>${kory.version}</version>
</dependency>
<!-- Add any network engine you want (for the JVM, not required for native code). -->
<dependency>
<groupId>io.github.kychnoo</groupId>
<artifactId>kory-ktor-cio</artifactId>
<version>${kory.version}</version>
</dependency>
<dependency>
<groupId>io.github.kychnoo</groupId>
<artifactId>kory-ktor-okhttp</artifactId>
<version>${kory.version}</version>
</dependency>
</dependencies>
</project>
import io.kory.core.message.content.Content
import io.kory.openai.chat.OpenAIClient
import io.kory.core.chat.response.ChatResponse
import kotlinx.coroutines.runBlocking
import io.kory.core.chat.client.ApiKey
suspend fun main() {
val apiKey = ApiKey.fromEnv("OPENAI_API_KEY") { e ->
// This callback is triggered if the key was not found in the system environment.
println("Received exception: ${e.message}, using fallback key...")
"Put your fallback api key here" // Or throw an Exception
}
// Create an OpenAI-compatible client.
// Works with any provider that exposes an OpenAI-compatible API.
val client = OpenAIClient(
apiKey = apiKey
)
// Send a catchable chat request using the DSL.
// `chat` builds the message list, the client handles serialization and HTTP.
client.chatCatching {
chat("model-name") {
user("Ping!")
}
}.onSuccess { response ->
// Print the output message.
println(response.outputText.ifEmpty { "No message" })
}.onFailure { error ->
// onFailure will be called if the function throws an exception.
println(error.message)
}
}client.chatStream {
chat(model = "model-name") {
user("Tell me a story")
}
}.collectHandler {
onChunk { chunk: ChatChunk ->
// Called on every chunk.
println(chunk.text)
}
onError { error ->
// Called if an error occurs during streaming.
println("${error::class.simpleName}: ${error.message}")
}
onCompleted {
// Called when streaming ends.
println("Stream finished!")
}
}// 1. Define a tool
class WeatherTool : KoryTool<WeatherTool.Args, String>(
name = "get_weather",
description = "Get weather in a city",
) {
@Serializable
data class Args(
@ToolParam(description = "City name")
val city: String
)
override val argsSerializer = serializer<Args>()
override suspend fun execute(args: Args): String =
"Sunny, 22°C in ${args.city}"
}
// 2. Use with automatic execution
val response = client.chatWithTools {
registerTools { tool(WeatherTool()) }
chat("model-name") {
user("What's the weather in Moscow?")
}
}
println(response.outputText)// 1. Define a tool arguments.
@Serializable // Add kotlinx-serialization dependencies to your project.
data class WeatherToolArgs(
@ToolParam(description = "City name")
val city: String
)
val weatherTool = koryTool<WeatherToolArgs, String>(
name = "get_weather",
description = "Get weather for the city",
) { args ->
"Sunny, 22°C in ${args.city}"
}
// 2. Use with automatic execution
val response = client.chatWithTools {
registerTools { tool(weatherTool) }
chat("model-name") {
user("What's the weather in Moscow?")
}
}
println(response.outputText)val response = client.chat {
reasoning = ReasoningConfig.Enabled(ReasoningConfig.Level.HIGH)
chat("model-name") {
user("Explain quantum computing")
}
}You can view the documentation here
Or generate it using
./gradlew dokkaGenerateHtml./gradlew build # Build all modules
./gradlew allTests # Run all tests
./gradlew jvmTest # Run JVM testsKotlin-first multi-module library for working with AI providers. Provides idiomatic DSL and clients for LLM interactions.
Flow<ChatChunk>.@Serializable with kotlinx.serialization.| Module | Description |
|---|---|
core |
Provider-agnostic abstractions: Chat, Message, Content, KoryTool, DSL builders, extensions. |
openai |
OpenAI-compatible client (OpenAIClient), request/response models, converters from core types. |
kory-ktor |
HTTP transport layer built on Ktor: KoryHttpClient, auth strategies, error handling. |
kory-ktor-cio |
CIO engine factory for kory-ktor (auto-discovered via ServiceLoader). |
kory-ktor-okhttp |
OkHttp engine factory for kory-ktor (auto-discovered via ServiceLoader). |
Add dependencies in:
Gradle kotlin:
// build.gradle.kts
val koryVersion = "0.0.1"
dependencies {
implementation("io.github.kychnoo:kory-core:$koryVersion")
implementation("io.github.kychnoo:kory-openai:$koryVersion")
implementation("io.github.kychnoo:kory-ktor:$koryVersion")
// Add any network engine you want (for the JVM, not required for native code).
implementation("io.github.kychnoo:kory-ktor-cio:$koryVersion")
implementation("io.github.kychnoo:kory-ktor-okhttp:$koryVersion")
}Gradle groovy:
// build.gradle
def koryVersion = "0.0.1"
dependencies {
implementation "io.github.kychnoo:kory-core:${koryVersion}"
implementation "io.github.kychnoo:kory-openai:${koryVersion}"
implementation "io.github.kychnoo:kory-ktor:${koryVersion}"
// Add any network engine you want (for the JVM, not required for native code).
implementation "io.github.kychnoo:kory-ktor-cio:${koryVersion}"
implementation "io.github.kychnoo:kory-ktor-okhttp:${koryVersion}"
}Maven:
<project>
...
<properties>
<kory.version>0.0.1</kory.version>
</properties>
<dependencies>
<dependency>
<groupId>io.github.kychnoo</groupId>
<artifactId>kory-core</artifactId>
<version>${kory.version}</version>
</dependency>
<dependency>
<groupId>io.github.kychnoo</groupId>
<artifactId>kory-openai</artifactId>
<version>${kory.version}</version>
</dependency>
<dependency>
<groupId>io.github.kychnoo</groupId>
<artifactId>kory-ktor</artifactId>
<version>${kory.version}</version>
</dependency>
<!-- Add any network engine you want (for the JVM, not required for native code). -->
<dependency>
<groupId>io.github.kychnoo</groupId>
<artifactId>kory-ktor-cio</artifactId>
<version>${kory.version}</version>
</dependency>
<dependency>
<groupId>io.github.kychnoo</groupId>
<artifactId>kory-ktor-okhttp</artifactId>
<version>${kory.version}</version>
</dependency>
</dependencies>
</project>
import io.kory.core.message.content.Content
import io.kory.openai.chat.OpenAIClient
import io.kory.core.chat.response.ChatResponse
import kotlinx.coroutines.runBlocking
import io.kory.core.chat.client.ApiKey
suspend fun main() {
val apiKey = ApiKey.fromEnv("OPENAI_API_KEY") { e ->
// This callback is triggered if the key was not found in the system environment.
println("Received exception: ${e.message}, using fallback key...")
"Put your fallback api key here" // Or throw an Exception
}
// Create an OpenAI-compatible client.
// Works with any provider that exposes an OpenAI-compatible API.
val client = OpenAIClient(
apiKey = apiKey
)
// Send a catchable chat request using the DSL.
// `chat` builds the message list, the client handles serialization and HTTP.
client.chatCatching {
chat("model-name") {
user("Ping!")
}
}.onSuccess { response ->
// Print the output message.
println(response.outputText.ifEmpty { "No message" })
}.onFailure { error ->
// onFailure will be called if the function throws an exception.
println(error.message)
}
}client.chatStream {
chat(model = "model-name") {
user("Tell me a story")
}
}.collectHandler {
onChunk { chunk: ChatChunk ->
// Called on every chunk.
println(chunk.text)
}
onError { error ->
// Called if an error occurs during streaming.
println("${error::class.simpleName}: ${error.message}")
}
onCompleted {
// Called when streaming ends.
println("Stream finished!")
}
}// 1. Define a tool
class WeatherTool : KoryTool<WeatherTool.Args, String>(
name = "get_weather",
description = "Get weather in a city",
) {
@Serializable
data class Args(
@ToolParam(description = "City name")
val city: String
)
override val argsSerializer = serializer<Args>()
override suspend fun execute(args: Args): String =
"Sunny, 22°C in ${args.city}"
}
// 2. Use with automatic execution
val response = client.chatWithTools {
registerTools { tool(WeatherTool()) }
chat("model-name") {
user("What's the weather in Moscow?")
}
}
println(response.outputText)// 1. Define a tool arguments.
@Serializable // Add kotlinx-serialization dependencies to your project.
data class WeatherToolArgs(
@ToolParam(description = "City name")
val city: String
)
val weatherTool = koryTool<WeatherToolArgs, String>(
name = "get_weather",
description = "Get weather for the city",
) { args ->
"Sunny, 22°C in ${args.city}"
}
// 2. Use with automatic execution
val response = client.chatWithTools {
registerTools { tool(weatherTool) }
chat("model-name") {
user("What's the weather in Moscow?")
}
}
println(response.outputText)val response = client.chat {
reasoning = ReasoningConfig.Enabled(ReasoningConfig.Level.HIGH)
chat("model-name") {
user("Explain quantum computing")
}
}You can view the documentation here
Or generate it using
./gradlew dokkaGenerateHtml./gradlew build # Build all modules
./gradlew allTests # Run all tests
./gradlew jvmTest # Run JVM tests