Package com.storedobject.ai
Class AI
java.lang.Object
com.storedobject.ai.AI
The AI class provides a centralized mechanism to manage and create AI-related services,
specifically chat-based interactions using tools and knowledge. This class ensures that
AI instances are tied to sessions and prevents redundant instances by using weak references.
It provides methods to retrieve API tokens and create chat interfaces with configurable memory
and tools capabilities.
- Author:
- Syam
-
Method Summary
Modifier and TypeMethodDescriptioncreateChat
(int rememberHistory, Knowledge knowledge) Creates a new chat instance with the specified memory retention limit and knowledge configuration.createChat
(Knowledge knowledge) Creates a chat instance using the provided Knowledge object with the default history retention limit.static AI
Retrieves an existing AI instance associated with the current session of the provided TransactionManager or creates a new one if no such instance exists.Retrieves an API token corresponding to the specified purpose.
-
Method Details
-
get
Retrieves an existing AI instance associated with the current session of the provided TransactionManager or creates a new one if no such instance exists. The method ensures that instances are tied to sessions using weak references to minimize memory usage.- Parameters:
tm
- The TransactionManager whose session is used to retrieve or create the associated AI instance.- Returns:
- The AI instance associated with the given TransactionManager's session.
-
getToken
Retrieves an API token corresponding to the specified purpose. If a token does not already exist for the given purpose, a new token is created, stored, and returned.- Parameters:
purpose
- the purpose for which the API token is required, represented as a string.- Returns:
- the
APIToken
associated with the specified purpose.
-
createChat
Creates a chat instance using the provided Knowledge object with the default history retention limit. Delegates tocreateChat(int rememberHistory, Knowledge knowledge)
with a default value of10
for therememberHistory
parameter.- Parameters:
knowledge
- TheKnowledge
object containing tools and data configurations required for the chat's functionality.- Returns:
- A new
Chat
instance configured with the providedKnowledge
object and default parameters. - Throws:
Exception
- If there is an issue, creating the chat instance or accessing required tokens.
-
createChat
Creates a new chat instance with the specified memory retention limit and knowledge configuration. This method configures the chat instance by setting up the AI model, chat memory, and tools from the providedKnowledge
object.- Parameters:
rememberHistory
- The number of recent chat messages to remember. The value is clamped between 1 and 100. Values below 1 are defaulted to 10, and values above 100 are capped at 100.knowledge
- TheKnowledge
object containing tools and data configurations required for the chat's functionality.- Returns:
- A new
Chat
instance configured with the providedKnowledge
object. - Throws:
Exception
- If there is an issue, creating the chat instance or accessing required tokens.
-