fix typos, add llm create command, utility.path

This commit is contained in:
Tangent / Rose / Nebula Rosa 2024-01-14 04:34:45 -07:00
parent 40cc2421fa
commit 06603df09e
2 changed files with 45 additions and 6 deletions

47
llm.lua
View File

@ -32,7 +32,8 @@ local action = arg[1]
-- On my system, it is impossible to call wsl directly from Lua. No idea why. -- On my system, it is impossible to call wsl directly from Lua. No idea why.
local function wsl_command(command, get_output) local function wsl_command(command, get_output) -- defaults to getting the output
-- print("wsl running:", command)
local command = "pwsh -Command wsl --exec \"" .. utility.escape_quotes(command) .. "\"" local command = "pwsh -Command wsl --exec \"" .. utility.escape_quotes(command) .. "\""
local output local output
@ -77,7 +78,7 @@ end
local function model_exists(model) local function model_exists(model)
local models = get_models() local models = get_models()
for _, name in ipairs(models) do for _, name in ipairs(models) do
if model == name then if model == name:sub(1, name:find(":") - 1) then -- ignore tags!
return true return true
end end
end end
@ -88,7 +89,46 @@ end
local execute = { local execute = {
create = function() create = function()
local model = arg[2]
if not model then print("A model name must be specified.") return false end
-- check for conflicts, then search local modelfiles -> create from local, then try a pull command, else return false -- check for conflicts, then search local modelfiles -> create from local, then try a pull command, else return false
if model_exists(model) then
print("A model called \"" .. model .. "\" already exists.")
return false
end
local search_path = utility.path .. "ollama-modelfiles"
if utility.OS == "Windows" then
search_path = search_path .. "\\"
else
search_path = search_path .. "/"
end
local success
utility.ls(search_path)(function(file_name)
if model == file_name then
-- WSL can't comprehend a Windows path and treads it as a local path extension, so we must modify the path
search_path = search_path:gsub("\\", "/"):gsub("(%a):", function(capture) return "/mnt/" .. capture:lower() end)
local output = wsl_command("ollama create " .. model .. " --file " .. search_path .. model)
print(output)
if output:find("success") then
success = true
else
success = false
end
end
end)
if type(success) ~= nil then return success end
local output = (wsl_command("ollama pull " .. model)):trim()
print(output)
if output:find("Error") then
return false
else
return true
end
end, end,
query = function() query = function()
local model = arg[2] local model = arg[2]
@ -135,7 +175,4 @@ end
-- print(query_dolphin("Say only the word 'cheese'.")) -- print(query_dolphin("Say only the word 'cheese'."))
-- TEMPORARY creation, need to make this system able to manage models automatically or semi-automatically
-- wsl_command("ollama create curt --file ")
-- print(query_model("curt", "How are you?")) -- print(query_model("curt", "How are you?"))

View File

@ -8,6 +8,8 @@ else
utility.OS = "UNIX-like" utility.OS = "UNIX-like"
end end
utility.path = arg[0]:match("@?(.*/)") or arg[0]:match("@?(.*\\)") -- related to discussion in https://stackoverflow.com/q/6380820
-- always uses outputting to a temporary file to guarantee safety -- always uses outputting to a temporary file to guarantee safety
function os.capture_safe(command, tmp_file_name) function os.capture_safe(command, tmp_file_name)
local file_name = tmp_file_name or utility.tmp_file_name() local file_name = tmp_file_name or utility.tmp_file_name()
@ -80,7 +82,7 @@ utility.ls = function(path)
command = "ls -1" command = "ls -1"
end end
if path then if path then
command = command .. "\"" .. path .. "\"" command = command .. " \"" .. path .. "\""
end end
local tmp_file_name = utility.tmp_file_name() local tmp_file_name = utility.tmp_file_name()