View Source ExOpenAI.Chat (ex_openai.ex v1.8.0)

Modules for interacting with the chat group of OpenAI APIs

API Reference: https://platform.openai.com/docs/api-reference/chat

Summary

Functions

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

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

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

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

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

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.

Functions

Link to this function

create_chat_completion(messages, model, opts \\ [])

View Source
@spec create_chat_completion(
  [ExOpenAI.Components.ChatCompletionRequestMessage.t()],
  (:"gpt-3.5-turbo-16k-0613"
   | :"gpt-3.5-turbo-0125"
   | :"gpt-3.5-turbo-1106"
   | :"gpt-3.5-turbo-0613"
   | :"gpt-3.5-turbo-0301"
   | :"gpt-3.5-turbo-16k"
   | :"gpt-3.5-turbo"
   | :"gpt-4-32k-0613"
   | :"gpt-4-32k-0314"
   | :"gpt-4-32k"
   | :"gpt-4-0613"
   | :"gpt-4-0314"
   | :"gpt-4"
   | :"gpt-4-vision-preview"
   | :"gpt-4-1106-preview"
   | :"gpt-4-turbo-preview"
   | :"gpt-4-0125-preview"
   | :"gpt-4-turbo-2024-04-09"
   | :"gpt-4-turbo"
   | :"gpt-4o-mini-2024-07-18"
   | :"gpt-4o-mini"
   | :"chatgpt-4o-latest"
   | :"gpt-4o-mini-audio-preview-2024-12-17"
   | :"gpt-4o-mini-audio-preview"
   | :"gpt-4o-audio-preview-2024-12-17"
   | :"gpt-4o-audio-preview-2024-10-01"
   | :"gpt-4o-audio-preview"
   | :"gpt-4o-2024-05-13"
   | :"gpt-4o-2024-08-06"
   | :"gpt-4o-2024-11-20"
   | :"gpt-4o"
   | :"gpt-4.5-preview-2025-02-27"
   | :"gpt-4.5-preview"
   | :"computer-use-preview-2025-03-11"
   | :"computer-use-preview-2025-02-04"
   | :"computer-use-preview"
   | :"o1-mini-2024-09-12"
   | :"o1-mini"
   | :"o1-preview-2024-09-12"
   | :"o1-preview"
   | :"o1-2024-12-17"
   | :o1
   | :"o3-mini-2025-01-31"
   | :"o3-mini")
  | String.t(),
  base_url: String.t(),
  openai_organization_key: String.t(),
  openai_api_key: String.t(),
  user: String.t(),
  top_p: float(),
  temperature: float(),
  metadata: ExOpenAI.Components.Metadata.t(),
  web_search_options: %{
    search_context_size: ExOpenAI.Components.WebSearchContextSize.t(),
    user_location: %{
      approximate: ExOpenAI.Components.WebSearchLocation.t(),
      type: :approximate
    }
  },
  top_logprobs: integer(),
  tools: [ExOpenAI.Components.ChatCompletionTool.t()],
  tool_choice: ExOpenAI.Components.ChatCompletionToolChoiceOption.t(),
  stream_options: ExOpenAI.Components.ChatCompletionStreamOptions.t(),
  stream: boolean(),
  store: boolean(),
  stop: ExOpenAI.Components.StopConfiguration.t(),
  service_tier: :default | :auto,
  seed: integer(),
  response_format:
    ExOpenAI.Components.ResponseFormatJsonObject.t()
    | ExOpenAI.Components.ResponseFormatJsonSchema.t()
    | ExOpenAI.Components.ResponseFormatText.t(),
  reasoning_effort: ExOpenAI.Components.ReasoningEffort.t(),
  presence_penalty: float(),
  prediction: ExOpenAI.Components.PredictionContent.t(),
  parallel_tool_calls: ExOpenAI.Components.ParallelToolCalls.t(),
  n: integer(),
  modalities: ExOpenAI.Components.ResponseModalities.t(),
  max_tokens: integer(),
  max_completion_tokens: integer(),
  logprobs: boolean(),
  logit_bias: map(),
  functions: [ExOpenAI.Components.ChatCompletionFunctions.t()],
  function_call:
    ExOpenAI.Components.ChatCompletionFunctionCallOption.t() | :auto | :none,
  frequency_penalty: float(),
  audio: %{
    format: :pcm16 | :opus | :flac | :mp3 | :wav,
    voice: :verse | :shimmer | :sage | :echo | :coral | :ballad | :ash | :alloy
  },
  stream_to: (... -> any()) | pid()
) ::
  ({:ok, ExOpenAI.Components.CreateChatCompletionStreamResponse.t()}
   | {:ok, ExOpenAI.Components.CreateChatCompletionResponse.t()})
  | {:error, any()}

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.

Endpoint: https://api.openai.com/v1/chat/completions

Method: POST

Docs: https://platform.openai.com/docs/api-reference/chat


