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.

ai: improve prompt to not comment on the code (#5822)

Myles Scolnickf21930a17d4a
diff --git a/frontend/src/components/editor/ai/ai-completion-editor.tsx b/frontend/src/components/editor/ai/ai-completion-editor.tsx
index 541cf0b248..283b95da43 100644
--- a/frontend/src/components/editor/ai/ai-completion-editor.tsx
+++ b/frontend/src/components/editor/ai/ai-completion-editor.tsx
@@ -72,7 +72,7 @@ export const AiCompletionEditor: React.FC<Props> = ({
const runtimeManager = useRuntimeManager();
const {
- completion,
+ completion: untrimmedCompletion,
input,
stop,
isLoading,
@@ -105,6 +105,7 @@ export const AiCompletionEditor: React.FC<Props> = ({
});
const inputRef = React.useRef<ReactCodeMirrorRef>(null);
+ const completion = untrimmedCompletion.trimEnd();
// Focus the input
useEffect(() => {
diff --git a/marimo/_server/ai/prompts.py b/marimo/_server/ai/prompts.py
index a336e45969..66d6afba0d 100644
--- a/marimo/_server/ai/prompts.py
+++ b/marimo/_server/ai/prompts.py
@@ -93,7 +93,7 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
f"Your output must be valid {language} code.\n"
"You can use the provided context to help you write the new cell.\n"
"You can reference variables from other cells, but you cannot redefine a variable if it already exists.\n"
- "Immediately start with the following format with no remarks. \n\n"
+ "Immediately start with the following format. Do NOT comment on the code, just output the code itself: \n\n"
"```\n{CELL_CODE}\n```"
)
@@ -121,7 +121,7 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
"<insert_here></insert_here> tags. Don't include the insert_here tags in your output.\n"
"Match the indentation in the original file in the inserted content, "
"don't include any indentation on blank lines.\n"
- "Immediately start with the following format with no remarks:\n\n"
+ "Immediately start with the following format. Do NOT comment on the code, just output the code itself:\n\n"
"```\n{INSERTED_CODE}\n```"
)
else:
@@ -131,7 +131,7 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
"Start at the indentation level in the original file in the rewritten content. "
"Don't stop until you've rewritten the entire section, even if you have no more changes to make, "
"always write out the whole section with no unnecessary elisions.\n"
- "Immediately start with the following format with no remarks:\n\n"
+ "Immediately start with the following format. Do NOT comment on the code, just output the code itself:\n\n"
"```\n{REWRITTEN_CODE}\n```"
)
@@ -156,6 +156,8 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
"code_from_other_cells", other_cell_codes
)
+ system_prompt += "\n\nAgain, just output the code itself."
+
return system_prompt
diff --git a/tests/_server/ai/snapshots/system_prompts.txt b/tests/_server/ai/snapshots/system_prompts.txt
index 2ca0e3db40..6d5d5e2101 100644
--- a/tests/_server/ai/snapshots/system_prompts.txt
+++ b/tests/_server/ai/snapshots/system_prompts.txt
@@ -7,7 +7,7 @@ You goal is to create a new cell in the notebook.
Your output must be valid python code.
You can use the provided context to help you write the new cell.
You can reference variables from other cells, but you cannot redefine a variable if it already exists.
-Immediately start with the following format with no remarks.
+Immediately start with the following format. Do NOT comment on the code, just output the code itself:
```
{CELL_CODE}
@@ -22,6 +22,8 @@ Immediately start with the following format with no remarks.
6. If an import already exists, do not import it again.
7. If a variable is already defined, use another name, or make it private by adding an underscore at the beginning.
+Again, just output the code itself.
+
==================== markdown ====================
You are an AI assistant integrated into the marimo notebook code editor.
@@ -29,12 +31,14 @@ You goal is to create a new cell in the notebook.
Your output must be valid markdown code.
You can use the provided context to help you write the new cell.
You can reference variables from other cells, but you cannot redefine a variable if it already exists.
-Immediately start with the following format with no remarks.
+Immediately start with the following format. Do NOT comment on the code, just output the code itself:
```
{CELL_CODE}
```
+Again, just output the code itself.
+
==================== sql ====================
You are an AI assistant integrated into the marimo notebook code editor.
@@ -42,7 +46,7 @@ You goal is to create a new cell in the notebook.
Your output must be valid sql code.
You can use the provided context to help you write the new cell.
You can reference variables from other cells, but you cannot redefine a variable if it already exists.
-Immediately start with the following format with no remarks.
+Immediately start with the following format. Do NOT comment on the code, just output the code itself:
```
{CELL_CODE}
@@ -51,6 +55,8 @@ Immediately start with the following format with no remarks.
## Rules for sql
1. The SQL must use duckdb syntax.
+Again, just output the code itself.
+
==================== idk ====================
You are an AI assistant integrated into the marimo notebook code editor.
@@ -58,12 +64,14 @@ You goal is to create a new cell in the notebook.
Your output must be valid idk code.
You can use the provided context to help you write the new cell.
You can reference variables from other cells, but you cannot redefine a variable if it already exists.
-Immediately start with the following format with no remarks.
+Immediately start with the following format. Do NOT comment on the code, just output the code itself:
```
{CELL_CODE}
```
+Again, just output the code itself.
+
==================== with custom rules ====================
You are an AI assistant integrated into the marimo notebook code editor.
@@ -71,7 +79,7 @@ You goal is to create a new cell in the notebook.
Your output must be valid python code.
You can use the provided context to help you write the new cell.
You can reference variables from other cells, but you cannot redefine a variable if it already exists.
-Immediately start with the following format with no remarks.
+Immediately start with the following format. Do NOT comment on the code, just output the code itself:
```
{CELL_CODE}
@@ -89,6 +97,8 @@ Immediately start with the following format with no remarks.
## Additional rules:
Always use type hints.
+Again, just output the code itself.
+
==================== with context ====================
You are an AI assistant integrated into the marimo notebook code editor.
@@ -96,7 +106,7 @@ You goal is to create a new cell in the notebook.
Your output must be valid python code.
You can use the provided context to help you write the new cell.
You can reference variables from other cells, but you cannot redefine a variable if it already exists.
-Immediately start with the following format with no remarks.
+Immediately start with the following format. Do NOT comment on the code, just output the code itself:
```
{CELL_CODE}
@@ -121,6 +131,8 @@ Immediately start with the following format with no remarks.
- Sample values: Alice, Bob, Charlie
+Again, just output the code itself.
+
==================== with is_insert=True ====================
Here's a python document from a Python notebook that I'm going to ask you to make an edit to.
@@ -135,7 +147,7 @@ def fib(n):
You can't replace the content, your answer will be inserted in place of the <insert_here></insert_here> tags. Don't include the insert_here tags in your output.
Match the indentation in the original file in the inserted content, don't include any indentation on blank lines.
-Immediately start with the following format with no remarks:
+Immediately start with the following format. Do NOT comment on the code, just output the code itself:
```
{INSERTED_CODE}
@@ -150,6 +162,8 @@ Immediately start with the following format with no remarks:
6. If an import already exists, do not import it again.
7. If a variable is already defined, use another name, or make it private by adding an underscore at the beginning.
+Again, just output the code itself.
+
==================== with cell_code ====================
Here's a python document from a Python notebook that I'm going to ask you to make an edit to.
@@ -164,7 +178,7 @@ def hello():
Only make changes that are necessary to fulfill the prompt, leave everything else as-is. All surrounding content will be preserved.
Start at the indentation level in the original file in the rewritten content. Don't stop until you've rewritten the entire section, even if you have no more changes to make, always write out the whole section with no unnecessary elisions.
-Immediately start with the following format with no remarks:
+Immediately start with the following format. Do NOT comment on the code, just output the code itself:
```
{REWRITTEN_CODE}
@@ -185,6 +199,8 @@ print('Hello, world!')
6. If an import already exists, do not import it again.
7. If a variable is already defined, use another name, or make it private by adding an underscore at the beginning.
+Again, just output the code itself.
+
==================== with selected_text ====================
Here's a python document from a Python notebook that I'm going to ask you to make an edit to.
@@ -199,7 +215,7 @@ def hello():
Only make changes that are necessary to fulfill the prompt, leave everything else as-is. All surrounding content will be preserved.
Start at the indentation level in the original file in the rewritten content. Don't stop until you've rewritten the entire section, even if you have no more changes to make, always write out the whole section with no unnecessary elisions.
-Immediately start with the following format with no remarks:
+Immediately start with the following format. Do NOT comment on the code, just output the code itself:
```
{REWRITTEN_CODE}
@@ -220,6 +236,8 @@ print('Hello, world!')
6. If an import already exists, do not import it again.
7. If a variable is already defined, use another name, or make it private by adding an underscore at the beginning.
+Again, just output the code itself.
+
==================== with other_cell_codes ====================
Here's a python document from a Python notebook that I'm going to ask you to make an edit to.
@@ -233,7 +251,7 @@ The section you'll need to rewrite is marked with <rewrite_this></rewrite_this>
Only make changes that are necessary to fulfill the prompt, leave everything else as-is. All surrounding content will be preserved.
Start at the indentation level in the original file in the rewritten content. Don't stop until you've rewritten the entire section, even if you have no more changes to make, always write out the whole section with no unnecessary elisions.
-Immediately start with the following format with no remarks:
+Immediately start with the following format. Do NOT comment on the code, just output the code itself:
```
{REWRITTEN_CODE}
@@ -259,6 +277,8 @@ import pandas as pd
import numpy as np
</code_from_other_cells>
+Again, just output the code itself.
+
==================== with VariableContext objects ====================
You are an AI assistant integrated into the marimo notebook code editor.
@@ -266,7 +286,7 @@ You goal is to create a new cell in the notebook.
Your output must be valid python code.
You can use the provided context to help you write the new cell.
You can reference variables from other cells, but you cannot redefine a variable if it already exists.
-Immediately start with the following format with no remarks.
+Immediately start with the following format. Do NOT comment on the code, just output the code itself:
```
{CELL_CODE}
@@ -288,3 +308,6 @@ Immediately start with the following format with no remarks.
- variable: `model`
- value_type: Model
- value_preview: <Model object>
+
+
+Again, just output the code itself.
\ No newline at end of file