Class: Anthropic::Resources::Messages
- Inherits:
-
Object
- Object
- Anthropic::Resources::Messages
- Defined in:
- lib/anthropic/resources/messages.rb,
lib/anthropic/resources/messages/batches.rb,
sig/anthropic/resources/messages.rbs,
sig/anthropic/resources/messages/batches.rbs
Defined Under Namespace
Classes: Batches
Constant Summary collapse
- MODELS_TO_WARN_WITH_THINKING_ENABLED =
["claude-opus-4-6", "claude-mythos-preview"].freeze
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#count_tokens(messages:, model:, cache_control: nil, output_config: nil, system_: nil, thinking: nil, tool_choice: nil, tools: nil, user_profile_id: nil, workspace_id: nil, request_options: {}) ⇒ Anthropic::Models::MessageTokensCount
Count the number of tokens in a Message.
-
#create(max_tokens:, messages:, model:, cache_control: nil, container: nil, inference_geo: nil, metadata: nil, output_config: nil, service_tier: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, user_profile_id: nil, workspace_id: nil, request_options: {}) ⇒ Anthropic::Models::Message
(also: #parse)
See Anthropic::Resources::Messages#stream for streaming counterpart.
-
#initialize(client:) ⇒ Messages
constructor
private
A new instance of Messages.
-
#stream(max_tokens:, messages:, model:, cache_control: nil, container: nil, inference_geo: nil, metadata: nil, output_config: nil, service_tier: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, user_profile_id: nil, workspace_id: nil, request_options: {}) ⇒ Anthropic::Streaming::MessageStream
See Anthropic::Resources::Messages#create for non-streaming counterpart.
-
#stream_raw(max_tokens:, messages:, model:, cache_control: nil, container: nil, inference_geo: nil, metadata: nil, output_config: nil, service_tier: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, user_profile_id: nil, workspace_id: nil, request_options: {}) ⇒ Anthropic::Internal::Stream<Anthropic::Models::RawMessageStartEvent, Anthropic::Models::RawMessageDeltaEvent, Anthropic::Models::RawMessageStopEvent, Anthropic::Models::RawContentBlockStartEvent, Anthropic::Models::RawContentBlockDeltaEvent, Anthropic::Models::RawContentBlockStopEvent>
See Anthropic::Resources::Messages#create for non-streaming counterpart.
Constructor Details
#initialize(client:) ⇒ Messages
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Messages.
354 355 356 357 |
# File 'lib/anthropic/resources/messages.rb', line 354 def initialize(client:) @client = client @batches = Anthropic::Resources::Messages::Batches.new(client: client) end |
Instance Attribute Details
#batches ⇒ Anthropic::Resources::Messages::Batches (readonly)
9 10 11 |
# File 'lib/anthropic/resources/messages.rb', line 9 def batches @batches end |
Instance Method Details
#count_tokens(messages:, model:, cache_control: nil, output_config: nil, system_: nil, thinking: nil, tool_choice: nil, tools: nil, user_profile_id: nil, workspace_id: nil, request_options: {}) ⇒ Anthropic::Models::MessageTokensCount
Count the number of tokens in a Message.
The Token Count API can be used to count the number of tokens in a Message, including tools, images, and documents, without creating it.
Learn more about token counting in our user guide
Some parameter documentations has been truncated, see Models::MessageCountTokensParams for more details.
319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/anthropic/resources/messages.rb', line 319 def count_tokens(params) parsed, = Anthropic::MessageCountTokensParams.dump_request(params) Anthropic::Helpers::Messages.distill_input_schema_models!(parsed, strict: nil) header_params = {user_profile_id: "anthropic-user-profile-id", workspace_id: "anthropic-workspace-id"} @client.request( method: :post, path: "v1/messages/count_tokens", headers: parsed.slice(*header_params.keys).transform_keys(header_params), body: parsed.except(*header_params.keys), model: Anthropic::MessageTokensCount, options: ) end |
#create(max_tokens:, messages:, model:, cache_control: nil, container: nil, inference_geo: nil, metadata: nil, output_config: nil, service_tier: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, user_profile_id: nil, workspace_id: nil, request_options: {}) ⇒ Anthropic::Models::Message Also known as: parse
See Anthropic::Resources::Messages#stream for streaming counterpart.
Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.
The Messages API can be used for either single queries or stateless multi-turn conversations.
Learn more about the Messages API in our user guide
Some parameter documentations has been truncated, see Models::MessageCreateParams for more details.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/anthropic/resources/messages.rb', line 70 def create(params) parsed, = Anthropic::MessageCreateParams.dump_request(params) if parsed[:stream] = "Please use `#stream` for the streaming use case." raise ArgumentError.new() end warn_thinking_enabled(parsed) tools, models = Anthropic::Helpers::Messages.distill_input_schema_models!(parsed, strict: nil) unwrap = ->(raw) { Anthropic::Helpers::Messages.parse_input_schemas!(raw, tools:, models:) } if .empty? && @client.timeout == Anthropic::Client::DEFAULT_TIMEOUT_IN_SECONDS model = parsed[:model]&.to_sym max_tokens = parsed[:max_tokens].to_i timeout = @client.calculate_nonstreaming_timeout( max_tokens, Anthropic::Client::MODEL_NONSTREAMING_TOKENS[model] ) = {timeout: timeout} else = {timeout: 600, **} end header_params = {user_profile_id: "anthropic-user-profile-id", workspace_id: "anthropic-workspace-id"} @client.request( method: :post, path: "v1/messages", headers: parsed.slice(*header_params.keys).transform_keys(header_params), body: parsed.except(*header_params.keys), model: Anthropic::Message, unwrap: unwrap, options: ) end |
#stream(max_tokens:, messages:, model:, cache_control: nil, container: nil, inference_geo: nil, metadata: nil, output_config: nil, service_tier: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, user_profile_id: nil, workspace_id: nil, request_options: {}) ⇒ Anthropic::Streaming::MessageStream
See Anthropic::Resources::Messages#create for non-streaming counterpart.
Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.
The Messages API can be used for either single queries or stateless multi-turn conversations.
Learn more about the Messages API in our user guide
Some parameter documentations has been truncated, see Models::MessageCreateParams for more details.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/anthropic/resources/messages.rb', line 168 def stream(params) parsed, = Anthropic::Models::MessageCreateParams.dump_request(params) unless parsed.fetch(:stream, true) = "Please use `#create` for the non-streaming use case." raise ArgumentError.new() end parsed.store(:stream, true) warn_thinking_enabled(parsed) tools, models = Anthropic::Helpers::Messages.distill_input_schema_models!(parsed, strict: nil) header_params = {user_profile_id: "anthropic-user-profile-id", workspace_id: "anthropic-workspace-id"} raw_stream = @client.request( method: :post, path: "v1/messages", headers: stream_headers( "accept" => "text/event-stream", "accept-encoding" => "identity", **parsed.slice(*header_params.keys) ).transform_keys(header_params), body: parsed.except(*header_params.keys), stream: Anthropic::Internal::Stream, model: Anthropic::Models::RawMessageStreamEvent, options: ) Anthropic::Streaming::MessageStream.new(raw_stream:, tools:, models:) end |
#stream_raw(max_tokens:, messages:, model:, cache_control: nil, container: nil, inference_geo: nil, metadata: nil, output_config: nil, service_tier: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, user_profile_id: nil, workspace_id: nil, request_options: {}) ⇒ Anthropic::Internal::Stream<Anthropic::Models::RawMessageStartEvent, Anthropic::Models::RawMessageDeltaEvent, Anthropic::Models::RawMessageStopEvent, Anthropic::Models::RawContentBlockStartEvent, Anthropic::Models::RawContentBlockDeltaEvent, Anthropic::Models::RawContentBlockStopEvent>
See Anthropic::Resources::Messages#create for non-streaming counterpart.
Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.
The Messages API can be used for either single queries or stateless multi-turn conversations.
Learn more about the Messages API in our user guide
Some parameter documentations has been truncated, see Models::MessageCreateParams for more details.
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/anthropic/resources/messages.rb', line 256 def stream_raw(params) parsed, = Anthropic::MessageCreateParams.dump_request(params) unless parsed.fetch(:stream, true) = "Please use `#create` for the non-streaming use case." raise ArgumentError.new() end parsed.store(:stream, true) Anthropic::Helpers::Messages.distill_input_schema_models!(parsed, strict: nil) header_params = {user_profile_id: "anthropic-user-profile-id", workspace_id: "anthropic-workspace-id"} @client.request( method: :post, path: "v1/messages", headers: stream_headers( "accept" => "text/event-stream", "accept-encoding" => "identity", **parsed.slice(*header_params.keys) ).transform_keys(header_params), body: parsed.except(*header_params.keys), stream: Anthropic::Internal::Stream, model: Anthropic::RawMessageStreamEvent, options: {timeout: 600, **} ) end |