Class 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 Detail

      • StringstreamSlowdown

        public StringstreamSlowdown​(@Nonnull
                                    Consumer<String> target,
                                    long minimumDelayMillis)
        Sets the target where we forward the collected strings.
        Parameters:
        target - the destination
        minimumDelayMillis - 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 of collected ending in a whitespace or punctuation character. This fragment is removed from collected. If the fragment is longer than 80 characters but doesn't match this rule, it's forwarded, anyway.