A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
example: cleanup mlx_chat
Myles Scolnick87f8ac939952
diff --git a/examples/ai/chat/mlx_chat.py b/examples/ai/chat/mlx_chat.pyindex 45dfa5b5fc..6d970a0712 100644--- a/examples/ai/chat/mlx_chat.py+++ b/examples/ai/chat/mlx_chat.py@@ -2,14 +2,14 @@# requires-python = ">=3.11"# dependencies = [# "marimo",-# "mlx-lm",-# "huggingface_hub",+# "mlx-lm==0.19.0",+# "huggingface-hub==0.25.1",# ]# ///import marimo-__generated_with = "0.9.1"+__generated_with = "0.9.4"app = marimo.App(width="medium")@@ -19,21 +19,22 @@ def __():from pathlib import Pathimport marimo as mofrom huggingface_hub import snapshot_download- return mo, load, generate, Path, snapshot_download+ return Path, generate, load, mo, snapshot_download[email protected](hide_code=True)def __(mo):mo.md(r"""# Using MLX with Marimo+## Chat ExampleThis example shows how to use [`mo.ui.chat`](https://docs.marimo.io/api/inputs/chat.html#marimo.ui.chat) to make a chatbot backed by Apple's MLX, using the `mlx_lm` library and marimo.[`mlx_lm`](https://github.com/ml-explore/mlx-examples/tree/main/llm) is a library for running large language models on Apple Silicon.[`mlx`](https://github.com/ml-explore/mlx) is a framework for running machine learning models on Apple Silicon.Convert your own models to MLX, or find community-converted ones at various quantizations [here](https://huggingface.co/mlx-community).-+### Things you can do to improve this example:- [`prompt caching`](https://github.com/ml-explore/mlx-examples/blob/main/llms/README.md#long-prompts-and-generations)- completions / notebook mode@@ -42,6 +43,7 @@ def __(mo):)return+@app.celldef __(Path, snapshot_download):def get_model_path(path_or_hf_repo: str) -> Path:@@ -61,118 +63,127 @@ def __(Path, snapshot_download):else:try:# If it doesn't exist locally, download it from Hugging Face- return Path(snapshot_download(- repo_id=path_or_hf_repo,- allow_patterns=[- "*.json",- "*.safetensors",- "*.py",- "tokenizer.model",- "*.tiktoken",- "*.txt",- ],- ))+ return Path(+ snapshot_download(+ repo_id=path_or_hf_repo,+ allow_patterns=[+ "*.json",+ "*.safetensors",+ "*.py",+ "tokenizer.model",+ "*.tiktoken",+ "*.txt",+ ],+ )+ )except Exception as e:- raise ValueError(f"Error downloading model from Hugging Face: {str(e)}")-+ raise ValueError(+ f"Error downloading model from Hugging Face: {str(e)}"+ )return (get_model_path,)+@app.celldef __(mo):MODEL_ID = mo.ui.text(- label="Hugging Face Model Repo or Local Path",+ label="Hugging Face Model Repo or Local Path",value="mlx-community/Llama-3.2-3B-Instruct-bf16",placeholder="Enter huggingfacerepo_id/model_id or local path",- full_width=True+ full_width=True,)-+load_model_button = mo.ui.run_button(label="Load Model")-+mo.hstack([MODEL_ID, load_model_button])- return (MODEL_ID, load_model_button,)+ return MODEL_ID, load_model_button+@app.celldef __(MODEL_ID, get_model_path, load, load_model_button, mo):mo.stop(not load_model_button.value, "Click 'Load Model' to proceed")-+try:- mo.md("⏳ Fetching model... This may take a while if downloading from Hugging Face.")+ mo.output.append(+ "⏳ Fetching model... This may take a while if downloading from Hugging Face."+ )model_path = get_model_path(MODEL_ID.value)- mo.md(f"📁 Model path: {model_path}")- mo.md("🔄 Loading model into memory...")+ mo.output.append(f"📁 Model path: {model_path}")+ mo.output.append("🔄 Loading model into memory...")model, tokenizer = load(model_path)- status = mo.md(f"✅ Model loaded successfully!")+ mo.output.append(f"✅ Model loaded successfully!")except Exception as e:- status = mo.md(f"❌ Error loading model: {str(e)}")+ mo.output.append(f"❌ Error loading model: {str(e)}")raise-- return (model, tokenizer, status)+ return model, model_path, tokenizer-def __(mo):- status- return-+[email protected](hide_code=True)def __(mo):# Create a text area for the system messagesystem_message = mo.ui.text_area(value="You are a helpful AI assistant.",label="System Message",full_width=True,- rows=3+ rows=3,)-- system_message # display the system message- return (MODEL_ID, system_message,)+ system_message # display the system message+ return (system_message,)+[email protected](hide_code=True)def __(mo):- temp_slider = mo.ui.slider(start=0.0, stop=2.0, step=0.1, value=0.7, label="Temperature Slider")+ temp_slider = mo.ui.slider(+ start=0.0, stop=2.0, step=0.1, value=0.7, label="Temperature Slider"+ )max_tokens = mo.ui.number(value=512, label="Max Tokens Per Turn")-- temp_slider, max_tokens # display the inputs- return(temp_slider, max_tokens,)+ temp_slider, max_tokens # display the inputs+ return max_tokens, temp_slider@app.cell-def __(generate, mo, model, tokenizer, system_message, temp_slider, max_tokens):+def __(+ generate,+ max_tokens,+ mo,+ model,+ system_message,+ temp_slider,+ tokenizer,+):def mlx_chat_model(messages, config):- # Include the system message as the first message- chat_messages = [- {"role": "system", "content": system_message.value}- ]-- # Add the rest of the messages- chat_messages.extend([- {"role": msg.role, "content": msg.content}- for msg in messages- ])-- # Use the tokenizer's chat template if available- if hasattr(tokenizer, "apply_chat_template") and tokenizer.chat_template:- prompt = tokenizer.apply_chat_template(- chat_messages,- tokenize=False,- add_generation_prompt=True- )- else:- # Fallback to simple concatenation if no chat template- prompt = "\n".join(f"{msg['role']}: {msg['content']}" for msg in chat_messages)- prompt += "\nassistant:"-- # Generate the response- response = generate(- model,- tokenizer,- prompt=prompt,- max_tokens=int(max_tokens.value), # Use the max_tokens input- temp=float(temp_slider.value), # Use the temperature slider+ # Include the system message as the first message+ chat_messages = [{"role": "system", "content": system_message.value}]++ # Add the rest of the messages+ chat_messages.extend(+ [{"role": msg.role, "content": msg.content} for msg in messages]+ )++ # Use the tokenizer's chat template if available+ if hasattr(tokenizer, "apply_chat_template") and tokenizer.chat_template:+ prompt = tokenizer.apply_chat_template(+ chat_messages, tokenize=False, add_generation_prompt=True+ )+ else:+ # Fallback to simple concatenation if no chat template+ prompt = "\n".join(+ f"{msg['role']}: {msg['content']}" for msg in chat_messages)+ prompt += "\nassistant:"++ # Generate the response+ response = generate(+ model,+ tokenizer,+ prompt=prompt,+ max_tokens=int(max_tokens.value), # Use the max_tokens input+ temp=float(temp_slider.value), # Use the temperature slider+ )++ return response.strip()- return response.strip()# Create the chat interfacechatbot = mo.ui.chat(@@ -186,10 +197,10 @@ def __(generate, mo, model, tokenizer, system_message, temp_slider, max_tokens):# Display the chatbotchatbot- return (chatbot, mlx_chat_model,)+ return chatbot, mlx_chat_model[email protected](hide_code=True)def __(mo):mo.md("""Access the chatbot's historical messages with `chatbot.value`.""")return