-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvzgpt.lua
More file actions
312 lines (285 loc) · 6.57 KB
/
vzgpt.lua
File metadata and controls
312 lines (285 loc) · 6.57 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
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
--- this file is automatically run by vzgpt when started up in lua mode
--- some constants (variables)
nummatches=40
temp=1.0
minp=0.001
chatprefix="|kysymys|"
chatsuffix="\n|vastaus|"
ender="\n"
--- essential data structures and manipulation functions
ctx={
lastset = 0,
lastvalid = 0,
lastrun = 0,
lastmatched = 0,
maxsize = 1024,
set = function(b)
b=sanitize(b,(15*ctx.maxsize)/16)
--printobj(b)
--print('<= set')
if b[1]~=ctx[1] then ctx.lastvalid=0 end
for i=1,math.min(ctx.lastvalid,#b,ctx.maxsize) do
if ctx[i]~=b[i] then break end
ctx.lastvalid=i
end
ctx.lastset=math.min(#b,ctx.maxsize)
for i=ctx.lastvalid+1,#ctx do
ctx[i]=b[i]
end
ctx.lastrun=0
ctx.lastmatched=0
end,
append = function(b)
b=detok(ctx.get())..b
ctx.set(b)
end,
get = function() return getcontext(ctx.lastset) end
}
setmetatable(ctx,{
__newindex = function(t,k,v) setslot(k,v) end,
__index = function(t,k) return getslot(k) end,
__tostring = function(t) return detok(ctx.get()) end,
__len = function(t) return t.lastset end
})
function sanitize(b,maxlgt)
if type(b)=='string' then b=tok(b) end
if #b>maxlgt then
c={}
for i=#b-maxlgt+1,#b do
c[i-(#b-maxlgt)]=b[i]
end
b=c
end
return b
end
--- generator functions
function validate()
if ctx.lastset>=ctx.maxsize then
h={}
for i=1,(ctx.maxsize/2) do h[i]=ctx[i+ctx.maxsize/2] end
ctx.set(h)
end
--if ctx.lastvalid<ctx.lastset-10 and not noisy then
-- io.write("["..tostring(ctx.lastset-ctx.lastvalid).."...]\n")
-- io.flush()
--end
while ctx.lastvalid<ctx.lastset do
ctx.lastvalid=ctx.lastvalid+1
if noisy then
io.write(detok(ctx[ctx.lastvalid]))
io.flush()
else
i=ctx.lastset-ctx.lastvalid-4
if i>0 then
i=(i%4)+1
io.write(string.sub('\\|/-',i,i).."\b")
io.flush()
end
end
--print('validating at ',ctx.lastvalid)
runstep(ctx.lastvalid)
ctx.lastrun=ctx.lastvalid
if breaking() then break end
end
end
function genstep()
validate()
if breaking() then return end
if ctx.lastrun~=ctx.lastset then
runstep(ctx.lastset)
ctx.lastrun=ctx.lastset
ctx.lastvalid=ctx.lastset
end
if ctx.lastmatched~=ctx.lastset then
match(nummaches,temp)
ctx.lastmatched=ctx.lastset
end
t=choosenext(nummatches,minp)
ctx.lastvalid=ctx.lastset
ctx.lastset=ctx.lastset+1
ctx[ctx.lastset]=t
return t
end
function listmatches()
if ctx.lastrun~=ctx.lastset then
runstep(ctx.lastset)
ctx.lastrun=ctx.lastset
end
m=getmatches()
for i=1,#m,2 do
print(i,detok(m[i]),m[i+1])
end
end
function gen(ender)
noisy=true
validate(true)
noisy=false
tt=''
while not breaking() do
t=detok(genstep())
io.write(t)
io.flush()
if ender~=nil then
tt=tt..t
if string.sub(tt,0-#ender)==ender then break end
end
end
io.write("\n")
end
--- interactive commands
function g()
gen(nil)
end
function l()
gen("\n")
-- generate until
-- string.sub(a,0-#ender)==ender
end
function p()
return detok(ctx.get())
end
function c()
while true do
txt=readline('human> ')
if txt==nil or txt=='' then break end
ctx.append(chatprefix..txt..chatsuffix)
io.write("\ncomputer> ")
io.flush()
--noisy=true
validate()
--noisy=false
l()
end
end
function fixlines()
txt=detok(ctx.get())
txt,n0=string.gsub(txt,'\n([^%s])',' %1')
txt,n1=string.gsub(txt,'\n%s+','\n')
ctx.set(txt)
io.write(tostring(n0+n1).." substitutions.\n")
end
function sb()
ctx.set('')
print("Press ^D when finished.")
while true do
l=readline('lines> ')
if l==nil then break end
if ctx.lastset>0 then ctx.append("\n") end
ctx.append(l)
end
print("Context initialized! Use fixlines() to remove hard linebreaks.")
end
function v()
io.write(string.format([[
temp=%f
nummatches=%.0f
minp=%f
chatprefix=%q
chatsuffix=%q
ender=%q
]],temp,nummatches,minp,chatprefix,chatsuffix,ender))
end
function h()
io.write([[
==========================[ vzgpt-lua help ]===========================
g : generate until ^C pressed <tab> : edit context buffer
l : generate until newline =<text> : set context buffer
c : chat mode -<text> : set last paragraph of buffer
v : show variables +<text> : append to context buffer
m : list matches sb : set buffer (multiple lines)
p : print context buffer
h : this help text
q : quit lua commands are also accepted
=======================================================================
]])
end
--- other functions related to the command line interface
function printobj(o)
if type(o)=='table' and o~=ctx then
io.write('{')
for j=1,#o do
if j>=2 then io.write(',') end
printobj(o[j])
end
io.write("}")
else
io.write(tostring(o))
end
end
function printobjs(...)
a={...}
for i=1,#a do
printobj(a[i])
io.write("\n")
end
end
function trim(s)
while string.sub(s,-1)=="\n" or string.sub(s,-1)==' ' do
s=string.sub(s,1,-2)
end
return s
end
function splitlastline(s)
s=trim(s)
--while string.sub(s,-1)=="\n" do s=string.sub(s,1,-2) end
--i=string.find(s,"\n",-1,true)
pt=0
for i=#s,1,-1 do
if string.sub(s,i,i)=="\n" then pt=i break end
end
if pt~=nil then
return trim(string.sub(s,1,pt-1)),string.sub(s,pt+1)
else
return '',s
end
end
function getcompletion(s,state)
--print('getcompletion',s,state)
if state>0 then return nil end
if s=='' then
return '='..detok(ctx.get())
elseif s=='=' then
return detok(ctx.get())
elseif s=='-' then
s,e=splitlastline(detok(ctx.get()))
return e
else
return nil
end
end
function cli()
h()
while true do
cmd=readline()
if cmd=='' then
elseif cmd=='quit' or cmd=='q' or cmd==nil then
print "QUIT!"
break
elseif string.sub(cmd,1,1)=='=' then
ctx.set(string.sub(cmd,2))
elseif string.sub(cmd,1,1)=='-' then
s,e=splitlastline(detok(ctx.get()))
ctx.set(s.."\n"..string.sub(cmd,2))
elseif string.sub(cmd,1,1)=='+' then
ctx.set(detok(ctx.get()).."\n"..string.sub(cmd,2))
else
op=nil
if _ENV[cmd]~=nil and type(_ENV[cmd])=='function' then
op,err=load('printobjs('..cmd..'())')
else
op,err=load('printobjs('..cmd..')')
end
if op==nil then
op,err=load(cmd) end
if op~=nil then
succ,msg=pcall(op,msg)
if not succ then
print("Runtime error: ",msg)
end
else
print("Syntax error: "..err)
end
end
end
end
noisy=false