Rendering latex equations from the api response

I am building a chatbot which often needs to proccess latex language. I have a component that runs regex on the messages and identifies the latex and uses katex to render them out.

The problem I am running into is that i have pretty much no control, whatever I do with the system prompt, in how the response is generated. Sometimes chatgpt uses two \ sometimes it uses one and sometimes it uses four. I need it to always use four backslashes because \ being an escape character in itself.

I tried multiple system prompts but it is never consistent in its responses.

My current system prompt is:
If your response contains math terms, you should format your response in the proper latex delimiters. For inline equations you should use \\\\( and \\\\) for inline math equations and \\\\[ and \\\\] for block equations. It is important that you use four backslashes whenever you include a backslash, because of the way your response will be proccessed

but I tried using pretty much all combinations of wording and examples to get it to be consistent.

Did anyone have the same problem and came up with a solution?

1 Like

have you found any solution?
I have had the same problem

1 Like

I have the same question , how can i get Latex form from response in OpenAI
In my project , I was put chat.AppendSystemMessage(“if question i ask you about math , answer by Latex form”);
in , but this one make the answer alway metion to math and latex , it make them aren’t natural

I went after making gpt-3.5-turbo-0125 reliable - the poorest AI yet. It is predisposed to dump backslashes for ChatGPT because these new models are useless for being anything other than ChatGPT. I want it to make formulas for forums.

system:

You are an expert AI assistant.

// response rules
Writing math formulas:
You have a MathJax render environment.
- Any LaTeX text between single dollar sign ($) will be rendered as a TeX formula;
- Use $(tex_formula)$ in-line delimiters to display equations instead of backslash;
- The render environment only uses $ (single dollarsign) as a container delimiter, never output $$.
Example: $x^2 + 3x$ is output for "x² + 3x" to appear as TeX.`

Response to math question:

To find the definite integral of the function f(x) = x^3 + 5x^2 over the interval [2, 5], we can follow these steps:

Step 1: Find the antiderivative of f(x).
The antiderivative of x^3 is \frac{1}{4}x^4 and the antiderivative of 5x^2 is \frac{5}{3}x^3. Therefore, the antiderivative of f(x) = x^3 + 5x^2 is:
$$\int f(x) ,dx = \frac{1}{4}x^4 + \frac{5}{3}x^3 + C$$
where C is the constant of integration.

Step 2: Evaluate the antiderivative at the upper and lower limits of integration.
Evaluate the antiderivative at x = 5 and x = 2:
At x = 5: \frac{1}{4}(5)^4 + \frac{5}{3}(5)^3 = \frac{625}{4} + \frac{1250}{3}
At x = 2: \frac{1}{4}(2)^4 + \frac{5}{3}(2)^3 = \frac{16}{4} + \frac{40}{3}

Step 3: Calculate the definite integral over the interval [2, 5].
The definite integral of f(x) = x^3 + 5x^2 over the interval [2, 5] is given by:
$$\int_{2}^{5} (x^3 + 5x^2) ,dx = \left[\frac{625}{4} + \frac{1250}{3}\right] - \left[\frac{16}{4} + \frac{40}{3}\right]$$

Step 4: Simplify the result to get the final answer.

(Yes, it still produced double dollar sign despite the programming, this model also has poor instruction following and poor attention ability to see context at all, and higher perplexity that can outwit top_p:0.9 with its lameness.


GPT4 answers:

Sure, let’s compute the definite integral of the function f(x) = x^3 + 5x^2 over the interval (2,5). We’ll denote this as \int_{2}^{5} f(x) \, dx.

  1. First, we need to find the antiderivative (indefinite integral) of the function f(x) = x^3 + 5x^2.

    The antiderivative of a function f is a function F whose derivative is f. In other words, if F'(x) = f(x), then \int f(x) \, dx = F(x) + C, where C is the constant of integration.

  2. The antiderivative of x^3 is \frac{1}{4}x^4 and the antiderivative of 5x^2 is \frac{5}{3}x^3 (using the power rule for integration, which states that the integral of x^n is \frac{1}{n+1}x^{n+1}).

    So, the antiderivative of f(x) is F(x) = \frac{1}{4}x^4 + \frac{5}{3}x^3.

  3. According to the Fundamental Theorem of Calculus, the definite integral of f over the interval (a,b) is F(b) - F(a).

    Therefore, \int_{2}^{5} f(x) \, dx = F(5) - F(2).

  4. Substitute 5 into F(x) to get F(5) = \frac{1}{4}5^4 + \frac{5}{3}5^3 = \frac{625}{4} + \frac{625}{3}.

  5. Substitute 2 into F(x) to get F(2) = \frac{1}{4}2^4 + \frac{5}{3}2^3 = 4 + \frac{40}{3}.

  6. Therefore, the integral is F(5) - F(2) = \left(\frac{625}{4} + \frac{625}{3}\right) - \left(4 + \frac{40}{3}\right) = \frac{625}{4} + \frac{625}{3} - 4 - \frac{40}{3}.

  7. This simplifies to \frac{375}{4} + \frac{585}{3}.

To simplify this to a decimal, the answer is approximately 425.83. This is the exact area under the curve f(x) = x^3 + 5x^2 from x = 2 to x = 5.