API GPT-4-Turbo-Preview has output in latex format in math

Hello community, I am encountering an issue with using the GPT-4-Turbo-Preview API for a simple ChatBot on my website. When someone asks a math-related question, the responses are provided in LaTeX format, which my website cannot interpret, resulting in the answers appearing as shown in the attached screenshot.

However, when using the GPT-4 API (not turbo), the responses are correctly delivered using Unicode characters without the need to specify this in the role. I have tried setting the system role to respond with Unicode characters in the GPT-4-Turbo-Preview API for mathematical terms, but it’s not perfect as parts of the response are still in LaTeX format.

Is there any way to resolve this issue? Thank you.

1 Like

The AI models are overtrained on being ChatGPT. That includes ChatGPT answers, ChatGPT markdown, and even telling the user that it is ChatGPT on a developer’s app. gpt-4-turbo is now just a canned answer machine, not an intelligence.

And LaTeX.

I see your demonstration, but not the attempts at system prompt. Let’s write one.

## Mathematics equation outputs
// When responding with math formulas in the response, you must write the formulae using only Unicode from the Mathematical Operators block and other Unicode symbols. The AI GUI render engine does not support TeX code. You must not use LaTeX in responses.

paste into notepad++

… done.

(thats not long enough…)

well. Ill take a moment ot explain why i use notepad ++…
oh thats now.

Same problem. Is it working now ?

Yeah this is an issue. I have managed to get some work around this. I was using gpt-4-vision-preview model and the api when answering mathematical question answers with blackslashes. I will assume you have control on the api response in your website. You can using a library like react-latex-next and provide your input to the component as fomratted(gpt response) you need to format the gpt response with regex:
let result = inputString.replace(/\(/g, ‘$’);
result = result.replace(/\)/g, ‘$’); this will return a clean output that you can then use in the component

1 Like

im in a similar issue, but i kind of give up prevent it to output LaTex, i only ask to place <latex>…</latex> around to later capture it and throw at https://latex.codecogs.com/ and render as a image.

Here’s my solution to correctly parsing the new markdown from the latest gpt-4-turbo model which includes Latex for math functions. Should work for others using react to display.

yarn add react-markdown remark-math rehype-raw rehype-katex

import React from ‘react’;
import ReactMarkdown from ‘react-markdown’;
import rehypeRaw from ‘rehype-raw’;
import rehypeKatex from ‘rehype-katex’;
import remarkMath from ‘remark-math’;

const MarkdownWithLatex = ({ markdownText }) => {

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

const remarkMathOptions = {
    singleDollarTextMath: false,
}


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

);
};

export default MarkdownWithLatex;

2 Likes

I think ChatGPT web app has some similar code to preprocess the markdown for correct math format, I hope they can open source this util someday.

1 Like

It’s a LaTeX renderer, should be able to find libraries to do that fairly easily for most languages.