PricingBlog

Handle Object Append On Streaming Response

  • stockton_f-1265439882705109108

    Stockton

    1 year ago

    I have a variable that is holding all the messages for my chat bot interaction.

    The variable is an array of objects.

    Each object represents a message.

    The object keys are role and content

    role can be either bot ,user, or system.

    When the chunks of responses are received I want to update the last (most recent) bot object only...

    The current formula appends each chunk as a separate object instead of updating the last on.

    Puzzling my brain on this one 🧠
    1265439883145777152-di_-_start_Toddle_editor_2024-07-23_at_4.42.18_PM.jpg
  • lucasg-1265442164012355656

    Lucas G

    1 year ago

    Have you tried using Set instead?
  • stockton_f-1265442862946975757

    Stockton

    1 year ago

    I've tried setting a specific object in the array by index but it's still creating more objects...
    1265442862888259615-di_-_start_Toddle_editor_2024-07-23_at_4.57.36_PM.jpg
  • chrislaupama-1265459717640028252

    Chris Laupama

    1 year ago

    You may need to seperate this out a bit ( that's why I did )

    First I would stream the results to a variable ( let's call that responseText )

    Then I would append the object to the array once I receive the "end" trigger from the response

    This gives me two things that are usable...

    1 - allows me to temporary show the text as it's streaming
    2 - once the data has finished streaming, I can set the responseText back to null which in turn will replace the data displayed from the streamed version to the version in the object within the array
  • chrislaupama-1265460385574813726

    Chris Laupama

    1 year ago

    ^ otherwise you could use drop last and replace the object with a new one containing the new data
  • stockton_f-1265493677741047849

    Stockton

    1 year ago

    Taking a look at how you did it @Chris Laupama, thanks for the inspiration.

    I should just use yours lol

    Much more elegant. I build mine before I knew what I now know.
  • chrislaupama-1265495095831036015

    Chris Laupama

    1 year ago

    I am thinking of making a much more nicer version that is simple, a component that takes some simple attributes and streams back a response without anyone having to set that part up
  • stockton_f-1265503827319324753

    Stockton

    1 year ago

    How is that different than your current component?
  • chrislaupama-1265507548304248948

    Chris Laupama

    1 year ago

    I only released an action package not a component, did I release a component? I can’t remember lol
  • Tod-1265507550284091443

    Tod

    1 year ago

    Great energy @Chris Laupama! Your continuous contribution to the toddle Community just made you advance to Community Level 12!
  • stockton_f-1265511141124214816

    Stockton

    1 year ago

    Oh yeah, technically you just released the action... but I was cloning it to see the component.. nice.
  • lucasg-1265539859263651851

    Lucas G

    1 year ago

    Path would be size minus 1
  • Just size would create a new entry