langchain4j-spring-boot-integration
This Claude Code skill provides configuration patterns and starter templates for integrating LangChain4j with Spring Boot applications. Use it when embedding LangChain4j into existing Spring Boot projects, building AI microservices with declarative AI Services, configuring auto-configuration beans for various AI providers, or implementing RAG systems combined with Spring Data repositories for production-ready applications.
git clone --depth 1 https://github.com/giuseppe-trisciuoglio/developer-kit /tmp/langchain4j-spring-boot-integration && cp -r /tmp/langchain4j-spring-boot-integration/plugins/developer-kit-java/skills/langchain4j-spring-boot-integration ~/.claude/skills/langchain4j-spring-boot-integrationSKILL.md
# LangChain4j Spring Boot Integration
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.
## When to Use
Use this skill when:
- Integrating LangChain4j into existing Spring Boot applications
- Building AI-powered microservices with Spring Boot
- Configuring AI model beans with `@Bean` annotations
- Setting up auto-configuration for AI models and services
- Creating declarative AI Services with Spring dependency injection
- Implementing RAG systems with Spring Data integrations
- Setting up chat memory with Spring context management
- Configuring multiple AI providers (OpenAI, Azure, Ollama, Anthropic)
- Building production-ready AI applications with Spring Boot
## Overview
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.
## Instructions
### 1. Add Dependencies
```xml
<!-- Core LangChain4j Spring Boot Starter -->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring-boot-starter</artifactId>
<version>1.8.0</version>
</dependency>
<!-- OpenAI Spring Boot Starter -->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
<version>1.8.0</version>
</dependency>
```
### 2. Configure Application Properties
```properties
# application.properties
langchain4j.open-ai.chat-model.api-key=${OPENAI_API_KEY}
langchain4j.open-ai.chat-model.model-name=gpt-4o-mini
langchain4j.open-ai.chat-model.temperature=0.7
langchain4j.open-ai.chat-model.timeout=PT60S
langchain4j.open-ai.chat-model.max-tokens=1000
```
Or using YAML:
```yaml
langchain4j:
open-ai:
chat-model:
api-key: ${OPENAI_API_KEY}
model-name: gpt-4o-mini
temperature: 0.7
timeout: 60s
max-tokens: 1000
```
### 3. Create Declarative AI Service
```java
import dev.langchain4j.service.spring.AiService;
@AiService
public interface CustomerSupportAssistant {
@SystemMessage("You are a helpful customer support agent for TechCorp.")
String handleInquiry(String customerMessage);
@UserMessage("Translate to {{language}}: {{text}}")
String translate(String text, String language);
}
```
### 4. Enable Component Scanning
```java
@SpringBootApplication
@ComponentScan(basePackages = {
"com.yourcompany",
"dev.langchain4j.service.spring"
})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
### 5. Inject and Use the AI Service
```java
@Service
public class CustomerService {
private final CustomerSupportAssistant assistant;
public CustomerService(CustomerSupportAssistant assistant) {
this.assistant = assistant;
}
public String processCustomerQuery(String query) {
return assistant.handleInquiry(query);
}
}
```
### 6. Verify the Integration
After setup, verify the configuration:
1. Start the application and check logs for `LangChain4jSpringBootAutoConfiguration` activation
2. Confirm AI service beans are registered: look for `CustomerSupportAssistant` in Spring context
3. Test the service: invoke `assistant.handleInquiry("test")` and verify a response is returned
## Configuration
**Property-Based Configuration:** Configure AI models through `application.properties` for different providers.
**Manual Bean Configuration:** For advanced configurations, define beans manually:
```java
@Configuration
public class AiConfig {
@Bean
public ChatModel chatModel(@Value("${OPENAI_API_KEY}") String apiKey) {
return OpenAiChatModel.builder()
.apiKey(apiKey)
.modelName("gpt-4o-mini")
.temperature(0.7)
.build();
}
}
```
**Multiple Providers:** Use explicit wiring when configuring multiple AI providers:
```java
@AiService(wiringMode = WiringMode.EXPLICIT)
interface MultiProviderAssistant {
@AiServiceAnnotation
ChatModel openAiModel;
@AiServiceAnnotation
ChatModel azureModel;
}
```
## Declarative AI Services
**Basic AI Service:** Create interfaces with `@AiService` annotation and define methods with message templates.
**Streaming AI Service:** Implement streaming responses using Project Reactor:
```java
@AiService
public interface StreamingAssistant {
@SystemMessage("You are a helpful assistant.")
Flux<String> chatStream(String message);
}
```
**Chat Memory:** Set up conversation memory with Spring context:
```java
@AiService
public interface ConversationalAssistant {
@SystemMessage("You are a helpful assistant with memory.")
String chat(@MemoryId String userId, String message);
}
```
## RAG Implementation
**Embedding Stores:** Configure embedding stores for RAG pipelines with Spring Data:
```java
@Configuration
public class RagConfig {
@Bean
public EmbeddingStore<TextSegment> embeddingStore() {
return PgVectorEmbeddingStore.builder()
.host("localhost")
.port(5432)
.database("vectordb")
.table("embeddings")
.dimension(1536)
.build();
}
@Bean
public EmbeddingModel embeddingModel() {
return OpenAiEmbeddingModel.withApiKey(System.getenv("OPENAI_API_KEY"));
}
}
@AiService
public interface RagAssistant {
String answer(@UserMessage("Question: {{question}}") String question);
}
```
**Document Ingestion:** Use `ContentInjector` and `DocumentSplitter` for processing documents.
**Content Retrieval:** Configure `EmbeddingStoreContentRetriever` for knowledge augmentation.
## Tool Integration
**Spring Component Tools:** DefineProvides chunking strategies for RAG systems. Generates chunk size recommendations (256-1024 tokens), overlap percentages (10-20%), and semantic boundary detection methods. Validates semantic coherence and evaluates retrieval precision/recall metrics. Use when building retrieval-augmented generation systems, vector databases, or processing large documents.
>
Implements document chunking, embedding generation, vector storage, and retrieval pipelines for Retrieval-Augmented Generation systems. Use when building RAG applications, creating document Q&A systems, or integrating AI with knowledge bases.
Provides AWS CloudFormation patterns for Auto Scaling including EC2, ECS, and Lambda. Use when creating Auto Scaling groups, launch configurations, launch templates, scaling policies, lifecycle hooks, and predictive scaling. Covers template structure with Parameters, Outputs, Mappings, Conditions, cross-stack references, and best practices for high availability and cost optimization.
Provides AWS CloudFormation patterns for Amazon Bedrock resources including agents, knowledge bases, data sources, guardrails, prompts, flows, and inference profiles. Use when creating Bedrock agents with action groups, implementing RAG with knowledge bases, configuring vector stores, setting up content moderation guardrails, managing prompts, orchestrating workflows with flows, and configuring inference profiles for model optimization.
Provides AWS CloudFormation patterns for CloudFront distributions, origins (ALB, S3, Lambda@Edge, VPC Origins), CacheBehaviors, Functions, SecurityHeaders, parameters, Outputs and cross-stack references. Use when creating CloudFront distributions with CloudFormation, configuring multiple origins, implementing caching strategies, managing custom domains with ACM, configuring WAF, and optimizing performance.
Provides AWS CloudFormation patterns for CloudWatch monitoring, metrics, alarms, dashboards, logs, and observability. Use when creating CloudWatch metrics, alarms, dashboards, log groups, log subscriptions, anomaly detection, synthesized canaries, Application Signals, and implementing template structure with Parameters, Outputs, Mappings, Conditions, cross-stack references, and CloudWatch best practices for monitoring production infrastructure.
Provides AWS CloudFormation patterns for DynamoDB tables, GSIs, LSIs, auto-scaling, and streams. Use when creating DynamoDB tables with CloudFormation, configuring primary keys, local/global secondary indexes, capacity modes (on-demand/provisioned), point-in-time recovery, encryption, TTL, and implementing template structure with Parameters, Outputs, Mappings, Conditions, cross-stack references.