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 native azure openai configuration #4

Merged
merged 1 commit into from
Aug 15, 2024
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
56 changes: 36 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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://<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",
Expand Down Expand Up @@ -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.
Expand All @@ -100,13 +115,13 @@ export OPENAI_API_KEY=your-api-key

The following key bindings are available for use with `avante.nvim`:

- <kbd>Leader</kbd><kbd>a</kbd><kbd>a</kbd> — show sidebar
- <kbd>c</kbd><kbd>o</kbd> — choose ours
- <kbd>c</kbd><kbd>t</kbd> — choose theirs
- <kbd>c</kbd><kbd>b</kbd> — choose both
- <kbd>c</kbd><kbd>0</kbd> — choose none
- <kbd>]</kbd><kbd>x</kbd> — move to previous conflict
- <kbd>[</kbd><kbd>x</kbd> — move to next conflict
- `<kbd>Leader</kbd><kbd>a</kbd><kbd>a</kbd>` — show sidebar
- `<kbd>c</kbd><kbd>o</kbd>` — choose ours
- `<kbd>c</kbd><kbd>t</kbd>` — choose theirs
- `<kbd>c</kbd><kbd>b</kbd>` — choose both
- `<kbd>c</kbd><kbd>0</kbd>` — choose none
- `<kbd>]</kbd><kbd>x</kbd>` — move to previous conflict
- `<kbd>[</kbd><kbd>x</kbd>` — move to next conflict

## Roadmap

Expand All @@ -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:
Expand Down
67 changes: 49 additions & 18 deletions lua/avante/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -654,6 +678,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",
Expand Down