Class AbstractAIProvider
java.lang.Object
org.ametys.runtime.plugin.component.AbstractLogEnabled
org.ametys.plugins.ai.provider.impl.AbstractAIProvider
- All Implemented Interfaces:
AIProvider,DeferredServiceable,LogEnabled,PluginAware,Disposable,Initializable,Component,Configurable
- Direct Known Subclasses:
GoogleAIGeminiProvider,MistralAIProvider,OllamaProvider,OpenAIProvider
public abstract class AbstractAIProvider
extends AbstractLogEnabled
implements AIProvider, Configurable, Initializable, PluginAware, DeferredServiceable, Component, Disposable
This is an abstract class for AI providers.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordInfo for an embedding model -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected AIHelperThe AI helperprotected dev.langchain4j.model.embedding.EmbeddingModelCurrent embedding modelprotected List<AbstractAIProvider.EmbeddingModelInfo> The provider models existing for embeddingprotected org.apache.hc.client5.http.impl.classic.CloseableHttpClientThe HTTP client used to fetch imagesprotected StringThe provider idprotected dev.langchain4j.model.image.ImageModelCurrent image modelprotected intThe context window for image generationThe provider models existing for image generationprotected I18nizableTextThe provider labelprotected StringThe plugin nameprotected dev.langchain4j.model.chat.ChatModelCurrent text modelprotected intThe context window for text generationThe provider models existing for text generationstatic final StringThe configuration parameter for the API keystatic final StringThe configuration parameter for embedding modelstatic final StringThe configuration parameter for image modelstatic final StringThe configuration parameter for image generation timeoutstatic final StringThe configuration parameter for URLstatic final StringThe configuration parameter for text model -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected PathHelper to download the given url to a temporary fileprotected dev.langchain4j.model.TokenCountEstimatorCreates a token count estimatoraskChatbot(dev.langchain4j.memory.ChatMemory memory) Get the response from the chatbot if embedding is supportedcomputeEmbedding(String text) Get the embedding of a textvoidconfigure(Configuration configuration) protected abstract dev.langchain4j.model.chat.ChatModelcreateChatModel(String modelName) Creates the text model implementationprotected abstract dev.langchain4j.model.embedding.EmbeddingModelcreateEmbeddingModel(String modelName) Creates the embedding model implementationprotected dev.langchain4j.model.image.ImageModelcreateImageModel(String modelName) Creates the image model implementationvoiddeferredService(ServiceManager manager) Pass theServiceManagerused to access other components.voiddispose()protected StringGet the API key from the configurationintGet the dimension of the embedding vectorGet a string key identifying the embedding generator.getId()Get the id of the providerGet the list of known embedding models for this providerGet the list of known image models for this providerGet the list of known text models for this providergetLabel()Get the label of the providerprotected StringGet the server URL from the configurationvoidprotected booleanIs normalization required for the embeddings of this providerbooleanCheck if the embedding is supportedbooleanCheck if the image generation is supportedNecessary for open models who often are not normalized.voidsetPluginInfo(String pluginName, String featureName, String id) Sets the plugin info relative to the current component.
Note : The feature name may be null if the targeted component in declared at plugin level.textToImage(String text) Get the URL of the generated imagetextToSummary(String text, int maxLength) Summarized a textMethods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
Field Details
-
CONFIG_API_KEY
The configuration parameter for the API key- See Also:
-
CONFIG_SERVER_URL
The configuration parameter for URL- See Also:
-
CONFIG_TEXT_MODEL
The configuration parameter for text model- See Also:
-
CONFIG_IMAGE_MODEL
The configuration parameter for image model- See Also:
-
CONFIG_IMAGE_TIMEOUT
The configuration parameter for image generation timeout- See Also:
-
CONFIG_EMBEDDING_MODEL
The configuration parameter for embedding model- See Also:
-
_id
The provider id -
_label
The provider label -
_pluginName
The plugin name -
_textModelNames
The provider models existing for text generation -
_imageModelNames
The provider models existing for image generation -
_embeddingModelInfos
The provider models existing for embedding -
_textModel
Current text model -
_textModelContextWindow
The context window for text generation -
_imageModel
Current image model -
_imageModelContextWindow
The context window for image generation -
_embeddingModel
Current embedding model -
_aiHelper
The AI helper -
_httpClient
The HTTP client used to fetch images
-
-
Constructor Details
-
AbstractAIProvider
public AbstractAIProvider()
-
-
Method Details
-
setPluginInfo
Description copied from interface:PluginAwareSets the plugin info relative to the current component.
Note : The feature name may be null if the targeted component in declared at plugin level.- Specified by:
setPluginInfoin interfacePluginAware- Parameters:
pluginName- Unique identifier for the plugin hosting the extensionfeatureName- Unique feature identifier (unique for a given pluginName)id- Unique identifier of this component
-
configure
- Specified by:
configurein interfaceConfigurable- Throws:
ConfigurationException
-
createChatModel
Creates the text model implementation- Parameters:
modelName- The model name- Returns:
- The chat model
-
createImageModel
Creates the image model implementation- Parameters:
modelName- The model name- Returns:
- The image model
-
createEmbeddingModel
protected abstract dev.langchain4j.model.embedding.EmbeddingModel createEmbeddingModel(String modelName) Creates the embedding model implementation- Parameters:
modelName- The model name- Returns:
- The embedding model
-
initialize
- Specified by:
initializein interfaceInitializable- Throws:
Exception
-
dispose
- Specified by:
disposein interfaceDisposable
-
deferredService
Description copied from interface:DeferredServiceablePass theServiceManagerused to access other components.- Specified by:
deferredServicein interfaceDeferredServiceable- Parameters:
manager- TheServiceManagerwhich thisDeferredServiceableuses. Must not benull.- Throws:
ServiceException- if an error occurs
-
getId
Description copied from interface:AIProviderGet the id of the provider- Specified by:
getIdin interfaceAIProvider- Returns:
- the id
-
getLabel
Description copied from interface:AIProviderGet the label of the provider- Specified by:
getLabelin interfaceAIProvider- Returns:
- the label
-
getKnownTextModels
Get the list of known text models for this provider- Returns:
- The list
-
getKnownImageModels
Get the list of known image models for this provider- Returns:
- The non null list
-
getKnownEmbeddingModels
Get the list of known embedding models for this provider- Returns:
- the non null list
-
_getTokenCountEstimator
Creates a token count estimator- Returns:
- The non null token count estimator
-
textToSummary
Description copied from interface:AIProviderSummarized a text- Specified by:
textToSummaryin interfaceAIProvider- Parameters:
text- the text to summarizemaxLength- the max number of characters that will be generated- Returns:
- the summarized text
- Throws:
Exception- if an error occurred during summarize process
-
isImageGenerationSupported
Description copied from interface:AIProviderCheck if the image generation is supported- Specified by:
isImageGenerationSupportedin interfaceAIProvider- Returns:
- boolean true if the generation is enabled, false otherwise
-
textToImage
Description copied from interface:AIProviderGet the URL of the generated image- Specified by:
textToImagein interfaceAIProvider- Parameters:
text- the prompt text- Returns:
- the generated image
- Throws:
Exception- if an error occurred during the image generation
-
_download
Helper to download the given url to a temporary file- Parameters:
uri- The uri to download- Returns:
- The created file
- Throws:
IOException- If an error occurred
-
getAPIKey
Get the API key from the configuration- Returns:
- the API key
-
getServerURL
Get the server URL from the configuration- Returns:
- the server URL
-
isEmbeddingSupported
Description copied from interface:AIProviderCheck if the embedding is supported- Specified by:
isEmbeddingSupportedin interfaceAIProvider- Returns:
- boolean true if the embedding is supported, false otherwise
-
askChatbot
Description copied from interface:AIProviderGet the response from the chatbot if embedding is supported- Specified by:
askChatbotin interfaceAIProvider- Parameters:
memory- the chat history message- Returns:
- the response from the chatbot
-
computeEmbedding
Description copied from interface:AIProviderGet the embedding of a text- Specified by:
computeEmbeddingin interfaceAIProvider- Parameters:
text- The text to embed- Returns:
- The embedding vector as a list of floats or null if an error occurred or if embedding is not supported
-
isEmbeddingNormalizationRequired
Is normalization required for the embeddings of this provider- Returns:
- true if normalization is required
-
normalize
Necessary for open models who often are not normalized.- Parameters:
vector- the vector to normalize- Returns:
- the normalized vector
-
getEmbeddingDimension
Description copied from interface:AIProviderGet the dimension of the embedding vector- Specified by:
getEmbeddingDimensionin interfaceAIProvider- Returns:
- The embedding dimension or -1 if not supported
-
getEmbeddingKey
Description copied from interface:AIProviderGet a string key identifying the embedding generator. Comparing this key between two calls allows to know if an existing embedding is still valid.- Specified by:
getEmbeddingKeyin interfaceAIProvider- Returns:
- The embedding generator key or null if embedding is not supported
-