Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert latex to plain text #91

Merged
merged 1 commit into from
Jul 30, 2024
Merged

Conversation

ldelossa
Copy link
Contributor

@ldelossa ldelossa commented Jul 30, 2024

Newest GPT models love to provide latex markup.

Unfortunately this makes things pretty impossible to read for larger questions.

Add a conversion to change latex markup to plain text.

Closes #87

Before:

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── user ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
show me a gamma correction formula
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── assistant ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Gamma correction is a nonlinear operation used to encode and decode luminance or tristimulus values in image processing. The formula for gamma correction can be expressed as follows:

For a pixel value \( I \) (where \( I \) is typically in the range [0, 1] or [0, 255]), the gamma corrected value \( I_{\text{corrected}} \) is given by:

[
I_{\text{corrected}} = I^{\gamma}
\]

Where:
- \( I \) is the original pixel value (normalized to [0, 1] if necessary).
- \( \gamma \) is the gamma value (a positive real number). A common value for gamma is 2.2.

If the pixel values are in the range [0, 255], the formula can be adjusted as follows:

[
I_{\text{corrected}} = 255 \times \left( \frac{I}{255} \right)^{\gamma}
\]

In this case, you first normalize the pixel value by dividing by 255, apply the gamma correction, and then scale it back to the original range.

### Inverse Gamma Correction

To reverse the gamma correction (to get back the original pixel value), you can use the inverse formula:

[
I_{\text{original}} = I_{\text{corrected}}^{\frac{1}{\gamma}}
\]

This allows you to retrieve the original pixel value from the gamma-corrected value.
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

After:

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── user ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
show me a gamma correction formula
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── assistant ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Gamma correction is a nonlinear operation used to encode and decode luminance or tristimulus values in image processing. The formula for gamma correction can be expressed as follows:

For a pixel value I (where I is typically in the range [0, 1] or [0, 255]), the gamma corrected value I_corrected is given by:


    I_corrected = I^γ


Where:
- I is the original pixel value (normalized between 0 and 1).
- γ is the gamma value (a positive real number). A common value for γ is around 2.2 for standard displays.

If the pixel values are in the range [0, 255], the formula can be adjusted as follows:


    I_corrected = 255 ×( I/255)^γ


In this case, you first normalize the pixel value by dividing by 255, apply the gamma correction, and then scale it back to the original range.

### Inverse Gamma Correction
To reverse the gamma correction (to get back the original pixel value), you can use the inverse formula:


    I_original = I_corrected^1/γ


This allows you to retrieve the original pixel value from the gamma-corrected value.
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

@npiv
Copy link
Owner

npiv commented Jul 30, 2024

Hi @ldelossa

This looks great, but it seems to break markdown answers. For example "write a python fibbonaci recursive example"

gives me

image

vs (the old behavior)

image

so the detect_and_format_message would need to be a little bit less permissive, or the option for latex formatting should be hinted by a param.

I'm happy to have a look/play around myself if you don't have time, as I would like very much to include the latex option, but will probably only have time this weekend.

@ldelossa
Copy link
Contributor Author

Uh oh! Should have tested a bit more. I wonder if simply doing the latex conversion after markdown parsing would help.

@ldelossa
Copy link
Contributor Author

Ah, I see a bit of an issue, the markdown processing turns things right into term codes for the console. Where to interject latex processing then is a bit tougher. 🤔

@ldelossa
Copy link
Contributor Author

@npiv latest push seems to fix markdown parsing in our simple testing here. I saw that the latex parsing would change '``' sequences to smart quotes. We just swap them back now.

Newest GPT models love to provide latex equations.

Unfortunately this makes things pretty impossible to read for larger
questions.

Add a conversion to change latex markup to plain text.

Signed-off-by: ldelossa <[email protected]>
@ldelossa
Copy link
Contributor Author

image
This is looking pretty good.

Copy link
Owner

@npiv npiv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah looks good, go ahead. I will release it and bump homebrew tomorrow.

Thanks a lot

@ldelossa
Copy link
Contributor Author

Thanks a ton. While I use it, if i find any edge cases ill make more touch up prs that fix weird rendering.

Thanks again for chatblade, i literally use this 100 times a day.

@ldelossa
Copy link
Contributor Author

Just a heads up, I can't hit merge cuz I don't have write access :)

@npiv npiv merged commit 225b622 into npiv:main Jul 30, 2024
@ldelossa
Copy link
Contributor Author

Hey @npiv was this ever released? Got a new laptop today and installed latest chatblade, but I don't think this is there.

@npiv
Copy link
Owner

npiv commented Aug 14, 2024

Hmm something went wrong, should be fixed now at least on pypi if you install via pip.

Homebrew will follow, but they can take a few hours usually

@ldelossa ldelossa deleted the ldelossa/convert-latex branch August 14, 2024 12:15
@ldelossa
Copy link
Contributor Author

@npiv thanks! just upgraded with pip, working :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ChatGPT now insists on providing any formulas as LaTeX.
2 participants