Class: OpenAI::Resources::Chat::Completions

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

Overview

Given a list of messages comprising a conversation, the model will return a response.

Defined Under Namespace

Classes: Messages

Instance Attribute Summary collapse

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:



417
418
419
420
# File 'lib/openai/resources/chat/completions.rb', line 417

def initialize(client:)
  @client = client
  @messages = OpenAI::Resources::Chat::Completions::Messages.new(client: client)
end

Instance Attribute Details

#messagesOpenAI::Resources::Chat::Completions::Messages (readonly)

Given a list of messages comprising a conversation, the model will return a response.



14
15
16
# File 'lib/openai/resources/chat/completions.rb', line 14

def messages
  @messages
end

Instance Method Details

#build_tools_with_models(tools, tool_models) ⇒ Object



147
148
149
# File 'lib/openai/resources/chat/completions.rb', line 147

def build_tools_with_models(tools, tool_models)
  OpenAI::Helpers::StructuredOutput::ChatCompletionParser.build_tools(tools, tool_models)
end

#create(messages:, model:, audio: nil, frequency_penalty: nil, function_call: nil, functions: nil, logit_bias: nil, logprobs: nil, max_completion_tokens: nil, max_tokens: nil, metadata: nil, modalities: nil, moderation: nil, n: nil, parallel_tool_calls: nil, prediction: nil, presence_penalty: nil, prompt_cache_key: nil, prompt_cache_options: nil, prompt_cache_retention: nil, reasoning_effort: nil, response_format: nil, safety_identifier: nil, seed: nil, service_tier: nil, stop: nil, store: nil, stream_options: nil, temperature: nil, tool_choice: nil, tools: nil, top_logprobs: nil, top_p: nil, user: nil, verbosity: nil, web_search_options: nil, request_options: {}) ⇒ OpenAI::Models::Chat::ChatCompletion

See OpenAI::Resources::Chat::Completions#stream_raw for streaming counterpart.

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

Starting a new project? We recommend trying Responses to take advantage of the latest OpenAI platform features. Compare Chat Completions with Responses.


Creates a model response for the given chat conversation. Learn more in the text generation, vision, and audio guides.

Parameter support can differ depending on the model used to generate the response, particularly for newer reasoning models. Parameters that are only supported for reasoning models are noted below. For the current state of unsupported parameters in reasoning models, refer to the reasoning guide.

Returns a chat completion object, or a streamed sequence of chat completion chunk objects if the request is streamed.

Parameters:

Returns:

See Also:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/openai/resources/chat/completions.rb', line 121

def create(params)
  parsed, options = OpenAI::Chat::CompletionCreateParams.dump_request(params)
  if parsed[:stream]
    message = "Please use `#stream_raw` for the streaming use case."
    raise ArgumentError.new(message)
  end

  model, tool_models = get_structured_output_models(parsed)

  unwrap = OpenAI::Helpers::StructuredOutput::ChatCompletionParser.build_unwrap(model, tool_models)

  @client.request(
    method: :post,
    path: "chat/completions",
    body: parsed,
    unwrap: unwrap,
    model: OpenAI::Chat::ChatCompletion,
    security: {bearer_auth: true},
    options: options
  )
end

#delete(completion_id, request_options: {}) ⇒ OpenAI::Models::Chat::ChatCompletionDeleted

Delete a stored chat completion. Only Chat Completions that have been created with the store parameter set to true can be deleted.

Parameters:

  • completion_id (String)

    The ID of the chat completion to delete.

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

Returns:

See Also:



404
405
406
407
408
409
410
411
412
# File 'lib/openai/resources/chat/completions.rb', line 404

def delete(completion_id, params = {})
  @client.request(
    method: :delete,
    path: ["chat/completions/%1$s", completion_id],
    model: OpenAI::Chat::ChatCompletionDeleted,
    security: {bearer_auth: true},
    options: params[:request_options]
  )
end

#get_structured_output_models(parsed) ⇒ Object



143
144
145
# File 'lib/openai/resources/chat/completions.rb', line 143

def get_structured_output_models(parsed)
  OpenAI::Helpers::StructuredOutput::ChatCompletionParser.get_models(parsed)
end

#list(after: nil, limit: nil, metadata: nil, model: nil, order: nil, request_options: {}) ⇒ OpenAI::Internal::CursorPage<OpenAI::Models::Chat::ChatCompletion>

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

List stored Chat Completions. Only Chat Completions that have been stored with the store parameter set to true will be returned.

