Math and AI workarounds

ozkn
Fri, 13 Dec 2024 09:12:43 +0000
Support

Add support for Latex style blocks and inlines for Math

artificial intelligence gives answers in latex format. I wrote a prompt to convert this latex format to mathjax format, but the prompt is not always implemented and responds in latex format. When I wrote my problem to the openai community, the following suggestion came.

"I decided it wasn’t even worth the effort of cluttering up a system prompt, when I could just do the following on my end with the results:

There are new suggestions from the openai community regarding the problems I am experiencing. Are you planning to make an update about this? suggestions are as follows.

Suggestion 1;

def parse_stream_to_katex(stream: Stream):
    """
    Takes an OpenAI Stream and replaces ChatGPT LaTeX delimiters
    with KateX ones.
    Yields text, not chunks
    """
    last_text = ""
    for chunk in stream:
        text = chunk.choices[0].delta.content
        if text:
            # Sometimes delimiters like \( can be split over two chunks.
            # If the previous chunk ended in \, prepend that to this chunk
            if last_text.endswith("\\"):
                text = last_text + text

            text = (
                text.replace(r"\[", "$$")
                .replace(r"\]", "$$")
                .replace(r"\(", "$")
                .replace(r"\)", "$")
            )
            last_text = text

            # If the text ends in \, we don't return it, we'll get include it in the next chunk
            if not text.endswith("\\"):
                yield text

Suggestion 2:

// MarkdownLaTeXRenderer.js
import React from 'react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import rehypeRaw from 'rehype-raw';
import rehypeKatex from 'rehype-katex';

const MarkdownLaTeXRenderer = ({ content }) => {
    // Replace \[ with $$ and \] with $$ to ensure compatibility
    const processedText = content
      .replace(/\\\[/g, '$$$')  // Replace all occurrences of \[ with $$
      .replace(/\\\]/g, '$$$') // Replace all occurrences of \] with $$
      .replace(/\\\(/g, '$$$')  // Replace all occurrences of \( with $$
      .replace(/\\\)/g, '$$$'); // Replace all occurrences of \) with $$

    const remarkMathOptions = {
        singleDollarTextMath: false,

};




return (
    <ReactMarkdown
        className="markdown-content"
        children={processedText}
        remarkPlugins={[[remarkMath, remarkMathOptions], remarkGfm]} // Pass options as the second element of the array
        rehypePlugins={[rehypeRaw, rehypeKatex]} // Include rehypeRaw for HTML, rehypeKatex for LaTeX
    />
);

};

export default MarkdownLaTeXRenderer;
’

16 messaggi - 8 partecipanti