Openai::Client
This gem is a wrapper for calling the OpenAI and GPT-3 APIs.
- Installation
- Usage
- OpenAI Models API
- List Models
- Find Model
- OpenAI Completions API
- Create Completion
- OpenAI Edits API
- Create Edit
- OpenAI Image API
- Create an Image
- Create an Image Edit
- Create an Image Variation
- OpenAI Embeddings API
- Create Embeddings
- OpenAI Moderations API
- Create Moderation
- OpenAI Files API
- List Files
- Find File
- Find File Content
- Upload File
- Delete File
- OpenAI Fine-Tunes API
- Create Fine-Tune
- List Fine-Tunes
- Find Fine-Tune
- List Fine-Tune Events
- Cancel Fine-Tune
- Delete Fine-Tune Model
Installation
Add this line to your application's Gemfile:
gem 'openai-client'
And then execute:
bundle
Or install it yourself as:
gem install openai-client
Usage
- API key (
access_token
) https://beta.openai.com/account/api-keys. - Organization ID (if needed) https://beta.openai.com/account/org-settings.
require 'openai-client'
Openai::Client.configure do |c|
c.access_token = 'access_token'
c.organization_id = 'organization_id' # optional
end
OpenAI Models API
List Models
Openai::Client.models.list
Find Model
Openai::Client.models.find(model_id)
# Models
Openai::Client.models.list
# Find a Model
Openai::Client.models.find(model_id)
OpenAI Completions API
Create Completion
request_body = {
model: 'text-davinci-003',
prompt: 'Say this is a test',
max_tokens: 7,
temperature: 0,
top_p: 1,
n: 1,
stream: false,
logprobs: nil,
stop: "\n"
}
Openai::Client.completions.create(request_body)
OpenAI Edits API
Create Edit
request_body = {
model: 'text-davinci-edit-001',
input: 'What day of the wek is it?',
instruction: 'Fix the spelling mistakes'
}
Openai::Client.edits.create(request_body)
OpenAI Image API
Create an Image
request_body = {
prompt: 'A cute baby sea otter',
n: 1, # between 1 and 10
size: '1024x1024', # 256x256, 512x512, or 1024x1024
response_format: 'url' # url or b64_json
}
response = Openai::Client.images.create(request_body)
Create an Image Edit
request_body = {
image: '/absolute/path/to/image/you/want/to/change/img.png'
mask: '/absolute/path/to/mask.png'
prompt: 'A cute baby sea otter wearing a beret',
n: 1, # between 1 and 10
size: '1024x1024', # 256x256, 512x512, or 1024x1024
response_format: 'url' # url or b64_json
}
response = Openai::Client.images.edit(request_body)
-
image
- must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask. -
mask
- an additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
Create an Image Variation
request_body = {
image: '/absolute/path/to/image.png'
n: 1, # between 1 and 10
size: '1024x1024', # 256x256, 512x512, or 1024x1024
response_format: 'url' # url or b64_json
}
response = Openai::Client.images.variations(request_body)
-
image
- must be a valid PNG file, less than 4MB, and square.
OpenAI Embeddings API
Create Embeddings
request_body = {
model: 'text-embedding-ada-002',
input: 'The food was delicious and the waiter...'
}
Openai::Client.embeddings.create(request_body)
OpenAI Moderations API
Create Moderation
request_body = {
model: 'text-moderation-latest', # text-moderation-stable or text-moderation-latest
input: 'I want to kill them.'
}
Openai::Client.moderations.create(request_body)
OpenAI Files API
List Files
Openai::Client.files.list
Find File
Openai::Client.files.find(file_id)
Find File Content
Openai::Client.files.find_content(file_id)
Upload File
request_body = {
file: '/absolute/path/to/file.jsonl',
purpose: 'fine-tune'
}
Openai::Client.files.upload(request_body)
The file format must be jsonl, where each line contains the prompt and completion properties.
Example (file.jsonl):
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
...
Delete File
Openai::Client.files.delete(file_id)
OpenAI Fine-Tunes API
Create Fine-Tune
request_body = {
training_file: "file-XGinujblHPwGLSztz8cPS8XY"
}
Openai::Client.fine_tunes.create(request_body)
List Fine-Tunes
Openai::Client.fine_tunes.list
Find Fine-Tune
Openai::Client.fine_tunes.find(fine_tune_id)
List Fine-Tune Events
Openai::Client.fine_tunes.find_events(fine_tune_id)
Cancel Fine-Tune
Openai::Client.fine_tunes.cancel(fine_tune_id)
Delete Fine-Tune Model
Openai::Client.models.delete(model_id)
- You must have the Owner role in your organization.
- Make sure you provide the Model ID and not the Fine-Tune ID.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/itikhonenko/openai-client.
License
The gem is available as open source under the terms of the MIT License.