LangChain4j Integration
Secure your LangChain4j agents and tools with AIM's Java SDK. Full support for @Tool annotations and AI services.
LangChain4j + AIM Features
@Tool Annotation Support
Combine @Tool with @SecureAction seamlessly
AI Service Monitoring
Track all LLM calls and responses
Memory Tracking
Monitor chat memory and context usage
RAG Security
Secure retrieval augmented generation pipelines
Prerequisites
- • Java 17+ and Maven/Gradle
- • LangChain4j 0.27.0+ dependency
- • AIM SDK installed
Basic Integration
Add both LangChain4j and AIM SDK dependencies to your project:
<dependencies>
<!-- LangChain4j -->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>0.27.0</version>
</dependency>
<!-- AIM SDK -->
<dependency>
<groupId>org.opena2a</groupId>
<artifactId>aim-sdk</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>Securing LangChain4j Tools
Combine LangChain4j's @Tool annotation with AIM's @SecureAction for verified, logged tool execution:
import dev.langchain4j.agent.tool.Tool;
import org.opena2a.aim.annotation.SecureAction;
import org.opena2a.aim.annotation.EnableAIMSecurity;
@EnableAIMSecurity(
baseUrl = "http://localhost:8080",
sdkToken = "${AIM_SDK_TOKEN}"
)
public class SecuredTools {
@Tool("Search the product catalog")
@SecureAction(
agentId = "catalog-agent",
capability = "catalog:search",
riskLevel = "low"
)
public List<Product> searchProducts(String query) {
// Tool execution is verified and logged
return productService.search(query);
}
@Tool("Place an order for a customer")
@SecureAction(
agentId = "catalog-agent",
capability = "order:create",
riskLevel = "high"
)
public Order createOrder(String customerId, List<String> productIds) {
// High-risk action with full audit trail
return orderService.create(customerId, productIds);
}
@Tool("Process a refund")
@SecureAction(
agentId = "catalog-agent",
capability = "payment:refund",
riskLevel = "critical",
jitAccess = true // Requires admin approval
)
public Refund processRefund(String orderId, double amount) {
// Pauses until admin approves in AIM dashboard
return paymentService.refund(orderId, amount);
}
}AI Service Integration
Wrap LangChain4j AI services with AIM security:
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import org.opena2a.aim.AIMClient;
import org.opena2a.aim.Agent;
public class SecuredAIService {
private final AIMClient aimClient;
private final Agent agent;
public SecuredAIService() {
// Initialize AIM
this.aimClient = new AIMClient(
System.getenv("AIM_BASE_URL"),
System.getenv("AIM_SDK_TOKEN")
);
this.agent = aimClient.registerAgent(
"langchain4j-assistant",
"langchain4j",
new String[]{"chat:send", "tool:execute", "memory:read"}
);
}
public Assistant createAssistant() {
ChatLanguageModel model = OpenAiChatModel.builder()
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName("gpt-4")
.build();
// Create secured tools
SecuredTools tools = new SecuredTools();
return AiServices.builder(Assistant.class)
.chatLanguageModel(model)
.tools(tools)
.build();
}
interface Assistant {
String chat(String userMessage);
}
}RAG Pipeline Security
Secure retrieval augmented generation pipelines:
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import org.opena2a.aim.annotation.SecureAction;
public class SecuredRAG {
private final ContentRetriever contentRetriever;
@SecureAction(
agentId = "rag-agent",
capability = "knowledge:retrieve",
riskLevel = "low"
)
public List<Content> retrieveContext(String query) {
// Document retrieval is logged
return contentRetriever.retrieve(query);
}
@SecureAction(
agentId = "rag-agent",
capability = "knowledge:query",
riskLevel = "medium",
resource = "customer-data"
)
public String queryKnowledgeBase(String query, String dataSource) {
// Track which data sources are accessed
List<Content> context = retrieveContext(query);
return generateResponse(query, context);
}
}