Modules@robinpath/openai
openai

@robinpath/openai

0.1.2Node.jsPublic

OpenAI module for RobinPath.

OpenAI

OpenAI module for RobinPath.

Package: @robinpath/openai | Category: Ai | Type: Integration

Authentication

openai.setApiKey "sk-..."

Call this once at the start of your script before using any other function. Credentials persist for the duration of the script execution.

Use Cases

Use the openai module when you need to:

  • Send a chat completion request to OpenAI -- Use openai.chat to perform this operation
  • Send a legacy completion request -- Use openai.complete to perform this operation
  • Generate images using DALL-E -- Use openai.generateImage to perform this operation
  • Edit an image using DALL-E with an optional mask -- Use openai.editImage to perform this operation
  • Create a variation of an existing image -- Use openai.createImageVariation to perform this operation

Quick Reference

FunctionDescriptionReturns
setApiKeySet the OpenAI API key for authenticationtrue if key was set
chatSend a chat completion request to OpenAI{content, role, model, toolCalls, usage}
completeSend a legacy completion request{text, model, usage}
generateImageGenerate images using DALL-E{images: [{url, b64Json, revisedPrompt}]}
editImageEdit an image using DALL-E with an optional mask{images: [{url, b64Json}]}
createImageVariationCreate a variation of an existing image{images: [{url, b64Json}]}
transcribeTranscribe audio to text using WhisperTranscription result with text
translateTranslate audio to English text using WhisperTranslation result with English text
speakConvert text to speech using TTS{audio, format, size}
createEmbeddingGenerate text embeddings{embeddings, model, usage}
createModerationCheck text for content policy violations{id, model, results: [{flagged, categories, categoryScores}]}
listModelsList all available OpenAI modelsArray of {id, created, ownedBy}
getModelGet details of a specific modelModel details object
uploadFileUpload a file to OpenAIUploaded file object with id, filename, purpose
listFilesList uploaded filesArray of file objects
deleteFileDelete an uploaded fileDeletion confirmation
getFileContentGet the content of an uploaded fileFile content as text
createFineTuneCreate a fine-tuning jobFine-tuning job object
listFineTunesList fine-tuning jobsArray of fine-tuning job objects
getFineTuneGet details of a fine-tuning jobFine-tuning job details
cancelFineTuneCancel a running fine-tuning jobCancelled job details
createBatchCreate a batch processing requestBatch object with id, status
getBatchGet details of a batch requestBatch details object
listBatchesList batch requestsArray of batch objects
cancelBatchCancel a batch requestCancelled batch details

Functions

setApiKey

Set the OpenAI API key for authentication

Module: openai | Returns: boolean -- true if key was set

openai.setApiKey "sk-..."
ParameterTypeRequiredDescription
apiKeystringYesYour OpenAI API key

chat

Send a chat completion request to OpenAI

Module: openai | Returns: object -- {content, role, model, toolCalls, usage}

openai.chat "Hello, how are you?" {"model": "gpt-4o"}
ParameterTypeRequiredDescription
messagesanyYesString or array of {role, content} message objects
optionsobjectNo{model, temperature, maxTokens, topP, tools, responseFormat}

complete

Send a legacy completion request

Module: openai | Returns: object -- {text, model, usage}

openai.complete "Once upon a time"
ParameterTypeRequiredDescription
promptstringYesText prompt for completion
optionsobjectNo{model, temperature, maxTokens, topP}

generateImage

Generate images using DALL-E

Module: openai | Returns: object -- {images: [{url, b64Json, revisedPrompt}]}

openai.generateImage "A sunset over mountains" {"model": "dall-e-3", "size": "1024x1024"}
ParameterTypeRequiredDescription
promptstringYesImage description prompt
optionsobjectNo{model, size, quality, style, n}

editImage

Edit an image using DALL-E with an optional mask

Module: openai | Returns: object -- {images: [{url, b64Json}]}

openai.editImage "/path/to/image.png" "Add a hat to the person"
ParameterTypeRequiredDescription
imagePathstringYesPath to the source image file
promptstringYesDescription of the edit to make
optionsobjectNo{mask, model, n, size}

