Skip to content

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.

example: add generative ui example (#2769)

Myles Scolnick09023e2ebede
diff --git a/examples/ai/chat/README.md b/examples/ai/chat/README.md
index fe28e9889d..abdf5d3000 100644
--- a/examples/ai/chat/README.md
+++ b/examples/ai/chat/README.md
@@ -6,11 +6,13 @@ These examples show how to make chatbots with marimo, using [`mo.ui.chat`](https
- `openai_example.py` shows how to make a chatbot powered by OpenAI models.
- `anthropic_example.py` shows how to make a chatbot powered by Anthropic models.
- `gemini.py` shows how to make a chatbot powered by Google models like Gemini.
+- `groq_example.py` shows how to make a chatbot powered by Groq models.
- `mlx_chat.py` shows a simple chatbot using local on-device models with Apple's [MLX](https://github.com/ml-explore/mlx), a machine learning framework from Apple that is similar to JAX and PyTorch. This specific example uses the [mlx-lm](https://github.com/ml-explore/mlx-examples/tree/main/llms) library. Note that Apple Silicon chips are required for using MLX.
- `llm_datasette.py` shows how to make a chatbot powered by Simon W's LLM library.
- `dagger_code_interpreter.py` shows how to make a basic code-interpreter chatbot powered by Dagger containers.
- `recipe_bot.py` shows how to make a chatbot that can parse recipes from images.
- `simplemind_example.py` shows how to integrate [simplemind](https://github.com/kennethreitz/simplemind).
+- `generative_ui.py` shows how to make a chatbot that can generate UI code.
Chatbot's in marimo are _reactive_: when the chatbot responds with a message,
all other cells referencing the chatbot are automatically run or marked
diff --git a/examples/ai/chat/generative_ui.py b/examples/ai/chat/generative_ui.py
new file mode 100644
index 0000000000..48c500c392
--- /dev/null
+++ b/examples/ai/chat/generative_ui.py
@@ -0,0 +1,95 @@
+# /// script
+# requires-python = ">=3.12"
+# dependencies = [
+# "ell-ai==0.0.14",
+# "marimo",
+# "openai==1.53.0",
+# "polars==1.12.0",
+# ]
+# ///
+
+import marimo
+
+__generated_with = "0.9.14"
+app = marimo.App(width="medium")
+
+
+def __():
+ import polars as pl
+ import marimo as mo
+ import os
+
+ has_api_key = os.environ.get("OPENAI_API_KEY") is not None
+ mo.stop(
+ not has_api_key,
+ mo.md("Please set the `OPENAI_API_KEY` environment variable").callout(),
+ )
+
+ # Grab a dataset
+ df = pl.read_csv("hf://datasets/scikit-learn/Fish/Fish.csv")
+ return df, has_api_key, mo, os, pl
+
+
+def __(df, mo):
+ import ell
+
+
+ @ell.tool()
+ def chart_data(x_encoding: str, y_encoding: str, color: str):
+ """Generate an altair chart"""
+ import altair as alt
+
+ return (
+ alt.Chart(df)
+ .mark_circle()
+ .encode(x=x_encoding, y=y_encoding, color=color)
+ .properties(width=500)
+ )
+
+
+ @ell.tool()
+ def filter_dataset(sql_query: str):
+ """
+ Filter a polars dataframe using SQL. Please only use fields from the schema.
+ When referring to the table in SQL, call it 'data'.
+ """
+ filtered = df.sql(sql_query, table_name="data")
+ return mo.ui.table(
+ filtered,
+ label=f"```sql\n{sql_query}\n```",
+ selection=None,
+ show_column_summaries=False,
+ )
+ return chart_data, ell, filter_dataset
+
+
+def __(chart_data, df, ell, filter_dataset, mo):
+ @ell.complex(model="gpt-4o", tools=[chart_data, filter_dataset])
+ def analyze_dataset(prompt: str) -> str:
+ """You are a data scientist that can analyze a dataset"""
+ return f"I have a dataset with schema: {df.schema}. \n{prompt}"
+
+
+ def my_model(messages):
+ response = analyze_dataset(messages)
+ if response.tool_calls:
+ return response.tool_calls[0]()
+ return response.text
+
+
+ mo.ui.chat(
+ my_model,
+ prompts=[
+ "Can you chart two columns of your choosing?",
+ "Can you find the min, max of all numeric fields?",
+ "What is the sum of {{column}}?",
+ ],
+ )
+ return analyze_dataset, my_model
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/examples/ai/chat/groq-example.py b/examples/ai/chat/groq_example.py
similarity index 100%
rename from examples/ai/chat/groq-example.py
rename to examples/ai/chat/groq_example.py
diff --git a/examples/ai/chat/recipe_bot.py b/examples/ai/chat/recipe_bot.py
index 7eddb453b2..b4fd25cf3f 100644
--- a/examples/ai/chat/recipe_bot.py
+++ b/examples/ai/chat/recipe_bot.py
@@ -1,3 +1,10 @@
+# /// script
+# requires-python = ">=3.12"
+# dependencies = [
+# "marimo",
+# "openai==1.53.0",
+# ]
+# ///
import marimo
__generated_with = "0.9.10"