Skip to content

Error when magic command prompt includes braces / curly brackets #27

Open
@JasonWeill

Description

@JasonWeill

Description

When using a Python kernel, I cannot use ChatGPT via the %%ai magic to analyze TypeScript code. The curly braces {} are being interpreted by the magic command, even if they are in a Python """ blockquote block.

Reproduce

  1. Create a new notebook.
  2. Run a cell containing %load_ext jupyter_ai.
  3. Run a cell with TypeScript in it. (See example below)
  4. See error (See example below.

Example input:

%%ai chatgpt
Analyze the following code, tell me what it does, and tell me something I can do to improve it.

"""
private _addToolbar(model: ICellModel): void {
  const cell = this._getCell(model);

  if (cell) {
    const toolbarWidget = new Toolbar();
    toolbarWidget.addClass(CELL_MENU_CLASS);

    const promises: Promise<void>[] = [];
    for (const { name, widget } of this._toolbar) {
      toolbarWidget.addItem(name, widget);
      if (
        widget instanceof ReactWidget &&
        (widget as ReactWidget).renderPromise !== undefined
      ) {
        (widget as ReactWidget).update();
        promises.push((widget as ReactWidget).renderPromise!);
      }
    }

    // Wait for all the buttons to be rendered before attaching the toolbar.
    Promise.all(promises)
      .then(() => {
        toolbarWidget.addClass(CELL_TOOLBAR_CLASS);
        (cell.layout as PanelLayout).insertWidget(0, toolbarWidget);

        // For rendered markdown, watch for resize events.
        cell.displayChanged.connect(this._resizeEventCallback, this);

        // Watch for changes in the cell's contents.
        cell.model.contentChanged.connect(this._changedEventCallback, this);

        // Hide the cell toolbar if it overlaps with cell contents
        this._updateCellForToolbarOverlap(cell);
      })
      .catch(e => {
        console.error('Error rendering buttons of the cell toolbar: ', e);
      });
  }
}
"""

Error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[5], line 1
----> 1 get_ipython().run_cell_magic('ai', 'chatgpt', 'Analyze the following code, tell me what it does, and tell me something I can do to improve it.\n\n"""\nprivate _addToolbar(model: ICellModel): void {\n  const cell = this._getCell(model);\n\n  if (cell) {\n    const toolbarWidget = new Toolbar();\n    toolbarWidget.addClass(CELL_MENU_CLASS);\n\n    const promises: Promise<void>[] = [];\n    for (const { name, widget } of this._toolbar) {\n      toolbarWidget.addItem(name, widget);\n      if (\n        widget instanceof ReactWidget &&\n        (widget as ReactWidget).renderPromise !== undefined\n      ) {\n        (widget as ReactWidget).update();\n        promises.push((widget as ReactWidget).renderPromise!);\n      }\n    }\n\n    // Wait for all the buttons to be rendered before attaching the toolbar.\n    Promise.all(promises)\n      .then(() => {\n        toolbarWidget.addClass(CELL_TOOLBAR_CLASS);\n        (cell.layout as PanelLayout).insertWidget(0, toolbarWidget);\n\n        // For rendered markdown, watch for resize events.\n        cell.displayChanged.connect(this._resizeEventCallback, this);\n\n        // Watch for changes in the cell\'s contents.\n        cell.model.contentChanged.connect(this._changedEventCallback, this);\n\n        // Hide the cell toolbar if it overlaps with cell contents\n        this._updateCellForToolbarOverlap(cell);\n      })\n      .catch(e => {\n        console.error(\'Error rendering buttons of the cell toolbar: \', e);\n      });\n  }\n}\n"""\n')

File /local/home/brgrange/micromamba/envs/docchat/lib/python3.11/site-packages/IPython/core/interactiveshell.py:2430, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
   2428 with self.builtin_trap:
   2429     args = (magic_arg_s, cell)
-> 2430     result = fn(*args, **kwargs)
   2432 # The code below prevents the output from being displayed
   2433 # when using magics with decodator @output_can_be_silenced
   2434 # when the last Python token in the expression is a ';'.
   2435 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File /local/home/brgrange/micromamba/envs/docchat/lib/python3.11/site-packages/jupyter_ai/magics.py:201, in AiMagics.ai(self, line, cell)
    199 # interpolate user namespace into prompt
    200 ip = get_ipython()
--> 201 prompt = prompt.format_map(FormatDict(ip.user_ns))
    203 # configure and instantiate provider
    204 ProviderClass = get_provider(provider_id)

ValueError: unexpected '{' in field name

Expected behavior

TypeScript code was analyzed by the AI.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBugs reported by usersscope:magicsIssues concerning the magic commandsstatus:triagedIssues that have been fully triaged

    Type

    No type

    Projects

    Status

    In Review

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions