Skip to content

Commit 66da297

Browse files
authored
Merge pull request #113 from brunocroh/main
feat: add support to deepseek as a provider
2 parents 2ce83df + 086ec4e commit 66da297

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

lua/parrot/config.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ local defaults = {
172172
command = { temperature = 1.1, top_p = 1 },
173173
},
174174
},
175+
deepseek = {
176+
api_key = "",
177+
endpoint = "https://api.deepseek.com/chat/completions",
178+
topic_prompt = topic_prompt,
179+
topic = {
180+
model = "deepseek-chat",
181+
params = { max_tokens = 64 },
182+
},
183+
params = {
184+
chat = { temperature = 1.1, top_p = 1 },
185+
command = { temperature = 1.1, top_p = 1 },
186+
},
187+
},
175188
custom = {
176189
style = "openai",
177190
api_key = "",

lua/parrot/provider/deepseek.lua

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
local OpenAI = require("parrot.provider.openai")
2+
local utils = require("parrot.utils")
3+
local Job = require("plenary.job")
4+
5+
local DeepSeek = setmetatable({}, { __index = OpenAI })
6+
DeepSeek.__index = DeepSeek
7+
8+
-- Available API parameters for DeepSeek
9+
-- https://api-docs.deepseek.com/api/deepseek-api
10+
local AVAILABLE_API_PARAMETERS = {
11+
-- required
12+
messages = true,
13+
model = true,
14+
-- optional
15+
frequency_penalty = true,
16+
max_tokens = true,
17+
presence_penalty = true,
18+
response_format = true,
19+
seed = true,
20+
stop = true,
21+
stream = true,
22+
stream_options = true,
23+
temperature = true,
24+
tool_choice = true,
25+
tools = true,
26+
logprobs = true,
27+
top_logprobs = true,
28+
top_p = true,
29+
user = true,
30+
}
31+
32+
function DeepSeek:new(endpoint, api_key)
33+
local instance = OpenAI.new(self, endpoint, api_key)
34+
instance.name = "deepseek"
35+
return setmetatable(instance, self)
36+
end
37+
38+
-- Preprocesses the payload before sending to the API
39+
---@param payload table
40+
---@return table
41+
function DeepSeek:preprocess_payload(payload)
42+
for _, message in ipairs(payload.messages) do
43+
message.content = message.content:gsub("^%s*(.-)%s*$", "%1")
44+
end
45+
return utils.filter_payload_parameters(AVAILABLE_API_PARAMETERS, payload)
46+
end
47+
48+
-- Returns the list of available models
49+
---@param online boolean
50+
---@return string[]
51+
function DeepSeek:get_available_models(online)
52+
local ids = {
53+
"deepseek-chat",
54+
"deepseek-reasoner",
55+
}
56+
if online and self:verify() then
57+
local job = Job:new({
58+
command = "curl",
59+
args = {
60+
"https://api.deepseek.com/models",
61+
"-H",
62+
"Authorization: Bearer " .. self.api_key,
63+
},
64+
on_exit = function(job)
65+
local parsed_response = utils.parse_raw_response(job:result())
66+
self:process_onexit(parsed_response)
67+
ids = {}
68+
local success, decoded = pcall(vim.json.decode, parsed_response)
69+
if success and decoded.models then
70+
for _, item in ipairs(decoded.models) do
71+
table.insert(ids, item.id)
72+
end
73+
end
74+
return ids
75+
end,
76+
})
77+
job:start()
78+
job:wait()
79+
end
80+
return ids
81+
end
82+
83+
return DeepSeek

lua/parrot/provider/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local OpenAI = require("parrot.provider.openai")
88
local Perplexity = require("parrot.provider.perplexity")
99
local GitHub = require("parrot.provider.github")
1010
local xAI = require("parrot.provider.xai")
11+
local DeepSeek = require("parrot.provider.deepseek")
1112
local logger = require("parrot.logger")
1213

1314
local M = {}
@@ -28,6 +29,7 @@ M.init_provider = function(prov_name, endpoint, api_key, style, models)
2829
openai = OpenAI,
2930
pplx = Perplexity,
3031
xai = xAI,
32+
deepseek = DeepSeek,
3133
}
3234

3335
if providers[prov_name] then

0 commit comments

Comments
 (0)