Skip to content

Commit

Permalink
Merge pull request #108 from alexrudall/remove-legacy-engine-calls
Browse files Browse the repository at this point in the history
Deprecate Engine
  • Loading branch information
alexrudall authored Sep 19, 2022
2 parents 580d3e9 + 0af91aa commit 89b5d1c
Show file tree
Hide file tree
Showing 31 changed files with 3,316 additions and 2,312 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Use the [OpenAI GPT-3 API](https://openai.com/blog/openai-api/) with Ruby! 🤖
Add this line to your application's Gemfile:

```ruby
gem 'ruby-openai'
gem "ruby-openai"
```

And then execute:
Expand Down Expand Up @@ -76,16 +76,21 @@ There are different models that can be used to generate text. For a full list an

```ruby
client.models.list
client.models.retrieve(id: 'text-ada-001')
client.models.retrieve(id: "text-ada-001")
```

### Completions

Hit the OpenAI API for a completion:

```ruby
response = client.completions(engine: "text-davinci-001", parameters: { prompt: "Once upon a time", max_tokens: 5 })
puts response.parsed_response['choices'].map{ |c| c["text"] }
response = client.completions(
parameters: {
model: "text-davinci-001",
prompt: "Once upon a time",
max_tokens: 5
})
puts response["choices"].map { |c| c["text"] }
=> [", there lived a great"]
```

Expand All @@ -111,9 +116,9 @@ You can use the embeddings endpoint to get a vector of numbers representing an i

```ruby
client.embeddings(
engine: "babbage-similarity",
parameters: {
input: "The food was delicious and the waiter..."
model: "babbage-similarity",
input: "The food was delicious and the waiter..."
}
)
```
Expand All @@ -130,7 +135,7 @@ Put your data in a `.jsonl` file like this:
and pass the path to `client.files.upload` to upload it to OpenAI, and then interact with it:

```ruby
client.files.upload(parameters: { file: 'path/to/puppy.jsonl', purpose: 'search' })
client.files.upload(parameters: { file: "path/to/puppy.jsonl", purpose: "search" })
client.files.list
client.files.retrieve(id: 123)
client.files.delete(id: 123)
Expand All @@ -148,7 +153,7 @@ Put your fine-tuning data in a `.jsonl` file like this:
and pass the path to `client.files.upload` to upload it to OpenAI and get its ID:

```ruby
response = client.files.upload(parameters: { file: 'path/to/sentiment.jsonl', purpose: 'fine-tune' })
response = client.files.upload(parameters: { file: "path/to/sentiment.jsonl", purpose: "fine-tune" })
file_id = JSON.parse(response.body)["id"]
```

Expand Down Expand Up @@ -189,8 +194,6 @@ This fine-tuned model name can then be used in classifications:
JSON.parse(response.body)["choices"].map { |c| c["text"] }
```

Do not pass the engine parameter when using a fine-tuned model.

### Moderations

Pass a string to check if it violates OpenAI's Content Policy:
Expand Down
1 change: 1 addition & 0 deletions lib/ruby/openai.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "httparty"
require "ruby/openai/engines"
require "ruby/openai/files"
require "ruby/openai/finetunes"
require "ruby/openai/models"
Expand Down
41 changes: 34 additions & 7 deletions lib/ruby/openai/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@ def classifications(version: default_version, parameters: {})
end

def completions(engine: nil, version: default_version, parameters: {})
if engine
post(url: "/#{version}/engines/#{engine}/completions", parameters: parameters)
else
post(url: "/#{version}/completions", parameters: parameters)
end
parameters = deprecate_engine(engine: engine, method: "completions", parameters: parameters)

post(url: "/#{version}/completions", parameters: parameters)
end

def edits(version: default_version, parameters: {})
post(url: "/#{version}/edits", parameters: parameters)
end

def embeddings(engine:, version: default_version, parameters: {})
post(url: "/#{version}/engines/#{engine}/embeddings", parameters: parameters)
def embeddings(engine: nil, version: default_version, parameters: {})
parameters = deprecate_engine(engine: engine, method: "embeddings", parameters: parameters)

post(url: "/#{version}/embeddings", parameters: parameters)
end

def engines
warn "[DEPRECATION WARNING] [ruby-openai] `Client#engines` is deprecated and will
be removed from ruby-openai v3.0. Use `Client#models` instead."

@engines ||= OpenAI::Engines.new(access_token: @access_token)
end

def files
Expand All @@ -47,8 +54,28 @@ def moderations(version: default_version, parameters: {})
post(url: "/#{version}/moderations", parameters: parameters)
end

def search(engine:, version: default_version, parameters: {})
warn "[DEPRECATION WARNING] [ruby-openai] `Client#search` is deprecated and will
be removed from the OpenAI API on 3 December 2022 and from ruby-openai v3.0.
More information: https://help.openai.com/en/articles/6272952-search-transition-guide"

post(url: "/#{version}/engines/#{engine}/search", parameters: parameters)
end

private

def deprecate_engine(engine:, method:, parameters:)
return parameters unless engine

parameters = { model: engine }.merge(parameters)

warn "[DEPRECATION WARNING] [ruby-openai] Passing `engine` directly to `Client##{method}` is
deprecated and will be removed in ruby-openai 3.0. Pass `model` within `parameters` instead:
client.completions(parameters: { #{parameters.map { |k, v| "#{k}: \"#{v}\"" }.join(', ')} })"

parameters
end

def default_version
"v1".freeze
end
Expand Down
36 changes: 36 additions & 0 deletions lib/ruby/openai/engines.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module OpenAI
class Engines
include HTTParty
base_uri "https://api.openai.com"

def initialize(access_token: nil)
@access_token = access_token || ENV.fetch("OPENAI_ACCESS_TOKEN")
end

def list(version: default_version)
self.class.get(
"/#{version}/engines",
headers: {
"Content-Type" => "application/json",
"Authorization" => "Bearer #{@access_token}"
}
)
end

def retrieve(id:, version: default_version)
self.class.get(
"/#{version}/engines/#{id}",
headers: {
"Content-Type" => "application/json",
"Authorization" => "Bearer #{@access_token}"
}
)
end

private

def default_version
"v1".freeze
end
end
end
Loading

0 comments on commit 89b5d1c

Please sign in to comment.