Kotlin extensions for LangChain4j
Konstantin Pavlov
Software Engineer @Twilio | Kotlin and FOSS Enthusiast | @kpavlov.me ??
Link to the post: https://kpavlov.me/blog/kotlin-extensions-for-langchain4j/
?? Excited to announce v0.1.0 of Kotlin extensions for LangChain4j!
Ever struggled with blocking API calls in your AI-powered applications? This release transforms LangChain4j's synchronous API into a modern, non-blocking Kotlin experience.
You can enjoy:
?? Before:
// Blocking calls that tie up threads
val response = model.chat(request) // blocking thread here
println(response.content())
? After:
领英推荐
// Non-blocking coroutines with structured concurrency
launch {
val response = model.chatAsync(request) // suspend function, releasing thread
println(response.content())
}
?? And for streaming responses:
val messages =
listOf(
SystemMessage.from("You are helpful assistant"),
UserMessage.from("Help me to write a post about Kotlin"),
)
model
.generateFlow(messages)
.collect {
logger.info("Received event: $it")
when (it) {
is Token -> {
logger.info("Token: '${it.token}'") // consume token
}
is Completion -> logger.info("Response: '${it.response.content()}'") // consume response
else -> fail("Unsupported event: $it")
}
}
Key Benefits of using Kotlin Coroutines:
Try it out:
#Kotlin #AI #LangChain4j #Coroutines #OpenAI #SoftwareDevelopment #OpenSource #Programming
Senior Software Engineer
3 个月Konstantin, thank you so much for making #LangChain4j?better! Great work!
Nice, maybe you also want to have a look at our Kotlin-based open-source multi-agent platform https://lmos-ai.org and our https://lmos-ai.github.io/arc/ framework. It's used in production at DT to run a multi agent system written in Kotlin and handles hundred thousands of customer queries . Arc is a Kotlin DSL on top of our Langchain4j Kotlin alternative, since Langchain4j was too new when we started. But Patrick Whelan is currently developing a Langchain4j extension for Arc. Maybe you want to exchange in that topic. We are also writing a new nervous system for LMOS based on Kotlin to support any transport protocol like HTTP, MQTT or AMQP for inter-agent communication without adapting the application code. LMOS is also open source and hopefully soon part of Eclipse Foundation. Feedback is very welcome.
Software Engineer @Twilio | Kotlin and FOSS Enthusiast | @kpavlov.me ??
4 个月Added Kotlin extension functions for non-blocking document processing with Coroutines https://github.com/kpavlov/langchain4j-kotlin/blob/main/docs/AsyncIO.md
Software Engineer @Twilio | Kotlin and FOSS Enthusiast | @kpavlov.me ??
4 个月Small update: Now external prompt template sources and rendering engine concepts are supported. Hopefully, I can backport it to the original LangChain4j