Class: OpenAI::Resources::Chat::Completions
- Inherits:
-
Object
- Object
- OpenAI::Resources::Chat::Completions
- Defined in:
- lib/openai/resources/chat/completions.rb,
lib/openai/resources/chat/completions/messages.rb,
sig/openai/helpers/sorbet.rbs,
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
-
#messages ⇒ OpenAI::Resources::Chat::Completions::Messages
readonly
Given a list of messages comprising a conversation, the model will return a response.
Instance Method Summary collapse
- #build_tools_with_models(tools, tool_models) ⇒ Object
-
#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.
-
#delete(completion_id, request_options: {}) ⇒ OpenAI::Models::Chat::ChatCompletionDeleted
Delete a stored chat completion.
- #get_structured_output_models(parsed) ⇒ Object
-
#initialize(client:) ⇒ Completions
constructor
private
A new instance of Completions.
-
#list(after: nil, limit: nil, metadata: nil, model: nil, order: nil, request_options: {}) ⇒ OpenAI::Internal::CursorPage<OpenAI::Models::Chat::ChatCompletion>
List stored Chat Completions.
-
#retrieve(completion_id, request_options: {}) ⇒ OpenAI::Models::Chat::ChatCompletion
Get a stored chat completion.
- #stream(params) ⇒ Object
-
#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.
-
#update(completion_id, metadata:, request_options: {}) ⇒ OpenAI::Models::Chat::ChatCompletion
Modify a stored chat completion.
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.
848 849 850 851 |
# File 'lib/openai/resources/chat/completions.rb', line 848 def initialize(client:) @client = client @messages = OpenAI::Resources::Chat::Completions::Messages.new(client: client) end |
Instance Attribute Details
#messages ⇒ OpenAI::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 end |
Instance Method Details
#build_tools_with_models(tools, tool_models) ⇒ Object
357 358 359 |
# File 'lib/openai/resources/chat/completions.rb', line 357 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.
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.
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/openai/resources/chat/completions.rb', line 331 def create(params) parsed, = OpenAI::Chat::CompletionCreateParams.dump_request(params) if parsed[:stream] = "Please use `#stream_raw` for the streaming use case." raise ArgumentError.new() 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: ) 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.
835 836 837 838 839 840 841 842 843 |
# File 'lib/openai/resources/chat/completions.rb', line 835 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
353 354 355 |
# File 'lib/openai/resources/chat/completions.rb', line 353 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>
List stored Chat Completions. Only Chat Completions that have been stored with
the store parameter set to true will be returned.
808 809 810 811 812 813 814 815 816 817 818 819 820 |
# File 'lib/openai/resources/chat/completions.rb', line 808 def list(params = {}) parsed, = 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: ) 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.
736 737 738 739 740 741 742 743 744 |
# File 'lib/openai/resources/chat/completions.rb', line 736 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
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/openai/resources/chat/completions.rb', line 361 def stream(params) parsed, = 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: ) 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.
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.
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 |
# File 'lib/openai/resources/chat/completions.rb', line 703 def stream_raw(params) parsed, = OpenAI::Chat::CompletionCreateParams.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) @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: ) end |
#update(completion_id, metadata:, request_options: {}) ⇒ OpenAI::Models::Chat::ChatCompletion
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.
768 769 770 771 772 773 774 775 776 777 778 |
# File 'lib/openai/resources/chat/completions.rb', line 768 def update(completion_id, params) parsed, = 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: ) end |