createImageVariation

Create a variation of an existing image

Module: openai | Returns: object -- {images: [{url, b64Json}]}

openai.createImageVariation "/path/to/image.png" {"n": 2}
ParameterTypeRequiredDescription
imagePathstringYesPath to the source image file
optionsobjectNo{model, n, size}

transcribe

Transcribe audio to text using Whisper

Module: openai | Returns: object -- Transcription result with text

openai.transcribe "/path/to/audio.mp3" {"language": "en"}
ParameterTypeRequiredDescription
audioPathstringYesPath to the audio file
optionsobjectNo{model, language, responseFormat, temperature}

translate

Translate audio to English text using Whisper

Module: openai | Returns: object -- Translation result with English text

openai.translate "/path/to/french-audio.mp3"
ParameterTypeRequiredDescription
audioPathstringYesPath to the audio file
optionsobjectNo{model, responseFormat, temperature}

speak

Convert text to speech using TTS

Module: openai | Returns: object -- {audio, format, size}

openai.speak "Hello world" {"voice": "nova", "model": "tts-1-hd"}
ParameterTypeRequiredDescription
textstringYesText to convert to speech
optionsobjectNo{model, voice, speed, responseFormat}

createEmbedding

Generate text embeddings

Module: openai | Returns: object -- {embeddings, model, usage}

openai.createEmbedding "Hello world" {"model": "text-embedding-3-small"}
ParameterTypeRequiredDescription
inputanyYesString or array of strings to embed
optionsobjectNo{model, dimensions}

createModeration

Check text for content policy violations

Module: openai | Returns: object -- {id, model, results: [{flagged, categories, categoryScores}]}

openai.createModeration "Some text to check"
ParameterTypeRequiredDescription
inputanyYesString or array of strings to moderate

listModels

List all available OpenAI models

Module: openai | Returns: array -- Array of {id, created, ownedBy}

openai.listModels
ParameterTypeRequiredDescription
(none)NoCall with no arguments

getModel

Get details of a specific model

Module: openai | Returns: object -- Model details object

openai.getModel "gpt-4o"
ParameterTypeRequiredDescription
modelIdstringYesModel ID to look up

uploadFile

Upload a file to OpenAI

Module: openai | Returns: object -- Uploaded file object with id, filename, purpose

openai.uploadFile "/path/to/data.jsonl" "fine-tune"
ParameterTypeRequiredDescription
filePathstringYesPath to the file to upload
purposestringYesPurpose: fine-tune, assistants, or batch

listFiles

List uploaded files

Module: openai | Returns: array -- Array of file objects

openai.listFiles "fine-tune"
ParameterTypeRequiredDescription
purposestringNoFilter by purpose

deleteFile

Delete an uploaded file

Module: openai | Returns: object -- Deletion confirmation

openai.deleteFile "file-abc123"
ParameterTypeRequiredDescription
fileIdstringYesID of the file to delete

getFileContent

Get the content of an uploaded file

Module: openai | Returns: string -- File content as text

openai.getFileContent "file-abc123"
ParameterTypeRequiredDescription
fileIdstringYesID of the file to retrieve

createFineTune

Create a fine-tuning job

Module: openai | Returns: object -- Fine-tuning job object

openai.createFineTune "file-abc123" {"model": "gpt-4o-mini-2024-07-18", "suffix": "my-model"}
ParameterTypeRequiredDescription
trainingFilestringYesFile ID of the training data
optionsobjectNo{model, hyperparameters, suffix, validationFile}

listFineTunes

List fine-tuning jobs

Module: openai | Returns: array -- Array of fine-tuning job objects

openai.listFineTunes {"limit": 10}
ParameterTypeRequiredDescription
optionsobjectNo{after, limit}

getFineTune

Get details of a fine-tuning job

Module: openai | Returns: object -- Fine-tuning job details

openai.getFineTune "ftjob-abc123"
ParameterTypeRequiredDescription
fineTuneIdstringYesFine-tuning job ID

cancelFineTune

Cancel a running fine-tuning job

Module: openai | Returns: object -- Cancelled job details

