carrier.nvim is a ChatGPT plugin for Neovim that allows you to chat with ChatGPT automatically provided with the context in your editor.
Some pieces of context that we give to the AI:
- The contents of recently-edited open buffers
- The contents of diagnostic information at your cursor
This makes the AI much more useful for day-to-day programming tasks than base ChatGPT because you don't have to self-manage context or copy and paste to a web interface.
- Put your
OPENAI_API_KEY
as an environment variable
export OPENAI_API_KEY=""
-
Have curl installed on your machine
-
Install
plenary.nvim
andcarrier.nvim
using your package manager:
Plug 'nvim-lua/plenary.nvim'
Plug 'CamdenClark/carrier.nvim'
{
"CamdenClark/carrier.nvim",
dependencies = {
"nvim-lua/plenary.nvim",
},
keys = {
{ "<leader>ao", "<cmd>CarrierLogOpen<cr>", desc = "Open Carrier log" },
{ "<leader>as", "<cmd>CarrierSendMessage<cr>", desc = "Send message" },
{ "<leader>ak", "<cmd>CarrierStopMessage<cr>", desc = "Stop current message" },
},
opts = {
model = "gpt-4-1106-preview",
},
}
Usage of carrier is extremely simple: call :CarrierSendMessage
.
If called when the carrier log
buffer is not open, you will be prompted for a message.
Then, carrier log
buffer will open with your user message in it formatted like so:
# User
How do I do binary search in Lua?
# Assistant
...
The response will be streamed into the buffer.
If you have a # User
message already specified in the carrier log buffer, :CarrierSendMessage
will immediately send that message to the AI.
If you call :CarrierSendMessage hello world
it will add hello world
to the drafted
user message and send it to the AI.
It's easy to stop a message in progress: call :CarrierStopMessage
.
:CarrierOpen
functions opens the carrier chat log.
Split opens in a horizontal split, while VSplit opens in a vertical split.
:CarrierOpen
:CarrierOpenSplit
:CarrierOpenVSplit
If you want to use Carrier with a different model in OpenAI, call setup with the model:
require('carrier').setup({
-- ...
model = "gpt-4"
})
To change on the fly, call :CarrierSwitchModel gpt-4
Carrier supports configuring the URL and headers with a different endpoint that shares API compatibility (IE: Azure OpenAI) with OpenAI, here's a reference implementation:
require('carrier').setup({
-- should be like "$AZURE_OPENAI_ENDPOINT/openai/deployments/$AZURE_OPENAI_DEPLOYMENT_NAME/chat/completions?api-version=2023-07-01-preview"
url = vim.env.AZURE_OPENAI_GPT4_URL,
headers = {
Api_Key = vim.env.AZURE_OPENAI_GPT4_KEY,
Content_Type = "application/json"
}
})
where you put the values for AZURE_OPENAI_GPT4_URL
and AZURE_OPENAI_GPT4_KEY
in the environment.
If you want to be able to switch URLs based on model, you should make some lua functions in your init.lua that are bound to re-call setup with the updated URL and API key.
Carrier supports configuring a callback function that is called when a response from the assistant finishes streaming.
require('carrier').setup({
on_complete = function() print("foo") end
})
Running tests requires plenary.nvim to be checked out in the parent directory of this repository. You can then run:
just test
or, more verbose:
nvim --headless --noplugin -u tests/minimal.vim -c "PlenaryBustedDirectory tests/ {minimal_init = 'tests/minimal.vim'}"
Or if you want to run a single test file:
just test chat_spec.lua
nvim --headless --noplugin -u tests/minimal.vim -c "PlenaryBustedDirectory tests/path_to_file.lua {minimal_init = 'tests/minimal.vim'}"
Read the nvim-lua-guide for more information on developing neovim plugins.