$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill langchain4j-spring-boot-integrationProvides integration patterns for LangChain4j with Spring Boot. Configures AI model beans, sets up chat memory with Spring context, integrates RAG pipelines with Spring Data, and handles auto-configuration, dependency injection, and Spring ecosystem integration. Use when embeddin
| 1 | # LangChain4j Spring Boot Integration |
| 2 | |
| 3 | Integrate LangChain4j with Spring Boot using declarative AI Services, auto-configuration, and Spring Boot starters. Configure AI model beans, set up chat memory, implement RAG pipelines with Spring Data, and build production-ready AI applications. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | Use this skill when: |
| 8 | - Integrating LangChain4j into existing Spring Boot applications |
| 9 | - Building AI-powered microservices with Spring Boot |
| 10 | - Configuring AI model beans with `@Bean` annotations |
| 11 | - Setting up auto-configuration for AI models and services |
| 12 | - Creating declarative AI Services with Spring dependency injection |
| 13 | - Implementing RAG systems with Spring Data integrations |
| 14 | - Setting up chat memory with Spring context management |
| 15 | - Configuring multiple AI providers (OpenAI, Azure, Ollama, Anthropic) |
| 16 | - Building production-ready AI applications with Spring Boot |
| 17 | |
| 18 | ## Overview |
| 19 | |
| 20 | LangChain4j Spring Boot integration provides declarative AI Services through Spring Boot starters, enabling automatic configuration of AI components based on properties. Combine Spring dependency injection with LangChain4j's AI capabilities using interface-based definitions with annotations. |
| 21 | |
| 22 | ## Instructions |
| 23 | |
| 24 | ### 1. Add Dependencies |
| 25 | |
| 26 | ```xml |
| 27 | <!-- Core LangChain4j Spring Boot Starter --> |
| 28 | <dependency> |
| 29 | <groupId>dev.langchain4j</groupId> |
| 30 | <artifactId>langchain4j-spring-boot-starter</artifactId> |
| 31 | <version>1.8.0</version> |
| 32 | </dependency> |
| 33 | |
| 34 | <!-- OpenAI Spring Boot Starter --> |
| 35 | <dependency> |
| 36 | <groupId>dev.langchain4j</groupId> |
| 37 | <artifactId>langchain4j-open-ai-spring-boot-starter</artifactId> |
| 38 | <version>1.8.0</version> |
| 39 | </dependency> |
| 40 | ``` |
| 41 | |
| 42 | ### 2. Configure Application Properties |
| 43 | |
| 44 | ```properties |
| 45 | # application.properties |
| 46 | langchain4j.open-ai.chat-model.api-key=${OPENAI_API_KEY} |
| 47 | langchain4j.open-ai.chat-model.model-name=gpt-4o-mini |
| 48 | langchain4j.open-ai.chat-model.temperature=0.7 |
| 49 | langchain4j.open-ai.chat-model.timeout=PT60S |
| 50 | langchain4j.open-ai.chat-model.max-tokens=1000 |
| 51 | ``` |
| 52 | |
| 53 | Or using YAML: |
| 54 | |
| 55 | ```yaml |
| 56 | langchain4j: |
| 57 | open-ai: |
| 58 | chat-model: |
| 59 | api-key: ${OPENAI_API_KEY} |
| 60 | model-name: gpt-4o-mini |
| 61 | temperature: 0.7 |
| 62 | timeout: 60s |
| 63 | max-tokens: 1000 |
| 64 | ``` |
| 65 | |
| 66 | ### 3. Create Declarative AI Service |
| 67 | |
| 68 | ```java |
| 69 | import dev.langchain4j.service.spring.AiService; |
| 70 | |
| 71 | @AiService |
| 72 | public interface CustomerSupportAssistant { |
| 73 | |
| 74 | @SystemMessage("You are a helpful customer support agent for TechCorp.") |
| 75 | String handleInquiry(String customerMessage); |
| 76 | |
| 77 | @UserMessage("Translate to {{language}}: {{text}}") |
| 78 | String translate(String text, String language); |
| 79 | } |
| 80 | ``` |
| 81 | |
| 82 | ### 4. Enable Component Scanning |
| 83 | |
| 84 | ```java |
| 85 | @SpringBootApplication |
| 86 | @ComponentScan(basePackages = { |
| 87 | "com.yourcompany", |
| 88 | "dev.langchain4j.service.spring" |
| 89 | }) |
| 90 | public class Application { |
| 91 | public static void main(String[] args) { |
| 92 | SpringApplication.run(Application.class, args); |
| 93 | } |
| 94 | } |
| 95 | ``` |
| 96 | |
| 97 | ### 5. Inject and Use the AI Service |
| 98 | |
| 99 | ```java |
| 100 | @Service |
| 101 | public class CustomerService { |
| 102 | |
| 103 | private final CustomerSupportAssistant assistant; |
| 104 | |
| 105 | public CustomerService(CustomerSupportAssistant assistant) { |
| 106 | this.assistant = assistant; |
| 107 | } |
| 108 | |
| 109 | public String processCustomerQuery(String query) { |
| 110 | return assistant.handleInquiry(query); |
| 111 | } |
| 112 | } |
| 113 | ``` |
| 114 | |
| 115 | ### 6. Verify the Integration |
| 116 | |
| 117 | After setup, verify the configuration: |
| 118 | 1. Start the application and check logs for `LangChain4jSpringBootAutoConfiguration` activation |
| 119 | 2. Confirm AI service beans are registered: look for `CustomerSupportAssistant` in Spring context |
| 120 | 3. Test the service: invoke `assistant.handleInquiry("test")` and verify a response is returned |
| 121 | |
| 122 | ## Configuration |
| 123 | |
| 124 | **Property-Based Configuration:** Configure AI models through `application.properties` for different providers. |
| 125 | |
| 126 | **Manual Bean Configuration:** For advanced configurations, define beans manually: |
| 127 | |
| 128 | ```java |
| 129 | @Configuration |
| 130 | public class AiConfig { |
| 131 | |
| 132 | @Bean |
| 133 | public ChatModel chatModel(@Value("${OPENAI_API_KEY}") String apiKey) { |
| 134 | return OpenAiChatModel.builder() |
| 135 | .apiKey(apiKey) |
| 136 | .modelName("gpt-4o-mini") |
| 137 | .temperature(0.7) |
| 138 | .build(); |
| 139 | } |
| 140 | } |
| 141 | ``` |
| 142 | |
| 143 | **Multiple Providers:** Use explicit wiring when configuring multiple AI providers: |
| 144 | |
| 145 | ```java |
| 146 | @AiService(wiringMode = WiringMode.EXPLICIT) |
| 147 | interface MultiProviderAssistant { |
| 148 | @AiServiceAnnotation |
| 149 | ChatModel openAiModel; |
| 150 | |
| 151 | @AiServiceAnnotation |
| 152 | ChatModel azureModel; |
| 153 | } |
| 154 | ``` |
| 155 | |
| 156 | ## Declarative AI Services |
| 157 | |
| 158 | **Basic AI Service:** Create interfaces with `@A |