Required Arguments:

  • messages: A list of messages comprising the conversation so far. Depending on the model you use, different message types (modalities) are supported, like text, images, and audio.

  • model: Model ID used to generate the response, like gpt-4o or o1. OpenAI offers a wide range of models with different capabilities, performance characteristics, and price points. Refer to the model guide to browse and compare available models.

Optional Arguments:

  • stream_to: "PID or function of where to stream content to"

  • audio: "Parameters for audio output. Required when audio output is requested with\nmodalities: [\"audio\"]. Learn more.\n"

  • frequency_penalty: "Number between -2.0 and 2.0. Positive values penalize new tokens based on\ntheir existing frequency in the text so far, decreasing the model's\nlikelihood to repeat the same line verbatim.\n"

  • function_call: "Deprecated in favor of tool_choice.\n\nControls which (if any) function is called by the model.\n\nnone means the model will not call a function and instead generates a\nmessage.\n\nauto means the model can pick between generating a message or calling a\nfunction.\n\nSpecifying a particular function via {\"name\": \"my_function\"} forces the\nmodel to call that function.\n\nnone is the default when no functions are present. auto is the default\nif functions are present.\n"

  • functions: "Deprecated in favor of tools.\n\nA list of functions the model may generate JSON inputs for.\n"

  • logit_bias: "Modify the likelihood of specified tokens appearing in the completion.\n\nAccepts a JSON object that maps tokens (specified by their token ID in the\ntokenizer) to an associated bias value from -100 to 100. Mathematically,\nthe bias is added to the logits generated by the model prior to sampling.\nThe exact effect will vary per model, but values between -1 and 1 should\ndecrease or increase likelihood of selection; values like -100 or 100\nshould result in a ban or exclusive selection of the relevant token.\n"

  • logprobs: "Whether to return log probabilities of the output tokens or not. If true,\nreturns the log probabilities of each output token returned in the\ncontent of message.\n"

  • max_completion_tokens: "An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and reasoning tokens.\n"

  • max_tokens: "The maximum number of tokens that can be generated in the\nchat completion. This value can be used to control\ncosts for text generated via API.\n\nThis value is now deprecated in favor of max_completion_tokens, and is\nnot compatible with o1 series models.\n"

  • modalities: ""

  • n: "How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs."

Example: 1

  • parallel_tool_calls: ""

  • prediction: "Configuration for a Predicted Output,\nwhich can greatly improve response times when large parts of the model\nresponse are known ahead of time. This is most common when you are\nregenerating a file with only minor changes to most of the content.\n"

  • presence_penalty: "Number between -2.0 and 2.0. Positive values penalize new tokens based on\nwhether they appear in the text so far, increasing the model's likelihood\nto talk about new topics.\n"

  • reasoning_effort: ""

  • response_format: "An object specifying the format that the model must output.\n\nSetting to { \"type\": \"json_schema\", \"json_schema\": {...} } enables\nStructured Outputs which ensures the model will match your supplied JSON\nschema. Learn more in the Structured Outputs\nguide.\n\nSetting to { \"type\": \"json_object\" } enables the older JSON mode, which\nensures the message the model generates is valid JSON. Using json_schema\nis preferred for models that support it.\n"

  • seed: "This feature is in Beta.\nIf specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.\nDeterminism is not guaranteed, and you should refer to the system_fingerprint response parameter to monitor changes in the backend.\n"

  • service_tier: "Specifies the latency tier to use for processing the request. This parameter is relevant for customers subscribed to the scale tier service:\n - If set to 'auto', and the Project is Scale tier enabled, the system\n will utilize scale tier credits until they are exhausted.\n - If set to 'auto', and the Project is not Scale tier enabled, the request will be processed using the default service tier with a lower uptime SLA and no latency guarentee.\n - If set to 'default', the request will be processed using the default service tier with a lower uptime SLA and no latency guarentee.\n - When not set, the default behavior is 'auto'.\n\n When this parameter is set, the response body will include the service_tier utilized.\n"

  • stop: ""

  • store: "Whether or not to store the output of this chat completion request for \nuse in our model distillation or\nevals products.\n"

  • stream: "If set to true, the model response data will be streamed to the client\nas it is generated using server-sent events.\nSee the Streaming section below\nfor more information, along with the streaming responses\nguide for more information on how to handle the streaming events.\n"

  • stream_options: ""

  • tool_choice: ""

  • tools: "A list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for. A max of 128 functions are supported.\n"

  • top_logprobs: "An integer between 0 and 20 specifying the number of most likely tokens to\nreturn at each token position, each with an associated log probability.\nlogprobs must be set to true if this parameter is used.\n"

  • web_search_options: "This tool searches the web for relevant results to use in a response.\nLearn more about the web search tool.\n"

  • metadata: ""

  • temperature: "What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.\nWe generally recommend altering this or top_p but not both.\n"

Example: 1

  • top_p: "An alternative to sampling with temperature, called nucleus sampling,\nwhere the model considers the results of the tokens with top_p probability\nmass. So 0.1 means only the tokens comprising the top 10% probability mass\nare considered.\n\nWe generally recommend altering this or temperature but not both.\n"

Example: 1

  • user: "A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.\n"

