AI Services
Declare and configure AI service interfaces with @RegisterAIService for CDI injection.
AI Services
AI services are the primary integration point between LangChain4j and CDI. You declare a Java interface annotated with @RegisterAIService, and the CDI extension creates a proxy implementation that delegates to LangChain4j's AiServices builder.
Declaring an AI Service
@RegisterAIService(
chatModelName = "#default",
toolProviderName = "myTools",
chatMemoryName = "chat-memory",
contentRetrieverName = "retriever"
)
public interface ChatAiService {
String chat(String userMessage);
}
The proxy is registered as a CDI bean and can be injected into any CDI-managed bean.
Annotation Attributes
| Attribute | Description |
|---|---|
chatModelName |
Name of the ChatLanguageModel CDI bean |
streamingChatModelName |
Name of the StreamingChatLanguageModel CDI bean |
toolProviderName |
Name of the ToolProvider CDI bean |
tools |
Array of tool classes (alternative to toolProviderName) |
chatMemoryName |
Name of the ChatMemory CDI bean |
chatMemoryProviderName |
Name of the ChatMemoryProvider CDI bean |
contentRetrieverName |
Name of the ContentRetriever CDI bean |
retrievalAugmentorName |
Name of the RetrievalAugmentor CDI bean |
Name Resolution
Attribute values are resolved using these patterns:
"#default"-- Use the default CDI bean of that type""(empty) -- Ignore this dependency"myBean"-- Use the CDI bean namedmyBean"${property.key}"-- Resolved via MicroProfile Config (requireslangchain4j-cdi-config)"#{el.expression}"-- Evaluated as Jakarta EL (requireslangchain4j-cdi-el)
Component Priority
When multiple component sources are specified:
RetrievalAugmentortakes precedence overContentRetrieverToolProvideris preferred over thetoolsarray
Configuration-Based Creation
Components can also be created from configuration properties:
dev.langchain4j.cdi.plugin.my-model.class=dev.langchain4j.model.openai.OpenAiChatModel
dev.langchain4j.cdi.plugin.my-model.scope=jakarta.enterprise.context.ApplicationScoped
dev.langchain4j.cdi.plugin.my-model.config.apiKey=${OPENAI_API_KEY}
dev.langchain4j.cdi.plugin.my-model.config.modelName=gpt-4o
Special Lookup Values
lookup:@default-- Select the default CDI beanlookup:@all-- All beans of this type as a listlookup:<name>-- Named bean lookup