-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm.lua
More file actions
executable file
·129 lines (106 loc) · 3.62 KB
/
llm.lua
File metadata and controls
executable file
·129 lines (106 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env luajit
package.path = (arg[0]:match("@?(.*/)") or arg[0]:match("@?(.*\\)")) .. "lib" .. package.config:sub(1, 1) .. "?.lua;" .. package.path
local utility = require "utility"
local argparse = utility.require("argparse")
local json = utility.require("dkjson")
local parser = argparse():help_max_width(80)
local create = parser:command("create", "create a model from a modelfile, or pull a model")
create:argument("model", "model name/tag"):args(1)
create:argument("modelfile", "modelfile"):args("?")
local run = parser:command("run", "run a model")
run:argument("model", "the model to run"):args(1)
run:argument("input", "input text to send (if not present, opens interactive mode)"):args("*")
local queue = parser:command("queue", "add queries to the queue")
queue:argument("model", "the model to run"):args(1)
queue:argument("input", "input text to send"):args("1+")
local background = parser:command("background", "run queries in the queue")
background:argument("maximum", "maximum number of queries to execute"):args("?")
local options = parser:parse()
-- concatenate input if needed
if options.input then
if #options.input == 1 then
options.input = options.input[1]
elseif #options.input > 1 then
options.input = table.concat(options.input, " ")
else
options.input = nil
end
end
utility.required_program("ollama")
local function query_model(model, prompt)
if prompt then
return utility.capture_safe("ollama run " .. model .. " --nowordwrap " .. prompt:enquote())
else -- interactive
return os.execute("ollama run " .. model)
end
end
local function create_model(model, modelfile)
if not modelfile then modelfile = model end
if utility.is_file(modelfile) then
return os.execute("ollama create " .. model .. " --file " .. modelfile:enquote())
else
return os.execute("ollama pull " .. model)
end
end
local queue_file_name = "llm-data.json"
local function get_queue()
utility.get_lock(queue_file_name)
local queue
pcall(function()
utility.open(queue_file_name, "r", function(file)
queue = json.decode(file:read("*all"))
end)
end)
if not queue then queue = {} end
return queue
end
local function save_queue(queue)
utility.open(queue_file_name, "w", function(file)
file:write(json.encode(queue, { indent = true }))
file:write("\n")
end)
utility.release_lock(queue_file_name)
end
local function queue_query(model, input)
local queue = get_queue()
queue[#queue + 1] = { model = model, input = input, status = "waiting", }
save_queue(queue)
end
local function background(maximum)
local function next_query(queue)
for id, query in ipairs(queue) do
if query.status == "waiting" then
query.status = "running"
return id, query
end
end
end
local count = 0
repeat
local queue = get_queue()
if not maximum then maximum = #queue end
local id, selected = next_query(queue)
save_queue(queue)
if selected then
local result = utility.capture_safe("luajit " .. utility.path .. "llm.lua run " .. selected.model .. " " .. selected.input:enquote())
local queue = get_queue()
queue[id].status = "finished"
queue[id].result = result
save_queue(queue)
count = count + 1
else
return -- nothing left to do!
end
until count == maximum
end
if options.create then
create_model(options.model, options.modelfile)
elseif options.run then
print(query_model(options.model, options.input))
elseif options.queue then
queue_query(options.model, options.input)
elseif options.background then
background(options.maximum)
else
print(parser:get_help()) -- should be impossible to get here
end