Parameters:

  • after (String)

    Identifier for the last chat completion from the previous pagination request.

  • limit (Integer)

    Number of Chat Completions to retrieve.

  • metadata (Hash{Symbol=>String}, nil)

    A list of metadata keys to filter the Chat Completions by. Example:

  • model (String)

    The model used to generate the Chat Completions.

  • order (Symbol, OpenAI::Models::Chat::CompletionListParams::Order)

    Sort order for Chat Completions by timestamp. Use asc for ascending order or `

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

Returns:

See Also:



378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/openai/resources/chat/completions.rb', line 378

def list(params = {})
  parsed, options = OpenAI::Chat::CompletionListParams.dump_request(params)
  query = OpenAI::Internal::Util.encode_query_params(parsed)
  @client.request(
    method: :get,
    path: "chat/completions",
    query: query,
    page: OpenAI::Internal::CursorPage,
    model: OpenAI::Chat::ChatCompletion,
    security: {bearer_auth: true},
    options: options
  )
end

#retrieve(completion_id, request_options: {}) ⇒ OpenAI::Models::Chat::ChatCompletion

Get a stored chat completion. Only Chat Completions that have been created with the store parameter set to true will be returned.

Parameters:

  • completion_id (String)

    The ID of the chat completion to retrieve.

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

Returns:

See Also:



315
316
317
318
319
320
321
322
323
# File 'lib/openai/resources/chat/completions.rb', line 315

def retrieve(completion_id, params = {})
  @client.request(
    method: :get,
    path: ["chat/completions/%1$s", completion_id],
    model: OpenAI::Chat::ChatCompletion,
    security: {bearer_auth: true},
    options: params[:request_options]
  )
end

#stream(params) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/openai/resources/chat/completions.rb', line 151

def stream(params)
  parsed, options = OpenAI::Chat::CompletionCreateParams.dump_request(params)

  parsed.store(:stream, true)

  response_format, tool_models = get_structured_output_models(parsed)

  input_tools = build_tools_with_models(parsed[:tools], tool_models)

  raw_stream = @client.request(
    method: :post,
    path: "chat/completions",
    headers: {"accept" => "text/event-stream"},
    body: parsed,
    stream: OpenAI::Internal::Stream,
    model: OpenAI::Chat::ChatCompletionChunk,
    security: {bearer_auth: true},
    options: options
  )

  OpenAI::Helpers::Streaming::ChatCompletionStream.new(
    raw_stream: raw_stream,
    response_format: response_format,
    input_tools: input_tools
  )
end

#stream_raw(messages:, model:, audio: nil, frequency_penalty: nil, function_call: nil, functions: nil, logit_bias: nil, logprobs: nil, max_completion_tokens: nil, max_tokens: nil, metadata: nil, modalities: nil, moderation: nil, n: nil, parallel_tool_calls: nil, prediction: nil, presence_penalty: nil, prompt_cache_key: nil, prompt_cache_options: nil, prompt_cache_retention: nil, reasoning_effort: nil, response_format: nil, safety_identifier: nil, seed: nil, service_tier: nil, stop: nil, store: nil, stream_options: nil, temperature: nil, tool_choice: nil, tools: nil, top_logprobs: nil, top_p: nil, user: nil, verbosity: nil, web_search_options: nil, request_options: {}) ⇒ OpenAI::Internal::Stream<OpenAI::Models::Chat::ChatCompletionChunk>

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

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

Starting a new project? We recommend trying Responses to take advantage of the latest OpenAI platform features. Compare Chat Completions with Responses.


Creates a model response for the given chat conversation. Learn more in the text generation, vision, and audio guides.

Parameter support can differ depending on the model used to generate the response, particularly for newer reasoning models. Parameters that are only supported for reasoning models are noted below. For the current state of unsupported parameters in reasoning models, refer to the reasoning guide.

Returns a chat completion object, or a streamed sequence of chat completion chunk objects if the request is streamed.

Parameters:

Returns:

See Also:



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/openai/resources/chat/completions.rb', line 283

def stream_raw(params)
  parsed, options = OpenAI::Chat::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)
  @client.request(
    method: :post,
    path: "chat/completions",
    headers: {"accept" => "text/event-stream", "accept-encoding" => "identity"},
    body: parsed,
    stream: OpenAI::Internal::Stream,
    model: OpenAI::Chat::ChatCompletionChunk,
    security: {bearer_auth: true},
    options: options
  )
end

#update(completion_id, metadata:, request_options: {}) ⇒ OpenAI::Models::Chat::ChatCompletion

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

Modify a stored chat completion. Only Chat Completions that have been created with the store parameter set to true can be modified. Currently, the only supported modification is to update the metadata field.

Parameters:

  • completion_id (String)

    The ID of the chat completion to update.

  • metadata (Hash{Symbol=>String}, nil)

    Set of 16 key-value pairs that can be attached to an object. This can be

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

Returns:

See Also:



343
344
345
346
347
348
349
350
351
352
353
# File 'lib/openai/resources/chat/completions.rb', line 343

def update(completion_id, params)
  parsed, options = OpenAI::Chat::CompletionUpdateParams.dump_request(params)
  @client.request(
    method: :post,
    path: ["chat/completions/%1$s", completion_id],
    body: parsed,
    model: OpenAI::Chat::ChatCompletion,
    security: {bearer_auth: true},
    options: options
  )
end