Skip to content

Commit

Permalink
fix: do not fetch models from API on startup
Browse files Browse the repository at this point in the history
- only do it in later invoked provider selections
- fix health check
- add check if ollama is installed
  • Loading branch information
frankroeder committed Aug 8, 2024
1 parent b688bd6 commit 334bdac
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 73 deletions.
5 changes: 3 additions & 2 deletions lua/parrot/chat_handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -983,11 +983,12 @@ function ChatHandler:model(params)
local prov = self:get_provider(is_chat)
local model_name = string.gsub(params.args, "^%s*(.-)%s*$", "%1")
local has_fzf, fzf_lua = pcall(require, "fzf-lua")
local fetch_online = true

if model_name ~= "" then
self:switch_model(is_chat, model_name, prov)
elseif has_fzf then
fzf_lua.fzf_exec(prov:get_available_models(), {
fzf_lua.fzf_exec(prov:get_available_models(fetch_online), {
prompt = "Model selection ❯",
fzf_opts = self.options.fzf_lua_opts,
complete = function(selection)
Expand All @@ -1000,7 +1001,7 @@ function ChatHandler:model(params)
end,
})
else
vim.ui.select(prov:get_available_models(), {
vim.ui.select(prov:get_available_models(fetch_online), {
prompt = "Select your model:",
}, function(selected_model)
self:switch_model(is_chat, selected_model, prov)
Expand Down
3 changes: 2 additions & 1 deletion lua/parrot/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ function M.setup(opts)
local available_models = {}
for _, prov_name in ipairs(M.available_providers) do
local _prov = init_provider(prov_name, M.providers[prov_name].endpoint, M.providers[prov_name].api_key)
available_models[prov_name] = _prov:get_available_models()
-- do not make an API call on startup
available_models[prov_name] = _prov:get_available_models(false)
end
M.available_models = available_models

Expand Down
47 changes: 1 addition & 46 deletions lua/parrot/health.lua
Original file line number Diff line number Diff line change
@@ -1,47 +1,5 @@
local M = {}

local check_provider = function(parrot, prov_key)
local providers = parrot.providers

if prov_key ~= "ollama" then
local api_key = providers[prov_key].api_key
if api_key then
vim.health.ok(prov_key .. " api_key is set")
else
vim.health.error(
"require('parrot').setup({provider {.."
.. prov_key
.. "..: {api_key: ???}}) is not set: "
.. vim.inspect(api_key)
)
end
end

local endpoint = providers[prov_key].endpoint
if endpoint and string.match(endpoint, "%S") then
vim.health.ok(prov_key .. " endpoint is set")
else
vim.health.error(
"require('parrot').setup({provider {.."
.. prov_key
.. "..: {endpoint: ???}}) is not set: "
.. vim.inspect(endpoint)
)
end

local topic_prompt = providers[prov_key].topic_prompt
if topic_prompt and string.match(topic_prompt, "%S") then
vim.health.ok(prov_key .. " topic_prompt is set")
else
vim.health.error(
"require('parrot').setup({provider {.."
.. prov_key
.. "..: {topic_prompt: ???}}) is not set: "
.. vim.inspect(topic_prompt)
)
end
end

function M.check()
vim.health.start("parrot.nvim checks")

Expand All @@ -63,14 +21,11 @@ function M.check()
else
vim.health.ok("require('parrot') succeeded")

if parrot._setup_called then
if parrot.did_setup then
vim.health.ok("require('parrot').setup() has been called")
else
vim.health.error("require('parrot').setup() has not been called")
end
for _, name in ipairs(parrot._available_providers) do
check_provider(parrot, name)
end
end

for _, name in ipairs({ "curl", "grep", "rg", "ln" }) do
Expand Down
2 changes: 1 addition & 1 deletion lua/parrot/provider/anthropic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function Anthropic:preprocess_payload(payload)
if payload.messages[1].role == "system" then
local system_prompt = payload.messages[1].content
-- remove the first message that serves as the system prompt as anthropic
-- expects the system prompt to be part of the curl request and not the messages
-- expects the system prompt to be part of the curl request body and not the messages
table.remove(payload.messages, 1)
payload.system = system_prompt
end
Expand Down
4 changes: 2 additions & 2 deletions lua/parrot/provider/groq.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ function Groq:process_onexit(res)
end
end

function Groq:get_available_models()
if self:verify() then
function Groq:get_available_models(online)
if online and self:verify() then
Job:new({
command = "curl",
args = {
Expand Down
39 changes: 21 additions & 18 deletions lua/parrot/provider/ollama.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,30 @@ end

function Ollama:get_available_models()
-- curl https://api.openai.com/v1/models -H "Authorization: Bearer $OPENAI_API_KEY"
local job = Job:new({
command = "curl",
args = { "-H", "Content-Type: application/json", "http://localhost:11434/api/tags" },
}):sync()
if vim.fn.executable("ollama") then
local job = Job:new({
command = "curl",
args = { "-H", "Content-Type: application/json", "http://localhost:11434/api/tags" },
}):sync()

local parsed_response = utils.parse_raw_response(job)
local success, parsed_data = pcall(vim.json.decode, parsed_response)
if not success then
logger.error("Error parsing JSON:" .. vim.inspect(parsed_data))
return {}
end
local names = {}
if parsed_data.models then
for _, model in ipairs(parsed_data.models) do
table.insert(names, model.name)
local parsed_response = utils.parse_raw_response(job)
local success, parsed_data = pcall(vim.json.decode, parsed_response)
if not success then
logger.error("Error parsing JSON:" .. vim.inspect(parsed_data))
return {}
end
local names = {}
if parsed_data.models then
for _, model in ipairs(parsed_data.models) do
table.insert(names, model.name)
end
else
logger.error("No models found. Please use 'ollama pull' to download one.")
return {}
end
else
logger.error("No models found. Please use 'ollama pull' to download one.")
return {}
return names
end
return names
return {}
end

return Ollama
5 changes: 2 additions & 3 deletions lua/parrot/provider/openai.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ function OpenAI:process_onexit(res)
end
end

function OpenAI:get_available_models()
function OpenAI:get_available_models(online)
-- curl https://api.openai.com/v1/models -H "Authorization: Bearer $OPENAI_API_KEY"

if self:verify() then
if online and self:verify() then
Job:new({
command = "curl",
args = {
Expand Down

0 comments on commit 334bdac

Please sign in to comment.