forked from foldl/chatllm.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreecobblers.py
83 lines (61 loc) · 2.31 KB
/
threecobblers.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
import sys, signal
from binding import PATH_BINDS
import chatllm
from chatllm import ChatLLM
import tool_numinamath
def build_sys_prompt():
return ""
LIB = chatllm.LibChatLLM(PATH_BINDS)
class Expert:
def __init__(self, domain: str, id: str, name: str, cls = ChatLLM) -> None:
self.domain = domain
self.id = id
self.name = name
self.cls = cls
def load(self, additional_params) -> None:
self.model = self.cls(LIB, ['-m', self.id] + additional_params)
class CallableLLM(ChatLLM):
def chat(self, user_input: str, input_id = None) -> str:
self.chunk_acc = ''
super().chat(user_input, input_id)
return self.chunk_acc
def callback_print(self, s: str) -> None:
self.chunk_acc = self.chunk_acc + s
def classify(model: CallableLLM, prompt: str, classes: list[str]) -> int:
class_str = '\n'.join([f"{i + 1}. {c}" for i, c in enumerate(classes)])
input = f"Please classify the question to one of these classes:\n{class_str}\n\nQuestion: {prompt}"
model.restart()
selected = model.chat(input)
for i, c in enumerate(classes):
if c in selected: return i
return len(classes) - 1
def run(additional_params, MODEL_CLASSIFIER: str = ':llama3.1'):
EXPERTS = [
Expert('Programming', ':deepseek-coder-v2:light', 'DeepSeek-Coder'),
Expert('Maths', ':numinamath', 'NuminaMath', tool_numinamath.ToolChatLLM),
Expert('Others', ':yi-1.5:6b', 'Yi 1.5'),
]
classifier = CallableLLM(LIB, ['-m', MODEL_CLASSIFIER, '--temp', '0'] + additional_params)
for e in EXPERTS:
e.load(additional_params)
def handler(signal_received, frame):
classifier.abort()
for e in EXPERTS:
e.model.abort()
sys.exit(0)
signal.signal(signal.SIGINT, handler)
classes = [e.domain for i, e in enumerate(EXPERTS)]
selected = None
print('Tip: use `/restart` to restart.')
while True:
s = input('You > ')
if s.startswith('/restart'):
selected = None
continue
if selected is None:
selected = EXPERTS[classify(classifier, s, classes=classes)]
selected.model.restart()
print(f'{selected.name} > ', end='', flush=True)
selected.model.chat(s)
if __name__ == '__main__':
run(sys.argv[1:])