"OpenAI" (Service Connection)
Connecting & Authenticating
Requests
"TestConnection" — returns Success for working connection, Failure otherwise
"Completion" — create text completion for a given prompt
| "Prompt" | (required) | the prompt for which to generate completions | |
| "BestOf" | Automatic | number of completions to generate before selecting the "best" | |
| "Echo" | Automatic | include the prompt in the completion | |
| "FrequencyPenalty" | Automatic | penalize tokens based on their existing frequency in the text so far (between -2 and 2) | |
| "LogProbs" | Automatic | include the log probabilities on the most likely tokens, as well as the chosen tokens (between 0 and 5) | |
| "MaxTokens" | Automatic | maximum number of tokens to generate | |
| "Model" | Automatic | name of the model to use | |
| "N" | Automatic | number of completions to return | |
| "PresencePenalty" | Automatic | penalize new tokens based on whether they appear in the text so far (between -2 and 2) | |
| "StopTokens" | None | up to four strings where the API will stop generating further tokens | |
| "Stream" | Automatic | return the result as server-sent events | |
| "Suffix" | Automatic | suffix that comes after a completion | |
| "Temperature" | Automatic | sampling temperature (between 0 and 2) | |
| "ToolChoice" | Automatic | which (if any) tool is called by the model | |
| "Tools" | Automatic | one or more LLMTool objects available to the model | |
| "TotalProbabilityCutoff" | None | an alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with the requested probability mass | |
| "User" | Automatic | unique identifier representing the end user |
"Chat" — create a response for the given chat conversation
| "Messages" | (required) | a list of messages in the conversation, each given as an association with "Role" and "Content" keys | |
| "FrequencyPenalty" | Automatic | penalize tokens based on their existing frequency in the text so far (between -2 and 2) | |
| "LogProbs" | Automatic | include the log probabilities on the most likely tokens, as well as the chosen tokens (between 0 and 5) | |
| "MaxTokens" | Automatic | maximum number of tokens to generate | |
| "Model" | Automatic | name of the model to use | |
| "N" | Automatic | number of chat completions to return | |
| "PresencePenalty" | Automatic | penalize new tokens based on whether they appear in the text so far (between -2 and 2) | |
| "StopTokens" | None | up to four strings where the API will stop generating further tokens | |
| "Stream" | Automatic | return the result as server-sent events | |
| "Suffix" | Automatic | suffix that comes after a completion | |
| "Temperature" | Automatic | sampling temperature (between 0 and 2) | |
| "ToolChoice" | Automatic | which (if any) tool is called by the model | |
| "Tools" | Automatic | one or more LLMTool objects available to the model | |
| "TotalProbabilityCutoff" | None | an alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with the requested probability mass | |
| "User" | Automatic | unique identifier representing the end user |
"Embedding" — create an embedding vector representing the input text
| "Input" | (required) | one or a list of texts to get embeddings for | |
| "EncodingFormat" | Automatic | format to return the embeddings | |
| "EncodingLength" | Automatic | number of dimensions of the result | |
| "Model" | Automatic | name of the model to use | |
| "User" | Automatic | unique identifier representing the end user |
"ImageCreate" — create a square image given a prompt
| "Prompt" | (required) | text description of the desired image | |
| "Model" | Automatic | name of the model to use | |
| "N" | Automatic | number of images to generate | |
| "Quality" | Automatic | control the quality of the result; possible values include "hd" | |
| "Size" | Automatic | size of the generated image | |
| "Style" | Automatic | style of generated images; possible values include "vivid" or "natural" | |
| "User" | Automatic | unique identifier representing the end user |
"ImageVariation" — create a variation of a given image
| "Image" | (required) | image to use as the basis for the variation | |
| "N" | Automatic | number of images to generate | |
| "Size" | Automatic | size of the generated image | |
| "User" | Automatic | unique identifier representing the end user |
"ImageEdit" — create an edited image given an original image and a prompt
| "Image" | (required) | image to edit; requires an alpha channel if a mask is not provided | |
| "Mask" | None | additional image whose fully transparent areas indicate where the input should be edited | |
| "N" | Automatic | number of images to generate | |
| "Prompt" | None | text description of the desired image edit | |
| "Size" | Automatic | size of the generated image | |
| "User" | Automatic | unique identifier representing the end user |
"AudioTranscription" — transcribe an audio recording into the input language
| "Audio" | (required) | the Audio object to transcribe | |
| "Language" | Automatic | language of the input audio | |
| "Model" | Automatic | name of the model to use | |
| "Prompt" | None | optional text to guide the model's style or continue a previous audio segment | |
| "Temperature" | Automatic | sampling temperature (between 0 and 1) | |
| "TimestampGranularities" | Automatic | the timestamp granularity of transcription (either "word" or "segment") |
"AudioTranslation" — translate an audio recording into English
| "Audio" | (required) | the Audio object to translate | |
| "Model" | Automatic | name of the model to use | |
| "Prompt" | None | optional text to guide the model's style or continue a previous audio segment | |
| "Temperature" | Automatic | sampling temperature (between 0 and 1) |
"SpeechSynthesize" — synthesize speech from text
| "Input" | (required) | the text to synthesize | |
| "Model" | Automatic | name of the model to use | |
| "Speed" | Automatic | the speed of the produced speech | |
| "Voice" | Automatic | the voice to use for the synthesis |
"ChatModelList" — list models available for the "Chat" request
"CompletionModelList" — list models available for the "Completion" request
"EmbeddingModelList" — list models available for the "Embedding" request
"ModerationModelList" — list models available for the "Moderation" request
"ImageModelList" — list models available for the image-related requests
"SpeechSynthesizeModelList" — list models available for the "SpeechSynthesize" request
"AudioModelList" — list models available for the "AudioTranscribe" request
"Moderation" — classify if text violates OpenAI's Content Policy
| "Input" | (required) | the text to classify | |
| "Model" | Automatic | name of the model to use |
Examples
open all close allBasic Examples (1)
openAI = ServiceConnect["OpenAI"]ServiceExecute[openAI, "Completion", {"Prompt" -> "please complete this text."}]Generate a response from a chat:
ServiceExecute[openAI, "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> "hello there"|>}}]Compute the embedding for a sentence:
ServiceExecute[openAI, "Embedding", {"Input" -> "The quick brown fox ..."}]Generate an Image from a prompt:
ServiceExecute[openAI, "ImageCreate", {"Prompt" -> "a cat in a box"}]Transcribe an Audio object:
ServiceExecute[openAI, "AudioTranscription", {"Audio" -> ExampleData[{"Audio", "MaleVoice"}]}]ServiceExecute["OpenAI", "SpeechSynthesize", {"Input" -> "Right then. Perfect timing."}]Scope (10)
Text (4)
Completion (1)
Change the sampling temperature:
ServiceExecute["OpenAI", "Completion", {"Prompt" -> "please complete this text.", "Temperature" -> 1.5}]Increase the number of characters returned:
ServiceExecute["OpenAI", "Completion", {"Prompt" -> "please complete this text.", "MaxTokens" -> 100}]ServiceExecute["OpenAI", "Completion", {"Prompt" -> "please complete this text.", "N" -> 2, "BestOf" -> Automatic}]Include the prompt in the returned completion:
ServiceExecute["OpenAI", "Completion", {"Prompt" -> "please complete this text.", "Echo" -> True}]Chat (2)
Respond to a chat containing multiple messages:
ServiceExecute["OpenAI", "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> "hello there"|>, <|"Role" -> "User", "Content" -> "I'm bored"|>}}]Change the sampling temperature:
ServiceExecute["OpenAI", "Chat", {"Messages" -> {<|"Role" -> "user", "Content" -> "hello there"|>}, "Temperature" -> 2}]Increase the number of characters returned:
ServiceExecute["OpenAI", "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> "hello there. Tell me something about bears"|>}, "MaxTokens" -> 100}]ServiceExecute["OpenAI", "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> "hello there. Tell me something brief about bears"|>}, "N" -> 2}]Allow the model to use an LLMTool:
tool = LLMTool["countcharacter", "string", StringLength[#string]&]ServiceExecute["OpenAI", "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> "count the characters in this request"|>}, "Tools" -> tool}]ServiceExecute["OpenAI", "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> {"hello there. Tell me something about this picture", [image]}|>}, "Model" -> "gpt-4o"}]Send a chat request asynchronously using ServiceSubmit and collect the response using the HandlerFunctions and HandlerFunctionsKeys options:
res = {};ServiceSubmit["OpenAI", "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> "hello there"|>, <|"Role" -> "User", "Content" -> "I'm bored"|>}, "Stream" -> True}, HandlerFunctionsKeys -> "BodyChunkProcessed", HandlerFunctions -> <|"BodyChunkReceived" -> Function[AppendTo[res, #]]|>];
TaskWait[%]
Query["BodyChunkProcessed", All, "ContentChunk"] /@ res//FlattenEmbedding (1)
Compute the embedding for multiple sentences:
sentences = StringSplit[ExampleData[{"Text", "TheRaven"}], "."];
sentences//LengthsentenceEmbeddings = ServiceExecute["OpenAI", "Embedding", {"Input" -> sentences}];FeatureSpacePlot[Thread[Labeled[sentenceEmbeddings[["Content"]], Table[StringTake[t, 12] <> "...", {t, sentences}]
]], LabelingFunction -> None]Compute the embeddings for a list of words:
words = StringSplit[ExampleData[{"Text", "TheRaven"}]];
wordEmbeddings = ServiceExecute["OpenAI", "Embedding", {"Input" -> words}];FeatureSpacePlot[Thread[Labeled[wordEmbeddings["Content"], words]], LabelingFunction -> None]Image (3)
ImageCreate (1)
Create an Image:
ServiceExecute["OpenAI", "ImageCreate", {"Prompt" -> "the cutest little cat"}]ServiceExecute["OpenAI", "ImageCreate", {"Prompt" -> "the cutest little cat", "N" -> 2}]Change the size of the returned Image:
ServiceExecute["OpenAI", "ImageCreate", {"Prompt" -> "the cutest little cat", "Size" -> 512}]ServiceExecute["OpenAI", "ImageCreate", {"Prompt" -> "the cutest little cat", "Model" -> "dall-e-3"}]ImageVariation (1)
Create a variation of an Image:
ServiceExecute["OpenAI", "ImageVariation", {"Image" -> [image]}]ServiceExecute["OpenAI", "ImageVariation", {"Image" -> [image], "N" -> 2}]Change the size of the returned Image:
ServiceExecute["OpenAI", "ImageVariation", {"Image" -> [image], "Size" -> 512}]ImageEdit (1)
Use an Image with an alpha channel to indicate where the editing will take place:
ServiceExecute["OpenAI", "ImageEdit", {"Image" -> [image], "Prompt" -> "put a hat on it"}]Use a non-transparent Image and a mask to indicate where the editing will take place:
ServiceExecute["OpenAI", "ImageEdit", {"Image" -> [image], "Mask" -> [image], "Prompt" -> "put a hat on it"}]ServiceExecute["OpenAI", "ImageEdit", {"Image" -> [image], "Prompt" -> "put a hat on it", "N" -> 2}]Change the size of the returned Image:
ServiceExecute["OpenAI", "ImageEdit", {"Image" -> [image], "Prompt" -> "put a hat on it", "Size" -> 512}]Audio (3)
AudioTranscription (1)
Transcribe an Audio object:
ServiceExecute["OpenAI", "AudioTranscription", {"Audio" -> ExampleData[{"Audio", "MaleVoice"}]}]Use a prompt to provide context for the transcription:
ServiceExecute["OpenAI", "AudioTranscription", {"Audio" -> ExampleData[{"Audio", "MaleVoice"}], "Prompt" -> "in a welsh farm, just before noon"}]Transcribe a recording made in a different language:
ServiceExecute["OpenAI", "AudioTranscription", {"Audio" -> \!\(\*AudioBox[""]\), "Language" -> Entity["Language", "Italian::39fbj"]}]Increase the temperature used for the sampling:
ServiceExecute["OpenAI", "AudioTranscription", {"Audio" -> ExampleData[{"Audio", "MaleVoice"}], "Temperature" -> .9}]Include timestamps in the transcription:
ServiceExecute["OpenAI", "AudioTranscription", {"Audio" -> ExampleData[{"Audio", "MaleVoice"}], "TimestampGranularities" -> "Word"}]AudioTranslation (1)
Translate an Audio object into English:
ServiceExecute["OpenAI", "AudioTranslation", {"Audio" -> ExampleData[{"Audio", "MaleVoice"}]}]Use a prompt to provide context for the translation:
ServiceExecute["OpenAI", "AudioTranslation", {"Audio" -> \!\(\*AudioBox[""]\), "Prompt" -> "in a welsh farm, just before noon"}]Increase the temperature used for the sampling:
ServiceExecute["OpenAI", "AudioTranscription", {"Audio" -> ExampleData[{"Audio", "MaleVoice"}], "Temperature" -> 1}]SpeechSynthesize (1)
Authentication (4)
If no connections exist, ServiceConnect will prompt a dialog where an API key can be entered:
ServiceConnect["OpenAI"]The API key can also be specified using the Authentication option:
ServiceConnect["OpenAI", Authentication -> "myOpenAIkey"]Use credentials stored in SystemCredential:
SystemCredential["key"] = "myOpenAIKey";
so = ServiceConnect["OpenAI", Authentication -> SystemCredential["key"]]The credentials are stored directly by the framework, since SystemCredential["key"] evaluates to a string:
so["Authentication"]Only store the SystemCredential key rather than its value by using RuleDelayed:
so2 = ServiceConnect["OpenAI", Authentication :> SystemCredential["key"]];
so2["Authentication"]
%//ReleaseHoldRetrieve the value of the authentication credentials used in a specific service object:
so = ServiceConnect["OpenAI", Authentication -> "myOpenAIkey"];
so["Authentication"]Overwrite the authentication credentials of an existing service object:
so = ServiceConnect["OpenAI", Authentication -> "myOpenAIkey"];
so["Authentication"]ServiceConnect[so, Authentication -> "otherKey"];
so["Authentication"]See Also
ServiceExecute ▪ ServiceConnect ▪ LLMFunction ▪ LLMSynthesize ▪ ChatEvaluate ▪ LLMConfiguration ▪ ImageSynthesize ▪ SpeechRecognize
Service Connections: AlephAlpha ▪ Anthropic ▪ Cohere ▪ DeepSeek ▪ GoogleGemini ▪ Groq ▪ MistralAI ▪ TogetherAI ▪ GoogleSpeech
History
Introduced in 2023 (13.3)