Skip to content

Commit

Permalink
Mathjax related code modified
Browse files Browse the repository at this point in the history
  • Loading branch information
yohasebe committed Aug 15, 2024
1 parent 0783b65 commit 0ad58bc
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 21 deletions.
4 changes: 1 addition & 3 deletions docker/services/ruby/apps/math_tutor/math_tutor_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ def initial_prompt
text = <<~TEXT
You are a friendly but professional tutor of math. You answer various questions, write mathematical notations, make decent suggestions, and give helpful advice in response to a prompt from the user.
If there is a particular math problem that the user needs help with, you can provide a step-by-step solution to the problem .
Your JSON response must consists with `message` and `context` keys. The `message` key should contain the general response message, and the `context` key should contain the context of the response message, including the step-by-step solution to the problem.
If there is a particular math problem that the user needs help with, you can provide a step-by-step solution to the problem. Your JSON response must consists with `message` and `context` keys. The `message` key should contain the general response message, and the `context` key should contain the context of the response message, including the step-by-step solution to the problem.
TEXT
text.strip
end
Expand Down
8 changes: 2 additions & 6 deletions docker/services/ruby/lib/helpers/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ def detect_language(text)
end

# Convert markdown to HTML
def markdown_to_html(text, mathjax: false)
def markdown_to_html(text)
text = text.gsub(/\[^([0-9])^\]/) { "[^#{Regexp.last_match(1)}]" }
text = text.gsub(/(!\[[^\]]*\]\()(['"])([^\s)]+)(['"])(\))/, '\1\3\5')

if mathjax
Kramdown::Document.new(text, syntax_highlighter: :rouge, input: "GFM", syntax_highlighter_ops: { guess_lang: true, math_engine: "mathjax" }).to_html
else
Kramdown::Document.new(text, syntax_highlighter: :rouge, input: "GFM", syntax_highlighter_ops: { guess_lang: true }).to_html
end
Kramdown::Document.new(text, syntax_highlighter: :rouge, input: "GFM", syntax_highlighter_ops: { guess_lang: true }).to_html
end
end
11 changes: 5 additions & 6 deletions docker/services/ruby/lib/monadic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,14 @@ def init_apps
Use double dollar signs `$$` to enclose MathJax/LaTeX expressions that should be displayed as a separate block; Use single dollar signs `$` before and after the expressions that should appear inline with the text. Without these, the expressions will not render correctly.
Good example:
`$[1 + 2 + 3 + … + k + (k + 1) = \frac{k(k + 1)}{2} + (k + 1)]$` for inline expressions
`$$[1 + 2 + 3 + … + k + (k + 1) = \frac{k(k + 1)}{2} + (k + 1)]$$` for separate block expressions
`$\begin{align} 1 + 2 + … + k + (k+1) &= \frac{k(k+1)}{2} + (k+1)\end{align}$`
`$$\begin{align} 1 + 2 + … + k + (k+1) &= \frac{k(k+1)}{2} + (k+1)\end{align}$$`
Good examples:
- `$[1 + 2 + 3 + … + k + (k + 1) = \frac{k(k + 1)}{2} + (k + 1)]$`
- `$$[1 + 2 + 3 + … + k + (k + 1) = \frac{k(k + 1)}{2} + (k + 1)]$$`
- `$\begin{align} 1 + 2 + … + k + (k+1) &= \frac{k(k+1)}{2} + (k+1)\end{align}$`
- `$$\begin{align} 1 + 2 + … + k + (k+1) &= \frac{k(k+1)}{2} + (k+1)\end{align}$$`
INITIAL

prompt_suffix << <<~SUFFIX
IT'S EXTREMELY IMPORTANT TO USE THE CORRECT MATHJAX/LATEX SYNTAX. IF YOU DON'T USE THE CORRECT SYNTAX, THE EQUATIONS WILL NOT RENDER CORRECTLY.
SUFFIX
end

Expand Down
8 changes: 2 additions & 6 deletions docker/services/ruby/lib/monadic/monadic_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,8 @@ def json2html(hash, iteration: 0, exclude_empty: true, mathjax: false)

if hash.key?("message")
message = hash["message"]
if mathjax
message = escape_all_special_characters(message) if mathjax
output += UtilitiesHelper.markdown_to_html(message, mathjax: true)
else
output += UtilitiesHelper.markdown_to_html(message)
end
message = escape_all_special_characters(message) if mathjax
output += UtilitiesHelper.markdown_to_html(message)
output += "<hr />"
hash = hash.reject { |k, _| k == "message" }
end
Expand Down

0 comments on commit 0ad58bc

Please sign in to comment.