Class: Anthropic::Resources::Completions

Inherits:
Object
  • Object
show all
Defined in:
lib/anthropic/resources/completions.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Completions

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 Completions.

Parameters:



127
128
129
# File 'lib/anthropic/resources/completions.rb', line 127

def initialize(client:)
  @client = client
end

Instance Method Details

#create(max_tokens_to_sample: , model: , prompt: , metadata: nil, stop_sequences: nil, temperature: nil, top_k: nil, top_p: nil, betas: nil, request_options: {}) ⇒ Anthropic::Models::Completion

See #create_streaming for streaming counterpart.

Some parameter documentations has been truncated, see Models::CompletionCreateParams for more details.

[Legacy] Create a Text Completion.

The Text Completions API is a legacy API. We recommend using the Messages API going forward.

Future models and features will not be compatible with Text Completions. See our migration guide for guidance in migrating from Text Completions to Messages.

Parameters:

  • max_tokens_to_sample (Integer)

    Body param: The maximum number of tokens to generate before stopping.

  • model (Symbol, String, Anthropic::Models::Model)

    Body param: The model that will complete your prompt.\n\nSee [models](https://do

  • prompt (String)

    Body param: The prompt that you want Claude to complete.

  • metadata (Anthropic::Models::Metadata)

    Body param: An object describing metadata about the request.

  • stop_sequences (Array<String>)

    Body param: Sequences that will cause the model to stop generating.

  • temperature (Float)

    Body param: Amount of randomness injected into the response.

  • top_k (Integer)

    Body param: Only sample from the top K options for each subsequent token.

  • top_p (Float)

    Body param: Use nucleus sampling.

  • betas (Array<String, Symbol, Anthropic::Models::AnthropicBeta>)

    Header param: Optional header to specify the beta version(s) you want to use.

  • request_options (Anthropic::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

See Also:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/anthropic/resources/completions.rb', line 46

def create(params)
  parsed, options = Anthropic::CompletionCreateParams.dump_request(params)
  if parsed[:stream]
    message = "Please use `#create_streaming` for the streaming use case."
    raise ArgumentError.new(message)
  end
  header_params = {betas: "anthropic-beta"}
  @client.request(
    method: :post,
    path: "v1/complete",
    headers: parsed.slice(*header_params.keys).transform_keys(header_params),
    body: parsed.except(*header_params.keys),
    model: Anthropic::Completion,
    options: {timeout: 600, **options}
  )
end

#create_streaming(max_tokens_to_sample: , model: , prompt: , metadata: nil, stop_sequences: nil, temperature: nil, top_k: nil, top_p: nil, betas: nil, request_options: {}) ⇒ Anthropic::Internal::Stream<Anthropic::Models::Completion>

See #create for non-streaming counterpart.

Some parameter documentations has been truncated, see Models::CompletionCreateParams for more details.

[Legacy] Create a Text Completion.

The Text Completions API is a legacy API. We recommend using the Messages API going forward.

Future models and features will not be compatible with Text Completions. See our migration guide for guidance in migrating from Text Completions to Messages.

Parameters:

  • max_tokens_to_sample (Integer)

    Body param: The maximum number of tokens to generate before stopping.

  • model (Symbol, String, Anthropic::Models::Model)

    Body param: The model that will complete your prompt.\n\nSee [models](https://do

  • prompt (String)

    Body param: The prompt that you want Claude to complete.

  • metadata (Anthropic::Models::Metadata)

    Body param: An object describing metadata about the request.

  • stop_sequences (Array<String>)

    Body param: Sequences that will cause the model to stop generating.

  • temperature (Float)

    Body param: Amount of randomness injected into the response.

  • top_k (Integer)

    Body param: Only sample from the top K options for each subsequent token.

  • top_p (Float)

    Body param: Use nucleus sampling.

  • betas (Array<String, Symbol, Anthropic::Models::AnthropicBeta>)

    Header param: Optional header to specify the beta version(s) you want to use.

  • request_options (Anthropic::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

See Also:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/anthropic/resources/completions.rb', line 102

def create_streaming(params)
  parsed, options = Anthropic::CompletionCreateParams.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)
  header_params = {betas: "anthropic-beta"}
  @client.request(
    method: :post,
    path: "v1/complete",
    headers: {
      "accept" => "text/event-stream",
      **parsed.slice(*header_params.keys)
    }.transform_keys(header_params),
    body: parsed.except(*header_params.keys),
    stream: Anthropic::Internal::Stream,
    model: Anthropic::Completion,
    options: {timeout: 600, **options}
  )
end