From 29f5c72f460c6bf60d0ad1a19185e3304568ecb2 Mon Sep 17 00:00:00 2001 From: Tangent Date: Sat, 13 Jan 2024 05:19:28 -0700 Subject: [PATCH] working on getting AI bullshit working --- README.md | 4 +++ popen-command-test.lua | 65 +++++++++++++++++++++--------------------- utility-functions.lua | 41 ++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 33 deletions(-) create mode 100644 utility-functions.lua diff --git a/README.md b/README.md index b07b428..15378f7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # .lua-files It's like dotfiles, but no, it's just Lua scripts I find useful. +## Most Useful Scripts +- `popen-command-test`: A badly-named WIP script for working with the doplhin-mixtral AI model through WSL using [ollama](https://github.com/jmorganca/ollama). +- `video-dl`: A few premade command lines for using `yt-dlp` to download what I want quicker. + ## Installation 1. Put this folder somewhere. 2. Add that somewhere to your path. (On Windows, search for Environment Variables (it's "part of" Control Panel) and use the UI to add them to System variables.) diff --git a/popen-command-test.lua b/popen-command-test.lua index 67816a4..dd81f37 100644 --- a/popen-command-test.lua +++ b/popen-command-test.lua @@ -1,38 +1,37 @@ +-- install command: curl https://ollama.ai/install.sh | sh -function os.command(command) - local file = assert(io.popen(command, 'r')) - local output = assert(file:read('*a')) - file:close() - return output +util = require("utility-functions") +-- util.required_program("wsl") -- This fails on my system. +util.required_program("pwsh") -- Apparently this is and isn't PowerShell. Isn't the future amazing? + +-- On my system, it is impossible to call wsl directly from Lua. No idea why. +local function wsl_command(command, output_return) + local file_name = ".tmp." .. util.uuid() + local output + + command = "pwsh -Command wsl --exec \"" .. util.escape_quotes(command) .. "\"" + + if output_return then + command = command .. " > " .. file_name + end + + os.execute(command) + + if output_return then + local file = io.open(file_name, "r") + local output = file:read("*a") + file:close() + os.execute("rm " .. file_name) -- TODO replace with version I know works from somewhere else + return output + end end -function string.trim6(s) - return s:match'^()%s*$' and '' or s:match'^%s*(.*%S)' +local function query_dolphin(prompt) + local command = "ollama run dolphin-mixtral \"" .. util.escape_quotes(prompt) .. "\"" + return wsl_command(command, true) + -- TODO trim the above + + -- os.execute("pwsh -Command wsl --exec \"ollama run dolphin-mixtral \\\"Say only the word 'cheese'.\\\"\" > test-output-file.txt") end -print("---") - --- local home = os.command("echo %userprofile%") --- home = string.trim6(home) --- print(home) - --- local dolphin_cheese = os.command("wsl -- ollama run dolphin-mixtral \"Say only the word 'cheese'.\"") --- local dolphin_cheese = os.command("wsl") --- os.execute("C:\\Windows\\System32\\wsl.exe") --- local dolphin_cheese = os.command("echo %path%") --- local dolphin_cheese = os.command("where wsl") --- print(dolphin_cheese) - --- print(os.command("bash -c \"$PATH\"")) --- os.execute("bash -c \"ollama help\"") - --- runs but doesn't return anything? --- local dolphin_cheese = os.command("pwsh -Command wsl -- ollama run dolphin-mixtral \"Say only the word 'cheese'.\"") --- print(dolphin) - --- os.execute("pwsh -Command wsl -- ollama run dolphin-mixtral \"Say only the word 'cheese'.\"") - --- this will output to file in the correct place :D -os.execute("pwsh -Command wsl --exec \"ollama run dolphin-mixtral \\\"Say only the word 'cheese'.\\\"\" > test-output-file.txt") - -print("---") +print(query_dolphin("Say only the word 'cheese'.")) diff --git a/utility-functions.lua b/utility-functions.lua new file mode 100644 index 0000000..a9da560 --- /dev/null +++ b/utility-functions.lua @@ -0,0 +1,41 @@ +math.randomseed(os.time()) + +-- TODO look for popen command and fall back to outputting to file if its unavailable +function os.command(command) + local file = assert(io.popen(command, 'r')) + local output = assert(file:read('*a')) + file:close() + return output +end + +-- trim6 from Lua users wiki (best all-round pure Lua performance) +function string.trim(s) + return s:match'^()%s*$' and '' or s:match'^%s*(.*%S)' +end + +local utility = {} + +-- NOTE: This will print errors sometimes. :D +utility.required_program = function(name) + if os.execute("where " .. tostring(name)) ~= 0 then + error(tostring(name) .. " must be installed and in the path") + end +end + +-- modified from my fork of lume +utility.uuid = function() + local fn = function(x) + local r = math.random(16) - 1 + r = (x == "x") and (r + 1) or (r % 4) + 9 + return ("0123456789abcdef"):sub(r, r) + end + return (("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"):gsub("[xy]", fn)) +end + +utility.escape_quotes = function(input) + input = input:gsub("\\", "\\\\") + input = input:gsub("\"", "\\\"") + return input +end + +return utility