Class: Anthropic::Resources::Messages
- Inherits:
-
Object
- Object
- Anthropic::Resources::Messages
- Defined in:
- lib/anthropic/resources/messages.rb,
lib/anthropic/resources/messages/batches.rb
Defined Under Namespace
Classes: Batches
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#count_tokens(messages:, model:, output_config: nil, system_: nil, thinking: nil, tool_choice: nil, tools: nil, request_options: {}) ⇒ Anthropic::Models::MessageTokensCount
Some parameter documentations has been truncated, see Models::MessageCountTokensParams for more details.
-
#create(max_tokens:, messages:, model:, 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, request_options: {}) ⇒ Anthropic::Models::Message
(also: #parse)
See #stream for streaming counterpart.
-
#stream(max_tokens:, messages:, model:, 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, request_options: {}) ⇒ Anthropic::Streaming::MessageStream
See #create for non-streaming counterpart.
-
#stream_raw(max_tokens:, messages:, model:, 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, request_options: {}) ⇒ Anthropic::Internal::Stream<Anthropic::Models::RawMessageStartEvent, Anthropic::Models::RawMessageDeltaEvent, Anthropic::Models::RawMessageStopEvent, Anthropic::Models::RawContentBlockStartEvent, Anthropic::Models::RawContentBlockDeltaEvent, Anthropic::Models::RawContentBlockStopEvent>
See #create for non-streaming counterpart.
Instance Attribute Details
#batches ⇒ Anthropic::Resources::Messages::Batches (readonly)
7 8 9 |
# File 'lib/anthropic/resources/messages.rb', line 7 def batches @batches end |
Instance Method Details
#count_tokens(messages:, model:, output_config: nil, system_: nil, thinking: nil, tool_choice: nil, tools: nil, request_options: {}) ⇒ Anthropic::Models::MessageTokensCount
Some parameter documentations has been truncated, see Models::MessageCountTokensParams for more details.
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
264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/anthropic/resources/messages.rb', line 264 def count_tokens(params) parsed, = Anthropic::MessageCountTokensParams.dump_request(params) Anthropic::Helpers::Messages.distill_input_schema_models!(parsed, strict: nil) @client.request( method: :post, path: "v1/messages/count_tokens", body: parsed, model: Anthropic::MessageTokensCount, options: ) end |
#create(max_tokens:, messages:, model:, 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, request_options: {}) ⇒ Anthropic::Models::Message Also known as: parse
See #stream for streaming counterpart.
Some parameter documentations has been truncated, see Models::MessageCreateParams for more details.
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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/anthropic/resources/messages.rb', line 58 def create(params) parsed, = Anthropic::MessageCreateParams.dump_request(params) if parsed[:stream] = "Please use `#stream` for the streaming use case." raise ArgumentError.new() end 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 @client.request( method: :post, path: "v1/messages", body: parsed, model: Anthropic::Message, unwrap: unwrap, options: ) end |
#stream(max_tokens:, messages:, model:, 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, request_options: {}) ⇒ Anthropic::Streaming::MessageStream
See #create for non-streaming counterpart.
Some parameter documentations has been truncated, see Models::MessageCreateParams for more details.
Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation with streaming.
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
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/anthropic/resources/messages.rb', line 141 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) tools, models = Anthropic::Helpers::Messages.distill_input_schema_models!(parsed, strict: nil) raw_stream = @client.request( method: :post, path: "v1/messages", headers: stream_headers("accept" => "text/event-stream"), body: parsed, stream: Anthropic::Internal::Stream, model: Anthropic::Models::RawMessageStreamEvent, options: ) Anthropic::Streaming::MessageStream.new(raw_stream:, tools:, models:) end |
#stream_raw(max_tokens:, messages:, model:, 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, request_options: {}) ⇒ Anthropic::Internal::Stream<Anthropic::Models::RawMessageStartEvent, Anthropic::Models::RawMessageDeltaEvent, Anthropic::Models::RawMessageStopEvent, Anthropic::Models::RawContentBlockStartEvent, Anthropic::Models::RawContentBlockDeltaEvent, Anthropic::Models::RawContentBlockStopEvent>
See #create for non-streaming counterpart.
Some parameter documentations has been truncated, see Models::MessageCreateParams for more details.
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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/anthropic/resources/messages.rb', line 212 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) @client.request( method: :post, path: "v1/messages", headers: stream_headers("accept" => "text/event-stream"), body: parsed, stream: Anthropic::Internal::Stream, model: Anthropic::RawMessageStreamEvent, options: {timeout: 600, **} ) end |