-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.py
376 lines (302 loc) · 13.9 KB
/
client.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
import requests
import json
import sys
import os
# Constants for API URL and other parameters
API_URL = "http://127.0.0.1:8080/" # Replace with your API URL if you've changed it
STREAM_MODE = True
VERBOSE = False
HISTORY = [] # Message history to keep the conversation going
PREFIX_MESSAGE = "<|im_start|>system You are a helpful assistant. <|im_end|> <|im_start|>user"
SUFIX_MESSAGE = "<|im_end|><|im_start|>assistant"
# ANSI color codes
RESET = "\033[0m"
BOLD = "\033[1m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
CYAN = "\033[36m"
# Displays the help menu with all available commands.
def print_help():
print(f"{CYAN}{BOLD}Available commands:{RESET}")
print(f"{YELLOW}help{RESET} : Displays this help menu.")
print(f"{YELLOW}update{RESET} : Checks for available updates and upgrades.")
print(f"{YELLOW}serve{RESET} : Starts the server.")
print(f"{YELLOW}list{RESET} : Lists all available models on the server.")
print(f"{YELLOW}pull hf/model/file.rkllm{RESET} : Downloads a model via a file from Hugging Face.")
print(f"{YELLOW}rm model.rkllm{RESET} : Remove the model.")
print(f"{YELLOW}load model.rkllm{RESET} : Loads a specific model.")
print(f"{YELLOW}unload{RESET} : Unloads the currently loaded model.")
print(f"{YELLOW}run{RESET} : Enters conversation mode with the model.")
print(f"{YELLOW}exit{RESET} : Exits the program.")
def print_help_chat():
print(f"{CYAN}{BOLD}Available commands:{RESET}")
print(f"{YELLOW}/help{RESET} : Displays this help menu.")
print(f"{YELLOW}/clear{RESET} : Clears the current conversation history.")
print(f"{YELLOW}/cls or /c{RESET} : Clears the console content.")
print(f"{YELLOW}/set stream{RESET} : Enables stream mode.")
print(f"{YELLOW}/unset stream{RESET} : Disables stream mode.")
print(f"{YELLOW}/set verbose{RESET} : Enables verbose mode.")
print(f"{YELLOW}/unset verbose{RESET} : Disables verbose mode.")
print(f"{YELLOW}/set system{RESET} : Modifies the system message.")
print(f"{YELLOW}exit{RESET} : Exits the conversation.\n")
# Check status of rkllama API
def check_status():
try:
response = requests.get(API_URL)
return response.status_code
except:
return 500
# Retrieves the list of available templates from the server.
def list_models():
try:
response = requests.get(API_URL + "models")
if response.status_code == 200:
models = response.json().get("models", [])
print(f"{GREEN}{BOLD}Available models:{RESET}")
for model in models:
print(f"- {model}")
else:
print(f"{RED}Error retrieving models: {response.status_code} - {response.text}{RESET}")
except requests.RequestException as e:
print(f"{RED}Query error: {e}{RESET}")
# Loads a specific template on the server.
def load_model(model_name, From=None, huggingface_path=None):
if From != None and huggingface_path != None:
payload = {"model_name": model_name, "huggingface_path": huggingface_path, "from": From}
else:
payload = {"model_name": model_name}
try:
response = requests.post(API_URL + "load_model", json=payload)
if response.status_code == 200:
print(f"{GREEN}{BOLD}Model {model_name} loaded successfully.{RESET}")
return True
else:
print(f"{RED}Error loading model: {response.status_code} - {response.json().get('error', response.text)}{RESET}")
return False
except requests.RequestException as e:
print(f"{RED}Query error: {e}{RESET}")
return False
# Unloads the currently loaded model.
def unload_model():
try:
response = requests.post(API_URL + "unload_model")
if response.status_code == 200:
print(f"{GREEN}{BOLD}Model successfully unloaded.{RESET}")
else:
print(f"{RED}Error when unloading model: {response.status_code} - {response.json().get('error', response.text)}{RESET}")
except requests.RequestException as e:
print(f"{RED}Query error: {e}{RESET}")
# Sends a message to the loaded model and displays the response.
def send_message(message):
global HISTORY
HISTORY.append({"role": "user", "content": message})
# if VERBOSE == True:
# print(HISTORY)
payload = {
"messages": HISTORY,
"stream": STREAM_MODE
}
try:
if STREAM_MODE:
with requests.post(API_URL + "generate", json=payload, stream=True) as response:
if response.status_code == 200:
print(f"{CYAN}{BOLD}Assistant:{RESET} ", end="")
assistant_message = ""
final_json = {
"usage": {
"tokens_per_second": 0,
"completion_tokens": 0
}
}
for line in response.iter_lines(decode_unicode=True):
if line:
try:
response_json = json.loads(line)
final_json = response_json
content_chunk = response_json["choices"][0]["content"]
sys.stdout.write(content_chunk)
sys.stdout.flush()
assistant_message += content_chunk
except json.JSONDecodeError:
print(f"{RED}Error detecting JSON response.{RESET}")
if VERBOSE == True:
tokens_per_second = final_json["usage"]["tokens_per_second"]
completion_tokens = final_json["usage"]["completion_tokens"]
print(f"\n\n{GREEN}Tokens per second{RESET}: {tokens_per_second}")
print(f"{GREEN}Number of tokens {RESET}: {completion_tokens}")
HISTORY.append({"role": "assistant", "content": assistant_message})
# Return to line after last token
print("\n")
else:
print(f"{RED}Streaming error: {response.status_code} - {response.text}{RESET}")
else:
response = requests.post(API_URL + "generate", json=payload)
if response.status_code == 200:
response_json = response.json()
assistant_message = response_json["choices"][0]["content"]
print(f"{CYAN}{BOLD}Assistant:{RESET} {assistant_message}")
if VERBOSE == True:
tokens_per_second = final_json["usage"]["tokens_per_second"]
completion_tokens = final_json["usage"]["completion_tokens"]
print(f"\n\n{GREEN}Tokens per second{RESET}: {tokens_per_second}")
print(f"{GREEN}Number of Tokens {RESET}: {completion_tokens}")
HISTORY.append({"role": "assistant", "content": assistant_message})
else:
print(f"{RED}Query error: {response.status_code} - {response.text}{RESET}")
except requests.RequestException as e:
print(f"{RED}Query error: {e}{RESET}")
# Function to change model if the old model loaded is not the same one to execute
def switch_model(new_model):
response = requests.get(API_URL + "current_model")
if response.status_code == 200:
current_model = response.json().get("model_name")
if current_model:
print(f"{YELLOW}Unloading the current model: {current_model}{RESET}")
unload_model()
if not load_model(new_model):
print(f"{RED}Unable to load model {new_model}.{RESET}")
return False
return True
# Function for remove model
def remove_model(model):
response = requests.get(API_URL + "current_model")
if response.status_code == 200:
current_model = response.json().get("model_name")
if current_model == model:
print(f"{YELLOW}Unloading the current model before deletion: {current_model}{RESET}")
unload_model()
response_rm = requests.delete(API_URL + "remove", json={"model": model})
if response_rm.status_code == 200:
print(f"{GREEN}The model has been successfully deleted!{RESET}")
# Function for download model
def pull_model(model):
if model is None or model == "":
repo = input(f"{CYAN}Repo ID{RESET} ( example: punchnox/Tinnyllama-1.1B-rk3588-rkllm-1.1.4 ): ")
filename = input(f"{CYAN}File{RESET} ( example: TinyLlama-1.1B-Chat-v1.0-rk3588-w8a8-opt-0-hybrid-ratio-0.5.rkllm ): ")
model = repo + "/" + filename
try:
response = requests.post(API_URL + "pull", json={"model": model}, stream=True)
if response.status_code != 200:
print(f"{RED}Error: Received status code {response.status_code}.{RESET}")
print(response.text)
return
def update_progress(progress):
bar_length = 50 # Length of the progress bar
block = int(round(bar_length * progress / 100))
text = f"\r{GREEN}Progress:{RESET} [{CYAN}{'#' * block}{RESET}{'-' * (bar_length - block)}] {progress:.2f}%"
sys.stdout.write(text)
sys.stdout.flush()
# Progress bar
for line in response.iter_lines(decode_unicode=True):
if line:
line = line.strip()
if line.endswith('%'): # Checks if the line contains a percentage
try:
progress = int(line.strip('%'))
update_progress(progress)
except ValueError:
print(f"\n{line}") # Displays non-numeric messages
else:
print(f"\n{line}") # Displays other messages
print(f"\n{GREEN}Download complete.{RESET}")
except requests.RequestException as e:
print(f"Error connecting to server: {e}")
# Interactive function for chatting with the model.
def chat():
global VERBOSE, STREAM_MODE, HISTORY, PREFIX_MESSAGE
os.system("clear")
print_help_chat()
while True:
user_input = input(f"{CYAN}You:{RESET} ")
if user_input == "/help":
print_help_chat()
elif user_input == "/clear":
HISTORY = []
print(f"{GREEN}Conversation history successfully reset{RESET}")
elif user_input == "/cls" or user_input == "/c":
os.system("clear")
elif user_input.lower() == "exit":
print(f"{RED}End of conversation.{RESET}")
break
elif user_input == "/set stream":
STREAM_MODE = True
print(f"{GREEN}Stream mode successfully activated!{RESET}")
elif user_input == "/unset stream":
STREAM_MODE = False
print(f"{RED}Stream mode successfully deactivated!{RESET}")
elif user_input == "/set verbose":
VERBOSE = True
print(f"{GREEN}Verbose mode successfully activated!{RESET}")
elif user_input == "/unset verbose":
VERBOSE = False
print(f"{RED}Verbose mode successfully deactivated!{RESET}")
elif user_input == "/set system":
system_prompt = input(f"{CYAN}System prompt: {RESET}")
PREFIX_MESSAGE = f"<|im_start|>{system_prompt}<|im_end|> <|im_start|>user"
print(f"{GREEN}System message successfully modified!")
else:
# If content is not a command, then send content to template
send_message(user_input)
def update():
setup_path = os.path.expanduser('~/RKLLAMA/setup.sh')
# Check if setup.sh exists
if not os.path.exists(setup_path):
print("setup.sh not found. Downloading the setup script...")
url = "https://raw.githubusercontent.com/NotPunchnox/rkllama/refs/heads/main/setup.sh"
# Download setup.sh
try:
urllib.request.urlretrieve(url, setup_path)
print("setup.sh downloaded successfully.")
except Exception as e:
print(f"Failed to download setup.sh: {e}")
return
# Run git pull and setup.sh
print("Updating the repository and running setup.sh...")
os.system('git pull')
os.system(f'bash {setup_path}')
def main():
# Check minimum number of entries
if len(sys.argv) < 2:
print_help()
return
command = sys.argv[1]
if check_status() != 200 and command not in ['serve', 'update']:
print(f"{RED}Error: Server not started!\n{RESET}rkllama serve{CYAN} command to start the server.{RESET}")
sys.exit(0)
# Start of condition sequence
if command == "help":
print_help()
elif command == "serve":
os.system(f"bash ~/RKLLAMA/server.sh")
elif command == "update":
update()
elif command =="list":
list_models()
elif command == "load_model":
if len(sys.argv) < 3:
print(f"{RED}Error: You must specify the model name.{RESET}")
else:
load_model(sys.argv[2])
elif command == "unload":
unload_model()
elif command == "run":
if len(sys.argv) == 3:
if not switch_model(sys.argv[2]):
return
elif len(sys.argv) >= 4:
load_model(sys.argv[2], sys.argv[3], sys.argv[4])
chat()
elif command == "rm":
if sys.argv[2] is None:
print(f"{RED}Error: You must specify the model name.{RESET}")
else:
remove_model(sys.argv[2])
elif command == "pull":
pull_model(sys.argv[2] if len(sys.argv) < 2 else "" )
else:
print(f"{RED}Unknown command: {command}.{RESET}")
print_help()
# Launching the main function: program start
if __name__ == "__main__":
main()