To add a new provider for the open-source DeepSeek V3 model from HuggingFace and make it the default provider in the avante.nvim plugin, follow the steps below:
Steps to Modify avante.nvim
1. Fork the Package
Fork the repository to your personal or organization GitHub account.
Clone the forked repository locally:
bash
git clone https://github.com/<your-username>/avante.nvim.git
cd avante.nvim
2. Add the DeepSeek V3 Provider
Create a New Provider Module
Create a new file for the DeepSeek V3 provider in ./repos/avante.nvim/lua/avante/providers/deepseek.lua:
Implement the provider logic in deepseek.lua. Below is an example implementation based on the structure of other existing providers (e.g., openai.lua):
lua
local Utils = require("avante.utils")
local Config = require("avante.config")
local P = require("avante.providers")
---@class AvanteProviderFunctor
local M = {}
M.api_key_name = nil -- No API key required for HuggingFace's open-source models
M.role_map = {
user = "user",
assistant = "assistant",
}
M.parse_messages = function(opts)
local messages = {}
table.insert(messages, { role = "system", content = opts.system_prompt })
vim
.iter(opts.messages)
:each(function(msg) table.insert(messages, { role = M.role_map[msg.role], content = msg.content }) end)
return messages
end
M.parse_response = function(data_stream, _, opts)
---@type table
local json = vim.json.decode(data_stream)
if json and json.generated_text then
opts.on_chunk(json.generated_text)
vim.schedule(function() opts.on_complete(nil) end)
end
end
M.parse_curl_args = function(_, code_opts)
local body = {
inputs = table.concat(vim.tbl_map(function(msg) return msg.content end, code_opts.messages), "\n"),
parameters = {
max_new_tokens = 1000, -- Adjust token limit as needed
temperature = 0.7, -- Adjust as needed
},
}
return {
url = "https://api-inference.huggingface.co/models/DeepSeek/DeepSeek-V3",
headers = {
["Content-Type"] = "application/json",
},
body = vim.json.encode(body),
}
end
return M
Key Notes:
The DeepSeek V3 model is hosted on HuggingFace, so the url is set to the HuggingFace API endpoint: https://api-inference.huggingface.co/models/DeepSeek/DeepSeek-V3.
Adjust the parameters in the request body, such as max_new_tokens and temperature, based on your requirements.
3. Register the DeepSeek Provider
Open ./repos/avante.nvim/lua/avante/providers/init.lua.
Add the new provider to the list of available providers:
lua
local M = {}
-- Add your new provider
M.deepseek = require("avante.providers.deepseek")
return M
4. Update the Default Provider
Open ./repos/avante.nvim/lua/avante/config.lua.
Change the provider field in the M.defaults table to "deepseek":
lua
M.defaults = {
provider = "deepseek", -- Set the default provider to DeepSeek
tokenizer = "hf", -- Use HuggingFace tokenizers for this provider
deepseek = {
endpoint = "https://api-inference.huggingface.co/models/DeepSeek/DeepSeek-V3",
model = "DeepSeek-V3",
timeout = 30000, -- Timeout in milliseconds
temperature = 0.7,
max_tokens = 1000,
},
-- Other default configurations ...
}
5. Test the Provider
Open Neovim and test the new provider:
bash
nvim
Trigger the avante.nvim commands (e.g., Ask or Chat) to ensure it works with the DeepSeek V3 model.
6. Commit and Push Changes
Add and commit your changes:
bash
git add .
git commit -m "Add DeepSeek V3 provider and set it as default"
Push the changes to your fork:
bash
git push origin main
Optionally, open a pull request to merge your changes into the original repository.
Summary of Changes
Added a new provider file: ./repos/avante.nvim/lua/avante/providers/deepseek.lua.
Registered the provider in ./repos/avante.nvim/lua/avante/providers/init.lua.
Set DeepSeek as the default provider in ./repos/avante.nvim/lua/avante/config.lua.
This setup ensures that the DeepSeek V3 model from HuggingFace is integrated into avante.nvim and configured as the default provider.
To add support for the DeepSeek V3 tokenizer in avante.nvim, follow these steps. This involves integrating the HuggingFace tokenizer library (or another tokenizer implementation) and making it available for DeepSeek V3 in the plugin.
Steps to Add DeepSeek V3 Tokenizer Support
1. Update the Tokenizer System
Open ./repos/avante.nvim/lua/avante/tokenizers.lua.
Add support for the DeepSeek V3 tokenizer. Modify the _init_tokenizers_lib function to include special handling for DeepSeek V3:
lua
local Utils = require("avante.utils")
---@class AvanteTokenizer
---@field from_pretrained fun(model: string): nil
---@field encode fun(string): integer[]
local tokenizers = nil
---@type "gpt-4o" | "DeepSeek-V3" | string
local current_model = "gpt-4o" -- Default tokenizer model
local M = {}
---Initialize the tokenizer library (e.g., HuggingFace tokenizers)
---@param model "gpt-4o" | "DeepSeek-V3" | string
---@return AvanteTokenizer|nil
M._init_tokenizers_lib = function(model)
if tokenizers ~= nil then return tokenizers end
-- Load the HuggingFace tokenizer library
local ok, core = pcall(require, "avante_tokenizers")
if not ok then return nil end
---@cast core AvanteTokenizer
tokenizers = core
-- Initialize the tokenizer based on the model
if model == "DeepSeek-V3" then
tokenizers.from_pretrained("DeepSeek/DeepSeek-V3")
else
tokenizers.from_pretrained(model)
end
return tokenizers
end
---Set up the tokenizer for a specific model
---@param model "gpt-4o" | "DeepSeek-V3" | string
---@param warning? [Redacted Person]
M.setup = function(model, warning)
current_model = model
warning = warning or true
vim.defer_fn(function() M._init_tokenizers_lib(model) end, 1000)
if warning then
local HF_TOKEN = os.getenv("HF_TOKEN")
if HF_TOKEN == nil and model ~= "gpt-4o" then
Utils.warn(
"Please set HF_TOKEN environment variable to use HuggingFace tokenizer for " .. model,
{ once = true }
)
end
end
end
---Check if the tokenizer is available
M.available = function() return M._init_tokenizers_lib(current_model) ~= nil end
---Encode a prompt into tokens
---@param prompt string
M.encode = function(prompt)
if not M.available() then return nil end
if not prompt or prompt == "" then return nil end
if type(prompt) ~= "string" then error("Prompt is not type string", 2) end
return tokenizers.encode(prompt)
end
---Count the number of tokens in a prompt
---@param prompt string
M.count = function(prompt)
if not M.available() then return math.ceil(#prompt * 0.5) end
local tokens = M.encode(prompt)
if not tokens then return 0 end
return #tokens
end
return M
2. Register the Tokenizer for DeepSeek V3
Open ./repos/avante.nvim/lua/avante/config.lua.
Update the M.defaults table to set the tokenizer for DeepSeek V3:
lua
M.defaults = {
provider = "deepseek", -- Default provider
tokenizer = "hf", -- Use HuggingFace tokenizer for DeepSeek V3
deepseek = {
endpoint = "https://api-inference.huggingface.co/models/DeepSeek/DeepSeek-V3",
model = "DeepSeek-V3",
timeout = 30000, -- Timeout in milliseconds
temperature = 0.7,
max_tokens = 1000,
},
-- Other default configurations...
}
Ensure the tokenizer field is set to "hf" because HuggingFace tokenizers will be used for DeepSeek V3.
3. Ensure DeepSeek V3 Uses the Tokenizer
Open ./repos/avante.nvim/lua/avante/providers/deepseek.lua.
Modify parse_curl_args to include the token count using the tokenizer:
lua
local Utils = require("avante.utils")
local Tokenizers = require("avante.tokenizers")
M.parse_curl_args = function(_, code_opts)
local body = {
inputs = table.concat(vim.tbl_map(function(msg) return msg.content end, code_opts.messages), "\n"),
parameters = {
max_new_tokens = 1000, -- Adjust token limit as needed
temperature = 0.7, -- Adjust as needed
},
}
-- Token counting
local token_count = Tokenizers.count(body.inputs)
Utils.debug("Token count for DeepSeek V3 request:", token_count)
return {
url = "https://api-inference.huggingface.co/models/DeepSeek/DeepSeek-V3",
headers = {
["Content-Type"] = "application/json",
},
body = vim.json.encode(body),
}
end
This ensures that the DeepSeek V3 provider uses the tokenizer to count tokens before sending requests.
4. Test the Tokenizer Integration
Test the tokenizer directly with the following code in Neovim:
lua
local Tokenizers = require("avante.tokenizers")
-- Set up the DeepSeek V3 tokenizer
Tokenizers.setup("DeepSeek-V3")
-- Test token encoding
local prompt = "DeepSeek V3 is a powerful open-source model."
print(Tokenizers.encode(prompt))
-- Test token counting
print(Tokenizers.count(prompt))
Run :luafile % in Neovim to see the token encoding and count for the given prompt.
5. Optional: Add HuggingFace Tokenizer Dependency
If the HuggingFace tokenizer library isn’t already available in the avante.nvim plugin, you may need to install it as a Lua dependency:
Add it to the dependencies in your fork's README.md or installation instructions:
lua
-- Install avante.nvim with the necessary tokenizer dependency
use {
"your-username/avante.nvim",
requires = {
"huggingface/tokenizers"
}
}
Alternatively, ensure that the avante_tokenizers module is implemented in your local environment or available as a dependency.
Summary of Changes
Updated tokenizers.lua to add support for the DeepSeek V3 tokenizer using HuggingFace's from_pretrained.
Registered DeepSeek V3 as a tokenizer in config.lua.
Integrated token counting and encoding into the DeepSeek V3 provider (deepseek.lua).
Tested the tokenizer integration using manual testing in Neovim.
You’ve now added full tokenizer support for DeepSeek V3! 🎉