Kotlin Support
# Kotlin 支持
Kotlin (opens new window) 是一种静态类型语言,面向 JVM(及其他平台),可实现简洁优雅的代码,并与 Java 库无缝互操作 (opens new window)。
LangChain4j 利用 Kotlin 的扩展功能 (opens new window),为 Java API 提供 Kotlin 特定的便利特性,使用户可以扩展现有的 Java 类,添加针对 Kotlin 的专属功能。
例如,Kotlin 扩展可以将 ChatLanguageModel
(opens new window) 的响应转换为 Kotlin 挂起函数 (opens new window):
val model = OpenAiChatModel.builder()
.apiKey("your-api-key")
// 在此处配置更多参数...
.build()
CoroutineScope(Dispatchers.IO).launch {
val response = model.chatAsync(
ChatRequest.builder()
.messages(
listOf(
SystemMessage.from("You are a helpful assistant"),
UserMessage.from("Hello!")
)
)
)
println(response.aiMessage().text())
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
LangChain4j 并不需要 Kotlin 库作为运行时依赖,但允许用户利用 Kotlin 的协程功能实现非阻塞执行,从而提升性能和效率。
编辑 (opens new window)
上次更新: 2025/04/01, 01:48:12