openai.cancelFineTune "ftjob-abc123"
ParameterTypeRequiredDescription
fineTuneIdstringYesFine-tuning job ID to cancel

createBatch

Create a batch processing request

Module: openai | Returns: object -- Batch object with id, status

openai.createBatch "file-abc123" "/v1/chat/completions"
ParameterTypeRequiredDescription
inputFileIdstringYesFile ID containing batch requests
endpointstringYesAPI endpoint for batch (e.g. /v1/chat/completions)
optionsobjectNo{completionWindow, metadata}

getBatch

Get details of a batch request

Module: openai | Returns: object -- Batch details object

openai.getBatch "batch_abc123"
ParameterTypeRequiredDescription
batchIdstringYesBatch ID to look up

listBatches

List batch requests

Module: openai | Returns: array -- Array of batch objects

openai.listBatches {"limit": 10}
ParameterTypeRequiredDescription
optionsobjectNo{after, limit}

cancelBatch

Cancel a batch request

Module: openai | Returns: object -- Cancelled batch details

openai.cancelBatch "batch_abc123"
ParameterTypeRequiredDescription
batchIdstringYesBatch ID to cancel

Error Handling

All functions throw on failure. Common errors:

ErrorCause
OpenAI API key not set. Use openai.setApiKey first.Check the error message for details
OpenAI API error: ${JSON.stringify(data.error ?? data)}Check the error message for details
API key is requiredCheck the error message for details
Model ID is requiredCheck the error message for details
File ID is requiredCheck the error message for details
Fine-tune job ID is requiredCheck the error message for details
Input file ID is requiredCheck the error message for details
Endpoint is requiredCheck the error message for details
@desc "Chat and validate result"
do
  set $result as openai.chat "Hello, how are you?" {"model": "gpt-4o"}
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate Models

Retrieve all items and loop through them.

@desc "Setup authentication"
do
  openai.setApiKey $token
enddo

@desc "List models and iterate results"
do
  set $result as openai.listModels
  each $item in $result
    print $item
  end
enddo

2. Create a new item with createImageVariation

Create a new resource and capture the result.

@desc "Setup authentication"
do
  openai.setApiKey $token
enddo

@desc "Create image variation"
do
  set $result as openai.createImageVariation "/path/to/image.png" {"n": 2}
  print "Created: " + $result
enddo

3. Create and update workflow

Create an item and then update it.

@desc "Setup authentication"
do
  openai.setApiKey $token
enddo

@desc "Create image variation and edit image"
do
  set $created as openai.createImageVariation "/path/to/image.png" {"n": 2}
  # Update the created item
  openai.editImage "/path/to/image.png" "Add a hat to the person"
enddo

4. Check before creating

List existing items and only create if needed.

@desc "Setup authentication"
do
  openai.setApiKey $token
enddo

@desc "List models and create image variation"
do
  set $existing as openai.listModels
  if $existing == null
    openai.createImageVariation "/path/to/image.png" {"n": 2}
    print "Item created"
  else
    print "Item already exists"
  end
enddo

5. Multi-step OpenAI workflow

Chain multiple openai operations together.

@desc "Setup authentication"
do
  openai.setApiKey $token
enddo

@desc "Chat, complete, and more"
do
  set $r_chat as openai.chat "Hello, how are you?" {"model": "gpt-4o"}
  set $r_complete as openai.complete "Once upon a time"
  set $r_generateImage as openai.generateImage "A sunset over mountains" {"model": "dall-e-3", "size": "1024x1024"}
  print "All operations complete"
enddo

6. Safe chat with validation

Check results before proceeding.

@desc "Setup authentication"
do
  openai.setApiKey $token
enddo

@desc "Chat and validate result"
do
  set $result as openai.chat "Hello, how are you?" {"model": "gpt-4o"}
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • anthropic -- Anthropic module for complementary functionality
  • ai -- AI module for complementary functionality
  • deepl -- DeepL module for complementary functionality
  • translate -- Translate module for complementary functionality
  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.2latest1 months ago
Install
$ robinpath add @robinpath/openai

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.2
LicenseMIT
Unpacked Size9.5 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

ai