Skip to content

Commit bdb77da

Browse files
authored
New UI for chat and lora (ml-explore#1344)
1 parent d39cfec commit bdb77da

7 files changed

Lines changed: 500 additions & 183 deletions

File tree

mlx_lm/chat.py

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import mlx.core as mx
66

7+
from .cli_ui import ChatUI
78
from .generate import stream_generate
89
from .models.cache import make_prompt_cache
910
from .sample_utils import make_sampler
@@ -96,10 +97,6 @@ def main():
9697
pipeline_group = group if args.pipeline else None
9798
tensor_group = group if not args.pipeline else None
9899

99-
def rprint(*args, **kwargs):
100-
if rank == 0:
101-
print(*args, **kwargs)
102-
103100
mx.random.seed(args.seed)
104101

105102
if group.size() > 1:
@@ -115,51 +112,48 @@ def rprint(*args, **kwargs):
115112
},
116113
)
117114

118-
def print_help():
119-
rprint("The command list:")
120-
rprint("- 'q' to exit")
121-
rprint("- 'r' to reset the chat")
122-
rprint("- 'h' to display these commands")
123-
124-
rprint(f"[INFO] Starting chat session with {args.model}.")
125-
print_help()
126-
prompt_cache = make_prompt_cache(model, args.max_kv_size)
127-
while True:
128-
query = input(">> " if rank == 0 else "")
129-
if query == "q":
130-
break
131-
if query == "r":
132-
prompt_cache = make_prompt_cache(model, args.max_kv_size)
133-
continue
134-
if query == "h":
135-
print_help()
136-
continue
137-
messages = []
138-
if args.system_prompt is not None:
139-
messages.append({"role": "system", "content": args.system_prompt})
140-
messages.append({"role": "user", "content": query})
141-
prompt = tokenizer.apply_chat_template(
142-
messages,
143-
add_generation_prompt=True,
144-
)
145-
for response in stream_generate(
146-
model,
147-
tokenizer,
148-
prompt,
149-
max_tokens=args.max_tokens,
150-
sampler=make_sampler(
151-
args.temp,
152-
args.top_p,
153-
xtc_threshold=args.xtc_threshold,
154-
xtc_probability=args.xtc_probability,
155-
xtc_special_tokens=(
156-
tokenizer.encode("\n") + list(tokenizer.eos_token_ids)
115+
with ChatUI(args, rank=rank) as ui:
116+
prompt_cache = make_prompt_cache(model, args.max_kv_size)
117+
while True:
118+
query = ui.prompt()
119+
if query == "q":
120+
ui.say_bye()
121+
break
122+
if query == "r":
123+
prompt_cache = make_prompt_cache(model, args.max_kv_size)
124+
ui.say_reset()
125+
continue
126+
if query == "h":
127+
ui.say_help()
128+
continue
129+
messages = []
130+
if args.system_prompt is not None:
131+
messages.append({"role": "system", "content": args.system_prompt})
132+
messages.append({"role": "user", "content": query})
133+
prompt = tokenizer.apply_chat_template(
134+
messages,
135+
add_generation_prompt=True,
136+
)
137+
last_response = None
138+
for response in stream_generate(
139+
model,
140+
tokenizer,
141+
prompt,
142+
max_tokens=args.max_tokens,
143+
sampler=make_sampler(
144+
args.temp,
145+
args.top_p,
146+
xtc_threshold=args.xtc_threshold,
147+
xtc_probability=args.xtc_probability,
148+
xtc_special_tokens=(
149+
tokenizer.encode("\n") + list(tokenizer.eos_token_ids)
150+
),
157151
),
158-
),
159-
prompt_cache=prompt_cache,
160-
):
161-
rprint(response.text, flush=True, end="")
162-
rprint()
152+
prompt_cache=prompt_cache,
153+
):
154+
ui.stream_token(response.text)
155+
last_response = response
156+
ui.end_turn(last_response)
163157

164158

165159
if __name__ == "__main__":

0 commit comments

Comments
 (0)