Kotlin extensions for LangChain4j

Kotlin extensions for LangChain4j

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:

  • ?? Thread efficiency: Handle thousands of concurrent AI requests without thread exhaustion
  • ?? Easy cancellation: Leverage structured concurrency for reliable cleanup
  • ?? Better scalability: Non-blocking operations improve resource utilization
  • ?? Idiomatic Kotlin: Seamless integration with other coroutine-based code
  • ??Runs on oldie JVMs: Kotlin Coroutines were released as Generally Available (GA) with Kotlin 1.3 in October 2018, so you can run it on ancient JVM versions

Try it out:

https://github.com/kpavlov/langchain4j-kotlin

#Kotlin #AI #LangChain4j #Coroutines #OpenAI #SoftwareDevelopment #OpenSource #Programming

Dmytro Liubarskyi

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.

Konstantin Pavlov

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

Konstantin Pavlov

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

  • 该图片无替代文字

要查看或添加评论,请登录

Konstantin Pavlov的更多文章

社区洞察

其他会员也浏览了