Example: "user-1234"

  • openai_api_key: "OpenAI API key to pass directly. If this is specified, it will override the api_key config value."

  • openai_organization_key: "OpenAI API key to pass directly. If this is specified, it will override the organization_key config value."

  • base_url: "Which API endpoint to use as base, defaults to https://api.openai.com/v1"

Link to this function

delete_chat_completion(completion_id, opts \\ [])

View Source
@spec delete_chat_completion(String.t(),
  base_url: String.t(),
  openai_organization_key: String.t(),
  openai_api_key: String.t()
) :: {:ok, ExOpenAI.Components.ChatCompletionDeleted.t()} | {:error, any()}

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

Endpoint: https://api.openai.com/v1/chat/completions/{completion_id}

Method: DELETE

Docs: https://platform.openai.com/docs/api-reference/chat


Required Arguments:

  • completion_id

Optional Arguments:

  • openai_api_key: "OpenAI API key to pass directly. If this is specified, it will override the api_key config value."

  • openai_organization_key: "OpenAI API key to pass directly. If this is specified, it will override the organization_key config value."

  • base_url: "Which API endpoint to use as base, defaults to https://api.openai.com/v1"

Link to this function

get_chat_completion(completion_id, opts \\ [])

View Source
@spec get_chat_completion(String.t(),
  base_url: String.t(),
  openai_organization_key: String.t(),
  openai_api_key: String.t()
) ::
  {:ok, ExOpenAI.Components.CreateChatCompletionResponse.t()} | {:error, any()}

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

Endpoint: https://api.openai.com/v1/chat/completions/{completion_id}

Method: GET

Docs: https://platform.openai.com/docs/api-reference/chat


Required Arguments:

  • completion_id

Optional Arguments:

  • openai_api_key: "OpenAI API key to pass directly. If this is specified, it will override the api_key config value."

  • openai_organization_key: "OpenAI API key to pass directly. If this is specified, it will override the organization_key config value."

  • base_url: "Which API endpoint to use as base, defaults to https://api.openai.com/v1"

Link to this function

get_chat_completion_messages(completion_id, opts \\ [])

View Source
@spec get_chat_completion_messages(String.t(),
  base_url: String.t(),
  openai_organization_key: String.t(),
  openai_api_key: String.t(),
  order: String.t(),
  limit: integer(),
  after: String.t(),
  stream_to: (... -> any()) | pid()
) :: {:ok, ExOpenAI.Components.ChatCompletionMessageList.t()} | {:error, any()}

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

Endpoint: https://api.openai.com/v1/chat/completions/{completion_id}/messages

Method: GET

Docs: https://platform.openai.com/docs/api-reference/chat


Required Arguments:

  • completion_id

Optional Arguments:

  • stream_to: "PID or function of where to stream content to"

  • after

  • limit

  • order

  • openai_api_key: "OpenAI API key to pass directly. If this is specified, it will override the api_key config value."

  • openai_organization_key: "OpenAI API key to pass directly. If this is specified, it will override the organization_key config value."

  • base_url: "Which API endpoint to use as base, defaults to https://api.openai.com/v1"

Link to this function

list_chat_completions(opts \\ [])

View Source
@spec list_chat_completions(
  base_url: String.t(),
  openai_organization_key: String.t(),
  openai_api_key: String.t(),
  order: String.t(),
  limit: integer(),
  after: String.t(),
  metadata: ExOpenAI.Components.Metadata.t(),
  model: String.t(),
  stream_to: (... -> any()) | pid()
) :: {:ok, ExOpenAI.Components.ChatCompletionList.t()} | {:error, any()}

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

Endpoint: https://api.openai.com/v1/chat/completions

Method: GET

Docs: https://platform.openai.com/docs/api-reference/chat


Required Arguments:

Optional Arguments:

  • stream_to: "PID or function of where to stream content to"

  • model

  • metadata

  • after

  • limit

  • order

  • openai_api_key: "OpenAI API key to pass directly. If this is specified, it will override the api_key config value."

  • openai_organization_key: "OpenAI API key to pass directly. If this is specified, it will override the organization_key config value."

  • base_url: "Which API endpoint to use as base, defaults to https://api.openai.com/v1"

Link to this function

update_chat_completion(completion_id, opts \\ [])

View Source
@spec update_chat_completion(String.t(),
  base_url: String.t(),
  openai_organization_key: String.t(),
  openai_api_key: String.t()
) ::
  {:ok, ExOpenAI.Components.CreateChatCompletionResponse.t()} | {:error, any()}

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.

Endpoint: https://api.openai.com/v1/chat/completions/{completion_id}

Method: POST

Docs: https://platform.openai.com/docs/api-reference/chat


Required Arguments:

  • completion_id

Optional Arguments:

  • openai_api_key: "OpenAI API key to pass directly. If this is specified, it will override the api_key config value."

  • openai_organization_key: "OpenAI API key to pass directly. If this is specified, it will override the organization_key config value."

  • base_url: "Which API endpoint to use as base, defaults to https://api.openai.com/v1"