Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bringing LangChain4j to Kotlin

Bringing LangChain4j to Kotlin

Presentation at Devclub.ee 2025-03-26

### **Summary of the Presentation: Bringing LangChain4j to Kotlin**

#### **Speaker:**
- **Konstantin Pavlov**, Software Engineer at Twilio.

#### **Overview:**
- The presentation discusses integrating **LangChain4j** with **Kotlin** to build AI-driven applications.
- Focus on using **Kotlin DSL (Domain-Specific Language)** for **concise, expressive, and safe** AI development.

---

### **Key Topics Covered:**

#### **1. LangChain4j in Kotlin**
- **LangChain4j** enables AI models to interact with various **data sources, memory, tools, and instructions**.
- Provides a **framework** for using **Large Language Models (LLMs)** within Kotlin applications.

#### **2. AI Components & Workflow**
- AI **character (virtual agent)** interacts with:
- 🌍 **World Knowledge**
- 📖 **Personal Knowledge**
- 📝 **Instructions (System Prompt)**
- 💬 **Chat Memory**
- 🛠 **Tools (Functions)**
- Queries are processed by an **LLM**, which returns responses.

#### **3. Retrieval-Augmented Generation (RAG)**
- **Indexing & Retrieval** for **efficient AI responses**.
- **Embedding models** used to store and fetch relevant data chunks.

#### **4. OpenAI & Model Context Protocol**
- Uses **OpenAI APIs** for response generation.
- **Model Context Protocol (MCP)** standardizes AI interactions.

#### **5. Running AI Workloads on Java**
- Java is chosen for **historical reasons** and **enterprise compatibility**.
- Kotlin offers advantages like **concise syntax, null safety, and better async handling**.

#### **6. Kotlin DSL for AI Development**
- **Cleaner and more readable** way to interact with AI models.
- Example:
```kotlin
model.chat {
messages(
userMessage("Tell me a joke about LLM"),
)
}
```
- Allows **extension functions** and **function types with receivers**.

#### **7. Testing AI Models with Mocks**
- Challenges:
- Non-deterministic responses.
- API costs and network dependencies.
- Complexity in **CI/CD**.
- **AI-Mocks**:
- Provides **fast, deterministic, offline tests**.
- Simulates **OpenAI completions** to test AI behavior.

#### **8. Running LLMs in Production**
- **Challenges**:
- High **latency** in LLM responses.
- **Cloud reliability** issues.
- **Security**: Prevent exposing sensitive data.
- **Monitoring**: Track **token usage & costs**.

---

