Class AnthropicChatModel
java.lang.Object
io.agentscope.core.model.ChatModelBase
io.agentscope.extensions.model.anthropic.AnthropicChatModel
- All Implemented Interfaces:
io.agentscope.core.model.Model
public class AnthropicChatModel
extends io.agentscope.core.model.ChatModelBase
Anthropic Chat Model implementation using the official Anthropic Java SDK.
This implementation provides complete integration with Anthropic's Messages API, including tool calling, streaming support, and extended thinking features.
Important notes:
- System messages are handled via the system parameter, not as messages
- Tool results must be in separate user messages
- Supports Claude models (claude-3-*, claude-sonnet-*, etc.)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for AnthropicChatModel. -
Constructor Summary
ConstructorsConstructorDescriptionAnthropicChatModel(String baseUrl, String apiKey, String modelName, boolean streamEnabled, io.agentscope.core.model.GenerateOptions defaultOptions, AnthropicBaseFormatter formatter, io.agentscope.core.model.transport.ProxyConfig proxyConfig, String cacheTtl) AnthropicChatModel(String baseUrl, String apiKey, String authToken, String modelName, boolean streamEnabled, io.agentscope.core.model.GenerateOptions defaultOptions, AnthropicBaseFormatter formatter, io.agentscope.core.model.transport.ProxyConfig proxyConfig, String cacheTtl) Creates an Anthropic chat model with optional bearer token authentication. -
Method Summary
Modifier and TypeMethodDescriptionstatic AnthropicChatModel.Builderbuilder()Creates a builder for constructing AnthropicChatModel instances.protected reactor.core.publisher.Flux<io.agentscope.core.model.ChatResponse>doStream(List<io.agentscope.core.message.Msg> messages, List<io.agentscope.core.model.ToolSchema> tools, io.agentscope.core.model.GenerateOptions options) Stream chat completion responses from Anthropic's API.Gets the model name for logging and identification.Methods inherited from class io.agentscope.core.model.ChatModelBase
getContextWindowSize, setContextWindowSize, setNativeStructuredOutput, setNativeStructuredOutputWithTools, stream, supportsNativeStructuredOutput, supportsNativeStructuredOutputWithTools
-
Constructor Details
-
AnthropicChatModel
public AnthropicChatModel(String baseUrl, String apiKey, String modelName, boolean streamEnabled, io.agentscope.core.model.GenerateOptions defaultOptions, AnthropicBaseFormatter formatter, io.agentscope.core.model.transport.ProxyConfig proxyConfig, String cacheTtl) -
AnthropicChatModel
public AnthropicChatModel(String baseUrl, String apiKey, String authToken, String modelName, boolean streamEnabled, io.agentscope.core.model.GenerateOptions defaultOptions, AnthropicBaseFormatter formatter, io.agentscope.core.model.transport.ProxyConfig proxyConfig, String cacheTtl) Creates an Anthropic chat model with optional bearer token authentication.apiKeyandauthTokenare mutually exclusive.- Parameters:
baseUrl- the base URL for the Anthropic API (null for default)apiKey- the API key for authentication (null to omit)authToken- the bearer token without theBearerprefix (null to omit)modelName- the model name to usestreamEnabled- whether streaming should be enableddefaultOptions- default generation optionsformatter- the message formatter to use (null for the default formatter)proxyConfig- the proxy configuration (null for no proxy)cacheTtl- the TTL for prompt-caching markers (null for default 5m)- Throws:
IllegalArgumentException- if both API key and bearer token are configured
-
-
Method Details
-
doStream
protected reactor.core.publisher.Flux<io.agentscope.core.model.ChatResponse> doStream(List<io.agentscope.core.message.Msg> messages, List<io.agentscope.core.model.ToolSchema> tools, io.agentscope.core.model.GenerateOptions options) Stream chat completion responses from Anthropic's API.This method internally handles message formatting using the configured formatter. It supports both streaming and non-streaming modes based on the streamEnabled setting.
Supports timeout and retry configuration through GenerateOptions.
- Specified by:
doStreamin classio.agentscope.core.model.ChatModelBase- Parameters:
messages- AgentScope messages to send to the modeltools- Optional list of tool schemas (null or empty if no tools)options- Optional generation options (null to use defaults)- Returns:
- Flux stream of chat responses
-
getModelName
Gets the model name for logging and identification.- Returns:
- the model name
-
builder
Creates a builder for constructing AnthropicChatModel instances.- Returns:
- a new builder
-