Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JasonWeill opened this issue Apr 4, 2023 · 2 comments · May be fixed by #1260
Open

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

JasonWeill opened this issue Apr 4, 2023 · 2 comments · May be fixed by #1260
Labels
bug Bugs reported by users scope:magics Issues concerning the magic commands status:triaged Issues that have been fully triaged
Milestone

Comments

@JasonWeill
Copy link
Collaborator

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.

@JasonWeill JasonWeill added the bug Bugs reported by users label Apr 4, 2023
@welcome
Copy link

welcome bot commented Apr 4, 2023

Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! 🤗

If you haven't done so already, check out Jupyter's Code of Conduct. Also, please try to follow the issue template as it helps other other community members to contribute more effectively.
welcome
You can meet the other Jovyans by joining our Discourse forum. There is also an intro thread there where you can stop by and say Hi! 👋

Welcome to the Jupyter community! 🎉

@ellisonbg ellisonbg added the scope:magics Issues concerning the magic commands label Apr 7, 2023
@ellisonbg ellisonbg added this to the 0.8.0 Release milestone Apr 21, 2023
@JasonWeill JasonWeill changed the title Error when asking an AI to analyze TypeScript code using a Python kernel Error when magic command prompt includes braces / curly brackets Aug 27, 2024
@krassowski
Copy link
Member

krassowski commented Aug 27, 2024

In upstream IPython there is logic for variable expansion and a @no_var_expand decorator allowing to suppress this https://ipython.readthedocs.io/en/stable/api/generated/IPython.core.magic.html#IPython.core.magic.no_var_expand. I think that logic may be smarter than what jupyter-ai does, but I am also not sure why jupyter-ai implements its custom variable expansion logic.

francoculaciati added a commit to francoculaciati/jupyter-ai that referenced this issue Feb 20, 2025
@dlqqq dlqqq added this to Jupyter AI Feb 24, 2025
@dlqqq dlqqq moved this to Needs Review in Jupyter AI Feb 24, 2025
@dlqqq dlqqq linked a pull request Feb 24, 2025 that will close this issue
@dlqqq dlqqq moved this from Needs Review to In Review in Jupyter AI Feb 27, 2025
@dlqqq dlqqq added the status:triaged Issues that have been fully triaged label Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bugs reported by users scope:magics Issues concerning the magic commands status:triaged Issues that have been fully triaged
Projects
Status: In Review
Development

Successfully merging a pull request may close this issue.

4 participants