Class AnthropicModel
- Author:
- anahata
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final StringThe user-friendly name of the model.private final StringThe unique identifier for the model.private final StringThe version identifier for the model.Fields inherited from class AbstractModel
defaultTemperature, defaultTopK, defaultTopP, description, enabled, MARKUP_TABLE_HEADER, maxInputTokens, maxOutputTokens, provider, rawDescription, supportedActions, supportedResponseModalities, tokenizerType -
Constructor Summary
ConstructorsConstructorDescriptionAnthropicModel(AnthropicProvider provider, String modelId, String displayName) Constructs a new Anthropic model instance with default empty version details.AnthropicModel(AnthropicProvider provider, String modelId, String displayName, String version) Constructs a new Anthropic model instance with full version details. -
Method Summary
Modifier and TypeMethodDescriptionintcountTokens(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.intcountTokens(String text) Counts the number of tokens in the given text string using this model's specific tokenizer.intcountTokens(AbstractToolCall<?, ?> toolCall) Counts the number of tokens consumed by the given tool call in the context window.intcountTokens(AbstractToolResponse<?> toolResponse) Counts the number of tokens consumed by the given tool execution response.generateContent(GenerationRequest request) The core method for interacting with an AI model.voidgenerateContentStream(GenerationRequest request, StreamObserver<Response<? extends AbstractModelMessage>> observer) Generates content asynchronously using token streaming.Gets the list of server-side tools available for this model.Gets the list of server-side tools that should be enabled by default for this model.Gets the unique identifier for this model (e.g., "models/gemini-1.5-pro").Returns the parentAnthropicProviderinstance owning this model.getToolDeclarationJson(AbstractTool<?, ?> tool, RequestConfig config) Gets the provider-specific JSON representation of a tool's declaration.booleanChecks if this model supports batch embedding generation.booleanChecks if this model supports content caching.booleanChecks if this model supports content generation.booleanChecks if this model supports single content embedding generation.booleanChecks if this model supports native function calling (tools).protected com.fasterxml.jackson.databind.node.ObjectNodepreparePayload(GenerationRequest request, boolean stream) Helper to prepare the final JSON payload containing the system instructions, history items, tools, and generation parameters.Methods inherited from class AbstractModel
getProviderId, getTokenizerType, hasDiscrepancy, isRegistered, matches, persist, remove, resetFromApi, toMarkupRow, toSafeModelFileName, toString
-
Field Details
-
modelId
The unique identifier for the model. -
displayName
The user-friendly name of the model. -
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
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
Returns the parentAnthropicProviderinstance owning this model.- Returns:
- The Anthropic provider instance.
-
countTokens
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:
countTokensin classAbstractModel- Parameters:
text- The text to count tokens for.- Returns:
- The token count, or 0 if the text is null or empty.
-
countTokens
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:
countTokensin classAbstractModel- Parameters:
toolCall- The tool call to count tokens for.- Returns:
- The total token count.
-
getModelId
Gets the unique identifier for this model (e.g., "models/gemini-1.5-pro").- Specified by:
getModelIdin classAbstractModel- Returns:
- The model ID.
-
isSupportsFunctionCalling
public boolean isSupportsFunctionCalling()Checks if this model supports native function calling (tools).- Specified by:
isSupportsFunctionCallingin classAbstractModel- Returns:
- true if supported.
-
isSupportsContentGeneration
public boolean isSupportsContentGeneration()Checks if this model supports content generation.- Specified by:
isSupportsContentGenerationin classAbstractModel- Returns:
- true if supported.
-
isSupportsBatchEmbeddings
public boolean isSupportsBatchEmbeddings()Checks if this model supports batch embedding generation.- Specified by:
isSupportsBatchEmbeddingsin classAbstractModel- Returns:
- true if supported.
-
isSupportsEmbeddings
public boolean isSupportsEmbeddings()Checks if this model supports single content embedding generation.- Specified by:
isSupportsEmbeddingsin classAbstractModel- Returns:
- true if supported.
-
isSupportsCachedContent
public boolean isSupportsCachedContent()Checks if this model supports content caching.- Specified by:
isSupportsCachedContentin classAbstractModel- Returns:
- true if supported.
-
getAvailableServerTools
Gets the list of server-side tools available for this model.- Specified by:
getAvailableServerToolsin classAbstractModel- Returns:
- A list of available server tools.
-
getDefaultServerTools
Gets the list of server-side tools that should be enabled by default for this model.- Specified by:
getDefaultServerToolsin classAbstractModel- 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
Description copied from class:AbstractModelThe 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:
generateContentin classAbstractModel- Parameters:
request- The generation request containing config and history.- Returns:
- A standardized
Responseobject.
-
generateContentStream
public void generateContentStream(GenerationRequest request, StreamObserver<Response<? extends AbstractModelMessage>> observer) Description copied from class:AbstractModelGenerates content asynchronously using token streaming.- Specified by:
generateContentStreamin classAbstractModel- Parameters:
request- The generation request containing config and history.observer- The observer that will receive the streaming response chunks.
-
getToolDeclarationJson
Description copied from class:AbstractModelGets 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:
getToolDeclarationJsonin classAbstractModel- Parameters:
tool- The tool to inspect.config- The request configuration (e.g. to check useNativeSchemas).- Returns:
- The JSON string representing the tool declaration.
-
countTokens
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:
countTokensin classAbstractModel- Parameters:
toolResponse- The tool response instance to count.- Returns:
- The precise token count.
-
countTokens
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
ImageMetadataUtilsutility, and performs the Anthropic-specific megapixel-based calculation locally to maintain strict, provider-decoupled modular boundaries.- Specified by:
countTokensin classAbstractModel- 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.
-

