Skip to content

Add Microsoft Azure support #238

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Jun 20, 2023

Conversation

rmachielse
Copy link

Problem

Resolves #231. We would like to use ruby-openai with Microsoft Azure OpenAI, however its endpoints and authentication headers are slightly different, making it impossible currently to use this gem.

Solution

This PR adds a configuration setting api_type, which can be set to :azure, similar to how https://github.com/openai/openai-python supports Microsoft Azure.

Alternatives

I'm aware of the changes being worked on in #150, it might be possible to create a subclass of OpenAI::Client instead once this has been implemented. I'm happy to rebase and refactor this PR once this is merged.

Additional context

  • Microsoft Azure currently only supports completions and embeddings, so these are the only endpoints implemented
  • A Microsoft Azure account is needed to rerecord the tests with VCR, and the account needs to have OpenAI access. Currently a form needs to be submitted for this. Also, a deployment has to be created for every model used in the tests, and the deployment name has to match the model name. Documentation should probably be written for this.

@rmachielse rmachielse force-pushed the feature/microsoft-azure branch from 1653394 to 026073d Compare April 11, 2023 15:10
@codeclimate
Copy link

codeclimate bot commented Apr 11, 2023

Code Climate has analyzed commit 026073d and detected 0 issues on this pull request.

View more on Code Climate.

@rmachielse
Copy link
Author

Now sure why circle is failing, it seems all the tests are missing the OPENAI_ACCESS_TOKEN

This was referenced May 9, 2023
@steffansluis steffansluis force-pushed the feature/microsoft-azure branch from fb2e672 to 4ece2ce Compare May 11, 2023 18:19
@steffansluis
Copy link
Contributor

steffansluis commented May 11, 2023

In cooperation with my colleague @rmachielse I've followed up on this. Initially I was looking at rebasing his changes, but I realized it would likely be easier to add the minimal required changes on top of main and slightly change the associated configuration changes.

In practice, the only differences that are really needed are to the uri and the headers, so I only ported those changes and added some tests for them. This works with the following (or similar) configuration (the example specifies config/initializers/openai.rb):

require 'openai'

OpenAI.configure do |config|
  config.access_token = Rails.application.config.app[:azure_openai_api_key]
  config.uri_base = "#{Rails.application.config.app[:azure_openai_uri]}/openai/deployments/gpt-35-turbo"
  config.api_type = :azure
  config.api_version = '2023-03-15-preview'
end

where azure_openai_uri is something like https://custom-domain.openai.azure.com.

In the process I realized there was an inconsistency with /es in the constructed URI, which I tried to fix with URI.join, however this resulted in some code constructions I didn't like so I ended up removing that part of the logic again. That unfortunately leads to the inconsistency where when using Azure, no trailing / is expected in the uri_base but when using the regular API a trailing / is expected and necessary.

I think the being able to use Azure outweighs this inconsistency so I went ahead with the changes like this. If you want I can see if I can come up with a clean way of addressing this. My suggestion would be to do it in a separate PR though, since the behavior was already problematic (no trailing / results in problems with the current logic) and being able to use the gem with Azure outweighs delaying this to fix it in my opinion.

Curious to hear what you think @alexrudall !

@steffansluis steffansluis force-pushed the feature/microsoft-azure branch 5 times, most recently from 516b2c3 to a98cc7a Compare May 11, 2023 18:40
@steffansluis steffansluis force-pushed the feature/microsoft-azure branch from a98cc7a to 15e2ceb Compare May 11, 2023 18:41
@rmachielse
Copy link
Author

@alexrudall what do you think? These minimal changes should make it quite straight forward to at least allow usage of this gem with Microsoft Azure.

@timrwilliams
Copy link

I'd also like to see this merged. I think more and more people will be transitioning across to the Azure version, especially within corporate environments so this would be very helpful.

@IanRAnderson
Copy link

I'm also hoping for this functionality. Been using this Gem for developing a proof of concept for a new feature, but my organization is moving to Azure OpenAI for a variety of reasons, so the ability to continue to use this gem with Azure would be great.

@eichert12
Copy link

We're also moving to an Azure based version of OpenAI so would love to see Azure support in this gem.

@rmachielse
Copy link
Author

@alexrudall seems like merging this PR would make a lot of people happy 😄 note that we simplified the implementation to the minimum necessary changes!

@alexrudall alexrudall merged commit 58fa9c0 into alexrudall:main Jun 20, 2023
@alexrudall
Copy link
Owner

Released in v4.2!

Thanks for your patience on this all! @rmachielse and @steffansluis thanks a lot for your work. @steffansluis I fixed the trailing / issue. I think.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Microsoft Azure support
6 participants