-
Notifications
You must be signed in to change notification settings - Fork 2
/
da.lua
496 lines (409 loc) · 13.6 KB
/
da.lua
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
-- Check for invalid repository name
local isMain = fs.exists("/DavinCC-main/")
local isDavin = fs.exists("/DavinCC/")
if isMain then
error("Incorrect repository name, please rename DavinCC-main to simply DavinCC.")
elseif not isDavin then
error("Incorrect repository location or name, please ensure DavinCC is installed on root, as /DavinCC/.")
end
-- Import completion and quill
package.path = "/DavinCC/?.lua;" .. package.path
local completion = require("lib/completion")
local quill = require("lib/quill")
local sketch = require("lib/sketch")
local flag = require("lib/flag")
-- User input for risk and personality
local personality, risk, cutoff, model, img, magnitude = ...
local tokens = 500
local isInput = true
local isPrompt = true
local isImg
local isCode
personality = personality or "standard"
personality = string.lower(personality)
model = model or "gpt-3.5"
-- Checking usage of chat method
local isChat = false
if model == "gpt-3.5" then
model = "gpt-3.5-turbo"
end
if model == "gpt-3.5" or model == "gpt-3.5-turbo" or model == "gpt-4" or model == "gpt-4-32k" then
isChat = true
end
-- Input conversion
if risk then
risk = tonumber(risk)
end
if cutoff then
cutoff = tonumber(cutoff)
end
if img then
img = string.lower(img)
else
img = "false"
end
if magnitude then
magnitude = string.lower(magnitude)
else
magnitude = "sm"
end
-- Magnitude conversion, defaults to small
local size = "256x256"
if magnitude == "sm" then
size = "256x256"
elseif magnitude == "md" or magnitude == "lg" then -- Medium AND large
size = "512x512"
elseif magnitude == "lg" then --! Large images not rendering, thus temporarily disabled
size = "1024x1024"
end
-- Input testing for non-number
if type(risk) ~= "number" then
risk = 0
end
if type(cutoff) ~= "number" then
cutoff = 10
end
-- Input testing for out of range
if risk < 0 then
risk = 0
elseif risk > 1 then
risk = 1
end
if cutoff < 0 then
cutoff = 0
elseif cutoff > 42 then
cutoff = 42
end
-- Translating string to boolean
if img == "true" or img == "t" or img == 1 then
img = "true"
isImg = true
elseif img == "false" or img == "f" or img == 0 then
img = "false"
isImg = false
end
-- Conversation variables
local prompt
local cont
local reply
local number
-- Prepare for inserting
if not fs.exists("/DavinCC/data/in.txt") then
quill.scribe("/DavinCC/data/in.txt", "w", "")
end
-- Configure based on new prompt
local function config(prompt)
local confPmpt = "[PMPT]"
local confPer = "[PER]"
local confIns = "[INS]"
local confImg = "[IMG]"
local confSelf = "[SELF]"
local confCode = "[CODE]"
local confVar = "[VAR]"
local confClr = "[CLR]"
isPrompt = true
--* Process prompt flags and check for [PMPT]
local tblPmpt = flag.pmpt(prompt)
if flag.isCall then
-- Check output
if tblPmpt then
risk = tblPmpt["r"] or risk
cutoff = tblPmpt["c"] or cutoff
tokens = tblPmpt["t"] or tokens
end
-- Remove from prompt
prompt = quill.replace(prompt, confPmpt .. flag.call, "")
prompt = string.gsub(prompt, " +", " ")
end
--* Process personality flags and check for [PER]
local tblPer = flag.per(prompt, risk, tokens, cutoff)
local replay
if flag.isCall then
-- Check output
if tblPer then
personality = tblPer["g"] or personality
replay = tblPer["r"] or replay
end
-- Remove from prompt
prompt = quill.replace(prompt, confPer .. flag.call, "")
prompt = string.gsub(prompt, " +", " ")
end
--* Process insert flags and check for [INS]
-- TODO: automatic absolute path detection
local tblIns = flag.ins(prompt)
if flag.isCall then
-- Remove from prompt before modifying
prompt = quill.replace(prompt, confIns .. flag.call, " ")
-- Check output
if tblIns then
if tblIns["u"] then
-- Prioritising url, with full webscrape
local request = http.get(tblIns["u"])
local response = request.readAll()
request.close()
-- Finding matches for text within HTML
local text = ""
for match in response:gmatch(">%s*(.-)%s*<") do text = text .. match end
text = quill.truncate(text)
-- Inserting scrape at command location
prompt = quill.insert(prompt, text, flag.isCall)
elseif tblIns["f"] then
-- Appending file name to path
local insFile = "/DavinCC/data/" .. tblIns["f"]
-- Checking for file extension
if not string.find(tblIns["f"], "%.") then
insFile = insFile .. ".txt"
end
-- Reading and inserting file contents at command location
local insertion = quill.scribe(insFile, "r")
prompt = quill.insert(prompt, insertion, flag.isCall)
end
end
-- Remove spaces after modifying
prompt = string.gsub(prompt, " +", " ")
end
--* Process image flags and check for [IMG]
local tblImg = flag.img(prompt)
if flag.isCall then
-- Check output
if tblImg then
number = tblImg["n"]
size = tblImg["s"]
end
-- Prepare for image
isImg = true
-- Remove from prompt
prompt = quill.replace(prompt, confImg .. flag.call, "")
prompt = string.gsub(prompt, " +", " ")
else
-- Stop preparing for image
isImg = false
end
--* Process code flags and check for [CODE]
local tblCode = flag.code(prompt)
if flag.isCall then
-- No output
-- Prepare for code
isCode = true
-- Remove from prompt
prompt = quill.replace(prompt, confCode .. flag.call, "")
prompt = string.gsub(prompt, " +", " ")
else
-- Stop preparing for image
isCode = false
end
--* Process self flags and check for [SELF]
local tblSelf = flag.self(prompt, risk, tokens, cutoff)
if flag.isCall then
-- Check output
if tblSelf then
personality = tblSelf["g"] or personality
isInput = false
end
-- Remove from prompt
prompt = quill.replace(prompt, confSelf .. flag.call, "")
prompt = string.gsub(prompt, " +", " ")
end
--* Process variable flags and check for [VAR]
local tblVar = flag.var(prompt)
if flag.isCall and isInput then
if tblVar then
-- Stop prompting
isPrompt = false
end
-- Remove from prompt
prompt = quill.replace(prompt, confVar .. flag.call, "")
prompt = string.gsub(prompt, " +", " ")
end
if not tblVar then
-- Re-enable prompting
isPrompt = true
end
--* Check for [CLR]
if quill.seek(prompt, confClr, "%s") then
-- Clear terminal and reset pos
term.clear()
term.setCursorPos(1, 1)
-- Remove from prompt
prompt = quill.replace(prompt, confClr, "")
prompt = string.gsub(prompt, " +", " ")
end
-- TODO: other configs... [SELF]-ggreet, [LIST]-llines
-- Return new trail-less prompt
-- prompt = quill.trailSpace(prompt)
return prompt
end
local function closedAsk()
local _, ans
while ans ~= "y" or ans ~= "n" do
sleep(1)
_, ans = os.pullEvent("char")
local x, y = term.getCursorPos()
term.setCursorPos(x, y - 1)
term.clearLine()
if ans == "y" then
return true
elseif ans == "n" then
return false
end
end
end
local function promptSelf()
-- Allow cancellation
print("Continue? (y/n)")
local ans = closedAsk()
if ans then
-- Continue conversation with self
cont = completion.continueSelf(reply, risk, tokens, cutoff, model)
-- Store and print truncated prompt
print(cont)
prompt = quill.truncate(cont)
quill.scribe("/DavinCC/data/out.txt", "w", prompt)
sleep(1)
isInput = false
else
isInput = true
prompt = read()
end
end
-- Quick and flavourless request
if personality == "none" then
-- Print arguments
print("Personality: \"" .. personality .. "\" Risk: " .. risk .. " Img: " .. img .. "\n")
-- Read input as red
term.setTextColour(colours.red)
prompt = read()
print("\n")
-- Configuring based on prompt commands
prompt = config(prompt)
-- Complete prompt (user input), risk (0-1), token limit
cont = completion.request(prompt, risk, tokens)
-- Print output as orange
term.setTextColour(colours.orange)
print(cont .. "\n")
-- Store truncated reply
reply = quill.truncate(cont)
quill.scribe("/DavinCC/out.txt", "w", reply)
-- Generating image if true
if isImg then
local links = sketch.generate(reply, number, size)
print("I made an image... Display? (y/n)\n")
local ans = closedAsk()
if ans then sketch.display(links) end
end
-- Generating code if true
if isCode then
quill.code(cont, "/DavinCC/data/programs/", "gpt")
print("I made a program... Run? (y/n)\n")
local ans = closedAsk()
if ans then os.run({}, "/DavinCC/data/programs/gpt.lua") end
end
-- Otherwise, conduct conversation with chosen personality
elseif not isChat then
-- Printing chosen arguments
print("Personality: \"" .. personality .. "\" Risk: " .. risk .. " Cutoff: " .. cutoff .. " Img: " .. img)
-- Select greeting file based on personality
personality = quill.firstUpper(personality)
local greetFile = "/DavinCC/greetings/greet" .. personality .. ".txt"
-- Initiate a conversation
completion.greet(greetFile)
quill.scribe(greetFile, "r")
-- Start with reply to "hello" prompt
term.setTextColour(colours.orange)
cont = completion.continue("hello", risk, tokens, cutoff, model)
reply = cont
print(reply)
-- Continue the conversation indefinately
while true do
-- Read input as red
print("\n")
term.setTextColour(colours.red)
if isInput then
prompt = read()
end
-- Configuring based on prompt commands
prompt = config(prompt)
-- Check for self conversation
if not isInput then
promptSelf()
print("\n")
end
if isPrompt then
-- Continue with prompt (user input), risk (0-1), token limit (max per reply), cutoff (how many replies to remember)
cont = completion.continue(prompt, risk, tokens, cutoff, model)
-- Print output as orange
term.setTextColour(colours.orange)
print(cont)
-- Store truncated reply
reply = quill.truncate(cont)
quill.scribe("/DavinCC/data/out.txt", "w", reply)
-- Generating image if true
if isImg then
local links = sketch.generate(reply, number, size)
print("I made an image... Display? (y/n)\n")
local ans = closedAsk()
if ans then sketch.display(links) end
end
-- Generating code if true
if isCode then
quill.code(cont, "/DavinCC/data/programs/", "gpt")
print("I made a program... Run? (y/n)\n")
local ans = closedAsk()
if ans then os.run({}, "/DavinCC/data/programs/gpt.lua") end
end
end
end
elseif isChat then
-- Printing chosen arguments
print("Personality: \"" .. personality .. "\" Risk: " .. risk .. " Cutoff: " .. cutoff .. " Img: " .. img)
-- Select greeting file based on personality
personality = quill.firstUpper(personality)
local greetFile = "/DavinCC/greetings/greet" .. personality .. ".json"
--TODO: Initiate a conversation
completion.greet(greetFile, false, true)
-- quill.scribe(greetFile, "r")
-- Start with reply to "hello" prompt
term.setTextColour(colours.orange)
cont = completion.chat("hello", risk, tokens, cutoff, model)
reply = cont
print(reply)
-- Continue the conversation indefinately
while true do
-- Read input as red
print("\n")
term.setTextColour(colours.red)
if isInput then
prompt = read()
end
--TODO: Configuring based on prompt commands
prompt = config(prompt)
if not isInput then
promptSelf()
print("\n")
end
if isPrompt then
-- Continue with prompt (user input), risk (0-1), token limit (max per reply), cutoff (how many replies to remember)
cont = completion.chat(prompt, risk, tokens, cutoff, model)
-- Print output as orange
term.setTextColour(colours.orange)
print(cont)
-- Store truncated reply
reply = quill.truncate(cont)
quill.scribe("/DavinCC/data/out.txt", "w", reply)
-- Generating image if true
if isImg then
local links = sketch.generate(reply, number, size)
print("I made an image... Display? (y/n)\n")
local ans = closedAsk()
if ans then sketch.display(links) end
end
-- Generating code if true
if isCode then
quill.code(cont, "/DavinCC/data/programs/", "gpt")
print("I made a program... Run? (y/n)\n")
local ans = closedAsk()
if ans then os.run({}, "/DavinCC/data/programs/gpt.lua") end
end
end
end
end