From d1c47601ef838aff23742ae0fa107ced9daac9c7 Mon Sep 17 00:00:00 2001 From: Wey Gu Date: Thu, 15 Aug 2024 11:54:15 +0800 Subject: [PATCH] feat: add azure openai support chore: also fixed markdown lint errors --- README.md | 56 +++++++++++++++++++++++-------------- lua/avante/init.lua | 67 +++++++++++++++++++++++++++++++++------------ 2 files changed, 85 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 1298e8826..206a37c4f 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,10 @@ **avante.nvim** is a Neovim plugin designed to emulate the behavior of the [Cursor](https://www.cursor.com) AI IDE, providing users with AI-driven code suggestions and the ability to apply these recommendations directly to their source files with minimal effort. - ⚠️⚠️ **WARNING: This plugin is still in a very early stage of development, so please be aware that the current code is very messy and unstable, and problems are likely to occur.** - https://github.com/user-attachments/assets/510e6270-b6cf-459d-9a2f-15b397d1fe53 - - ## Features - **AI-Powered Code Assistance**: Interact with AI to ask questions about your current code file and receive intelligent suggestions for improvement or modification. @@ -44,13 +40,20 @@ Default setup configuration: ```lua { - provider = "claude", -- openai, claude + provider = "claude", -- "claude" or "openai" or "azure" openai = { endpoint = "https://api.openai.com", model = "gpt-4o", temperature = 0, max_tokens = 4096, }, + azure = { + endpoint = "", -- Example: "https://.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", @@ -81,14 +84,26 @@ Default setup configuration: Given its early stage, `avante.nvim` currently supports the following basic functionalities: -1. Set `ANTHROPIC_API_KEY` environment variable: -```sh -export ANTHROPIC_API=your-api-key -``` -Or set `OPENAI_API_KEY` environment variable: -```sh -export OPENAI_API_KEY=your-api-key -``` +1. Set the appropriate API key as an environment variable: + + For Claude: + + ```sh + export ANTHROPIC_API_KEY=your-api-key + ``` + + For OpenAI: + + ```sh + export OPENAI_API_KEY=your-api-key + ``` + + For Azure OpenAI: + + ```sh + export AZURE_OPENAI_API_KEY=your-api-key + ``` + 2. Open a code file in Neovim. 3. Use the `:AvanteAsk` command to query the AI about the code. 4. Review the AI's suggestions. @@ -100,13 +115,13 @@ export OPENAI_API_KEY=your-api-key The following key bindings are available for use with `avante.nvim`: -- Leaderaa — show sidebar -- co — choose ours -- ct — choose theirs -- cb — choose both -- c0 — choose none -- ]x — move to previous conflict -- [x — move to next conflict +- `Leaderaa` — show sidebar +- `co` — choose ours +- `ct` — choose theirs +- `cb` — choose both +- `c0` — choose none +- `]x` — move to previous conflict +- `[x` — move to next conflict ## Roadmap @@ -130,6 +145,7 @@ Contributions to avante.nvim are welcome! If you're interested in helping out, p ## Development To set up the development environment: + 1. Install [StyLua](https://github.com/JohnnyMorganz/StyLua) for Lua code formatting. 2. Install [pre-commit](https://pre-commit.com) for managing and maintaining pre-commit hooks. 3. After cloning the repository, run the following command to set up pre-commit hooks: diff --git a/lua/avante/init.lua b/lua/avante/init.lua index 01350fedd..81ab9459e 100644 --- a/lua/avante/init.lua +++ b/lua/avante/init.lua @@ -265,12 +265,49 @@ local function call_openai_api_stream(prompt, original_content, on_chunk, on_com local user_prompt = user_prompt_tpl:gsub("${{question}}", prompt):gsub("${{code}}", original_content) - 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 @@ -295,21 +332,8 @@ local function call_openai_api_stream(prompt, original_content, on_chunk, on_com 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 @@ -654,6 +678,13 @@ M.config = { temperature = 0, max_tokens = 4096, }, + azure = { + endpoint = "", -- example: "https://.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",