feat: add native azure openai configuration (#4)

chore: also fixed markdown lint errors
This commit is contained in:
Wey Gu
2024-08-15 13:44:25 +08:00
committed by GitHub
parent b1d7debd49
commit b3132f4e6f
2 changed files with 85 additions and 38 deletions

View File

@@ -298,12 +298,49 @@ local function call_openai_api_stream(question, code_lang, code_content, on_chun
.. code_content
.. "\n```"
local url = utils.trim_suffix(M.config.openai.endpoint, "/") .. "/v1/chat/completions"
local url, headers, body
if M.config.provider == "azure" then
url = M.config.openai.endpoint
api_key = os.getenv("AZURE_OPENAI_API_KEY") or os.getenv("OPENAI_API_KEY")
if not api_key then
error("Azure OpenAI API key is not set. Please set AZURE_OPENAI_API_KEY or OPENAI_API_KEY environment variable.")
end
url = M.config.azure.endpoint
.. "/openai/deployments/"
.. M.config.azure.deployment
.. "/chat/completions?api-version="
.. M.config.azure.api_version
headers = {
["Content-Type"] = "application/json",
["api-key"] = api_key,
}
body = {
messages = {
{ role = "system", content = system_prompt },
{ role = "user", content = user_prompt },
},
temperature = M.config.azure.temperature,
max_tokens = M.config.azure.max_tokens,
stream = true,
}
else
url = utils.trim_suffix(M.config.openai.endpoint, "/") .. "/v1/chat/completions"
headers = {
["Content-Type"] = "application/json",
["Authorization"] = "Bearer " .. api_key,
}
body = {
model = M.config.openai.model,
messages = {
{ role = "system", content = system_prompt },
{ role = "user", content = user_prompt },
},
temperature = M.config.openai.temperature,
max_tokens = M.config.openai.max_tokens,
stream = true,
}
end
print("Sending request to OpenAI API...")
print("Sending request to " .. (M.config.provider == "azure" and "Azure OpenAI" or "OpenAI") .. " API...")
curl.post(url, {
---@diagnostic disable-next-line: unused-local
@@ -334,21 +371,8 @@ local function call_openai_api_stream(question, code_lang, code_content, on_chun
end)
end
end,
headers = {
["Content-Type"] = "application/json",
["Authorization"] = "Bearer " .. api_key,
["api_key"] = api_key,
},
body = fn.json_encode({
model = M.config.openai.model,
messages = {
{ role = "system", content = system_prompt },
{ role = "user", content = user_prompt },
},
temperature = M.config.openai.temperature,
max_tokens = M.config.openai.max_tokens,
stream = true,
}),
headers = headers,
body = fn.json_encode(body),
})
end
@@ -711,6 +735,13 @@ M.config = {
temperature = 0,
max_tokens = 4096,
},
azure = {
endpoint = "", -- example: "https://<your-resource-name>.openai.azure.com"
deployment = "", -- Azure deployment name (e.g., "gpt-4o", "my-gpt-4o-deployment")
api_version = "2024-05-13",
temperature = 0,
max_tokens = 4096,
},
claude = {
endpoint = "https://api.anthropic.com",
model = "claude-3-5-sonnet-20240620",