Package org.ametys.plugins.ai
Class AIHelper
java.lang.Object
org.ametys.runtime.plugin.component.AbstractLogEnabled
org.ametys.plugins.ai.AIHelper
- All Implemented Interfaces:
DeferredServiceable,LogEnabled,Initializable,Component,Contextualizable,Serviceable
public class AIHelper
extends AbstractLogEnabled
implements Component, DeferredServiceable, Contextualizable, Serviceable, Initializable
This is helper to use AI as image or text generation.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected AIProviderExtensionPointThe extension point providing access to registered AI providers.protected AmetysObjectResolverThe ametys object resolverprotected AbstractCacheManagerThe Ametys cache managerprotected ContextThe avalon contextprotected CurrentUserProviderThe current user providerprotected JSONUtilsThe JSON utils used to convert JSON to objectsprotected RenderingContextHandlerThe rendering context handlerprotected javax.jcr.RepositoryThe jcr repositoryprotected SearcherFactoryThe searcher factoryprotected SiteManagerThe site managerprotected SolrClientProviderThe solr client providerprotected UploadManagerThe upload manager used to upload imagesprotected URIResolverExtensionPointThe ametys uri resolverprotected WorkspaceSelectorThe workspace selectorstatic final StringAvalon rolestatic final StringThe site configuration parameter for the chatbot activationstatic final StringThe site configuration parameter for the chatbot namestatic final StringThe site configuration parameter for the chatbot promptstatic final StringThe site configuration parameter for the a specific image promptstatic final StringThe site configuration parameter for the semantic indexation activationstatic final StringThe site configuration parameter for the a specific text prompt -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaskChatbot(String userMessage, String historyJson, String siteName, String sitemapName) Get the chatbot response according to the current provider.computeEmbedding(String text) Get the embedding of a textvoidcontextualize(Context context) voiddeferredService(ServiceManager manager) Pass theServiceManagerused to access other components.protected AIProviderGet the current AI Provider classintGet the dimension of the embedding vectorGet a string key identifying the embedding generator.Initialize the prompt for image generation.getTextSummaryPrompt(int maxLength) Initialize the prompt with a given maximum length minus 10 characters to prevent the LLM to exceed the limit.voidbooleanisChatbotEnabled(String siteName) Checks if a configured AI provider is available and supports embedding and semantic indexation and chatbot are activated on the current site.booleanChecks if a configured AI provider is available and supports embedding.booleanChecks if a configured AI provider is available and supports text summarization.booleanChecks if a configured AI provider is available and supports image generation.booleanisSemanticIndexationActivated(String siteName) Checks if a configured AI provider is available and supports embedding and semantic indexation is activated on the current site.resourceToSummary(Resource resource, int maxLength) Summarize a resourcevoidservice(ServiceManager manager) textToImage(String text, String name) Generate an image with a given texttextToSummary(String text, int maxLength) Summarize a given textMethods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
Field Details
-
ROLE
Avalon role -
SITE_CONFIG_IMAGE_PROMPT
The site configuration parameter for the a specific image prompt- See Also:
-
SITE_CONFIG_TEXT_PROMPT
The site configuration parameter for the a specific text prompt- See Also:
-
SITE_CONFIG_SEMANTIC_INDEXATION_ACTIVATE
The site configuration parameter for the semantic indexation activation- See Also:
-
SITE_CONFIG_CHAT_ACTIVATE
The site configuration parameter for the chatbot activation- See Also:
-
SITE_CONFIG_CHAT_PROMPT
The site configuration parameter for the chatbot prompt- See Also:
-
SITE_CONFIG_CHAT_NAME
The site configuration parameter for the chatbot name- See Also:
-
_aiProviderEP
The extension point providing access to registered AI providers. -
_uploadManager
The upload manager used to upload images -
_currentUserProvider
The current user provider -
_jsonUtils
The JSON utils used to convert JSON to objects -
_searcherFactory
The searcher factory -
_uriResolverEP
The ametys uri resolver -
_ametysObjectResolver
The ametys object resolver -
_solrClientProvider
The solr client provider -
_siteManager
The site manager -
_context
The avalon context -
_workspaceSelector
The workspace selector -
_repository
The jcr repository -
_cacheManager
The Ametys cache manager -
_renderingContextHandler
The rendering context handler
-
-
Constructor Details
-
AIHelper
public AIHelper()
-
-
Method Details
-
service
- Specified by:
servicein interfaceServiceable- Throws:
ServiceException
-
initialize
- Specified by:
initializein interfaceInitializable- Throws:
Exception
-
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
-
contextualize
- Specified by:
contextualizein interfaceContextualizable- Throws:
ContextException
-
isEnabled
Checks if a configured AI provider is available and supports text summarization. This includes verifying the provider exists, has a valid text model, and is properly configured.- Returns:
- true if all is good, false otherwise
-
textToSummary
Summarize a given text- Parameters:
text- The input text to be summarized.maxLength- The maximum number of characters for the summary.- Returns:
- A map with the Ai-generated summary under the key "summary".
-
isImageGenerationSupported
Checks if a configured AI provider is available and supports image generation. This includes verifying the provider exists, has a valid text model, and is properly configured.- Returns:
- true if all is good, false otherwise
-
isEmbeddingSupported
Checks if a configured AI provider is available and supports embedding.- Returns:
- true if supported, false otherwise
-
isSemanticIndexationActivated
Checks if a configured AI provider is available and supports embedding and semantic indexation is activated on the current site.- Parameters:
siteName- The site to consider- Returns:
- true if activated
-
isChatbotEnabled
Checks if a configured AI provider is available and supports embedding and semantic indexation and chatbot are activated on the current site. This includes verifying the provider exists, has a valid text model, and is properly configured.- Parameters:
siteName- The site to consider- Returns:
- true if all is good, false otherwise
-
textToImage
Generate an image with a given text- Parameters:
text- The input text to generate the image with.name- The name for the generated image file.- Returns:
- A map with the URL of the Ai-generated image under the key "image".
-
resourceToSummary
Summarize a resource- Parameters:
resource- the resource to be summarized.maxLength- The maximum number of characters for the summary.- Returns:
- the summary.
- Throws:
Exception- if an error occurred
-
getCurrentAIProvider
Get the current AI Provider class- Returns:
- the AI provider
-
getTextSummaryPrompt
Initialize the prompt with a given maximum length minus 10 characters to prevent the LLM to exceed the limit.- Parameters:
maxLength- the number of characters allowed- Returns:
- the text of the prompt
-
getImageGenerationPrompt
Initialize the prompt for image generation.- Returns:
- the text of the prompt
-
askChatbot
public String askChatbot(String userMessage, String historyJson, String siteName, String sitemapName) throws IOException Get the chatbot response according to the current provider.- Parameters:
userMessage- texthistoryJson- the history of the conversation in JSON formatsiteName- The current site namesitemapName- The current sitemap name- Returns:
- the chatbot response message
- Throws:
IOException- if an error occurs
-
computeEmbedding
Get the embedding of a text- Parameters:
text- The text to get the embedding for- Returns:
- The embedding vector for the given text or null if an error occurred or embedding is not supported
-
getEmbeddingDimension
Get the dimension of the embedding vector- Returns:
- The embedding dimension or -1 if not supported
-
getEmbeddingGeneratorKey
Get a string key identifying the embedding generator. Comparing this key between two calls allows to know if an existing embedding is still valid.- Returns:
- The embedding generator key or null if embedding is not supported
-