Hello, openai api does not write the equation in latex format in mathjax format. Even though I entered a long prompt examples about this, the prompt skips the examples. My prompt example:
For latex expressions, use the $ symbol for LaTeX formatting. For example, if an expression is given as \(\frac{24}{18 - x}\),\[\frac{24}{18 - x}\], rewrite it as $\frac{24}{18 -x}$. Writing math formulas: You have a MathJax rendering 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 rendering environment only uses $ (single dollarsign) as a container delimiter. Example: $x^2 + 3x$ is output for "x² + 3x" to appear as TeX.`
Apart from this prompt, I added a sample answer draft on the prompt.
Note: Out of 5-6 examples, only 1 is mathjax as I want. Not all of them work, others are in latex format.
Exactly. If you can easily conform to what the model wants to do naturally, then that’s far better than trying to instruct the model to behave differently.
I found myself wanting to do this replacing, but with a stream. I haven’t tested much, but this seems to work:
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