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.
842 843 844 845 |
# File 'lib/openai/resources/chat/completions.rb', line 842 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
354 355 356 |
# File 'lib/openai/resources/chat/completions.rb', line 354 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.
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/openai/resources/chat/completions.rb', line 328 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.
829 830 831 832 833 834 835 836 837 |
# File 'lib/openai/resources/chat/completions.rb', line 829 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
350 351 352 |
# File 'lib/openai/resources/chat/completions.rb', line 350 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.
802 803 804 805 806 807 808 809 810 811 812 813 814 |
# File 'lib/openai/resources/chat/completions.rb', line 802 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.
730 731 732 733 734 735 736 737 738 |
# File 'lib/openai/resources/chat/completions.rb', line 730 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
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/openai/resources/chat/completions.rb', line 358 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.
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
# File 'lib/openai/resources/chat/completions.rb', line 697 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.
762 763 764 765 766 767 768 769 770 771 772 |
# File 'lib/openai/resources/chat/completions.rb', line 762 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 |