Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Azure OpenAI details are not recognized #150

Closed
marton-eifert opened this issue Oct 23, 2023 · 2 comments
Closed

Azure OpenAI details are not recognized #150

marton-eifert opened this issue Oct 23, 2023 · 2 comments

Comments

@marton-eifert
Copy link

marton-eifert commented Oct 23, 2023

Hello, I was trying out the 0.3.0 version on my local (Windows) laptop using R 4.3.1 in RStudio.
Since I have an Azure OpenAI Services deployment running gpt-35-turbo, I wanted to set the env variables according to descriptions in the README.
I changed my (project scope and user scope) .Renviron file accordingly and it contains the following lines now (entries masked):

AZURE_OPENAI_TASK="azure"
AZURE_OPENAI_ENDPOINT="<my_endpoint_url>"
AZURE_OPENAI_DEPLOYMENT_NAME="<my_deloyment_name>"
AZURE_OPENAI_KEY="<my_api_endpoint_key>"
AZURE_OPENAI_API_VERSION="2023-07-01-preview"

However, it does not recognize the variables, it still asks me for the OPENAI_API_KEY environment variable which I did not provide because I don't have one and I do want to use my Azure endpoint.

One other thing that is missing in the README --> What is the "TASK" supposed to be? I only found something called AZURE_OPENAI_TYPE in the context of the Microsoft Quickstart guide and "openai Python library" -- which I, by the way, have successfully set up with my AZURE_OPENAI_xxx environment variables elsewhere already --.
That one is referring to the API_TYPE which in the case of Azure OpenAI Services should take the value "azure" as above.
So is TYPE = TASK? Or is it something else?

@chendakeng
Copy link

Yes, I have the same problems as you. I am googling all over and cannot find anything related to "task". I hope the team can give us a clear instruction with examples of what to fill in for these environment variables.

@calderonsamuel
Copy link
Collaborator

Inspecting the source code shows that the base request for the azure openai method is defined as the following.

request_base_azure_openai <-
  function(task = Sys.getenv("AZURE_OPENAI_TASK"),
           base_url = Sys.getenv("AZURE_OPENAI_ENDPOINT"),
           deployment_name = Sys.getenv("AZURE_OPENAI_DEPLOYMENT_NAME"),
           token = Sys.getenv("AZURE_OPENAI_KEY"),
           api_version = Sys.getenv("AZURE_OPENAI_API_VERSION")
  ) {
    httr2::request(base_url) %>%
      httr2::req_url_path_append("openai/deployments") %>%
      httr2::req_url_path_append(deployment_name) %>%
      httr2::req_url_path_append(task) %>%
      httr2::req_url_path_append(api_version) %>%
      httr2::req_headers("api-key" = token,
                         "Content-Type" = "application/json") %>%
      httr2::req_method("POST")
  }

So this is how the request is translated. The task gets appended to path after the model name.

request_base_azure_openai(
  base_url = "https://www.example.com", 
  deployment_name = "gpt-35-turbo-instruct", 
  task = "completions", 
  api_version = "2023-05-15",
  token = "MY_API_KEY"
)
#> <httr2_request>
#> POST
#> https://www.example.com/openai/deployments/gpt-35-turbo-instruct/completions/2023-05-15
#> Headers:
#> • api-key: 'MY_API_KEY'
#> • Content-Type: 'application/json'
#> Body: empty

By looking at the REST documentation, it looks to me like you can just set it as "completions".

AZURE_OPENAI_TASK="completions"

The same documentation tells us that there is still a small bug that will prevent it from working. This should be how the base request is defined, with the api version as a url query parameter:

request_base_azure_openai <-
  function(task = Sys.getenv("AZURE_OPENAI_TASK"),
           base_url = Sys.getenv("AZURE_OPENAI_ENDPOINT"),
           deployment_name = Sys.getenv("AZURE_OPENAI_DEPLOYMENT_NAME"),
           token = Sys.getenv("AZURE_OPENAI_KEY"),
           api_version = Sys.getenv("AZURE_OPENAI_API_VERSION")
  ) {
    httr2::request(base_url) %>%
      httr2::req_url_path_append("openai/deployments") %>%
      httr2::req_url_path_append(deployment_name) %>%
      httr2::req_url_path_append(task) %>%
      httr2::req_url_query("api-version" = api_version) %>%
      httr2::req_headers("api-key" = token,
                         "Content-Type" = "application/json") %>%
      httr2::req_method("POST")
  }

request_base_azure_openai(
  base_url = "https://www.example.com", 
  deployment_name = "gpt-35-turbo-instruct", 
  task = "completions", 
  api_version = "2023-05-15",
  token = "MY_API_KEY"
)
#> <httr2_request>
#> POST
#> https://www.example.com/openai/deployments/gpt-35-turbo-instruct/completions?api-version=2023-05-15
#> Headers:
#> • api-key: 'MY_API_KEY'
#> • Content-Type: 'application/json'
#> Body: empty

I'll make a PR about this

Created on 2023-11-02 with reprex v2.0.2

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

No branches or pull requests

3 participants