Class: Anthropic::Resources::Completions

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

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:



134
135
136
# File 'lib/anthropic/resources/completions.rb', line 134

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, workspace_id: nil, request_options: {}) ⇒ Anthropic::Models::Completion

See Anthropic::Resources::Completions#create_streaming for streaming counterpart.

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

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

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.

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

  • workspace_id (String)

    Header param: Optional header to select the Workspace for this request. The valu

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

Returns:

See Also:



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

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", workspace_id: "anthropic-workspace-id"}
  @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, workspace_id: nil, request_options: {}) ⇒ Anthropic::Internal::Stream<Anthropic::Models::Completion>

See Anthropic::Resources::Completions#create for non-streaming counterpart.

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

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

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.

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

  • workspace_id (String)

    Header param: Optional header to select the Workspace for this request. The valu

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

Returns:

See Also:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/anthropic/resources/completions.rb', line 106

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", workspace_id: "anthropic-workspace-id"}
  @client.request(
    method: :post,
    path: "v1/complete",
    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::Completion,
    options: {timeout: 600, **options}
  )
end