Preventing Flash of Incomplete Markdown when streaming AI responses
engineering.streak.com
Preventing Flash of Incomplete Markdown when streaming AI responses
1–10 of 14 posts
Re: Preventing Flash of Incomplete Markdown when streaming AI responses
#2it formats its own stuff with markdown, so if i ask it for markdown but dont explicitly specify a downloadable file, it will produce valid markdown up to where it conflicts with its own markdown, and then it gets choppy and chunked.
its an issue of my prompting is what im sure some customer service rep would be told to tell me :p because theres money to be made in courses for prompting skills perhaps, idk. (cynical view).
sure is enjoyable to struggle together with the AI to format its responses correctly :'D
Re: Preventing Flash of Incomplete Markdown when streaming AI responses
#3EDIT: One library I found is https://github.com/thetarnav/streaming-markdown which seems to combine incremental parsing with optimistic rendering, which works good enough in practice, I guess.
Re: Preventing Flash of Incomplete Markdown when streaming AI responses
#4Is there a general solution to this problem? I assume you can only start buffering tokens once you see a construct, for which there are continuations, that once completed, would lead to the previous text being rendered differently. Of course you don't want to keep buffering for too long, since this would defeat the purpose of streaming. And you never know if the potential construct will actually be generated. Also, t…
However, the second thing (not mentioned in the post) is that we are not rendering the markdown to HTML on the server, so []() markdown is sent to the client as []() markdown, not converted into . So even if a []() type link exists in a code block, that text will still be sent to the client as []() text, only sent in a single chunk and perhaps with the link URL replaced. The client has its own library to render the markdown to HTML in React. Also, the answers are typically short so even if OpenAI outputs some malformed markdown links, worst case is that we end up buffering more than we need to and the user experiences a pause after which the entire response is visible at once (the last step is to flush any buffered text to the client).
Re: Preventing Flash of Incomplete Markdown when streaming AI responses
#5(This comment when subjected to this processing could look like: "Could this result in edge cases with ")
Re: Preventing Flash of Incomplete Markdown when streaming AI responses
#6fun read, its weird interacting with chatgpt around markdown sometimes. it formats its own stuff with markdown, so if i ask it for markdown but dont explicitly specify a downloadable file, it will produce valid markdown up to where it conflicts with its own markdown, and then it gets choppy and chunked. its an issue of my prompting is what im sure some customer service rep would be told to tell me :p because theres m…
Re: Preventing Flash of Incomplete Markdown when streaming AI responses
#7Could this result in edge cases with [ where due to some misformatting or intentional syntax that looks like the start of a markdown link, the entire response is hidden from the user? (This comment when subjected to this processing could look like: "Could this result in edge cases with ")
There are some easy wins that could improve this further: line endings within links are generally not valid markdown, so if the code ever sees \n then just flush buffered text to the client and reset the state to TEXT.
Re: Preventing Flash of Incomplete Markdown when streaming AI responses
#8Re: Preventing Flash of Incomplete Markdown when streaming AI responses
#9Is there a general solution to this problem? I assume you can only start buffering tokens once you see a construct, for which there are continuations, that once completed, would lead to the previous text being rendered differently. Of course you don't want to keep buffering for too long, since this would defeat the purpose of streaming. And you never know if the potential construct will actually be generated. Also, t…