I am displying answers from ChatGPT in Streamlit just using st.write . Yesterday I learned here, that indented text will be shown as a code_block.
Recently thought, it detected the programming language automatically and I would like to reproduce that on purpose. Sadly I was not able to catch the response in text this time so I do not have a reference and I canβt seem to reproduce it either.
Yes that is exactly what I meant. Maybe it was setup in the response, similar to indentet text becoming code_block but sadly I canβt tell the reason for getting the language.
I think what probably happened is that the response from GPT included the programming language, e.g. (sharing a screenshot to prevent Discourse from formatting this as code)
Thank you very much!
Iβll try this in a few variations and give an update if I find anythinig interesting.
As far as the code goes. I send a prompt like this (The added text is what I am experimenting with) :
prompt = user_input + β(if writing code, start with a summary, then start with < code> and end it with </ code>) around every code. Then put the used language in on the End of the codeβ
Then sending out the request and I indent the code part in the response:
pattern = re.compile(r'(<code>.*?</code>)', re.DOTALL)
# Split the input text into segments using the pattern
segments = pattern.split(response)
# Process each segment and reassemble the text
formatted_text = ""
for segment in segments:
if segment.startswith('<code>'):
# Indent the code within <code> and </code> tags
code_text = segment[6:-7].strip() # Remove <code> and </code> tags
indented_lines = code_text.splitlines()
indented_text = "\n".join([" " + line for line in indented_lines])
formatted_text += "\n" + indented_text + "\n"
else:
# Keep text outside of <code> and </code> tags unchanged
formatted_text += segment
response = formatted_text.lstrip()
And somehow in one single instance, the programming language was detetcted before.
FYI I added the summary infront of the code because it makes the indentation somehow buggy if there is no previous text otherwise.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking βAccept allβ, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.