|
| 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 |
0 commit comments