mirror of
https://github.com/cloudstack-llc/mlx-knife.git
synced 2026-07-21 10:05:26 -04:00
57bf6d86be
Major Features Added: • Complete run command implementation with interactive/single-shot modes • MLXRunner core engine ported from 1.x with modular architecture • OpenAI-compatible server with SIGINT-robust supervisor mode • Experimental push feature properly isolated behind environment variable Key Improvements: - Full feature parity with 1.1.1 stable releases - Enhanced human output formatting across all commands - Clean separation of stable (184 tests) vs experimental features - Updated demo GIF showcasing improved 2.0 interface Fixes: - Pull operation cache pollution (Issue #30) with preflight access checks - Test stability improvements across all environments Architecture: - Modular runner design with focused helper modules - Thread-safe model loading and memory management - stable testing across Python 3.9-3.13 Ready for use as comprehensive 1.x alternative.
52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
def apply_user_prompt(tokenizer: Any, prompt: str, use_chat_template: bool = True) -> str:
|
|
"""Format a single user prompt using the tokenizer's chat template if present."""
|
|
template = getattr(tokenizer, 'chat_template', None)
|
|
if use_chat_template and isinstance(template, str) and template:
|
|
messages = [{"role": "user", "content": prompt}]
|
|
try:
|
|
return tokenizer.apply_chat_template(
|
|
messages,
|
|
tokenize=False,
|
|
add_generation_prompt=True,
|
|
)
|
|
except Exception:
|
|
# Fall back to raw prompt if chat template application fails
|
|
pass
|
|
return prompt
|
|
|
|
|
|
def format_conversation(tokenizer: Any, messages: List[Dict[str, str]]) -> str:
|
|
"""Format conversation history into a prompt using chat template if available.
|
|
|
|
Falls back to legacy Human/Assistant formatting when no chat template exists.
|
|
"""
|
|
template = getattr(tokenizer, 'chat_template', None)
|
|
if isinstance(template, str) and template:
|
|
try:
|
|
return tokenizer.apply_chat_template(
|
|
messages,
|
|
tokenize=False,
|
|
add_generation_prompt=True,
|
|
)
|
|
except Exception:
|
|
# Fall back to legacy format if template application fails
|
|
pass
|
|
|
|
formatted_parts = []
|
|
for msg in messages:
|
|
role = msg["role"]
|
|
content = msg["content"]
|
|
if role == "system":
|
|
formatted_parts.append(f"System: {content}")
|
|
elif role == "user":
|
|
formatted_parts.append(f"Human: {content}")
|
|
elif role == "assistant":
|
|
formatted_parts.append(f"Assistant: {content}")
|
|
return "\n\n".join(formatted_parts) + "\n\nAssistant: "
|
|
|