Skip to content

itikhonenko/openai-client

Repository files navigation

Openai::Client

This gem is a wrapper for calling the OpenAI and GPT-3 APIs.

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

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)

API Documentation

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)

API Documentation

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)

API Documentation

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.

API Documentation

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.

API Documentation

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)

API Documentation

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)

API Documentation

OpenAI Files API

List Files

Openai::Client.files.list

API Documentation

Find File

Openai::Client.files.find(file_id)

API Documentation

Find File Content

Openai::Client.files.find_content(file_id)

API Documentation

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>"}
...

API Documentation

Delete File

Openai::Client.files.delete(file_id)

API Documentation

OpenAI Fine-Tunes API

Create Fine-Tune

request_body = {
  training_file: "file-XGinujblHPwGLSztz8cPS8XY"
}

Openai::Client.fine_tunes.create(request_body)

API Documentation

List Fine-Tunes

Openai::Client.fine_tunes.list

API Documentation

Find Fine-Tune

Openai::Client.fine_tunes.find(fine_tune_id)

API Documentation

List Fine-Tune Events

Openai::Client.fine_tunes.find_events(fine_tune_id)

API Documentation

Cancel Fine-Tune

Openai::Client.fine_tunes.cancel(fine_tune_id)

API Documentation

Delete Fine-Tune Model

Openai::Client.models.delete(model_id)

API Documentation

  • 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.

About

The gem that makes it easier to use the OpenAI API

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published