Class AnthropicBaseFormatter
java.lang.Object
io.agentscope.core.formatter.AbstractBaseFormatter<com.anthropic.models.messages.MessageParam,Object,com.anthropic.models.messages.MessageCreateParams.Builder>
io.agentscope.extensions.model.anthropic.formatter.AnthropicBaseFormatter
- All Implemented Interfaces:
io.agentscope.core.formatter.Formatter<com.anthropic.models.messages.MessageParam,Object, com.anthropic.models.messages.MessageCreateParams.Builder>
- Direct Known Subclasses:
AnthropicChatFormatter,AnthropicMultiAgentFormatter
public abstract class AnthropicBaseFormatter
extends io.agentscope.core.formatter.AbstractBaseFormatter<com.anthropic.models.messages.MessageParam,Object,com.anthropic.models.messages.MessageCreateParams.Builder>
Abstract base formatter for Anthropic API with shared logic for handling Anthropic-specific
requirements.
This class handles:
- System message extraction and application (Anthropic requires system via system parameter)
- Tool choice configuration with GenerateOptions
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionList<com.anthropic.models.messages.MessageParam>applyCacheControl(List<com.anthropic.models.messages.MessageParam> formattedMessages) Apply the automatic cache control strategy to formatted messages.voidapplyOptions(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, io.agentscope.core.model.GenerateOptions options, io.agentscope.core.model.GenerateOptions defaultOptions) Apply generation options to Anthropic request parameters.voidapplySystemMessage(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, List<io.agentscope.core.message.Msg> messages) Extract and apply system message if present.voidapplySystemMessage(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, List<io.agentscope.core.message.Msg> messages, boolean cacheControlEnabled) Extract and apply system message if present, optionally marking it for prompt caching.voidapplyTools(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, List<io.agentscope.core.model.ToolSchema> tools) Apply tool schemas to Anthropic request parameters.voidConfigure the TTL applied to prompt-caching markers (e.g.Methods inherited from class io.agentscope.core.formatter.AbstractBaseFormatter
convertToolResultToString, doFormat, doFormat, extractTextContent, extractTextContent, format, format, formatRoleLabel, getOptionOrDefault, hasMediaContent, saveBase64DataToTempFile, shouldBypassHistoryMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.agentscope.core.formatter.Formatter
applyToolChoice, applyToolChoice, applyTools, parseResponse
-
Field Details
-
messageConverter
-
-
Constructor Details
-
AnthropicBaseFormatter
protected AnthropicBaseFormatter()
-
-
Method Details
-
applyOptions
public void applyOptions(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, io.agentscope.core.model.GenerateOptions options, io.agentscope.core.model.GenerateOptions defaultOptions) Apply generation options to Anthropic request parameters.- Parameters:
paramsBuilder- Anthropic request parameters builderoptions- Generation options to applydefaultOptions- Default options to use if options parameter is null
-
applyTools
public void applyTools(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, List<io.agentscope.core.model.ToolSchema> tools) Apply tool schemas to Anthropic request parameters. This method uses the options saved from applyOptions to apply tool choice configuration.- Parameters:
paramsBuilder- Anthropic request parameters buildertools- List of tool schemas to apply (may be null or empty)
-
applySystemMessage
public void applySystemMessage(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, List<io.agentscope.core.message.Msg> messages) Extract and apply system message if present. Anthropic API requires system message to be set via the system parameter, not as a message.This method is called by Model to extract the first system message from the messages list and apply it to the system parameter.
- Parameters:
paramsBuilder- Anthropic request parameters buildermessages- All messages including potential system message
-
applySystemMessage
public void applySystemMessage(com.anthropic.models.messages.MessageCreateParams.Builder paramsBuilder, List<io.agentscope.core.message.Msg> messages, boolean cacheControlEnabled) Extract and apply system message if present, optionally marking it for prompt caching.When
cacheControlEnabledis true, the system prompt is sent as a text block carryingcache_control: {"type": "ephemeral"}so long system prompts can be cached by Anthropic.- Parameters:
paramsBuilder- Anthropic request parameters buildermessages- All messages including potential system messagecacheControlEnabled- whether prompt caching is enabled
-
cacheTtl
Configure the TTL applied to prompt-caching markers (e.g."1h"). -
applyCacheControl
public List<com.anthropic.models.messages.MessageParam> applyCacheControl(List<com.anthropic.models.messages.MessageParam> formattedMessages) Apply the automatic cache control strategy to formatted messages.Adds
cache_control: {"type": "ephemeral"}to the last cacheable content block of the last message. Anthropic caches everything up to and including the marked block, so this caches the entire conversation prefix across turns.- Parameters:
formattedMessages- the formatted Anthropic messages- Returns:
- the messages with cache control applied
-