forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
290 lines (251 loc) · 9.03 KB
/
Copy pathchat.py
File metadata and controls
290 lines (251 loc) · 9.03 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
"""soup chat — interactive chat with a fine-tuned model."""
import json
from pathlib import Path
from typing import Optional
import typer
from rich.console import Console
from rich.panel import Panel
console = Console()
def chat(
model: str = typer.Option(
...,
"--model",
"-m",
help="Path to LoRA adapter directory or full model",
),
base_model: Optional[str] = typer.Option(
None,
"--base",
"-b",
help="Base model ID. Auto-detected from adapter_config.json if not set.",
),
device: Optional[str] = typer.Option(
None,
help="Device: cuda, mps, cpu. Auto-detected if not set.",
),
max_tokens: int = typer.Option(
512,
"--max-tokens",
help="Maximum number of tokens to generate",
),
temperature: float = typer.Option(
0.7,
"--temperature",
"-t",
help="Sampling temperature (0.0 = greedy, 1.0 = creative)",
),
system_prompt: Optional[str] = typer.Option(
None,
"--system",
"-s",
help="System prompt for the conversation",
),
trust_remote_code: bool = typer.Option(
False,
"--trust-remote-code",
help=(
"Allow loading models that ship custom Python via auto_map. "
"Default deny (v0.36.0). Only enable if you trust the source."
),
),
hub: str = typer.Option(
"hf",
"--hub",
help=(
"Source hub for the base model: hf (default) / modelscope / "
"modelers. Non-HF hubs require the matching SDK; the base model "
"is snapshotted to a cwd-contained cache before chat starts "
"(v0.53.10 #152)."
),
),
):
"""Chat with a fine-tuned model in the terminal."""
# v0.53.10 #152 — pre-fetch base from a non-HF hub before any path
# resolution. Local paths and HF repo IDs are passed through unchanged.
if hub and hub != "hf":
from soup_cli.utils.hubs import apply_hub_to_cli_model
try:
model, base_model = apply_hub_to_cli_model(
model, base_model, hub, console=console
)
except (TypeError, ValueError) as exc:
console.print(f"[red]{exc}[/]")
raise typer.Exit(code=2) from exc
except ImportError as exc:
console.print(f"[red]{exc}[/]")
raise typer.Exit(code=1) from exc
model_path = Path(model)
if not model_path.exists():
console.print(f"[red]Model path not found: {model_path}[/]")
raise typer.Exit(1)
# Detect if it's a LoRA adapter or full model
adapter_config_path = model_path / "adapter_config.json"
is_adapter = adapter_config_path.exists()
# Resolve base model
if is_adapter and not base_model:
base_model = _detect_base_model(adapter_config_path)
if not base_model:
console.print(
"[red]Cannot detect base model from adapter_config.json.[/]\n"
"Please specify with [bold]--base[/] flag."
)
raise typer.Exit(1)
# Detect device
if not device:
from soup_cli.utils.gpu import detect_device
device, _ = detect_device()
console.print(
Panel(
f"Model: [bold]{model_path}[/]\n"
+ (f"Base: [bold]{base_model}[/]\n" if is_adapter else "")
+ f"Device: [bold]{device}[/]\n"
f"Type: [bold]{'LoRA adapter' if is_adapter else 'Full model'}[/]",
title="Loading model",
)
)
# Resolve --trust-remote-code (v0.36.0 Part B). Uses the base model id
# for LoRA adapters since that's what gets executed; otherwise the
# local model path.
from soup_cli.utils.trust_remote import (
model_requires_trust_remote_code,
resolve_trust_remote_code,
)
probe_target = base_model or str(model_path)
requires = model_requires_trust_remote_code(str(model_path)) or False
resolved_trust = resolve_trust_remote_code(
probe_target,
requested=trust_remote_code,
console=console,
requires_remote_code=requires,
)
# Load model + tokenizer
model_obj, tokenizer = _load_model(
model_path=str(model_path),
base_model=base_model,
is_adapter=is_adapter,
device=device,
trust_remote_code=resolved_trust,
)
console.print("[bold green]Model loaded![/] Type your message. Commands:")
console.print(" [dim]/quit[/] - exit chat")
console.print(" [dim]/clear[/] - reset conversation history")
console.print(" [dim]/system <text>[/] - set system prompt")
console.print()
# Chat loop
history = []
if system_prompt:
history.append({"role": "system", "content": system_prompt})
while True:
try:
user_input = console.input("[bold blue]You:[/] ")
except (KeyboardInterrupt, EOFError):
console.print("\n[dim]Goodbye![/]")
break
user_input = user_input.strip()
if not user_input:
continue
# Handle commands
if user_input.lower() == "/quit":
console.print("[dim]Goodbye![/]")
break
elif user_input.lower() == "/clear":
history = []
if system_prompt:
history.append({"role": "system", "content": system_prompt})
console.print("[dim]Conversation cleared.[/]\n")
continue
elif user_input.lower().startswith("/system "):
new_system = user_input[8:].strip()
# Remove old system prompt if exists
history = [msg for msg in history if msg["role"] != "system"]
history.insert(0, {"role": "system", "content": new_system})
console.print(f"[dim]System prompt set: {new_system}[/]\n")
continue
# Add user message
history.append({"role": "user", "content": user_input})
# Generate response
response = _generate(
model_obj, tokenizer, history,
max_tokens=max_tokens,
temperature=temperature,
device=device,
)
history.append({"role": "assistant", "content": response})
console.print(f"[bold green]Assistant:[/] {response}\n")
def _detect_base_model(adapter_config_path: Path) -> Optional[str]:
"""Read base_model_name_or_path from adapter_config.json."""
try:
with open(adapter_config_path, encoding="utf-8") as f:
config = json.load(f)
return config.get("base_model_name_or_path")
except (json.JSONDecodeError, OSError):
return None
def _load_model(
model_path: str,
base_model: Optional[str],
is_adapter: bool,
device: str,
trust_remote_code: bool = False,
):
"""Load model and tokenizer. Supports LoRA adapters and full models."""
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
console.print("[dim]Loading tokenizer...[/]")
tokenizer = AutoTokenizer.from_pretrained(
model_path, trust_remote_code=trust_remote_code
)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
if is_adapter:
from peft import PeftModel
console.print(f"[dim]Loading base model: {base_model}...[/]")
base = AutoModelForCausalLM.from_pretrained(
base_model,
trust_remote_code=trust_remote_code,
device_map="auto",
torch_dtype=torch.float16,
)
console.print(f"[dim]Loading LoRA adapter: {model_path}...[/]")
model_obj = PeftModel.from_pretrained(base, model_path)
else:
console.print(f"[dim]Loading model: {model_path}...[/]")
model_obj = AutoModelForCausalLM.from_pretrained(
model_path,
trust_remote_code=trust_remote_code,
device_map="auto",
torch_dtype=torch.float16,
)
model_obj.eval()
return model_obj, tokenizer
def _generate(
model,
tokenizer,
messages: list[dict],
max_tokens: int = 512,
temperature: float = 0.7,
device: str = "cuda",
) -> str:
"""Generate a response from the model given message history."""
import torch
from soup_cli.utils.vllm import encode_chat_prompt
inputs = encode_chat_prompt(
messages, tokenizer, fallback_on_error=False, return_tensors="pt"
)
input_ids = inputs["input_ids"].to(model.device)
attention_mask = inputs["attention_mask"].to(model.device)
with torch.no_grad():
gen_kwargs = {
"input_ids": input_ids,
"attention_mask": attention_mask,
"max_new_tokens": max_tokens,
"do_sample": temperature > 0,
"pad_token_id": tokenizer.pad_token_id,
}
if temperature > 0:
gen_kwargs["temperature"] = temperature
gen_kwargs["top_p"] = 0.9
outputs = model.generate(**gen_kwargs)
# Decode only new tokens
new_tokens = outputs[0][input_ids.shape[1]:]
response = tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
return response