$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill langchain4j-ai-services-patternsProvides patterns to build declarative AI Services with LangChain4j for LLM integration, chatbot development, AI agent implementation, and conversational AI in Java. Generates type-safe AI services using interface-based patterns, annotations, memory management, and tools integrat
| 1 | # LangChain4j AI Services Patterns |
| 2 | |
| 3 | This skill provides guidance for building declarative AI Services with LangChain4j using interface-based patterns, annotations for system and user messages, memory management, tools integration, and advanced AI application patterns that abstract away low-level LLM interactions. |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | LangChain4j AI Services define AI functionality using Java interfaces with annotations, providing type-safe, declarative AI with minimal boilerplate. |
| 8 | |
| 9 | ## When to Use |
| 10 | |
| 11 | Use this skill when: |
| 12 | - Building declarative AI services with minimal boilerplate using Java interfaces |
| 13 | - Creating type-safe conversational AI with memory management |
| 14 | - Implementing AI agents with function/tool calling capabilities |
| 15 | - Designing AI services returning structured data (enums, POJOs, lists) |
| 16 | - Integrating RAG patterns declaratively |
| 17 | |
| 18 | ## Instructions |
| 19 | |
| 20 | Follow these steps to create declarative AI Services with LangChain4j: |
| 21 | |
| 22 | ### 1. Define AI Service Interface |
| 23 | |
| 24 | Create a Java interface with method signatures for AI interactions: |
| 25 | |
| 26 | ```java |
| 27 | interface Assistant { |
| 28 | String chat(String userMessage); |
| 29 | } |
| 30 | ``` |
| 31 | |
| 32 | ### 2. Add Annotations for System and User Messages |
| 33 | |
| 34 | Use `@SystemMessage` and `@UserMessage` annotations to define prompts: |
| 35 | |
| 36 | ```java |
| 37 | interface CustomerSupportBot { |
| 38 | @SystemMessage("You are a helpful customer support agent for TechCorp") |
| 39 | String handleInquiry(String customerMessage); |
| 40 | |
| 41 | @UserMessage("Analyze sentiment: {{it}}") |
| 42 | Sentiment analyzeSentiment(String feedback); |
| 43 | } |
| 44 | ``` |
| 45 | |
| 46 | ### 3. Create AI Service Instance |
| 47 | |
| 48 | Use `AiServices` builder or create to instantiate the service: |
| 49 | |
| 50 | ```java |
| 51 | // Simple creation |
| 52 | Assistant assistant = AiServices.create(Assistant.class, chatModel); |
| 53 | |
| 54 | // Or with builder for advanced configuration |
| 55 | Assistant assistant = AiServices.builder(Assistant.class) |
| 56 | .chatModel(chatModel) |
| 57 | .build(); |
| 58 | ``` |
| 59 | |
| 60 | ### 4. Configure Memory for Multi-turn Conversations |
| 61 | |
| 62 | Add memory management using `@MemoryId` for multi-user scenarios: |
| 63 | |
| 64 | ```java |
| 65 | interface MultiUserAssistant { |
| 66 | String chat(@MemoryId String userId, String userMessage); |
| 67 | } |
| 68 | |
| 69 | Assistant assistant = AiServices.builder(MultiUserAssistant.class) |
| 70 | .chatModel(model) |
| 71 | .chatMemoryProvider(userId -> MessageWindowChatMemory.withMaxMessages(10)) |
| 72 | .build(); |
| 73 | ``` |
| 74 | |
| 75 | ### 5. Integrate Tools for Function Calling |
| 76 | |
| 77 | Register tools using `@Tool` annotation to enable AI function execution: |
| 78 | |
| 79 | ```java |
| 80 | class Calculator { |
| 81 | @Tool("Add two numbers") double add(double a, double b) { return a + b; } |
| 82 | } |
| 83 | |
| 84 | interface MathGenius { |
| 85 | String ask(String question); |
| 86 | } |
| 87 | |
| 88 | MathGenius mathGenius = AiServices.builder(MathGenius.class) |
| 89 | .chatModel(model) |
| 90 | .tools(new Calculator()) |
| 91 | .build(); |
| 92 | ``` |
| 93 | |
| 94 | ### 6. Validate and Test |
| 95 | |
| 96 | Test AI services with concrete validation patterns: |
| 97 | |
| 98 | ```java |
| 99 | // 1. Test with sample inputs |
| 100 | String response = assistant.chat("Hello, how are you?"); |
| 101 | assert response != null && !response.isEmpty(); |
| 102 | |
| 103 | // 2. Validate structured outputs with assertions |
| 104 | Sentiment result = bot.analyzeSentiment("Great product!"); |
| 105 | assert result == Sentiment.POSITIVE; |
| 106 | |
| 107 | // 3. Log tool calls with side effects for audit |
| 108 | MathGenius math = AiServices.builder(MathGenius.class) |
| 109 | .chatModel(model) |
| 110 | .tools(new Calculator()) |
| 111 | .build(); |
| 112 | |
| 113 | // 4. Test memory isolation between users |
| 114 | String userA = assistant.chat("User A message", "session-a"); |
| 115 | String userB = assistant.chat("User B message", "session-b"); |
| 116 | assert !userA.equals(userB); // Verify memory isolation |
| 117 | ``` |
| 118 | |
| 119 | ## Examples |
| 120 | |
| 121 | See [examples.md](references/examples.md) for comprehensive practical examples including: |
| 122 | - Basic chat interfaces |
| 123 | - Stateful assistants with memory |
| 124 | - Multi-user scenarios |
| 125 | - Structured output extraction |
| 126 | - Tool calling and function execution |
| 127 | - Streaming responses |
| 128 | - Error handling |
| 129 | - RAG integration |
| 130 | - Production patterns |
| 131 | |
| 132 | ## API Reference |
| 133 | |
| 134 | Complete API documentation, annotations, interfaces, and configuration patterns are available in [references.md](references/references.md). |
| 135 | |
| 136 | ## Best Practices |
| 137 | |
| 138 | 1. **Use type-safe interfaces** instead of string-based prompts |
| 139 | 2. **Implement proper memory management** with appropriate limits |
| 140 | 3. **Design clear tool descriptions** with parameter documentation |
| 141 | 4. **Handle errors gracefully** with custom error handlers |
| 142 | 5. **Use structured output** for predictable responses |
| 143 | 6. **Implement validation** for user inputs |
| 144 | 7. **Monitor performance** for production deployments |
| 145 | |
| 146 | ## Dependencies |
| 147 | |
| 148 | ```xml |
| 149 | <!-- Maven --> |
| 150 | <dependency> |
| 151 | <groupId>dev.langchain |