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
Include `Custom-AI-rules`; docs touchups (#2795)
Srihari Thyagarajan68d83dec328a
diff --git a/docs/_static/docs-ai-completion-custom-assist-rules.png b/docs/_static/docs-ai-completion-custom-assist-rules.pngnew file mode 100644index 0000000000..87001d8be5Binary files /dev/null and b/docs/_static/docs-ai-completion-custom-assist-rules.png differdiff --git a/docs/api/layouts/callout.md b/docs/api/layouts/callout.mdindex 76549f3ca8..a6833f001c 100644--- a/docs/api/layouts/callout.md+++ b/docs/api/layouts/callout.md@@ -6,7 +6,7 @@def __():callout_kind = mo.ui.dropdown(label="Color",- options=["neutral", "danger", "warn", "success"],+ options=["info", "neutral", "danger", "warn", "success"],value="neutral",)returndiff --git a/docs/api/plotting.md b/docs/api/plotting.mdindex 5a0536ddd1..62a103ec25 100644--- a/docs/api/plotting.md+++ b/docs/api/plotting.md@@ -122,3 +122,4 @@ You can use all the popular plotting libraries with marimo. Such as:- [HoloViews](http://holoviews.org/)- [hvPlot](https://hvplot.holoviz.org/)- [Leafmap](https://leafmap.org/)+- [Pygwalker](https://kanaries.net/pygwalker)diff --git a/docs/guides/configuration/index.md b/docs/guides/configuration/index.mdindex 9c2990a682..bf477a167f 100644--- a/docs/guides/configuration/index.md+++ b/docs/guides/configuration/index.md@@ -21,7 +21,7 @@ App Configuration is specific to each notebook and is stored in the `notebook.py- Notebook width- Notebook title-- Custom CSS+- [Custom CSS](/guides/configuration/theming)- Automatically download HTML snapshotsConfigure these settings through the notebook menu.@@ -44,10 +44,10 @@ You can customize the following:- [Hotkeys](/guides/editor_features/hotkeys.md)- Completion (auto-completion, AI copilot, etc.)- Display (theme, font size, output placement, etc.)-- Autosave-- Package management+- Autosave+- [Package management](/guides/editor_features/package_management.md#package-management)- Server settings-- VIM keybindings+- [VIM keybindings](/guides/editor_features/overview.md#vim-keybindings)- Formatting settings- [AI assistance](/guides/editor_features/ai_completion.md)diff --git a/docs/guides/editor_features/ai_completion.md b/docs/guides/editor_features/ai_completion.mdindex f9ed08b04a..0be3f37cbe 100644--- a/docs/guides/editor_features/ai_completion.md+++ b/docs/guides/editor_features/ai_completion.md@@ -16,8 +16,11 @@ _Get started with Copilot_:1. Install [Node.js](https://nodejs.org/en/download).2. Enable Copilot via the settings menu in the marimo editor.-_Note_: Copilot is not yet available in our conda distribution; please install-marimo using `pip` if you need Copilot.+```{admonition} Installation Requirement+:class: note+ Copilot is not yet available in our conda distribution; please install+ marimo using ``pip`` if you need Copilot.+```## Codeium Copilot@@ -71,14 +74,51 @@ activate_on_typing = true## Generate code with our AI assistant-marimo has built-in support for generating and refactoring code with AI, with a-variety of providers. marimo works with both hosted AI providers, such as-OpenAI and Anthropic, as well as local models served via Ollama.+marimo has built-in support for generating and refactoring code with AI, with a variety of providers. marimo works with hosted AI providers, such as OpenAI, Anthropic, and Google, as well as local models served via Ollama.++### Custom AI Rules-Below we describe how to connect marimo to your AI provider. Once enabled, you-can generate entirely new cells by clicking the "Generate with AI" button at-the bottom of your notebook. You can also refactor existing cells by inputting-`Ctrl/Cmd-Shift-e` in a cell, opening an input to modify the cell using AI.+You can customize how the AI assistant behaves by adding rules in the marimo settings. These rules help ensure consistent code generation across all AI providers. You can find more information about marimo's supported plotting libraries and data handling in the [plotting guide](https://docs.marimo.io/guides/working_with_data/plotting.html#plotting) and [working with data guide](https://docs.marimo.io/guides/working_with_data/index.html).++<div align="center">+ <figure>+ <img src="/_static/docs-ai-completion-custom-assist-rules.png"/>+ <figcaption>Configure custom AI rules in settings</figcaption>+ </figure>+</div>++For example, you can add rules about:+- Preferred plotting libraries (matplotlib, plotly, altair)+- Data handling practices+- Code style conventions+- Error handling preferences++Example custom rules:+```+Use plotly for interactive visualizations and matplotlib for static plots+Prefer polars over pandas for data manipulation due to better performance+Include docstrings for all functions using NumPy style+Use Type hints for all function parameters and return values+Handle errors with try/except blocks and provide informative error messages+Follow PEP 8 style guidelines+When working with data:+- Use altair, plotly for declarative visualizations+- Prefer polars over pandas+- Ensure proper error handling for data operations+For plotting:+- Use px.scatter for scatter plots+- Use px.line for time series+- Include proper axis labels and titles+- Set appropriate color schemes+```++To locate your configuration file, run:+```bash+marimo config show+```+At the top, the path to your `marimo.toml` file will be shown. You can Ctrl/Cmd+click the path to open it in your editor. For more information about configuration, see the [Configuration Guide](/guides/configuration/index).++Below we describe how to connect marimo to your AI provider. Once enabled, you can generate entirely new cells by clicking the "Generate with AI" button at the bottom of your notebook. You can also refactor existing cells by inputting `Ctrl/Cmd-Shift-e` in a cell, opening an input to modify the cell using AI.<div align="center"><figure>@@ -99,6 +139,9 @@ the bottom of your notebook. You can also refactor existing cells by inputtingapi_key = "sk-proj-..."# Choose a model, we recommend "gpt-4-turbo"model = "gpt-4-turbo"+# Available models: gpt-4-turbo-preview, gpt-4, gpt-3.5-turbo+# See https://platform.openai.com/docs/models for all available models+# Change the base_url if you are using a different OpenAI-compatible APIbase_url = "https://api.openai.com/v1"```@@ -119,32 +162,6 @@ model = "claude-3-5-sonnet-20240620"api_key = "sk-ant-..."```-### Using other AI providers--marimo supports OpenAI's GPT-3.5 API by default. If your provider is compatible with OpenAI's API, you can use it by changing the `base_url` in the configuration.--For other providers not compatible with OpenAI's API, please submit a [feature request](https://github.com/marimo-team/marimo/issues) or "thumbs up" an existing one.--### Using local models with Ollama--Ollama allows you to run open-source LLMs (e.g. Llama 3.1, Phi 3, Mistral,-Gemma 2) on your local machine. To integrate Ollama with marimo:--1. Download and install [Ollama](https://ollama.com/).-2. Download the model you want to use:- 1. `ollama pull llama3.1`- 2. We also recommend `codellama` (code specific).-3. Start the Ollama server: `ollama run llama3.1`-4. Visit <http://localhost:11434> to confirm that the server is running.-5. Add the following to your `marimo.toml`:--```toml-[ai.open_ai]-api_key = "ollama" # This is not used, but required-model = "llama3.1" # or the model you downloaded from above-base_url = "http://localhost:11434/v1"-```-### Using Google AITo use Google AI with marimo:@@ -162,4 +179,48 @@ model = "gemini-1.5-flash"api_key = "AI..."```-You can now use Google AI for code generation and refactoring in marimo.+### Using local models with Ollama++Ollama allows you to run open-source LLMs on your local machine. To integrate Ollama with marimo:++1. Download and install [Ollama](https://ollama.com/).+2. Download the model you want to use:+ ```bash+ # View available models at https://ollama.com/library+ ollama pull llama3.1+ ollama pull codellama # recommended for code generation++ # View your installed models+ ollama ls+ ```+3. Start the Ollama server in a terminal:+ ```bash+ ollama serve+ # In a new terminal+ ollama run codellama # or any model from ollama ls+ ```+4. Visit http://127.0.0.1:11434 to confirm that the server is running.++ > **Note**: If you get a "port already in use" error, you may need to close an existing Ollama instance. On Windows, click the up arrow in the taskbar, find the Ollama icon, and select "Quit". This is a known issue (see [Ollama Issue #3575](https://github.com/ollama/ollama/issues/3575)). Once you've closed the existing Ollama instance, you should be able to run `ollama serve` successfully.++5. Open a new terminal and start marimo:+ ```bash+ marimo edit notebook.py+ ```++6. Add the following to your `marimo.toml`:++```toml+[ai.open_ai]+api_key = "ollama" # This is not used, but required+model = "codellama" # or another model from `ollama ls`+base_url = "http://127.0.0.1:11434/v1"+```++### Using other AI providers++marimo supports OpenAI's API by default. Many providers offer OpenAI API-compatible endpoints, which can be used by simply changing the `base_url` in your configuration. For example, providers like [GROQ](https://console.groq.com/docs/openai) follow this pattern.++For a comprehensive list of compatible providers and their configurations, please refer to the [liteLLM Providers documentation](https://litellm.vercel.app/docs/providers).++For providers not compatible with OpenAI's API, please submit a [feature request](https://github.com/marimo-team/marimo/issues/new?template=feature_request.yaml) or "thumbs up" an existing one.diff --git a/examples/ai/chat/openai_example.py b/examples/ai/chat/openai_example.pyindex 3d76b3d59c..670d432460 100644--- a/examples/ai/chat/openai_example.py+++ b/examples/ai/chat/openai_example.py@@ -2,12 +2,13 @@# requires-python = ">=3.11"# dependencies = [# "marimo",+# "openai==1.54.1",# ]# ///import marimo-__generated_with = "0.9.0"+__generated_with = "0.9.14"app = marimo.App(width="medium")@@ -57,16 +58,16 @@ def __(mo, openai_key):"gpt-4o",system_message="You are a helpful assistant.",api_key=openai_key,- allow_attachments=[- "image/png",- "image/jpeg"- ],),prompts=["Hello","How are you?","I'm doing great, how about you?",],+ allow_attachments=[+ "image/png",+ "image/jpeg"+ ],)chatbotreturn (chatbot,)diff --git a/frontend/e2e-tests/py/layout_grid.py b/frontend/e2e-tests/py/layout_grid.pyindex 10c23231be..80fd98e4b1 100644--- a/frontend/e2e-tests/py/layout_grid.py+++ b/frontend/e2e-tests/py/layout_grid.py@@ -1,12 +1,12 @@import marimo-__generated_with = "0.1.2"+__generated_with = "0.9.14"app = marimo.App(layout_file="layouts/layout_grid.grid.json")@app.celldef __(mo):- mo.md("# Grid Layout")+ mo.md("""# Grid Layout""")return@@ -20,25 +20,25 @@ def __(mo, search):def __(mo):search = mo.ui.text(label="Search")search- return search,+ return (search,)@app.celldef __(mo):- mo.md("text 1")+ mo.md("""text 1""")return@app.celldef __(mo):- mo.md("text 2")+ mo.md("""text 2""")return@app.celldef __():import marimo as mo- return mo,+ return (mo,)if __name__ == "__main__":