Skip to content

request_openai removes the query parameter api-version for an AzureProvider #63

@pabvald

Description

@pabvald

Cc: @svilupp

While developing the support for Azure OpenAI in PromptingTools.jl, I came across the following issue. If I use the OpenAI.create_chat with an AzureProvider the ?api-version=2023-03-15-preview is removed from the URL:

julia> provider.base_url
"https://<resource-name>.openai.azure.com/openai/deployments/gpt-4o-2"

julia> OpenAI.build_url(provider, "chat/completions")
"https://<resource-name>.openai.azure.com/openai/deployments/gpt-4o-2/chat/completions?api-version=2023-03-15-preview"

julia> OpenAI.create_chat(provider, model, messages)
ERROR: HTTP.Exceptions.StatusError(404, "POST", "/openai/deployments/gpt-4o-2/chat/completions", HTTP.Messages.Response:
"""
HTTP/1.1 404 Not Found
Content-Length: 56
Content-Type: application/json
apim-request-id: 5236bf72-58a1-4440-95aa-eb5220891959
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
Date: Tue, 24 Sep 2024 07:38:29 GMT

{"error":{"code":"404","message": "Resource not found"}}""")
...

See how even though the result of OpenAI.build_url(provider, "chat/completions") includes the query parameter:

julia> OpenAI.build_url(provider, "chat/completions")
"https://<resource-name>.openai.azure.com/openai/deployments/gpt-4o-2/chat/completions?api-version=2023-03-15-preview"

the URL used in the request doesn't:

ERROR: HTTP.Exceptions.StatusError(404, "POST", "/openai/deployments/gpt-4o-2/chat/completions", 

To get the correct URL, I need to use OpenAI.openai_request pasing the argument query = Dict("api-version" => provider.api_version):

julia> OpenAI.openai_request(
               "/chat/completions",
               provider;
               method = "POST",
               http_kwargs = NamedTuple(),
               messages = messages,
               query = Dict("api-version" => provider.api_version),
               streamcallback = nothing
           )
OpenAIResponse{JSON3.Object{Vector{UInt8}, Vector{UInt64}}}(200, {
              "choices": [
                           {
                              "finish_reason": "stop",
                                      "index": 0,
                                    "message": {
                                                  "content": "I'm just a computer program, so I don't have feelings, but I'm here and ready to help you! How can I assist you today?",
                                                     "role": "assistant"
                                               }
                           }
                         ],
              "created": 1727163808,
                   "id": "chatcmpl-AAuUik0iKDBdWfFyZBy6UZcoAFSWl",
                "model": "gpt-4o-2024-05-13",
               "object": "chat.completion",
   "system_fingerprint": "fp_67802d9a6d",
                "usage": {
                            "completion_tokens": 28,
                                "prompt_tokens": 12,
                                 "total_tokens": 40
                         }
})

The issue seems to come from the HTTP.request function, which removes the query parameters from the URL and only includes them if they are provided through the query argument. The issue doesn't occur when using HTTP.post.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions