[UX] Add thinking support to vllm chat (#57045)
This commit is contained in:
@@ -46,21 +46,35 @@ def _interactive_cli(args: argparse.Namespace) -> tuple[str, OpenAI]:
|
|||||||
|
|
||||||
|
|
||||||
def _print_chat_stream(stream, stats: bool = False) -> str:
|
def _print_chat_stream(stream, stats: bool = False) -> str:
|
||||||
output = ""
|
output: str = ""
|
||||||
start = time.perf_counter()
|
in_reasoning: bool = False
|
||||||
|
start: float = time.perf_counter()
|
||||||
ttft: float | None = None
|
ttft: float | None = None
|
||||||
completion_tokens = 0
|
completion_tokens: int = 0
|
||||||
for chunk in stream:
|
for chunk in stream:
|
||||||
if chunk.usage is not None:
|
if chunk.usage is not None:
|
||||||
completion_tokens = chunk.usage.completion_tokens
|
completion_tokens = chunk.usage.completion_tokens
|
||||||
if not chunk.choices:
|
if not chunk.choices:
|
||||||
continue
|
continue
|
||||||
delta = chunk.choices[0].delta
|
delta = chunk.choices[0].delta
|
||||||
|
reasoning = getattr(delta, "reasoning", None) or getattr(
|
||||||
|
delta, "reasoning_content", None
|
||||||
|
)
|
||||||
|
if ttft is None and (reasoning or delta.content):
|
||||||
|
ttft = time.perf_counter() - start
|
||||||
|
if reasoning:
|
||||||
|
if not in_reasoning:
|
||||||
|
print("<think>", flush=True)
|
||||||
|
in_reasoning = True
|
||||||
|
print(reasoning, end="", flush=True)
|
||||||
if delta.content:
|
if delta.content:
|
||||||
if ttft is None:
|
if in_reasoning:
|
||||||
ttft = time.perf_counter() - start
|
print("\n</think>", flush=True)
|
||||||
|
in_reasoning = False
|
||||||
output += delta.content
|
output += delta.content
|
||||||
print(delta.content, end="", flush=True)
|
print(delta.content, end="", flush=True)
|
||||||
|
if in_reasoning:
|
||||||
|
print("\n</think>", end="", flush=True)
|
||||||
print()
|
print()
|
||||||
if stats:
|
if stats:
|
||||||
_print_metrics(start, ttft, completion_tokens)
|
_print_metrics(start, ttft, completion_tokens)
|
||||||
|
|||||||
Reference in New Issue
Block a user