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.
Any ideas or leads on this are highly appreciated
Do you mean that
st.write detected the programming language of the response from GPT automatically?
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)
Happy to dig in further if you can share the app
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:
# 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"
# 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.
This worked exactly as you said with ‘’'.
Thank you very much!
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.