koog-sauce:连接 Koog 与 Spring AI、LangChain4j 的集成库
kpavlov/koog-sauce
koog-sauce 发布,为 Koog 框架补齐与 Spring AI、LangChain4j 的集成能力,分别提供 SpringAiLLMClient 和 Langchain4jLLMClient(支持标准与流式交互)。
Koog-sauce is a missing ingredient that connects Koog with other frameworks.
Features
- Spring AI Integration - Provides a Koog's
SpringAiLLMClient, which uses Spring AI's ChatClient under the hood. - LangChain4j Integration - Offers
Langchain4jLLMClientfor seamless integration with LangChain4j, supporting both standard and streaming interactions. - AI Agent Builder - Simplifies creation and configuration of AI agents with a fluent builder pattern, making it easy to set up complex agent behaviors.
Requirements
- JDK 17 or higher
- Gradle 8.14.1 or higher
Getting Started
Add Dependency
Add the dependency to your build.gradle.kts file:
dependencies {
// Core library
implementation("me.kpavlov:koog-sauce:[LATEST]")
// For Spring AI integration
implementation("me.kpavlov:koog-sauce-spring-ai:[LATEST]")
implementation("org.springframework.ai:spring-ai-openai:1.0.0")
// For LangChain4j integration
implementation("me.kpavlov:koog-sauce-langchain4j:[LATEST]")
implementation("dev.langchain4j:langchain4j:0.24.0")
implementation("dev.langchain4j:langchain4j-open-ai:0.24.0")
// Koog library
implementation("ai.koog:koog:0.4.0") // or newer
}Build the Project
./gradlew build
Or using the Makefile:
make build
Usage
Spring AI Integration
// Create Spring AI ChatClient val chatClient = org.springframework.ai.chat.client.ChatClient.builder( org.springframework.ai.openai.OpenAiChatModel .builder() .openAiApi( org.springframework.ai.openai.api.OpenAiApi .builder() .apiKey("your-api-key") .build(), ).build(), ).build() // Create SpringAiLLMClient val llmClient = me.kpavlov.koog.sauce.spring.ai.chat.SpringAiLLMClient(chatClient) // Build a prompt using Koog DSL val prompt = ai.koog.prompt.dsl.Prompt.build("myPrompt") { system("You are a helpful assistant") user("Tell me about Kotlin Multiplatform") } // Define the model to use val model = ai.koog.prompt.llm.LLModel( ai.koog.prompt.llm.LLMProvider.OpenAI, "gpt-4.1-nano", listOf(ai.koog.prompt.llm.LLMCapability.Completion), 100500 ) // Execute the prompt suspend fun executePrompt() { val responses = llmClient.execute(prompt, model) // Process the response val response = responses.first() println("Response: ${response.content}") }
See the complete example.
LangChain4j Integration
// Create LangChain4j ChatModel val chatModel = dev.langchain4j.model.openai.OpenAiChatModel.builder() .apiKey("your-api-key") .modelName("gpt-4") .build() // Create Langchain4jLLMClient val llmClient = me.kpavlov.koog.sauce.langchain4j.Langchain4jLLMClient(chatModel = chatModel) // Build a prompt using Koog DSL val prompt = ai.koog.prompt.dsl.Prompt.build("myPrompt") { system("You are a helpful assistant") user("Tell me about LangChain4j") } // Define the model to use val model = ai.koog.prompt.llm.LLModel( ai.koog.prompt.llm.LLMProvider.OpenAI, "gpt-4", listOf(ai.koog.prompt.llm.LLMCapability.Completion), 100500, ) // Execute the prompt suspend fun executePrompt() { val responses = llmClient.execute(prompt, model) println("Response: ${responses.first().content}") }
See the complete example.
AI Agent Builder
// Create a prompt executor val promptExecutor = ai.koog.prompt.executor.model.PromptExecutor( llmClient = llmClient, defaultModel = model ) // Create an AI agent using the builder val agent = me.kpavlov.koog.sauce.agents.core.agent.AIAgent<String, String> { this.promptExecutor = promptExecutor this.strategy = YourCustomStrategy() // Implement AIAgentStrategy this.agentConfig = YourAgentConfig() // Implement AIAgentConfigBase this.toolRegistry = ToolRegistry.builder() .registerTool(YourCustomTool()) // Add your tools .build() } // Use the agent suspend fun executeAgent() { val result = agent.execute("Tell me about Kotlin") println("Agent result: $result") }
These examples demonstrate how to:
- Integrate with Spring AI and LangChain4j
- Build AI agents with the fluent builder pattern
- Configure different components like prompt executors, strategies, and tools
Development
Available Make Commands
make build: Build the projectmake test: Run testsmake clean: Clean the projectmake publish: Publish to Maven Localmake doc: Generate KDoc documentationmake help: Show help message
Running Tests
./gradlew testOr using the Makefile:
make testIntegration Tests
The project includes comprehensive tests for both Spring AI and LangChain4j integrations:
- Spring AI Tests:
SpringOpenAiTestdemonstrates how to use and test the Spring AI integration with OpenAI models. - LangChain4j Tests:
Langchain4jLLMClientTesttests the standard LangChain4j LLM client functionality.StreamingLangchain4jLLMClientTesttests the streaming capabilities of the LangChain4j LLM client.
These tests serve as additional examples of how to use the integrations in your own projects.
Generating Documentation
./gradlew dokkaGeneratePublicationHtml
Or using the Makefile:
make doc
The documentation is automatically generated and published to GitHub Pages when changes are pushed to the main branch. You can access the latest documentation at:
https://kpavlov.github.io/koog-sauce/
License
This project is licensed under the MIT License - see the LICENSE file for details.
来源:GitHub · github.com
