Class: Anthropic::Resources::Messages

Inherits:
Object
  • Object
show all
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

Instance Attribute Details

#batchesAnthropic::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: , 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



254
255
256
257
258
259
260
261
262
263
# File 'lib/anthropic/resources/messages.rb', line 254

def count_tokens(params)
  parsed, options = Anthropic::MessageCountTokensParams.dump_request(params)
  @client.request(
    method: :post,
    path: "v1/messages/count_tokens",
    body: parsed,
    model: Anthropic::MessageTokensCount,
    options: options
  )
end

#create(max_tokens: , messages: , model: , metadata: 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



56
57
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
# File 'lib/anthropic/resources/messages.rb', line 56

def create(params)
  parsed, options = Anthropic::MessageCreateParams.dump_request(params)
  if parsed[:stream]
    message = "Please use `#stream` for the streaming use case."
    raise ArgumentError.new(message)
  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 options.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]
    )
    options = {timeout: timeout}
  else
    options = {timeout: 600, **options}
  end

  @client.request(
    method: :post,
    path: "v1/messages",
    body: parsed,
    model: Anthropic::Message,
    unwrap: unwrap,
    options: options
  )
end

#stream(max_tokens: , messages: , model: , metadata: 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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/anthropic/resources/messages.rb', line 137

def stream(params)
  parsed, options = Anthropic::Models::MessageCreateParams.dump_request(params)
  unless parsed.fetch(:stream, true)
    message = "Please use `#create` for the non-streaming use case."
    raise ArgumentError.new(message)
  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: options
  )
  Anthropic::Streaming::MessageStream.new(raw_stream:, tools:, models:)
end

#stream_raw(max_tokens: , messages: , model: , metadata: 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



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/anthropic/resources/messages.rb', line 206

def stream_raw(params)
  parsed, options = Anthropic::MessageCreateParams.dump_request(params)
  unless parsed.fetch(:stream, true)
    message = "Please use `#create` for the non-streaming use case."
    raise ArgumentError.new(message)
  end
  parsed.store(:stream, true)
  @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, **options}
  )
end