Annotation Interface RegisterAiService
AiServices in a declarative manner that the application can then use simply by
using the class as a CDI bean.
Under the hood LangChain4j's AiServices.builder(Class) is called
while also providing the builder with the proper
invalid reference
ChatLanguageModel
tools bean (optional),
ChatMemoryProvider and ContentRetriever beans (which by default are configured if such beans exist).
NOTE: The resulting CDI bean is RequestScoped by default. If you need to change the scope,
simply annotate the class with a CDI scope.
CAUTION: When using anything other than the request scope, you need to be very careful with the chat memory implementation.
NOTE: When the application also contains the quarkus-micrometer extension, metrics are automatically generated
for the method invocations.
See also ToolBox, SeedMemory
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classMarker that is used to tell Quarkus to use thethat has been configured as a CDI bean by any of the extensions providing such capability (such asinvalid reference
ChatLanguageModelquarkus-langchain4j-openaiandquarkus-langchain4j-hugging-face).static final classMarker that is used to tell Quarkus to use the retriever that the user has configured as a CDI bean.static final classMarker that is used to tell Quarkus to use theImageModelthat the user has configured as a CDI bean.static final classMarker that is used to tell Quarkus to use theModerationModelthat the user has configured as a CDI bean.static final classMarker that is used to tell Quarkus to use theRetrievalAugmentorthat the user has configured as a CDI bean.static final classMarker that is used to tell Quarkus to use the ToolHallucinationStrategy that the user has configured as a CDI bean.static final classMarker that is used to tell Quarkus to use theToolProviderthat the user has configured as a CDI bean.static final classMarker that is used to tell Quarkus to use thethat has been configured as a CDI bean by * any of the extensions providing such capability (such asinvalid reference
StreamingChatLanguageModelquarkus-langchain4j-openaiandquarkus-langchain4j-hugging-face).static final classMarker that is used when the user does not want any memory configured for the AiServicestatic final classMarker that is used to tell Quarkus to not use any retrieval augmentor even if a CDI bean implementing the `RetrievalAugmentor` interface exists.static final classMarker class to indicate that no retriever should be usedstatic final classMarker that is used when the user does not want any tool provider -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionConfigures the way to obtain theto use.invalid reference
ChatLanguageModelConfigures the way to obtain theChatMemoryProvider.intDefines the maximum number of sequential calls to tools while handling a single chat request.WhenchatLanguageModelSupplieris set toBeanChatLanguageModelSupplier.class(which is the default) this allows the selection of theCDI bean to use.invalid reference
ChatLanguageModelConfigures the way to obtain theModerationModelto use.Configures the way to obtain theRetrievalAugmentorto use (when using RAG).Configures the way to obtain theto use.invalid reference
StreamingChatLanguageModelClass<? extends Function<dev.langchain4j.agent.tool.ToolExecutionRequest, dev.langchain4j.data.message.ToolExecutionResultMessage>> Strategy to be used when the AI service hallucinates the tool name.Configures a toolProviderSupplier.Class<?>[]Tool classes to use.
-
Element Details
-
streamingChatLanguageModelSupplier
Class<? extends Supplier<dev.langchain4j.model.chat.StreamingChatModel>> streamingChatLanguageModelSupplierConfigures the way to obtain theto use. If not configured, the default CDI bean implementing the model is looked up. Such a bean provided automatically by extensions such asinvalid reference
StreamingChatLanguageModelquarkus-langchain4j-openai,quarkus-langchain4j-azure-openaiorquarkus-langchain4j-hugging-face- Default:
io.quarkiverse.langchain4j.RegisterAiService.BeanStreamingChatLanguageModelSupplier.class
-
chatLanguageModelSupplier
Configures the way to obtain theto use. If not configured, the default CDI bean implementing the model is looked up. Such a bean provided automatically by extensions such asinvalid reference
ChatLanguageModelquarkus-langchain4j-openai,quarkus-langchain4j-azure-openaiorquarkus-langchain4j-hugging-face- Default:
io.quarkiverse.langchain4j.RegisterAiService.BeanChatLanguageModelSupplier.class
-
modelName
String modelNameWhenchatLanguageModelSupplieris set toBeanChatLanguageModelSupplier.class(which is the default) this allows the selection of theCDI bean to use.invalid reference
ChatLanguageModelIf not set, the default model (i.e. the one configured without setting the model name) is used. An example of the default model configuration is the following:
quarkus.langchain4j.openai.chat-model.model-name=gpt-4-turbo-previewIf set, it uses the model configured by name. For example if this is set to
somenamean example configuration value for that named model could be:quarkus.langchain4j.somename.openai.chat-model.model-name=gpt-4-turbo-previewIf the model needs to be selected at runtime, then
ModelNamecan be used as a method parameter of an AiService.- Default:
"<default>"
-
maxSequentialToolInvocations
int maxSequentialToolInvocationsDefines the maximum number of sequential calls to tools while handling a single chat request. If this number is exceeded, the chat request will fail. If not specified (left to zero) for a specific AI service, the AI service will use the value of the commonquarkus.langchain4j.ai-service.max-tool-executionsproperty. If that property is unset too, the default is 10 invocations.- Default:
0
-
tools
-
toolHallucinationStrategy
Class<? extends Function<dev.langchain4j.agent.tool.ToolExecutionRequest,dev.langchain4j.data.message.ToolExecutionResultMessage>> toolHallucinationStrategyStrategy to be used when the AI service hallucinates the tool name.- Default:
io.quarkiverse.langchain4j.RegisterAiService.BeanIfExistsToolHallucinationStrategy.class
-
chatMemoryProviderSupplier
Class<? extends Supplier<dev.langchain4j.memory.chat.ChatMemoryProvider>> chatMemoryProviderSupplierConfigures the way to obtain theChatMemoryProvider.Be default, Quarkus configures a
ChatMemoryProviderbean that uses aInMemoryChatMemoryStorebean as the backing store. The default type for the actualChatMemoryisMessageWindowChatMemoryand it is configured with the value of thequarkus.langchain4j.chat-memory.memory-window.max-messagesconfiguration property (which default to 10) as a way of limiting the number of messages in each chat.If the application provides its own
ChatMemoryProviderbean, that takes precedence over what Quarkus provides as the default.If the application provides an implementation of
ChatMemoryStore, then that is used instead of the defaultInMemoryChatMemoryStore.In the most advances case, an arbitrary
ChatMemoryProvidercan be used by having a customSupplier<ChatMemoryProvider>configured in this property.Supplier<dev.langchain4j.memory.chat.ChatMemoryProvider>needs to be provided.- Default:
io.quarkiverse.langchain4j.RegisterAiService.BeanChatMemoryProviderSupplier.class
-
retrievalAugmentor
Configures the way to obtain theRetrievalAugmentorto use (when using RAG). The Supplier may or may not be a CDI bean (but most typically it will, so consider adding a bean-defining annotation to it). If it is not a CDI bean, Quarkus will create an instance by calling its no-arg constructor.If unspecified, Quarkus will attempt to locate a CDI bean that implements
RetrievalAugmentorand use it if one exists.- Default:
io.quarkiverse.langchain4j.RegisterAiService.BeanIfExistsRetrievalAugmentorSupplier.class
-
moderationModelSupplier
Configures the way to obtain theModerationModelto use. By default, Quarkus will look for a CDI bean that implementsModerationModelif at least one method is annotated with @Moderate. If an arbitraryModerationModelinstance is needed, a custom implementation ofSupplier<dev.langchain4j.model.moderation.ModerationModel>needs to be provided.- Default:
io.quarkiverse.langchain4j.RegisterAiService.BeanIfExistsModerationModelSupplier.class
-
toolProviderSupplier
-