Class StringstreamSlowdown
- java.lang.Object
-
- com.composum.ai.backend.base.service.StringstreamSlowdown
-
- All Implemented Interfaces:
AutoCloseable,Consumer<String>
public class StringstreamSlowdown extends Object implements Consumer<String>, AutoCloseable
For a stream of String segments (like the ChatGPT tokens that trickle in from ChatGPT when streaming) this collects the parts and forwards the collected part every 500ms to a given consumer. This decreases the overhead of displaying the changed content in the browser.
-
-
Constructor Summary
Constructors Constructor Description StringstreamSlowdown(Consumer<String> target, long minimumDelayMillis)Sets the target where we forward the collected strings.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaccept(String s)voidclose()voidflush()protected longgetTime()protected StringretrieveForwardFragment()Returns the longest fragment ofcollectedending in a whitespace or punctuation character.
-
-
-
Constructor Detail
-
StringstreamSlowdown
public StringstreamSlowdown(@Nonnull Consumer<String> target, long minimumDelayMillis)
Sets the target where we forward the collected strings.- Parameters:
target- the destinationminimumDelayMillis- the minimum delay in milliseconds for which we wait before forwarding anything.
-
-
Method Detail
-
getTime
protected long getTime()
-
flush
public void flush()
-
retrieveForwardFragment
protected String retrieveForwardFragment()
Returns the longest fragment ofcollectedending in a whitespace or punctuation character. This fragment is removed fromcollected. If the fragment is longer than 80 characters but doesn't match this rule, it's forwarded, anyway.
-
close
public void close() throws Exception- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-
-