Class AnthropicModel

java.lang.Object
uno.anahata.asi.agi.provider.AbstractModel
uno.anahata.asi.anthropic.AnthropicModel

public class AnthropicModel extends AbstractModel
Model implementation for Anthropic's Claude.
Author:
anahata
  • Field Details

    • modelId

      private final String modelId
      The unique identifier for the model.
    • displayName

      private final String displayName
      The user-friendly name of the model.
    • version

      private final String version
      The version identifier for the model.
  • Constructor Details

    • AnthropicModel

      public AnthropicModel(AnthropicProvider provider, String modelId, String displayName, String version)
      Constructs a new Anthropic model instance with full version details.
      Parameters:
      provider - The parent provider.
      modelId - The model ID.
      displayName - The display name.
      version - The model version.
    • AnthropicModel

      public AnthropicModel(AnthropicProvider provider, String modelId, String displayName)
      Constructs a new Anthropic model instance with default empty version details.
      Parameters:
      provider - The parent provider.
      modelId - The model ID.
      displayName - The display name.
  • Method Details

    • getProvider

      public AnthropicProvider getProvider()
      Returns the parent AnthropicProvider instance owning this model.
      Returns:
      The Anthropic provider instance.
    • countTokens

      public int countTokens(String text)
      Counts the number of tokens in the given text string using this model's specific tokenizer.

      Utilizes TokenizerUtils to perform offline, BPE token counting based on configured TokenizerType.

      Specified by:
      countTokens in class AbstractModel
      Parameters:
      text - The text to count tokens for.
      Returns:
      The token count, or 0 if the text is null or empty.
    • countTokens

      public 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.

      Serializes the tool call into a standard Anthropic 'tool_use' block JSON object, containing 'id', 'name', and 'input' properties, and counts its tokens.

      Specified by:
      countTokens in class AbstractModel
      Parameters:
      toolCall - The tool call to count tokens for.
      Returns:
      The total token count.
    • getModelId

      public String getModelId()
      Gets the unique identifier for this model (e.g., "models/gemini-1.5-pro").
      Specified by:
      getModelId in class AbstractModel
      Returns:
      The model ID.
    • isSupportsFunctionCalling

      public boolean isSupportsFunctionCalling()
      Checks if this model supports native function calling (tools).
      Specified by:
      isSupportsFunctionCalling in class AbstractModel
      Returns:
      true if supported.
    • isSupportsContentGeneration

      public boolean isSupportsContentGeneration()
      Checks if this model supports content generation.
      Specified by:
      isSupportsContentGeneration in class AbstractModel
      Returns:
      true if supported.
    • isSupportsBatchEmbeddings

      public boolean isSupportsBatchEmbeddings()
      Checks if this model supports batch embedding generation.
      Specified by:
      isSupportsBatchEmbeddings in class AbstractModel
      Returns:
      true if supported.
    • isSupportsEmbeddings

      public boolean isSupportsEmbeddings()
      Checks if this model supports single content embedding generation.
      Specified by:
      isSupportsEmbeddings in class AbstractModel
      Returns:
      true if supported.
    • isSupportsCachedContent

      public boolean isSupportsCachedContent()
      Checks if this model supports content caching.
      Specified by:
      isSupportsCachedContent in class AbstractModel
      Returns:
      true if supported.
    • getAvailableServerTools

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

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

      protected com.fasterxml.jackson.databind.node.ObjectNode preparePayload(GenerationRequest request, boolean stream)
      Helper to prepare the final JSON payload containing the system instructions, history items, tools, and generation parameters.
      Parameters:
      request - the source generation request details.
      stream - true if the request is streaming.
      Returns:
      the fully constructed JSON ObjectNode payload.
    • generateContent

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

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

      public String getToolDeclarationJson(AbstractTool<?,?> tool, RequestConfig config)
      Description copied from class: AbstractModel
      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.
      Specified by:
      getToolDeclarationJson in class AbstractModel
      Parameters:
      tool - The tool to inspect.
      config - The request configuration (e.g. to check useNativeSchemas).
      Returns:
      The JSON string representing the tool declaration.
    • countTokens

      public 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.

      Serializes the tool response into an OpenAI-compatible JSON structure and counts its tokens, ensuring strict exception logging and rethrowing for thread stability.

      Specified by:
      countTokens in class AbstractModel
      Parameters:
      toolResponse - The tool response instance to count.
      Returns:
      The precise token count.
    • countTokens

      public 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).

      Calculates the exact, model-specific multimodal token count for Anthropic image data. Delegates the header-only image dimension reading to the core ImageMetadataUtils utility, and performs the Anthropic-specific megapixel-based calculation locally to maintain strict, provider-decoupled modular boundaries.

      Specified by:
      countTokens in class AbstractModel
      Parameters:
      data - The raw binary data of the file or attachment.
      mimeType - The detected MIME type of the binary data (e.g. "image/png").
      Returns:
      The precise, billing-identical multimodal token count.