-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlm-chat.py
More file actions
executable file
·300 lines (241 loc) · 8.11 KB
/
lm-chat.py
File metadata and controls
executable file
·300 lines (241 loc) · 8.11 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
#!/usr/bin/env python3
import argparse
import cmd
import itertools
import json
import os
import signal
import sys
import time
import urllib.error
import urllib.request
def should_colorize():
t = os.getenv("TERM")
return t and t != "dumb" and sys.stdout.isatty()
def res(response, color):
color_default = ""
color_yellow = ""
if (color == "auto" and should_colorize()) or color == "always":
color_default = "\033[0m"
color_yellow = "\033[33m"
print("\r", end="")
assistant_response = ""
for line in response:
line = line.decode("utf-8").strip()
if line.startswith("data: {"):
choice = ""
json_line = json.loads(line[len("data: ") :])
if "choices" in json_line and json_line["choices"]:
choice = json_line["choices"][0]["delta"]
if "content" in choice:
choice = choice["content"]
else:
continue
if choice:
print(f"{color_yellow}{choice}{color_default}", end="", flush=True)
assistant_response += choice
print("")
return assistant_response
def add_api_key(args, headers=None):
# static analyzers suggest for dict, this is a safer way of setting
# a default value, rather than using the parameter directly
headers = headers or {}
if getattr(args, "api_key", None):
api_key_min = 20
if len(args.api_key) < api_key_min:
print("Warning: Provided API key is invalid.")
headers["Authorization"] = f"Bearer {args.api_key}"
return headers
class LmChatShell(cmd.Cmd):
def __init__(self, args):
super().__init__()
self.conversation_history = []
self.args = args
self.request_in_process = False
self.prompt = args.prefix
self.url = f"{args.url}/chat/completions"
def handle_args(self):
prompt = " ".join(self.args.ARGS) if self.args.ARGS else None
if not sys.stdin.isatty():
stdin = sys.stdin.read()
if prompt:
prompt += f"\n\n{stdin}"
else:
prompt = stdin
if prompt:
self.default(prompt)
return True
return False
def do_EOF(self, user_content):
print("")
return True
def default(self, user_content):
if user_content in ["/bye", "exit"]:
return True
self.conversation_history.append(
{"role": "user", "content": user_content}
)
self.request_in_process = True
response = self._req()
if not response:
return True
self.conversation_history.append(
{"role": "assistant", "content": response}
)
self.request_in_process = False
def _make_request_data(self):
data = {
"stream": True,
"messages": self.conversation_history,
}
if self.args.model is not None:
data["model"] = self.args.model
json_data = json.dumps(data).encode("utf-8")
headers = {
"Content-Type": "application/json",
}
headers = add_api_key(self.args, headers)
request = urllib.request.Request(
self.url, data=json_data, headers=headers, method="POST"
)
return request
def _req(self):
request = self._make_request_data()
i = 0.01
total_time_slept = 0
response = None
# Adjust timeout based on whether we're in initial connection phase
max_timeout = (
30 if getattr(self.args, "initial_connection", False) else 16
)
for c in itertools.cycle(
["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
):
try:
response = urllib.request.urlopen(request)
break
except Exception:
if sys.stdout.isatty():
print(f"\r{c}", end="", flush=True)
if total_time_slept > max_timeout:
break
total_time_slept += i
time.sleep(i)
i = min(i * 2, 0.1)
if response:
return res(response, self.args.color)
# Only show error and kill if not in initial connection phase
if not getattr(self.args, "initial_connection", False):
print(f"\rError: could not connect to: {self.url}")
else:
print(f"Could not connect to: {self.url}")
return None
def loop(self):
while True:
self.request_in_process = False
try:
self.cmdloop()
except KeyboardInterrupt:
print("")
if not self.request_in_process:
print("Use Ctrl + d or /bye or exit to quit.")
continue
break
class TimeoutException(Exception):
pass
def alarm_handler(signum, frame):
"""
Signal handler for SIGALRM. Raises TimeoutException when invoked.
"""
raise TimeoutException()
def chat(args):
list_models = args.command == "list" or args.command == "ls"
if list_models:
url = f"{args.url}/models"
headers = add_api_key(args)
req = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(req) as response:
data = json.loads(response.read())
ids = [model["id"] for model in data.get("data", [])]
for idi in ids:
print(idi)
elif args.command != "run":
return 1
try:
shell = LmChatShell(args)
if not list_models:
if shell.handle_args():
return 0
shell.loop()
except TimeoutException as e:
print(f"Timeout Exception: {e}")
# Handle the timeout, e.g., print a message and exit gracefully
print("")
finally:
# Reset the alarm to 0 to cancel any pending alarms
signal.alarm(0)
return 0
def main():
parser = argparse.ArgumentParser(
description="Chat with local models (docker model runner by default)."
)
# Create mutually exclusive group for URL options
url_group = parser.add_mutually_exclusive_group()
url_group.add_argument(
"--url",
help="Base URL for the model API",
)
url_group.add_argument(
"--dmr",
action="store_true",
help="Use docker model runner (default: http://127.0.0.1:12434/engines/llama.cpp/v1)",
)
url_group.add_argument(
"--llamacpp",
action="store_true",
help="Use llama.cpp server (default: http://127.0.0.1:8080/v1)",
)
url_group.add_argument(
"--ollama",
action="store_true",
help="Use ollama server (default: http://127.0.0.1:11434/v1)",
)
url_group.add_argument(
"--openrouter",
action="store_true",
help="Use openrouter server (default: https://openrouter.ai/api/v1)",
)
subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("list", aliases=["ls"], help="List available models")
parser_run = subparsers.add_parser(
"run", help="Run interactive chat with a model"
)
parser_run.add_argument(
"model",
help="Model identifier (e.g. hf.co/ggml-org/gpt-oss-20b-gguf)",
)
parser_run.add_argument(
"ARGS", nargs="*", help="overrides the default prompt, and the output is returned without entering the chatbot"
)
args = parser.parse_args()
# Set URL based on selected option
if args.url:
# Custom URL provided
pass # args.url already set
elif args.llamacpp:
args.url = "http://127.0.0.1:8080/v1"
elif args.ollama:
args.url = "http://127.0.0.1:11434/v1"
elif args.openrouter:
args.url = "https://openrouter.ai/api/v1"
else:
# Default to docker model runner (--dmr behavior)
args.url = "http://127.0.0.1:12434/engines/llama.cpp/v1"
args.prefix = "> "
args.color = "auto"
if chat(args):
parser.print_help()
return 1
return 0
if __name__ == "__main__":
sys.exit(main())