Skip to content

Commit

Permalink
ref: move 'remove' feature to 'ollama' module
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-souza committed Sep 26, 2024
1 parent 212854d commit 47abc65
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 77 deletions.
37 changes: 34 additions & 3 deletions lua/ollama/ollama.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ function M.run(model)
vim.cmd(vim.iter(shell_cmd):join(" "))
end

---@class OllamaListOptions
---@field remote boolean

-- List Ollama models
---@return OllamaModel[]
function M.fetch_models()
Expand Down Expand Up @@ -73,4 +70,38 @@ function M.install(model)
return job:start()
end

---List installed models
---@return OllamaModel[]
function M.list()
vim.print("Listing models...")

local output = vim.fn.system("ollama ls | grep -v NAME | awk '{ print $1 }'")
local installed_models = vim.split(output, "\n")

return installed_models
end

-- Remove Ollama model
---@param model OllamaModel
---@return any
function M.remove(model)
vim.print("Removing model", model)

local job = Job:new({
command = "ollama",
args = { "rm", model },
cwd = vim.fn.stdpath("data"),
on_exit = function(j, code)
if code == 0 then
print("Model " .. model .. " removed 🎉")
else
print("Failed to remove model 😭" .. model)
logger.error("Failed to remove model", j:stderr_result())
end
end,
})

return job:start()
end

return M
53 changes: 0 additions & 53 deletions lua/ollero/ollama.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@ local commands = require("ollero.commands")

local Ollama = {}

---@param callback function(options: string) | nil
function Ollama.list_remote(callback)
-- TODO: load this from https://ollama.ai/library
local options = {
"llama2",
"mistral",
"orca-mini",
"codellama",
"llama2-uncensored",
"vicuna",
"wizard-vicuna-uncensored",
}
local cb = callback or noop
return cb(options)
end

---@param callback function | nil
function Ollama.init(callback)
-- TODO: ensure ollama is installed
Expand All @@ -34,43 +18,6 @@ function Ollama.init(callback)
-- return exec(ollama_init, callback or noop)
end

---@param callback function | nil
function Ollama.list(callback)
local shell_cmd = commands.CommandBuilder
:new()
:run("ollama ls | grep : | awk '{ print $1 }'")
:build()
return exec(shell_cmd, callback or noop)
end

---@param model string
---@param callback function(input string) | nil
function Ollama.install(model, callback)
vim.notify("Installing " .. model .. "...")
local shell_cmd =
commands.CommandBuilder:new():run("ollama pull " .. model):build()
local cb = (callback or noop)
return cb(shell_cmd)
end

---@param model string
---@param callback function(input string) | nil
function Ollama.run(model, callback)
vim.notify("Running " .. model .. "...")
local shell_cmd =
commands.CommandBuilder:new():run("ollama run " .. model):build()
local cb = (callback or noop)
return cb(shell_cmd)
end

---@param model string
---@param callback function | nil
function Ollama.rm(model, callback)
local shell_cmd =
commands.CommandBuilder:new():run("ollama rm " .. model):build()
return exec(shell_cmd, callback or noop)
end

---@param filepath string
---@param callback function
function Ollama.create_model(filepath, callback)
Expand Down
31 changes: 10 additions & 21 deletions lua/ollero/ollero.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,35 +91,24 @@ function Ollero.install_model()
{ prompt = "📦 Select an Ollama Model to install" },
function(choice)
vim.notify("Installing model: " .. choice)
ollama_v2.install(choice) -- kickoff new model
ollama_v2.install(choice)
end
)
end

---Remove Model
function Ollero.remove_model()
ollama.list(function(output)
local options = utils.split(output)

---@param model string
local function on_select(model)
if model == nil then
return
end
local options = ollama_v2.list()

vim.notify("Removing " .. model .. "...")

ollama.rm(model, function()
vim.notify(model .. " removed!")
end)
vim.ui.select(
options,
{ prompt = "🗑️ Select an Ollama Model to remove" },
function(choice)
vim.notify("Removing model: " .. choice)
ollama_v2.remove(choice)
vim.notify("Model " .. choice .. " removed")
end

vim.ui.select(
options,
{ prompt = "🗑️ Select a model to removed" },
on_select
)
end)
)
end

---Build Model
Expand Down

0 comments on commit 47abc65

Please sign in to comment.