AnthropicChatModel
public class AnthropicChatModel extends ChatModelBase
io/agentscope/extensions/model/anthropic/AnthropicChatModel.java:63–460Anthropic 聊天模型,与模型服务通信并返回生成或向量化结果。位于 io.agentscope.extensions.model.anthropic 包,继承自 ChatModelBase。核心成员包括 doStream、getModelName、builder、AnthropicChatModel。
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
| 类Class | 类型Kind | 说明Description |
|---|---|---|
Builder | 类class | AnthropicChatModel的构建器。 Builder for AnthropicChatModel. |
构造器摘要Constructor Summary
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
protected Flux<ChatResponse> doStream(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options) | 以流式方式执行并返回结果。 Stream chat completion responses from Anthropic's API. |
public String getModelName() | 获取模型名称。 Gets the model name for logging and identification. |
public static Builder builder()static | 返回构建器实例。 Creates a builder for constructing AnthropicChatModel instances. |
构造器详情Constructor Detail
AnthropicChatModel
public AnthropicChatModel(String baseUrl, String apiKey, String modelName, boolean streamEnabled, GenerateOptions defaultOptions, AnthropicBaseFormatter formatter, ProxyConfig proxyConfig, String cacheTtl)
初始化 AnthropicChatModel 实例。
Creates a new instance.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
baseUrl | String | 基类 URL(类型 `String`)。The base url. |
apiKey | String | API 键(类型 `String`)。The api key. |
modelName | String | 模型名称(类型 `String`)。The model name. |
streamEnabled | boolean | 流已启用(类型 `boolean`)。The stream enabled. |
defaultOptions | GenerateOptions | 默认选项(类型 `GenerateOptions`)。The default options. |
formatter | AnthropicBaseFormatter | 格式化器(类型 `AnthropicBaseFormatter`)。The formatter. |
proxyConfig | ProxyConfig | 代理配置(类型 `ProxyConfig`)。The proxy config. |
cacheTtl | String | 缓存存活时间(类型 `String`)。The cache ttl. |
io/agentscope/extensions/model/anthropic/AnthropicChatModel.java:75–94AnthropicChatModel
public AnthropicChatModel(String baseUrl, String apiKey, String authToken, String modelName, boolean streamEnabled, GenerateOptions defaultOptions, AnthropicBaseFormatter formatter, ProxyConfig proxyConfig, String cacheTtl)
初始化 AnthropicChatModel 实例。
Creates an Anthropic chat model with optional bearer token authentication.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
baseUrl | String | 基类 URL(类型 `String`)。the base URL for the Anthropic API (null for default) |
apiKey | String | API 键(类型 `String`)。the API key for authentication (null to omit) |
authToken | String | 认证令牌(类型 `String`)。the bearer token without the `Bearer ` prefix (null to omit) |
modelName | String | 模型名称到使用。the model name to use |
streamEnabled | boolean | 流已启用(类型 `boolean`)。whether streaming should be enabled |
defaultOptions | GenerateOptions | 默认选项(类型 `GenerateOptions`)。default generation options |
formatter | AnthropicBaseFormatter | 格式化器(类型 `AnthropicBaseFormatter`)。the message formatter to use (null for the default formatter) |
proxyConfig | ProxyConfig | 代理配置(类型 `ProxyConfig`)。the proxy configuration (null for no proxy) |
cacheTtl | String | 缓存存活时间(类型 `String`)。the TTL for prompt-caching markers (null for default 5m) |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if both API key and bearer token are configured
io/agentscope/extensions/model/anthropic/AnthropicChatModel.java:112–157方法详情Method Detail
doStream
protected Flux<ChatResponse> doStream(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options)
以流式方式执行并返回结果。
Stream chat completion responses from Anthropic's API.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 消息列表(类型 `List`)。AgentScope messages to send to the model |
tools | List<ToolSchema> | 工具列表(类型 `List`)。Optional list of tool schemas (null or empty if no tools) |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。Optional generation options (null to use defaults) |
Flux<ChatResponse>io/agentscope/extensions/model/anthropic/AnthropicChatModel.java:176–291getModelName
public String getModelName()
获取模型名称。
Gets the model name for logging and identification.
Stringio/agentscope/extensions/model/anthropic/AnthropicChatModel.java:298–301builder
public static Builder builder()
返回构建器实例。
Creates a builder for constructing AnthropicChatModel instances.
Builderio/agentscope/extensions/model/anthropic/AnthropicChatModel.java:308–310嵌套类详情Nested Class Detail
Builder
public static class BuilderAnthropicChatModel的构建器。位于 io.agentscope.extensions.model.anthropic 包。核心成员包括 baseUrl、apiKey、authToken、modelName、stream、defaultOptions。
Builder for AnthropicChatModel.