### **Resources & Links**
- **Demo Code**: [GitHub - Elven Project](https://github.com/kpavlov/elven)
- **AI Mocks**: [AI Mocks Documentation](https://kpavlov.github.io/ai-mocks)
- **OpenAI Responses API**: [OpenAI Docs](https://platform.openai.com/docs/guides/responses-vs-chat-completions)
- **Speaker Contact**:
- **GitHub**: [@kpavlov](https://github.com/kpavlov)
- **Website**: [kpavlov.me](https://kpavlov.me)
- **LinkedIn**: [in/kpavlov](https://www.linkedin.com/in/kpavlov)

---

### **Conclusion**
The presentation explores **bringing LangChain4j to Kotlin**, emphasizing **Kotlin DSL, AI integration, testing with mocks, and production challenges**. It provides a **practical approach** to running **AI-driven applications efficiently** on the JVM. 🚀

Avatar for Konstantin Pavlov

Konstantin Pavlov

March 26, 2025
Tweet

More Decks by Konstantin Pavlov

Other Decks in Technology

Transcript

  1. AI Character (Virtual Agent) 🌍 World Knowledge 📖 Personal Knowledge

    📝 Instructions (System Prompt) 💬 Chat Memory 🛠 Tools (Functions) Query Response 💾 State 🤖 LLM
  2. Reply Query 😵 Large Language Model Query LLM Response 🤖

    Large Language Model 2 Query LLM Response
  3. Reply Query 🤖 Large Language Model 2 Query LLM Response

    🤖 Large Language Model 1 Query LLM Response Framweork
  4. Reply Query 🧠 AI Services 🤖 Large Language Model Query

    + Parameters LLM Response 🌍 World Knowledge 📖 Personal Knowledge
  5. Embedding Store Reply Query 🤖 Embedding Model Query 🧩 Chunks

    🧠 AI Services 🤖 Large Language Model Query + Parameters 🧩 LLM Response
  6. Embedding Store Reply Query 🤖 Embedding Model Query 🧩 Chunks

    🧠 AI Services 🤖 Chat Language Model 💬 Chat Memory Messages + Parameters 🧩 💬 Messages LLM Response
  7. Embedding Store Reply Query 🤖 Embedding Model Query 🧩 Chunks

    🧠 AI Services 🛠 Tools (Functions) 🤖 Chat Language Model 💬 Chat Memory Messages + Parameters 🧩 💬 🛠 Messages LLM Response 🌡
  8. Server Process Server Process Host Transport Layer Transport Layer MCP

    Client MCP Client MCP Server MCP Server https://modelcontextprotocol.io/ https://github.com/modelcontextprotocol/servers
  9. Embedding Store Reply Query 🤖 Embedding Model Query 🧩 Chunks

    🧠 AI Services 🛠 Tools (Functions) 🤖 Chat Language Model 💬 Chat Memory Messages + Parameters 🧩 💬 🛠 Messages LLM Response 🌡 🤖 Moderation Model Is Query OK?
  10. Why Run AI Workloads on Java? "Historical reasons" Can’t run

    on Python/JavaScript JVM is the Organization choice
  11. LLM Integration on JVM LangChain4J No Spring Quarkus Spring (yes,

    there is langchain4j-spring) Multiple LLMs Complex workflow Uncommon LLMs / Vector DBs
  12. Why Kotlin? 📝 Concise and expressive syntax 🛡 Built-in Null

    safety 🔄 Write asynchronous code that feels synchronous
  13. Kotlin DSL // Function type with receiver fun chatRequest(block: ChatRequestBuilder.()

    -> Unit): ChatRequest { val builder = ChatRequestBuilder() block.invoke(builder) return builder.build() }
  14. Kotlin DSL fun chatRequest(block: ChatRequestBuilder.() -> Unit): ChatRequest { val

    builder = ChatRequestBuilder() block.invoke(builder) return builder.build() } // Extension function fun ChatLanguageModel.chat(block: ChatRequestBuilder.() -> Unit): ChatResponse { val model = this return model.chat(chatRequest(block)) }
  15. Testing Challenges Non-deterministic responses API costs add up quickly Network

    dependencies CI/CD complexity and fragility Simulating edge cases
  16. Simple LC4J Mock val model = ChatModelMock.thatAlwaysResponds( "Yes, Sir", )

    // ChatModelMock.thatAlwaysThrowsException() // ChatModelMock.thatAlwaysThrowsExceptionWithMessage(String) val service: JokeService = AiServices.create( JokeService::class.java, model, ) println(service.joke("John")) // "Yes, Sir"
  17. AI-Mocks Black-box testing Fast derterministic tests Zero API costs Works

    offline and on CI/CD Simulates negative scenarios Supports Server-Sent Events (SSE) kpavlov.github.io/ai-mocks
  18. Simulating OpenAI Completion Response val mockOpenAi = MockOpenai() mockOpenAi.completion {

    userMessageContains("Tell me a joke about LLM") } responds { assistantContent = "Why did LLM cross road? Hallucination." } val model: JokeService = OpenAiChatModel.builder() .baseUrl(mockOpenAi.baseUrl()) // other .build()
  19. Running LLM Integration on Production Challenges ⏱️ High LLM latencies

    🔄 Cloud LLMs may not be reliable ✅ Validate user input and LLM output 🔒 Avoid sharing sensitive data with LLMs 📊 Monitor token usage and costs