Skip to content

Commit

Permalink
Merge pull request #283 from alexrudall/4.2
Browse files Browse the repository at this point in the history
4.2
  • Loading branch information
alexrudall authored Jun 20, 2023
2 parents 58fa9c0 + 6820be7 commit 8c66f19
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.2.0] - 2023-06-20

### Added

- Add Azure OpenAI Service support. Thanks to [@rmachielse](https://github.com/rmachielse) and [@steffansluis](https://github.com/steffansluis) for the PR and to everyone who requested this feature!

## [4.1.0] - 2023-05-15

### Added
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ruby-openai (4.1.0)
ruby-openai (4.2.0)
faraday (>= 1)
faraday-multipart (>= 1)

Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ OpenAI.configure do |config|
end
```

### Azure

To use the [Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/) API, you can configure the gem like this:

```ruby
OpenAI.configure do |config|
config.access_token = ENV.fetch("AZURE_OPENAI_API_KEY")
config.uri_base = ENV.fetch("AZURE_OPENAI_URI")
config.api_type = :azure
config.api_version = "2023-03-15-preview"
end
```

where `AZURE_OPENAI_URI` is e.g. `https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo`

### Models

There are different models that can be used to generate text. For a full list and to retrieve information about a single model:
Expand Down
5 changes: 3 additions & 2 deletions lib/openai/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ def conn(multipart: false)

def uri(path:)
if OpenAI.configuration.api_type == :azure
OpenAI.configuration.uri_base + path + "?api-version=#{OpenAI.configuration.api_version}"
base = File.join(OpenAI.configuration.uri_base, path)
"#{base}?api-version=#{OpenAI.configuration.api_version}"
else
OpenAI.configuration.uri_base + OpenAI.configuration.api_version + path
File.join(OpenAI.configuration.uri_base, OpenAI.configuration.api_version, path)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/openai/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module OpenAI
VERSION = "4.1.0".freeze
VERSION = "4.2.0".freeze
end
24 changes: 22 additions & 2 deletions spec/openai/client/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,21 @@

it { expect(uri).to eq("https://api.openai.com/v1/chat") }

context "uri_base without trailing slash" do
before do
OpenAI.configuration.uri_base = "https://api.openai.com"
end

after do
OpenAI.configuration.uri_base = "https://api.openai.com/"
end

it { expect(uri).to eq("https://api.openai.com/v1/chat") }
end

describe "with Azure" do
before do
OpenAI.configuration.uri_base = "https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo"
OpenAI.configuration.uri_base = uri_base
OpenAI.configuration.api_type = :azure
end

Expand All @@ -205,7 +217,15 @@
let(:path) { "/chat" }
let(:uri) { OpenAI::Client.send(:uri, path: path) }

it { expect(uri).to eq("https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo/chat?api-version=v1") }
context "with a trailing slash" do
let(:uri_base) { "https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo/" }
it { expect(uri).to eq("https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo/chat?api-version=v1") }
end

context "without a trailing slash" do
let(:uri_base) { "https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo" }
it { expect(uri).to eq("https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo/chat?api-version=v1") }
end
end
end

Expand Down

0 comments on commit 8c66f19

Please sign in to comment.