Ask HN: How do you add guard rails in LLM response without breaking streaming?
1–10 of 26 posts
Re: Ask HN: How do you add guard rails in LLM response without breaking streaming?
#2Re: Ask HN: How do you add guard rails in LLM response without breaking streaming?
#3Re: Ask HN: How do you add guard rails in LLM response without breaking streaming?
#4Give examples of how the LLM should respond. Always give it a default response as well (e.g. "If the user response does not fall into any of these categories, say x").
> I can manually add validation on the response but then it breaks streaming and hence is visibly slower in response.
I've had this exact issue (streaming + JSON). Here's how I approached it: 1. Instruct the LLM to return the key "test" in its response. 2. Make the streaming call. 3. Build your JSON response as a string as you get chunks from the stream. 4. Once you detect "key" in that string, start sending all subsequent chunks wherever you need. 5. Once you get the end quotation, end the stream.
Re: Ask HN: How do you add guard rails in LLM response without breaking streaming?
#5Re: Ask HN: How do you add guard rails in LLM response without breaking streaming?
#6Re: Ask HN: How do you add guard rails in LLM response without breaking streaming?
#7You could of course use us and get that out of the box if you have access to Databricks.
Re: Ask HN: How do you add guard rails in LLM response without breaking streaming?
#8First prompt validates the input. Second prompt starts the actual content generation.
Combine both streams with SSE on the front end and don't render the content stream result until the validation stream returns "OK". In the SSE, encode the chunks of each stream with a stream ID. You can also handle it on the server side by cancelling execution once the first stream ends.
Generally, the experience is good because the validation prompt is shorter and faster to last (and only) token.
The SSE stream ends up like this:
data: ing|tomatoes
data: ing|basil
data: ste|3. Chop the
I have a writeup (and repo) of the general technique of multi-streaming: https://chrlschn.dev/blog/2024/05/need-for-speed-llms-beyond... (animated gif at the bottom).Re: Ask HN: How do you add guard rails in LLM response without breaking streaming?
#9Hi, I run the model serving team at Databricks. Usually you run regex filters, LLAMA Guard, etc on chunks at a time so you are still streaming but it's in batches of tokens rather than single tokens at a time. Hope that helps! You could of course use us and get that out of the box if you have access to Databricks.