alexrudall/ruby-openai

Azure client Faraday errors when trying to use client.completions

Opened this issue · 4 comments

Describe the bug
First off, thanks for creating this and all the work you put into it. This is a fantastic resource. I can't figure out why I can get everything working as expected with an Azure OpenAI endpoint except the client.completions calls.
Assistants, vector stores , etc all work as expected with the same azure endpoint. It's just the completions that are not working

To Reproduce

#./Myai.rb
class MyAi
    attr_reader :client
    def initialize()
        OpenAI.configure do |config|
            config.access_token = ENV["xxxx"]
            config.uri_base = ENV["yyyy"]
            config.api_type = :azure
            config.api_version = "2024-10-01-preview"
            config.log_errors = true
         end
     @client = OpenAI::Client.new()
end

irb> require_relative "Myai"
irb> az = MyAi.new
irb> client = az.client
irb> comp = client.completions(parameters: {model: "gpt-4o",prompt: "How are you today"})
OpenAI HTTP Error (spotted in ruby-openai 7.1.0): {"error"=>{"code"=>"404", "message"=>"Resource not found"}}
....

irb> client.vector_stores.list
client.vector_stores.list
=> 
{"object"=>"list",
 "data"=>
  [{"id"=>"anIdhere",
    "object"=>"vector_store",
    "name"=>"MyVectorStore",
    "status"=>"completed",
    "usage_bytes"=>1394703,
    "created_at"=>1729186048,
    "file_counts"=>{"in_progress"=>0, "completed"=>99, "failed"=>4, "cancelled"=>0, "total"=>103},
    "metadata"=>{},
    "expires_after"=>nil,
    "expires_at"=>nil,
    "last_active_at"=>1729198838},
....

Expected behavior
Expect to get a completion response back. I have an azure deployment called "gpt-4o"

Desktop (please complete the following information):

  • OS: M1 Max Mac Studio "Sonoma 14.7.1"

Additional context
Files, vector stores, assistants all work as expected. It's just completions where I get the Faraday 404.

Figured it out. The problem is MS has a different pattern for URLs for completions. The format of the url is:

<AzureEndpoint>/deployments/<deploymentId>/chat/completions?<api-version>

deploymentID is just the name of the deployment you created in Azure Openai. It can equal the the underlying model name but it doesn't have to to e.g.

<AzureEndpoint>/deployments/gpt-4o/chat/completions?<apiVersion>
or
<AzureEndpoint>/deployments/MyDeploymentName/chat/completions?<apiVersion>

ruby-openai is sending

<AzureEndpoint>/completions?{{api-version}}

Poking around and found the solution. If you need to use completions api in azure then you need to configure the base_uri as shown below AND you have to call the completions api as shown in the examples for chat with messages parameter

https://<yourAzureEndpoint>/openai/deployments/<yourDeploymentName>/chat/

resp = client.completions(parameters: { model: "gpt-4o",messages:[{role: "system", content: "You are a helpful agent"},{role: "user", content: "Hello"}],temperature: 0.7} )

If you use the example for completions

 resp = client.completions( parameters: {model: "gpt-4o",prompt: "Once upon a time", max_tokens: 5})
OpenAI HTTP Error (spotted in ruby-openai 7.1.0): Unsupported data type

/Users/gbl2/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/faraday-2.12.0/lib/faraday/response/raise_error.rb:30:in `on_complete': the server responded with status 400 (Faraday::BadRequestError)

For using assistants, vector stores, files api the base_uri should be:

https://<yourAzureEndpoint>/openai/
agnyp commented

Thank you!

@gbleydon - thanks so much for this!