"OpenAI" (Service Connection)

Use the OpenAI API with the Wolfram Language.

Connecting & Authenticating

ServiceConnect["OpenAI"] creates a connection to the OpenAI API. If a previously saved connection can be found, it will be used; otherwise, a new authentication request will be launched.
Use of this connection requires internet access and an OpenAI account.

Requests

ServiceExecute["OpenAI","request",params] sends a request to the OpenAI API, using parameters params. The following gives possible requests.

Text

Request:

"Completion" create text completion for a given prompt

Parameters:
  • "Prompt"Automaticthe prompt for which to generate completions
    "N"Automaticnumber of completions to return
    "Temperature"Automaticsampling temperature (between 0 and 2)
    "Echo"Automaticinclude the prompt in the completion
    "MaxTokens"Automaticmaximum number of tokens to generate
    "Suffix"Automaticsuffix that comes after a completion
    "StopTokens"Noneup to four strings where the API will stop generating further tokens
    "Model"Automaticname of the model to use
    "FrequencyPenalty"Automaticpenalize tokens based on their existing frequency in the text so far (between -2 and 2)
    "PresencePenalty"Automaticpenalize new tokens based on whether they appear in the text so far (between -2 and 2)
    "LogProbs"Automaticinclude the log probabilities on the most likely tokens, as well as the chosen tokens (between 0 and 5)
    "TotalProbabilityCutoff"Nonean alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with the requested probability mass
    "BestOf"Automaticnumber of completions to generate before selecting the "best"
    "User"Automaticunique identifier representing the end user
    "Stream"Automaticreturn the result as server-sent events
  • Request:

    "Chat" create a response for the given chat conversation

    Parameters:
  • "Messages"{}a list of messages in the conversation, each given as an association with "role" and "content" keys
    "N"Automaticnumber of chat completions to return
    "Temperature"Automaticsampling temperature (between 0 and 2)
    "MaxTokens"Automaticmaximum number of tokens to generate
    "Suffix"Automaticsuffix that comes after a completion
    "StopTokens"Noneup to four strings where the API will stop generating further tokens
    "Model"Automaticname of the model to use
    "FrequencyPenalty"Automaticpenalize tokens based on their existing frequency in the text so far (between -2 and 2)
    "PresencePenalty"Automaticpenalize new tokens based on whether they appear in the text so far (between -2 and 2)
    "LogProbs"Automaticinclude the log probabilities on the most likely tokens, as well as the chosen tokens (between 0 and 5)
    "TotalProbabilityCutoff"Nonean alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with the requested probability mass
    "User"Automaticunique identifier representing the end user
    "Stream"Automaticreturn the result as server-sent events
  • Request:

    "Edit" create a new edit for the provided input and instruction

    Parameters:
  • "Input"Automatictext to edit
    "Instruction"""instruction for how to edit the prompt
    "N"Automaticnumber of edits to return
    "Temperature"Automaticsampling temperature (between 0 and 2)
    "Model"Automaticname of the model to use
    "TotalProbabilityCutoff"Automatican alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with the requested probability mass
    "User"Automaticunique identifier representing the end user
  • Request:

    "Embedding" create an embedding vector representing the input text

    Parameters:
  • "Input"""one or a list of texts to get embeddings for
    "Model"Automaticname of the model to use
    "User"Automaticunique identifier representing the end user
  • Image

    Request:

    "ImageCreate" create a square image given a prompt

    Parameters:
  • "Prompt"Automatictext description of the desired image
    "N"Automaticnumber of images to generate
    "Size"Automaticsize of the generated image
    "User"Automaticunique identifier representing the end user
    "Model"Automaticname of the model to use
    "Quality"Automaticcontrol the quality of the result; possible values include "hd"
    "Style"Automaticstyle of generated images; possible values include "vivid" or "natural"
  • Request:

    "ImageVariation" create a variation of a given image

    Parameters:
  • "Image"(required)image to use as the basis for the variation
    "N"Automaticnumber of images to generate
    "Size"Automaticsize of the generated image
    "User"Automaticunique identifier representing the end user
  • Request:

    "ImageEdit" create an edited image given an original image and a prompt

    Parameters:
  • "Image"(required)image to edit; requires an alpha channel if a mask is not provided
    "Mask"Noneadditional image whose fully transparent areas indicate where the input should be edited
    "Prompt"Nonetext description of the desired image edit
    "N"Automaticnumber of images to generate
    "Size"Automaticsize of the generated image
    "User"Automaticunique identifier representing the end user
  • Audio

    Request:

    "AudioTranscription" transcribe an audio recording into the input language

    Parameters:
  • "Audio"(required)the Audio object to transcribe
    "Language"Automaticlanguage of the input audio
    "Prompt"Noneoptional text to guide the model's style or continue a previous audio segment
    "Temperature"Automaticsampling temperature (between 0 and 1)
    "Model"Automaticname of the model to use
  • Request:

    "AudioTranslation" translate an audio recording into English

    Parameters:
  • "Audio"(required)the Audio object to translate
    "Prompt"Noneoptional text to guide the model's style or continue a previous audio segment
    "Temperature"Automaticsampling temperature (between 0 and 1)
    "Model"Automaticname of the model to use
  • Request:

    "SpeechSynthesize" synthesize speech from text

    Parameters:
  • "Input"(required)the text to synthesize
    "Voice"Automaticthe voice to use for the synthesis
    "Speed"Automaticthe speed of the produced speech
    "Model"Automaticname of the model to use
  • Model Lists

    Request:

    "ChatModelList" list models available for the "Chat" request

    Request:

    "ModerationModelList" list models available for the "Moderation" request

    Request:

    "ImageModelList" list models available for the image-related requests

    Request:

    "SpeechSynthesizeModelList" list models available for the "SpeechSynthesize" request

    Moderation

    Request:

    "Moderation" classify if text violates OpenAI's Content Policy

    Parameters:
  • "Input"(required)the text to classify
    "Model"Automaticname of the model to use
  • Examples

    open allclose all

    Basic Examples  (1)

    Create a new connection:

    Complete a piece of text:

    Generate a response from a chat:

    Generate an Image from a prompt:

    Transcribe an Audio object:

    Synthesize a piece of text:

    Scope  (12)

    Text  (5)

    Completion  (1)

    Change the sampling temperature:

    Increase the number of characters returned:

    Return multiple completions:

    Include the prompt in the returned completion:

    Chat  (2)

    Respond to a chat containing multiple messages:

    Change the sampling temperature:

    Increase the number of characters returned:

    Return multiple completions:

    Send multimodal input:

    Embedding  (1)

    Compute the embedding for multiple sentences:

    Plot the results:

    Compute the embeddings for a list of words:

    Plot the results:

    Edit  (1)

    Provide both input and instruction:

    Change the sampling temperature:

    Return multiple edits:

    Image  (4)

    ImageCreate  (2)

    Create an Image:

    Return multiple results:

    Change the size of the returned Image:

    Use a different model:

    ImageVariation  (1)

    Create a variation of an Image:

    Return multiple results:

    Change the size of the returned Image:

    ImageEdit  (1)

    Use an Image with an alpha channel to indicate where the editing will take place:

    Use a non-transparent Image and a mask to indicate where the editing will take place:

    Return multiple results:

    Change the size of the returned Image:

    Audio  (3)

    AudioTranscription  (1)

    Transcribe an Audio object:

    Use a prompt to provide context for the transcription:

    Transcribe a recording made in a different language:

    Increase the temperature used for the sampling:

    AudioTranslation  (1)

    Translate an Audio object into English:

    Use a prompt to provide context for the translation:

    Increase the temperature used for the sampling:

    SpeechSynthesize  (1)

    Synthesize a piece of text:

    Use a different voice for the synthesis: