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: datasette/llm chatbot (#2542)

Myles Scolnickfc3c61b2954d
diff --git a/examples/ai/chat/README.md b/examples/ai/chat/README.md
index aa83c43e09..ceb7a06832 100644
--- a/examples/ai/chat/README.md
+++ b/examples/ai/chat/README.md
@@ -6,6 +6,7 @@ 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
+- `llm_datasette.py` shows how to make a chatbot powered by Simon W's LLM library
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/llm_datasette.py b/examples/ai/chat/llm_datasette.py
new file mode 100644
index 0000000000..3f81639308
--- /dev/null
+++ b/examples/ai/chat/llm_datasette.py
@@ -0,0 +1,51 @@
+# /// script
+# requires-python = ">=3.12"
+# dependencies = [
+# "llm==0.16",
+# "marimo",
+# ]
+# ///
+
+import marimo
+
+__generated_with = "0.9.3"
+app = marimo.App()
+
+
[email protected](hide_code=True)
+def __(mo):
+ mo.md(r"""## Using <https://llm.datasette.io> with `mo.ui.chat()`""")
+ return
+
+
[email protected](hide_code=True)
+def __(mo):
+ mo.md("""To set a key, run: `llm keys set openai` in your terminal""")
+ return
+
+
+def __():
+ import marimo as mo
+ import llm
+ return llm, mo
+
+
+def __(llm, mo):
+ model = llm.get_model("gpt-4o-mini")
+ conversation = model.conversation()
+
+ chat = mo.ui.chat(lambda messages: conversation.prompt(messages[-1].content))
+ chat
+ return chat, conversation, model
+
+
+def __(chat):
+ chat.value
+ return
+
+
+if __name__ == "__main__":
+ app.run()