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

feat: add support for Azure OpenAI deployment #293

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ Custom OpenAI API host with the configuration option `api_host_cmd` or
environment variable called `$OPENAI_API_HOST`. It's useful if you can't access
OpenAI directly

For Azure deployments, you also need to set environment variables
`$OPENAI_API_TYPE` to `azure`, `$OPENAI_API_BASE` to your own resource URL,
e.g. `https://{your-resource-name}.openai.azure.com`, and `$OPENAI_API_AZURE_ENGINE`
to your deployment ID. Optionally, if you need a different API version,
set `$OPENAI_API_AZURE_VERSION` as well. Note that edit models have been deprecated
so they might not work.

```lua
-- Packer
use({
Expand Down
37 changes: 35 additions & 2 deletions lua/chatgpt/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Api.chat_completions(custom_params, cb, should_stop)
"-H",
"Content-Type: application/json",
"-H",
"Authorization: Bearer " .. Api.OPENAI_API_KEY,
Api.AUTHORIZATION_HEADER,
"-d",
vim.json.encode(params),
},
Expand Down Expand Up @@ -103,7 +103,7 @@ function Api.make_call(url, params, cb)
"-H",
"Content-Type: application/json",
"-H",
"Authorization: Bearer " .. Api.OPENAI_API_KEY,
Api.AUTHORIZATION_HEADER,
"-d",
"@" .. TMP_MSG_FILENAME,
},
Expand Down Expand Up @@ -222,6 +222,31 @@ local function loadApiKey(envName, configName, optionName, callback, defaultValu
end
end

local function loadAzureConfigs()
loadApiKey("OPENAI_API_BASE", "OPENAI_API_BASE", "azure_api_base_cmd", function(value)
Api.OPENAI_API_BASE = value
end)
loadApiKey("OPENAI_API_AZURE_ENGINE", "OPENAI_API_AZURE_ENGINE", "azure_api_engine_cmd", function(value)
Api.OPENAI_API_AZURE_ENGINE = value
end)
loadApiHost("OPENAI_API_AZURE_VERSION", "OPENAI_API_AZURE_VERSION", "azure_api_version_cmd", function(value)
Api.OPENAI_API_AZURE_VERSION = value
end, "2023-05-15")

if Api["OPENAI_API_BASE"] and Api["OPENAI_API_AZURE_ENGINE"] then
Api.COMPLETIONS_URL = Api.OPENAI_API_BASE
.. "/openai/deployments/"
.. Api.OPENAI_API_AZURE_ENGINE
.. "/completions?api-version="
.. Api.OPENAI_API_AZURE_VERSION
Api.CHAT_COMPLETIONS_URL = Api.OPENAI_API_BASE
.. "/openai/deployments/"
.. Api.OPENAI_API_AZURE_ENGINE
.. "/chat/completions?api-version="
.. Api.OPENAI_API_AZURE_VERSION
end
end

function Api.setup()
loadApiHost("OPENAI_API_HOST", "OPENAI_API_HOST", "api_host_cmd", function(value)
Api.OPENAI_API_HOST = value
Expand All @@ -233,6 +258,14 @@ function Api.setup()
loadApiKey("OPENAI_API_KEY", "OPENAI_API_KEY", "api_key_cmd", function(value)
Api.OPENAI_API_KEY = value
end)

loadConfigFromEnv("OPENAI_API_TYPE", "OPENAI_API_TYPE")
if Api["OPENAI_API_TYPE"] == "azure" then
loadAzureConfigs()
Api.AUTHORIZATION_HEADER = "api-key: " .. Api.OPENAI_API_KEY
else
Api.AUTHORIZATION_HEADER = "Authorization: Bearer " .. Api.OPENAI_API_KEY
end
end

function Api.exec(cmd, args, on_stdout_chunk, on_complete, should_stop, on_stop)
Expand Down