01
Dev.to Kotlin
作者:Mojave Hao
發布:02-28 11:18
Introducing KOllama: A Kotlin‑First Ollama Client with Full Type Safety
KOllama is a Kotlin client for Ollama, powered by Ktor Client and designed to make local LLM interactions delightful.
重點摘要
Full type safety: requests and responses as Kotlin data classes
Kotlin‑first API: suspend functions, Flow streaming, and DSL builders
Built on Ktor with easy customization and multi‑modal support
KOllama is a Kotlin‑first client for Ollama that emphasizes full type safety and idiomatic Kotlin APIs. Implemented with Ktor Client, it maps Ollama's JSON to Kotlin data classes (preserving wire names with @SerialName), provides Flow‑based streaming, suspend functions, and a type‑safe DSL for options. KOllama also supports multi‑modal inputs (images), embeddings, model listing and pulling, and chat with tool support, covering the full Ollama API surface. The library contrasts with alternatives by favoring Kotlin idioms over Java‑style callbacks or half‑baked KMP APIs. Its design principles are Kotlin‑first ergonomics, sealed classes for multiple field shapes to avoid runtime casts, and semantic field names that keep compatibility. KOllama is early but functional, with plans for tests, Maven Central publishing, documentation, and community contributions on GitHub.
介绍 KOllama:以 Kotlin 为先、具完全类型安全的 Ollama 客户端
KOllama 是基于 Ktor Client 的 Kotlin 专用 Ollama 客户端,旨在让本地 LLM 交互更符合 Kotlin 风格并更愉快。
重點摘要
将请求与响应定义为 Kotlin 数据类以实现完整类型安全
Kotlin‑first 设计:suspend 函数、Flow 串流与 DSL 选项
基于 Ktor,易于自定义 HttpClient 且支持多模态(文本+图像)
KOllama 是为 Kotlin 项目构建的 Ollama 客户端,使用 Ktor Client,把 Ollama 的 JSON 映射为可序列化的 Kotlin 数据类(通过 @SerialName 保持线格式兼容),并提供 Flow 串流、suspend 接口与类型安全的 DSL。它将难懂的 API 字段替换为语义清晰的命名,支持生成文本、串流令牌、多模态输入、嵌入、列出与拉取模型以及带工具支持的聊天等完整功能。相比 Ollama4j 的回调和部分 KMP 库的设计妥协,KOllama 更贴近 Kotlin 的惯用方式。项目仍在早期开发,计划添加全面测试、发布到 Maven Central、完善文档与示例,欢迎社区参与与贡献。
介紹 KOllama:以 Kotlin 為先、具完全型別安全性的 Ollama 客戶端
KOllama 是基於 Ktor Client 的 Kotlin 專用 Ollama 客戶端,旨在讓本地 LLM 的互動更符合 Kotlin 風格並更愉快。
重點摘要
以 Kotlin 資料類提供完整型別安全
原生 Kotlin API:suspend、Flow 串流與 DSL 建構器
基於 Ktor,支援自訂 HttpClient 與多模式輸入(文字+影像)
KOllama 是為 Kotlin 專案打造的 Ollama 客戶端,使用 Ktor Client 並將請求與回應映射為 Kotlin 資料類(透過 @SerialName 保留原始欄位名),提供了 Flow 為基礎的串流、suspend 函式與型別安全的 DSL 選項。它將 Ollama 的晦澀欄位改為語意清晰的命名(如 evaluatedInputTokens、contextSize),並涵蓋模型列舉、拉取、嵌入與聊天等完整 API。與 Ollama4j 的回呼式串流或其他 KMP 套件相比,KOllama 更貼近 Kotlin 的編程習慣。專案仍在早期,但已能使用核心功能,接下來計畫補充測試、發布到 Maven Central 並完善文件與範例,歡迎社群回饋與貢獻。