Skip to content

Commit

Permalink
allow dollar symbols to delimit inline math in human messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dlqqq committed Nov 6, 2024
1 parent 55ca34e commit ec568ea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/jupyter-ai-magics/jupyter_ai_magics/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
You may use Markdown to format your response.
If your response includes code, they must be enclosed in Markdown fenced code blocks (with triple backticks before and after).
If your response includes mathematical notation, they must be expressed in LaTeX markup and enclosed in LaTeX delimiters.
- Single dollar signs ($) should never be used as delimiters for inline math.
- Human messages may still use `$` symbols to delimit inline math.
However, your response should never use `$` symbols to delimit inline math.
- Valid inline math: `\\( \\infty \\)`
- Valid display math: `\\[ \\infty \\]`
- Invalid inline math: `$\\infty$`
Expand Down
22 changes: 14 additions & 8 deletions packages/jupyter-ai/src/components/rendermime-markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ function isTextNode(node: Node | null): node is Text {
* Escapes all `$` symbols present in an HTML element except those within the
* following elements: `pre`, `code`, `samp`, `kbd`.
*
* This prevents `$` symbols from being used as inline math delimiters, allowing
* `$` symbols to be used literally to denote quantities of USD. This does not
* escape literal `$` within elements that display their contents literally,
* like code elements. This overrides JupyterLab's default rendering of MarkDown
* w/ LaTeX.
* This prevents `$` symbols from being used as inline math delimiters in AI
* messages, allowing `$` symbols to be used literally to denote quantities of
* USD. This does not escape literal `$` within elements that display their
* contents literally, like code elements. This overrides JupyterLab's default
* rendering of MarkDown w/ LaTeX for AI messages.
*
* The Jupyter AI system prompt should explicitly request that the LLM not use
* `$` as an inline math delimiter. This is the default behavior.
Expand All @@ -82,7 +82,7 @@ function escapeDollarSymbols(el: HTMLElement) {
}
}

// Replace each `$` symbol with `\$` for each text node, unless there is
// Replaces each `$` symbol with `\$` for each text node, unless there is
// another `$` symbol adjacent or it is already escaped. Examples:
// - `$10 - $5` => `\$10 - \$5` (escaped)
// - `$$ \infty $$` => `$$ \infty $$` (unchanged)
Expand Down Expand Up @@ -131,8 +131,14 @@ function RendermimeMarkdownBase(props: RendermimeMarkdownProps): JSX.Element {
);
}

// step 2: render LaTeX via MathJax, while escaping single dollar symbols.
escapeDollarSymbols(renderer.node);
// step 2: render LaTeX via MathJax, while escaping single dollar symbols
// in agent messages.
if (
props.parentMessage?.type === 'agent' ||
props.parentMessage?.type === 'agent-stream'
) {
escapeDollarSymbols(renderer.node);
}
props.rmRegistry.latexTypesetter?.typeset(renderer.node);

// insert the rendering into renderingContainer if not yet inserted
Expand Down

0 comments on commit ec568ea

Please sign in to comment.