Class AbstractModel

java.lang.Object
uno.anahata.asi.agi.provider.AbstractModel
Direct Known Subclasses:
AnthropicModel, GeminiModel, OpenAiCompatibleModel, OpenAiModel

public abstract class AbstractModel extends Object
The abstract base class for a specific AI model (e.g., "gemini-1.5-pro-latest"). In the V2 architecture, this class is the definitive entry point for generating content, creating a clean, object-oriented API where the model itself is the actor.
Author:
anahata-gemini-pro-2.5
  • Field Details

    • MARKUP_TABLE_HEADER

      public static final String MARKUP_TABLE_HEADER
      Markdown table header for model listings.
      See Also:
    • provider

      protected transient AbstractAiProvider provider
      The parent AI provider that owns this model instance. Mark transient to prevent serialization of provider graph in model caches.
    • enabled

      protected boolean enabled
      Whether this model is enabled and offered in model selectors and tool listings.
    • tokenizerType

      protected TokenizerType tokenizerType
      The optional model-specific tokenizer. If null, the provider's default is used.
    • displayName

      protected String displayName
      User-facing or API display name for this model.
    • description

      protected String description
      Human-readable description of this model's capabilities and limitations.
    • version

      protected String version
      The version identifier for this model.
    • maxInputTokens

      protected Integer maxInputTokens
      The maximum number of input tokens allowed, or null if unspecified.
    • maxOutputTokens

      protected Integer maxOutputTokens
      The maximum number of output tokens this model can generate in a single turn, or null if unspecified.
    • defaultTemperature

      protected Float defaultTemperature
      The default temperature setting for this model, or null if unspecified.
    • defaultTopP

      protected Float defaultTopP
      The default Top-P setting for this model, or null if unspecified.
    • defaultTopK

      protected Integer defaultTopK
      The default Top-K setting for this model, or null if unspecified.
    • supportedResponseModalities

      protected List<ResponseModality> supportedResponseModalities
      The list of response modalities supported by this model (e.g., TEXT, IMAGE, AUDIO, VIDEO).
    • supportedActions

      protected List<String> supportedActions
      The list of API actions supported by this model (e.g. 'generateContent', 'batchEmbedContents', 'chat/completions').
    • rawDescription

      protected String rawDescription
      The raw description or raw JSON representation of the model as received from the remote API endpoint.
  • Constructor Details

    • AbstractModel

      public AbstractModel()
  • Method Details

    • isRegistered

      public boolean isRegistered()
      Checks if this model is registered in its parent provider's master persisted models list.
      Returns:
      true if this model exists in provider.getModels(), false otherwise.
    • hasDiscrepancy

      public boolean hasDiscrepancy()
      Checks if this locally registered model entity has different configuration values compared to its corresponding cached API model in the provider.
      Returns:
      true if any field value differs, false if identical or no cached API model is available.
      Throws:
      IllegalStateException - if called on an unregistered model.
    • resetFromApi

      public void resetFromApi() throws IOException
      Resets this locally registered model entity from its cached API model counterpart and persists changes to disk.
      Throws:
      IOException - if persisting the updated model fails.
      IllegalStateException - if this model is not registered or no cached API model is available.
    • toSafeModelFileName

      public static String toSafeModelFileName(String modelId)
      Converts a model ID into a safe filename with .kryo extension.
      Parameters:
      modelId - The raw model ID string.
      Returns:
      A safe filename for disk storage.
    • persist

      public void persist() throws IOException
      Persists this model entity directly to disk in its parent provider's models directory (~/.anahata/asi/<provider>/models/<model_id>.kryo).
      Throws:
      IOException - If creating the directory or saving the file fails.
    • remove

      public void remove() throws IOException
      Deletes the persisted .kryo file for this model from disk and removes it from its parent provider.
      Throws:
      IOException - If deleting the file fails.
    • getTokenizerType

      public TokenizerType getTokenizerType()
      Gets the effective tokenizer type for this model.
      Returns:
      The model's tokenizer if set, otherwise the parent provider's tokenizer.
    • countTokens

      public abstract int countTokens(String text)
      Counts the number of tokens in the given text string using this model's specific tokenizer.
      Parameters:
      text - The text to count tokens for.
      Returns:
      The number of tokens, or 0 if the text is null or empty.
    • countTokens

      public abstract int countTokens(AbstractToolCall<?,?> toolCall)
      Counts the number of tokens consumed by the given tool call in the context window.

      Model subclasses override this method to serialize the tool call into its exact wire-format (JSON, Protobuf, etc.) to ensure 100% accurate billing.

      Parameters:
      toolCall - The tool call to count tokens for.
      Returns:
      The precise token count.
    • countTokens

      public abstract int countTokens(byte[] data, String mimeType)
      Counts the number of tokens consumed by raw binary data based on its MIME type and model-specific multimodal billing rules.

      This generic signature provides complete decoupling from domain part classes, allowing the model to tokenize any binary payload (such as blob parts or tool attachments).

      Parameters:
      data - The raw binary data.
      mimeType - The MIME type of the binary data (e.g. "image/png").
      Returns:
      The precise token count, or 0 if no model is active or the data is null.
    • countTokens

      public abstract int countTokens(AbstractToolResponse<?> toolResponse)
      Counts the number of tokens consumed by the given tool execution response.

      Model subclasses override this to serialize the response into its exact wire-format (Protobuf FunctionResponse, JSON, etc.) to ensure 100% accurate billing.

      Parameters:
      toolResponse - The tool response to count tokens for.
      Returns:
      The exact number of tokens.
    • getModelId

      public abstract String getModelId()
      Gets the unique identifier for this model (e.g., "models/gemini-1.5-pro").
      Returns:
      The model ID.
    • getProviderId

      public final String getProviderId()
      Delegate method to get the id of this models provider.
      Returns:
      The unique ID of the provider.
    • isSupportsFunctionCalling

      public abstract boolean isSupportsFunctionCalling()
      Checks if this model supports native function calling (tools).
      Returns:
      true if supported.
    • isSupportsContentGeneration

      public abstract boolean isSupportsContentGeneration()
      Checks if this model supports content generation.
      Returns:
      true if supported.
    • isSupportsBatchEmbeddings

      public abstract boolean isSupportsBatchEmbeddings()
      Checks if this model supports batch embedding generation.
      Returns:
      true if supported.
    • isSupportsEmbeddings

      public abstract boolean isSupportsEmbeddings()
      Checks if this model supports single content embedding generation.
      Returns:
      true if supported.
    • isSupportsCachedContent

      public abstract boolean isSupportsCachedContent()
      Checks if this model supports content caching.
      Returns:
      true if supported.
    • matches

      public boolean matches(Pattern queryPattern, Set<ResponseModality> targetModalities)
      Checks if this model matches a search query (regex or substring) AND all target response modalities.
      Parameters:
      queryPattern - Optional compiled regex pattern to match against model attributes. If null, matches all.
      targetModalities - Optional set of required response modalities. If not empty, the model must support ALL specified modalities.
      Returns:
      true if the model satisfies all active criteria (AND logic).
    • getAvailableServerTools

      public abstract List<ServerTool> getAvailableServerTools()
      Gets the list of server-side tools available for this model.
      Returns:
      A list of available server tools.
    • getDefaultServerTools

      public abstract List<ServerTool> getDefaultServerTools()
      Gets the list of server-side tools that should be enabled by default for this model.
      Returns:
      The list of default server tools.
    • generateContent

      public abstract Response generateContent(GenerationRequest request)
      The core method for interacting with an AI model. It takes a configuration object and a list of messages and returns a standardized Response.
      Parameters:
      request - The generation request containing config and history.
      Returns:
      A standardized Response object.
    • generateContentStream

      public abstract void generateContentStream(GenerationRequest request, StreamObserver<Response<? extends AbstractModelMessage>> observer)
      Generates content asynchronously using token streaming.
      Parameters:
      request - The generation request containing config and history.
      observer - The observer that will receive the streaming response chunks.
    • getToolDeclarationJson

      public abstract String getToolDeclarationJson(AbstractTool<?,?> tool, RequestConfig config)
      Gets the provider-specific JSON representation of a tool's declaration. This is used by the UI to show exactly what is being sent to the model.
      Parameters:
      tool - The tool to inspect.
      config - The request configuration (e.g. to check useNativeSchemas).
      Returns:
      The JSON string representing the tool declaration.
    • toString

      public String toString()
      Returning the display name
      Overrides:
      toString in class Object
      Returns:
      the display name
    • toMarkupRow

      public String toMarkupRow()
      Formats this model as a Markdown table row for model listing and discovery tools.
      Returns:
      A Markdown row representing this model.