-
Notifications
You must be signed in to change notification settings - Fork 92
/
alpaca_turbo.py
executable file
·501 lines (423 loc) · 16.7 KB
/
alpaca_turbo.py
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
497
498
499
500
501
#!/bin/python3
"""
▄▄▄· ▄▄▌ ▄▄▄· ▄▄▄· ▄▄· ▄▄▄· ▄▄▄▄▄▄• ▄▌▄▄▄ ▄▄▄▄·
▐█ ▀█ ██• ▐█ ▄█▐█ ▀█ ▐█ ▌▪▐█ ▀█ •██ █▪██▌▀▄ █·▐█ ▀█▪▪
▄█▀▀█ ██▪ ██▀·▄█▀▀█ ██ ▄▄▄█▀▀█ ▐█.▪█▌▐█▌▐▀▀▄ ▐█▀▀█▄ ▄█▀▄
▐█ ▪▐▌▐█▌▐▌▐█▪·•▐█ ▪▐▌▐███▌▐█ ▪▐▌ ▐█▌·▐█▄█▌▐█•█▌██▄▪▐█▐█▌.▐▌
▀ ▀ .▀▀▀ .▀ ▀ ▀ ·▀▀▀ ▀ ▀ ▀▀▀ ▀▀▀ .▀ ▀·▀▀▀▀ ▀█▄▀▪
https;//github.comViperX7/Alpaca-Turbo
"""
import os
import platform
import sys
import time
import django
from rich import print as eprint
from utils.interaction import Process
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "turbo_server.settings")
django.setup()
from ai_model_manager.models import AIModel
from chatbot.models import Conversation, Message
class Assistant:
def __init__(self, aimodel: AIModel = None):
if aimodel is None:
print("No model specified using the first one from database")
print("Checking all available Models")
no_models = len(AIModel.objects.all())
print(f"Total {no_models} models found")
print()
print("select a model ")
print()
print("\t0. <<<Load new models>>>\n")
for i,mod in enumerate(AIModel.objects.all()):
print(f"\t{i+1}. {mod.name}")
print()
try:
choice = int(input("choice [1]: ")) -1
except:
choice = 0
if choice > -1:
aimodel = AIModel.objects.all()[choice]
else:
path = input("Enter path of directory containing all models [./models]")
if not path:
path = "./models"
res = AIModel.add_models_from_dir(path)
print(f"Added {len(res)} models to db")
exit()
self.DEBUG = "-d" in sys.argv
if aimodel:
self.model: AIModel = aimodel
self.settings = self.model.settings
self.prompt = self.model.prompt
else:
self.model: AIModel = None
self.settings = None
self.prompt = None
self.conversation: Conversation = Conversation()
self.conversation.save()
self.last_in_mem = 0
# Configurables
self.threads = 4
self.use_bos = True
self.enable_history = False
self.new_chat()
# Internal state store
self.is_loaded = ""
self.current_state = "Initialised"
self.old_preprompt = None
self.is_first_request = True
# fixed values
self.end_marker = b"RSTsr"
def new_chat(self, conv: Conversation = None):
"save current conv and set proper title and start new conv"
try:
self.conversation.title = " ".join(
self.conversation[0].user_request.split(" ")[:6]
)
self.conversation.save()
except (IndexError, TypeError):
pass
Conversation.clear_blank()
self.conversation = conv if conv else Conversation()
if not conv:
setattr(self.conversation, "title", "New Chat")
self.conversation.save()
def remove_all_chat(self):
r_count = Conversation.remove_all_conv()
return f"{r_count} files removed"
def load_chat(self, id):
"""load chat"""
# data = {"can't load generation going on"}
# if self.current_state != "generating":
conv = Conversation.objects.filter(id=id).first()
self.conversation = conv
data = list(conv)
return data
def get_conv_logs(self):
"""conversation logs"""
data = Conversation.get_all_conversations()
return data
def remove_chat(self, uuid):
"""-"""
uuid = str(uuid)
print(uuid)
if uuid != str(self.conversation.id):
Conversation.objects.filter(id=uuid).delete()
def clear_chat(self):
"""clear current history context"""
result = "can't save generation going on"
if self.current_state != "generating":
self.conversation = Conversation()
self.conversation.save()
result = "success"
self.is_first_request = True
self.use_bos = True
return result
def safe_kill(self):
"""kill the bot if not in use"""
if self.current_state == "generating":
return "Can't kill bot busy"
self.process.killx()
self.is_first_request = True
self.current_state = "Initialised"
if self.conversation:
self.conversation.save()
self.is_loaded = ""
return "killed the bot"
@staticmethod
def get_bin_path():
system_name = platform.system()
if os.path.exists("bin/cmain"):
name = "bin/cmain"
elif system_name == "Linux":
name = "bin/main"
elif system_name == "Windows":
name = "bin/main.exe"
elif system_name == "Darwin":
name = "bin/mac"
else:
print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
print("XXXXXXXXXXXXXXX CHAT BINARY MISSING XXXXXXXXXXXXXXXXX")
print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
exit()
return name
@property
def command(self):
command = [
Assistant.get_bin_path(),
# "--color",
"-i",
"--seed",
f"{self.settings.seed}",
"-ins",
"-t",
f"{self.threads}",
"-b",
f"{self.settings.batch_size}",
"--top_k",
f"{self.settings.top_k}",
"--top_p",
f"{self.settings.top_p}",
"--repeat_last_n",
f"{self.settings.repeat_last_n}",
"--repeat_penalty",
f"{self.settings.repetition_penalty}",
"--temp",
f"{self.settings.temperature}",
"--n_predict",
f"{self.settings.n_predict}",
"-m",
f"{self.model.path}",
# "--interactive-start",
"--interactive-first",
]
return command
def load_model(self):
"""load binary in memory"""
if self.is_loaded:
return f"model already loaded {self.model.path}"
try:
self.process = Process(self.command, timeout=10000)
for _ in range(6):
_ = (
[print("ERRoR" * 20), exit(1)]
if "error" in self.process.readline().decode("utf-8").lower()
else None
)
is_loaded = False
for _ in range(100):
if is_loaded:
continue
ppt = self.process.read(1)
is_loaded = b"d" == ppt
for _ in range(12):
(self.process.readline())
self.process.recvuntil(self.end_marker)
self.current_state = "prompt"
self.is_loaded = self.model.path
except Exception as e:
print(e)
self.is_loaded = ""
return f"Failed loading {self.model.path}"
return f"loaded successfully {self.model.path}"
def unload_model(self):
if self.is_loaded:
self.safe_kill()
self.is_loaded = False
def stop_generation(self):
"""Interrupts generation"""
if self.current_state == "generating":
self.current_state = "stoping_generation"
# self.process.send("\003")
self.process.interrupt()
time.sleep(1)
self.current_state = "prompt"
return "Stopped"
return f"failed to stop current status {self.current_state}"
def chatbot(self, message: Message, enable_history=False):
"""chatbot"""
# self.conversation.append(prompt)
# build history chahe
final_prompt_2_send = []
hist_start = self.last_in_mem
hist_end = message.index
print(f"hist_start: {hist_start}, hist_end: {hist_end}")
if hist_start >= hist_end:
hist_start = 0
self.unload_model()
self.load_model()
print(f"hist_start: {hist_start}, hist_end: {hist_end}")
data2use = message.conversation[hist_start:hist_end]
eprint(data2use)
for convo in data2use:
for sequence in convo.get_prompt():
final_prompt_2_send.append(sequence)
final_prompt_2_send = "".join(final_prompt_2_send)
final_prompt_2_send = final_prompt_2_send.replace("\r","")
if message.preprompt or hist_start == 0:
final_prompt_2_send = [
self.conversation.get_messages()[0].preprompt,
final_prompt_2_send,
]
self.send_prompts(final_prompt_2_send)
self.last_in_mem = message.index
for char in self.stream_generation():
message.ai_response += char
message.ai_response = message.ai_response.replace("\r","")
message.save()
yield char
def completion(self, message: Message, count=-1):
"""add completion support"""
self.conversation.save()
message.conversation.save()
final_prompt_2_send = [message.user_request]
final_prompt_2_send = "".join(final_prompt_2_send)
self.send_prompts(final_prompt_2_send)
sp_count = 0
interrupted = False
for char in self.stream_generation():
if count != -1:
sp_count += 1 if " " in char else 0
if sp_count >= count and not interrupted:
self.process.interrupt() # this sometimes misses a word or two
interrupted = True
res = char.replace("\n", "") if interrupted else char
message.ai_response += res
message.save()
# print(res,end="")
yield res
def send_prompts(self, txtblob):
"""send the prompts with bos token"""
eprint(txtblob)
txtblob = txtblob if isinstance(txtblob, list) else [txtblob]
_ = eprint(txtblob) if self.DEBUG else None
if self.current_state == "prompt":
bos = len(txtblob)
self.process.recvuntil("n_inps> ")
self.process.sendline(str(bos))
self.process.recvuntil("n_threads> ")
self.process.sendline(str(int(self.threads)))
self.process.recvuntil("top_k> ")
self.process.sendline(str(self.settings.top_k))
self.process.recvuntil("top_p> ")
self.process.sendline(str(self.settings.top_p))
self.process.recvuntil("temperature> ")
self.process.sendline(str(self.settings.temperature))
self.process.recvuntil("repeat_penalty> ")
self.process.sendline(str(self.settings.repetition_penalty))
self.process.recvuntil("n_batch> ")
self.process.sendline(str(self.settings.batch_size))
self.process.recvuntil("antiprompt> ")
self.process.sendline(str(self.prompt.antiprompt))
for txt in txtblob:
lines = txt.split("\n")
lines[-1] += "@end@" if self.use_bos else "@done@"
self.use_bos = False
for line in lines:
self.process.recvuntil(") : ")
self.process.sendline(line)
# print(self.process.readline())
self.current_state = "generating"
else:
print("CRITICAL")
print("Either already generating or int yet ready")
def stream_generation(self):
"""returns a generator that returns the generation"""
buffer = b""
marker_detected = b""
antiprompt_detected = b""
char_old = b""
while self.current_state == "generating":
char = self.process.read(1)
buffer += char # update the buffer
# Detect end of text if detected try to confirm else reset
if (
char == self.end_marker.decode()[0].encode("utf-8")
or len(marker_detected) > 0
):
marker_detected += char
char_old += char
# print("==========")
# print(marker_detected)
# print(self.end_marker[:len(marker_detected)])
if marker_detected in self.end_marker[: len(marker_detected)]:
# print("cont")
continue
marker_detected = b""
elif (
char == self.prompt.antiprompt[0].encode("utf-8")
or len(antiprompt_detected) > 0
):
print("Antiprompt buffering started")
antiprompt_detected += char
char_old += char
# print("==========")
# print(antiprompt_detected)
# print(self.end_antiprompt[:len(antiprompt_detected)])
if antiprompt_detected in self.prompt.antiprompt[
: len(antiprompt_detected)
].encode("utf-8"):
# print("cont")
continue
antiprompt_detected = b""
if self.prompt.antiprompt.encode("utf-8") in buffer:
buffer = buffer[:-1] if buffer[-1] == 10 else buffer
char_old = char_old.replace(self.prompt.antiprompt.encode("utf-8"), b"")
char_old = char_old[:-1] if char_old[-1] == 10 else char_old
if self.end_marker in buffer:
buffer = buffer.replace(self.end_marker, b"")
char_old += char
char_old = char_old.replace(self.end_marker, b"")
self.current_state = "prompt"
try:
res = char_old.decode("utf-8")
except UnicodeDecodeError:
res = f">{str(char_old)}<"
yield res
# print(f"\nStream Ended {buffer}")
break
try:
# Load the full character cache
char = char_old + char
# print single printable chars
if len(char) == 1 and char[0] <= 0x7E and char[0] >= 0x21:
char = char.decode("utf-8")
char_old = b""
elif len(char) >= 4:
char = char.decode("utf-8")
char_old = b""
else:
char_old = char
continue
except UnicodeDecodeError:
char_old = char
continue
# print(char, end="")
yield char
# return buffer
def sane_check_msg(self, msg):
print("====")
print(msg.conversation.id)
print(msg.preprompt)
print(self.old_preprompt)
print("====")
if self.old_preprompt is None:
self.old_preprompt = msg.preprompt
elif self.old_preprompt is not None and self.old_preprompt != msg.preprompt:
self.old_preprompt = msg.preprompt
elif self.old_preprompt == msg.preprompt:
msg.preprompt = None
msg.preprompt = msg.preprompt if msg.preprompt is not None else None
msg.preprompt = (
self.prompt.preprompt
if self.is_first_request and msg.index == 1
else msg.preprompt
)
self.is_first_request = False
msg.format = self.prompt.format if msg.format is None else msg.format
return msg
def send_conv(self, preprompt, fmt, prompt):
"""function to simplify interface"""
msg = self.conversation.add_message(prompt, preprompt=preprompt, format=fmt)
msg = self.sane_check_msg(msg)
resp = self.chatbot(msg)
return resp
@staticmethod
def repl():
"""Repl for my chat bot"""
assistant = Assistant(None)
assistant.load_model()
assistant.enable_history = False
fmt = assistant.prompt.format
# assistant.pre_prompt = ""
preprompt = assistant.prompt.preprompt
while True:
# print("=====")
prompt = input(">>>>>> ")
preprompt = ""
resp = assistant.send_conv(preprompt, fmt, prompt)
for char in resp:
print(char, end="")
print()
_ = Assistant.repl() if __name__ == "__main__" else None