Skip to content

Commit

Permalink
feat: Model selector (initial commit)
Browse files Browse the repository at this point in the history
  • Loading branch information
gptlang committed Jul 16, 2024
1 parent 6f143f2 commit 02d6e9a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
35 changes: 35 additions & 0 deletions lua/CopilotChat/copilot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
---@field save fun(self: CopilotChat.Copilot, name: string, path: string):nil
---@field load fun(self: CopilotChat.Copilot, name: string, path: string):table
---@field running fun(self: CopilotChat.Copilot):boolean
---@field select_model fun(self: CopilotChat.Copilot, callback: fun(string):nil):nil

local log = require('plenary.log')
local curl = require('plenary.curl')
Expand Down Expand Up @@ -499,6 +500,40 @@ function Copilot:ask(prompt, opts)
end, on_error)
end

--- Fetch & allow model selection
---@param callback fun(string):nil
function Copilot:select_model(callback)
local url = 'https://api.githubcopilot.com/models'
local headers = generate_headers(self.token.token, self.sessionid, self.machineid)
curl.get(url, {
headers = headers,
proxy = self.proxy,
insecure = self.allow_insecure,
on_error = function(err)
err = 'Failed to get response: ' .. vim.inspect(err)
log.error(err)
end,
callback = function(response)
if response.status ~= 200 then
local msg = 'Failed to fetch models: ' .. tostring(response.status)
log.error(msg)
return
end

local models = vim.json.decode(response.body)['data']
local selections = {}
for _, model in ipairs(models) do
table.insert(selections, model['version'])
end
vim.ui.select(selections, {
prompt = 'Select a model',
}, function(choice)
callback(choice)
end)
end,
})
end

--- Generate embeddings for the given inputs
---@param inputs table<CopilotChat.copilot.embed>: The inputs to embed
---@param opts CopilotChat.copilot.embed.opts: Options for the request
Expand Down
12 changes: 11 additions & 1 deletion lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,17 @@ function M.toggle(config, source)
end
end

-- @returns string
--- @returns string
function M.response()
return state.response
end

function M.select_model()
state.copilot:select_model(function(model)
state.config.model = model
end)
end

--- Ask a question to the Copilot model.
---@param prompt string
---@param config CopilotChat.config|CopilotChat.config.prompt|nil
Expand Down Expand Up @@ -859,6 +865,10 @@ function M.setup(config)
range = true,
})

vim.api.nvim_create_user_command('CopilotChatModels', function()
M.select_model()
end, { force = true })

vim.api.nvim_create_user_command('CopilotChatOpen', function()
M.open()
end, { force = true })
Expand Down

1 comment on commit 02d6e9a

@gptlang
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mention: #366

Please sign in to comment.