Skip to content

Commit

Permalink
Escale LaTeX delimeters with regex instead of prompt engineering
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-i committed Feb 6, 2024
1 parent 2ea0a2e commit 46b1107
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/jupyter-ai-magics/jupyter_ai_magics/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
You are talkative and you provide lots of specific details from the foundation model's context.
You may use Markdown to format your response.
Code blocks must be formatted in Markdown.
Math should be rendered with inline TeX markup, surrounded by $. Use $ and $$ style LaTeX delimiters only.
Math should be rendered with inline TeX markup, surrounded by $.
If you do not know the answer to a question, answer truthfully by responding that you do not know.
The following is a friendly conversation between you and a human.
""".strip()
Expand Down
14 changes: 13 additions & 1 deletion packages/jupyter-ai/src/components/rendermime-markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@ function RendermimeMarkdownBase(props: RendermimeMarkdownProps): JSX.Element {
);
const containerRef = useRef<HTMLDivElement>(null);

/**
* Escape LaTeX delimeters by adding extra backslashes for proper rendering by @jupyterlab/rendermime.
*/
function escapeLatexDelimiters(latexString: string) {
return latexString
.replace(/\\\(/g, '\\\\(')
.replace(/\\\)/g, '\\\\)')
.replace(/\\\[/g, '\\\\[')
.replace(/\\\]/g, '\\\\]');
}

useEffect(() => {
const renderContent = async () => {
const mdStr = escapeLatexDelimiters(props.markdownStr);
const model = props.rmRegistry.createModel({
data: { [MD_MIME_TYPE]: props.markdownStr }
data: { [MD_MIME_TYPE]: mdStr }
});

const renderer = props.rmRegistry.createRenderer(MD_MIME_TYPE);
Expand Down

0 comments on commit 46b1107

Please sign in to comment.