langchain4j-ai-services-patterns by giuseppe-trisciuoglio/developer-kit
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill langchain4j-ai-services-patterns本技能提供使用 LangChain4j 构建声明式 AI 服务的指导,涵盖基于接口的模式、系统和用户消息的注解、内存管理、工具集成以及抽象底层 LLM 交互的高级 AI 应用模式。
在以下场景中使用此技能:
LangChain4j AI 服务允许您使用带有注解的普通 Java 接口来定义 AI 驱动的功能,无需手动构建提示词和解析响应。此模式以最少的样板代码提供类型安全、声明式的 AI 能力。
按照以下步骤使用 LangChain4j 创建声明式 AI 服务:
创建一个包含 AI 交互方法签名的 Java 接口:
public interface Assistant {
String chat(String userMessage);
}
使用 @SystemMessage 和 @UserMessage 注解来定义提示词:
public interface CustomerSupportBot {
@SystemMessage("You are a helpful customer support agent for TechCorp")
String handleInquiry(String customerMessage);
@UserMessage("Analyze sentiment: {{it}}")
Sentiment analyzeSentiment(String feedback);
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
使用 AiServices 构建器创建实现:
Assistant assistant = AiServices.builder(Assistant.class)
.chatModel(chatModel)
.build();
为多轮对话添加内存管理:
interface MultiUserAssistant {
String chat(@MemoryId String userId, String userMessage);
}
Assistant assistant = AiServices.builder(MultiUserAssistant.class)
.chatModel(model)
.chatMemoryProvider(userId -> MessageWindowChatMemory.withMaxMessages(10))
.build();
注册工具以使 AI 能够执行外部函数:
class Calculator {
@Tool("Add two numbers") double add(double a, double b) { return a + b; }
}
MathGenius mathGenius = AiServices.builder(MathGenius.class)
.chatModel(model)
.tools(new Calculator())
.build();
interface Assistant {
String chat(String userMessage);
}
// 创建实例 - LangChain4j 生成实现
Assistant assistant = AiServices.create(Assistant.class, chatModel);
// 使用服务
String response = assistant.chat("Hello, how are you?");
interface CustomerSupportBot {
@SystemMessage("You are a helpful customer support agent for TechCorp")
String handleInquiry(String customerMessage);
@UserMessage("Analyze sentiment: {{it}}")
String analyzeSentiment(String feedback);
}
CustomerSupportBot bot = AiServices.create(CustomerSupportBot.class, chatModel);
interface MultiUserAssistant {
String chat(@MemoryId String userId, String userMessage);
}
Assistant assistant = AiServices.builder(MultiUserAssistant.class)
.chatModel(model)
.chatMemoryProvider(userId -> MessageWindowChatMemory.withMaxMessages(10))
.build();
class Calculator {
@Tool("Add two numbers") double add(double a, double b) { return a + b; }
}
interface MathGenius {
String ask(String question);
}
MathGenius mathGenius = AiServices.builder(MathGenius.class)
.chatModel(model)
.tools(new Calculator())
.build();
请参阅 examples.md 获取全面的实际示例,包括:
完整的 API 文档、注解、接口和配置模式可在 references.md 中找到。
<!-- Maven -->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
<version>1.8.0</version>
</dependency>
// Gradle
implementation 'dev.langchain4j:langchain4j:1.8.0'
implementation 'dev.langchain4j:langchain4j-open-ai:1.8.0'
每周安装数
333
代码仓库
GitHub 星标数
173
首次出现
2026年2月3日
安全审计
安装于
claude-code266
gemini-cli253
opencode252
cursor250
codex247
github-copilot232
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.
Use this skill when:
LangChain4j AI Services allow you to define AI-powered functionality using plain Java interfaces with annotations, eliminating the need for manual prompt construction and response parsing. This pattern provides type-safe, declarative AI capabilities with minimal boilerplate code.
Follow these steps to create declarative AI Services with LangChain4j:
Create a Java interface with method signatures for AI interactions:
public interface Assistant {
String chat(String userMessage);
}
Use @SystemMessage and @UserMessage annotations to define prompts:
public interface CustomerSupportBot {
@SystemMessage("You are a helpful customer support agent for TechCorp")
String handleInquiry(String customerMessage);
@UserMessage("Analyze sentiment: {{it}}")
Sentiment analyzeSentiment(String feedback);
}
Use AiServices builder to create implementation:
Assistant assistant = AiServices.builder(Assistant.class)
.chatModel(chatModel)
.build();
Add memory management for multi-turn conversations:
interface MultiUserAssistant {
String chat(@MemoryId String userId, String userMessage);
}
Assistant assistant = AiServices.builder(MultiUserAssistant.class)
.chatModel(model)
.chatMemoryProvider(userId -> MessageWindowChatMemory.withMaxMessages(10))
.build();
Register tools to enable AI to execute external functions:
class Calculator {
@Tool("Add two numbers") double add(double a, double b) { return a + b; }
}
MathGenius mathGenius = AiServices.builder(MathGenius.class)
.chatModel(model)
.tools(new Calculator())
.build();
interface Assistant {
String chat(String userMessage);
}
// Create instance - LangChain4j generates implementation
Assistant assistant = AiServices.create(Assistant.class, chatModel);
// Use the service
String response = assistant.chat("Hello, how are you?");
interface CustomerSupportBot {
@SystemMessage("You are a helpful customer support agent for TechCorp")
String handleInquiry(String customerMessage);
@UserMessage("Analyze sentiment: {{it}}")
String analyzeSentiment(String feedback);
}
CustomerSupportBot bot = AiServices.create(CustomerSupportBot.class, chatModel);
interface MultiUserAssistant {
String chat(@MemoryId String userId, String userMessage);
}
Assistant assistant = AiServices.builder(MultiUserAssistant.class)
.chatModel(model)
.chatMemoryProvider(userId -> MessageWindowChatMemory.withMaxMessages(10))
.build();
class Calculator {
@Tool("Add two numbers") double add(double a, double b) { return a + b; }
}
interface MathGenius {
String ask(String question);
}
MathGenius mathGenius = AiServices.builder(MathGenius.class)
.chatModel(model)
.tools(new Calculator())
.build();
See examples.md for comprehensive practical examples including:
Complete API documentation, annotations, interfaces, and configuration patterns are available in references.md.
<!-- Maven -->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
<version>1.8.0</version>
</dependency>
// Gradle
implementation 'dev.langchain4j:langchain4j:1.8.0'
implementation 'dev.langchain4j:langchain4j-open-ai:1.8.0'
Weekly Installs
333
Repository
GitHub Stars
173
First Seen
Feb 3, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code266
gemini-cli253
opencode252
cursor250
codex247
github-copilot232
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
106,200 周安装