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: code-interpreter with dagger (#2599)
Myles Scolnicke78e5745861b
diff --git a/examples/ai/chat/README.md b/examples/ai/chat/README.mdindex 544ffec6b0..d0771f363a 100644--- a/examples/ai/chat/README.md+++ b/examples/ai/chat/README.md@@ -6,8 +6,9 @@ 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-- `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.+- `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.Chatbot's in marimo are _reactive_: when the chatbot responds with a message,all other cells referencing the chatbot are automatically run or markeddiff --git a/examples/ai/chat/dagger_code_interpreter.py b/examples/ai/chat/dagger_code_interpreter.pynew file mode 100644index 0000000000..7dfe432ddf--- /dev/null+++ b/examples/ai/chat/dagger_code_interpreter.py@@ -0,0 +1,163 @@+# /// script+# requires-python = ">=3.12"+# dependencies = [+# "dagger-io==0.13.5",+# "ell-ai==0.0.13",+# "marimo",+# "openai==1.51.0",+# ]+# ///++import marimo++__generated_with = "0.9.9"+app = marimo.App(width="medium")++[email protected](hide_code=True)+def __():+ import marimo as mo+ import ell+ import textwrap+ return ell, mo, textwrap++[email protected](hide_code=True)+def __(mo):+ mo.md(+ """+ # Chatbot code-interpreter with [Dagger](https://dagger.io/)++ This example shows how to create a code-interpreter that executes code using [Dagger](https://dagger.io/) so the code is run in an isolated container.++ This example requires Docker running on your computer.+ """+ )+ return++[email protected](hide_code=True)+def __(mo):+ backend = mo.ui.dropdown(["ollama", "openai"], label="Backend", value="openai")+ backend+ return (backend,)++[email protected](hide_code=True)+def __(mo):+ # OpenAI config+ import os+ import openai++ input_key = mo.ui.text(+ label="OpenAI API key",+ kind="password",+ value=os.environ.get("OPENAI_API_KEY", ""),+ )+ input_key+ return input_key, openai, os++[email protected](hide_code=True)+def __(backend, input_key, mo):+ def _get_open_ai_client():+ openai_key = input_key.value++ import openai++ mo.stop(+ not openai_key,+ mo.md(+ "Please set the `OPENAI_API_KEY` environment variable or provide it in the input field"+ ),+ )++ return openai.Client(api_key=openai_key)+++ def _get_ollama_client():+ import openai++ return openai.Client(+ api_key="ollama",+ base_url="http://localhost:11434/v1",+ )+++ client = (+ _get_ollama_client()+ if backend.value == "ollama"+ else _get_open_ai_client()+ )+ model = "llama3.1" if backend.value == "ollama" else "gpt-4-turbo"+ return client, model+++def __():+ import dagger+ return (dagger,)+++def __(dagger, ell, mo):+ @ell.tool()+ async def execute_code(code: str):+ """+ Execute python using Dagger. You MUST have print() in the last expression.+ """+ async with dagger.Connection() as _dag:+ container = (+ _dag.container()+ .from_("python:3.12-slim")+ .with_new_file("/app/script.py", contents=code)+ )++ result = await container.with_exec(+ ["python", "/app/script.py"]+ ).stdout()++ return mo.vstack(+ [+ mo.ui.code_editor(code, language="python", disabled=True),+ mo.md(result),+ ]+ )+ return (execute_code,)+++def __(client, ell, execute_code, mo, model):+ @ell.complex(model=model, tools=[execute_code], client=client)+ def custom_chatbot(messages, config) -> str:+ """You are data scientist with access to writing python code."""+ return [+ ell.user(message.content)+ if message.role == "user"+ else ell.assistant(message.content)+ for message in messages+ ]+++ def my_model(messages, config):+ response = custom_chatbot(messages, config)+ if response.tool_calls:+ return response.tool_calls[0]()+ return mo.md(response.text)+ return custom_chatbot, my_model+++def __(mo, my_model):+ mo.ui.chat(+ my_model,+ prompts=[+ "What is the square root of {{number}}?",+ f"Can you sum this list using python: {list(range(1, 10))}",+ ],+ )+ return+++if __name__ == "__main__":+ app.run()diff --git a/examples/ai/tools/chat_with_tools.py b/examples/ai/tools/chat_with_tools.pyindex bebeaa2b00..b1d64bd98c 100644--- a/examples/ai/tools/chat_with_tools.py+++ b/examples/ai/tools/chat_with_tools.py@@ -59,7 +59,7 @@ def __(mo):input_key = mo.ui.text(label="OpenAI API key",kind="password",- value=os.environ.get("OPENAI_API_KEY"),+ value=os.environ.get("OPENAI_API_KEY", ""),)input_keyreturn input_key, os, os_keydiff --git a/examples/ai/tools/code_interpreter.py b/examples/ai/tools/code_interpreter.pyindex fc6ed1f1d6..9f5bfa6d81 100644--- a/examples/ai/tools/code_interpreter.py+++ b/examples/ai/tools/code_interpreter.py@@ -50,7 +50,7 @@ def __(backend, mo):input_key = mo.ui.text(label="OpenAI API key",kind="password",- value=os.environ.get("OPENAI_API_KEY"),+ value=os.environ.get("OPENAI_API_KEY", ""),)input_key if backend.value == "openai" else Nonereturn input_key, openai, os, os_keydiff --git a/marimo/_runtime/packages/module_name_to_pypi_name.py b/marimo/_runtime/packages/module_name_to_pypi_name.pyindex 5a0697b0d7..44b28d87ec 100644--- a/marimo/_runtime/packages/module_name_to_pypi_name.py+++ b/marimo/_runtime/packages/module_name_to_pypi_name.py@@ -375,6 +375,7 @@ def module_name_to_pypi_name() -> dict[str, str]:"curator": "elasticsearch-curator","curl": "pycurl","daemon": "python-daemon",+ "dagger": "dagger-io","dare": "DARE","dateutil": "python-dateutil","dawg": "DAWG",