$npx -y skills add ducpm2303/claude-java-plugins --skill java-spring-aiUse when the user asks to add AI features, integrate Spring AI or LangChain4J, build a chatbot, implement RAG (retrieval-augmented generation), use vector stores, stream LLM responses, or call AI tools/functions in a Spring Boot project.
| 1 | # Spring AI / LangChain4J Skill |
| 2 | |
| 3 | Detect the framework in use, then apply the correct patterns. |
| 4 | |
| 5 | ## Step 1 — Detect framework and version |
| 6 | |
| 7 | Check `pom.xml` or `build.gradle`: |
| 8 | - `spring-ai-*` dependency → **Spring AI** (note version: 1.0.x GA or 0.8.x milestone) |
| 9 | - `langchain4j-*` dependency → **LangChain4J** (note version: 0.x or 1.x) |
| 10 | - Neither present → offer to add one (recommend Spring AI for Spring Boot 3.x, LangChain4J for Boot 2.x) |
| 11 | |
| 12 | Check Spring Boot version: |
| 13 | - Boot 3.x → Spring AI 1.x preferred, LangChain4J 0.35+ |
| 14 | - Boot 2.x → LangChain4J 0.30.x (Spring AI requires Boot 3.x) |
| 15 | |
| 16 | --- |
| 17 | |
| 18 | ## Mode: `review` |
| 19 | |
| 20 | User asks to review existing AI code. Check for: |
| 21 | |
| 22 | **Spring AI:** |
| 23 | - [ ] `ChatClient` built via `ChatClient.Builder` (not raw `ChatModel`) for fluent API |
| 24 | - [ ] Prompt templates use `PromptTemplate` with variables — no string concatenation |
| 25 | - [ ] Streaming uses `stream().content()` or `Flux<String>` — not blocking `.call()` for real-time responses |
| 26 | - [ ] `@Retryable` or Spring AI retry config on ChatClient calls — LLMs are flaky |
| 27 | - [ ] Secrets (`spring.ai.openai.api-key`) come from env vars or Vault, never hardcoded |
| 28 | - [ ] `VectorStore` queries use `SearchRequest.query(text).withTopK(n)` — not raw SQL |
| 29 | - [ ] RAG advisor (`QuestionAnswerAdvisor`) attached to ChatClient — not manual context injection |
| 30 | - [ ] Token usage logged at DEBUG, not INFO (avoid log noise) |
| 31 | |
| 32 | **LangChain4J:** |
| 33 | - [ ] AI services use `@AiService` interface — not `ChatLanguageModel.generate()` directly |
| 34 | - [ ] System prompts in `@SystemMessage` annotation — not hardcoded strings |
| 35 | - [ ] Memory uses `MessageWindowChatMemory` or `TokenWindowChatMemory` — not unlimited history |
| 36 | - [ ] Streaming via `StreamingChatLanguageModel` with `TokenStream` — not blocking |
| 37 | - [ ] Embeddings via `EmbeddingModel` + `EmbeddingStore` for RAG — not in-memory list search |
| 38 | - [ ] Tools annotated with `@Tool` on service methods — not manual function dispatch |
| 39 | - [ ] API key from `@Value("${langchain4j.openai.api-key}")` — never literal |
| 40 | |
| 41 | --- |
| 42 | |
| 43 | ## Mode: `chat` |
| 44 | |
| 45 | User asks to add a basic chatbot or chat endpoint. |
| 46 | |
| 47 | ### Spring AI |
| 48 | 1. Add dependency (see `references/patterns.md` → Spring AI Setup) |
| 49 | 2. Inject `ChatClient.Builder`, build a `ChatClient` bean |
| 50 | 3. Create `ChatController` with `@PostMapping("/chat")` |
| 51 | 4. Use `chatClient.prompt().user(message).call().content()` for simple response |
| 52 | 5. For streaming: return `Flux<String>` with `chatClient.prompt().user(message).stream().content()` |
| 53 | 6. Add `ANTHROPIC_API_KEY` / `OPENAI_API_KEY` to `application.yml` via `${env-var}` |
| 54 | |
| 55 | ### LangChain4J |
| 56 | 1. Add `langchain4j-spring-boot-starter` + provider dependency |
| 57 | 2. Define `@AiService` interface with `@SystemMessage` |
| 58 | 3. Register as Spring bean via `AiServices.builder(MyAssistant.class).chatLanguageModel(model).build()` |
| 59 | 4. Expose via `@RestController` |
| 60 | |
| 61 | --- |
| 62 | |
| 63 | ## Mode: `rag` |
| 64 | |
| 65 | User asks to implement RAG (chat over documents, knowledge base, semantic search). |
| 66 | |
| 67 | ### Spring AI RAG |
| 68 | 1. Choose vector store: PgVector (PostgreSQL), Chroma, Redis, Weaviate, Qdrant (see `references/patterns.md`) |
| 69 | 2. Add `spring-ai-{store}-store-spring-boot-starter` |
| 70 | 3. Ingest pipeline: |
| 71 | - `DocumentReader` (PDF, text, web) → `TokenTextSplitter` → `VectorStore.add()` |
| 72 | - Run at startup via `ApplicationRunner` or dedicated `@PostMapping("/ingest")` |
| 73 | 4. Query pipeline: |
| 74 | - Attach `QuestionAnswerAdvisor(vectorStore)` to `ChatClient` |
| 75 | - Spring AI auto-retrieves context and injects into prompt |
| 76 | 5. Tune: `SearchRequest.withTopK(5).withSimilarityThreshold(0.7)` |
| 77 | |
| 78 | ### LangChain4J RAG |
| 79 | 1. Add `EmbeddingStore` (Chroma, Qdrant, in-memory for dev) |
| 80 | 2. `EmbeddingStoreIngestor` with `DocumentSplitter` and `EmbeddingModel` |
| 81 | 3. `EmbeddingStoreContentRetriever` → `RetrievalAugmentor` → `AiServices` builder |
| 82 | |
| 83 | --- |
| 84 | |
| 85 | ## Mode: `tools` |
| 86 | |
| 87 | User asks to give the AI the ability to call Java methods (function/tool calling). |
| 88 | |
| 89 | ### Spring AI |
| 90 | 1. Define a `@Bean` of type `Function<Input, Output>` — Spring AI auto-registers it |
| 91 | 2. Or use `@Description` on a `record` parameter for rich schema |
| 92 | 3. Pass function names to `ChatClient`: `.options(OpenAiChatOptions.builder().withFunction("myFunction").build())` |
| 93 | 4. Spring AI handles the tool call loop automatically |
| 94 | |
| 95 | ### LangChain4J |
| 96 | 1. Annotate service methods with `@Tool("description of what this tool does")` |
| 97 | 2. Register the service as a tool: `AiServices.builder(...).tools(myToolService).build()` |
| 98 | 3. The model decides when to call — no manual dispatch needed |
| 99 | |
| 100 | --- |
| 101 | |
| 102 | ## Mode: `memory` |
| 103 | |
| 104 | User asks to add conversation memory / chat history. |
| 105 | |
| 106 | ### Spring AI |
| 107 | - `MessageChatMemoryAdvisor` with `InMemoryCh |