Git Product home page Git Product logo

Comments (3)

bernaferrari avatar bernaferrari commented on May 27, 2024

Is there a way to know the stream is done?

This is my script for parsing everything except the last children, so it streams but avoids the bug I described. It is not optimized, but does the job. I welcome improvements, so I don't need to call statusStream.update() outside of the loop:

const stream = await streamObject({
  model: openai.chat("gpt-4-turbo"),
  schema: z.object({
    children: z.array(z.object({})),
  }),
  system: `You should do this. Follow the API schema here: ...`,
});

let fullJson;
let partialJson = {};
for await (const partialObject of stream.partialObjectStream) {
  fullJson = partialObject;

  const pJson = modifyJsonByRemovingLastNode(fullJson as any);

  if (JSON.stringify(pJson) === JSON.stringify(partialJson)) {
    continue;
  }
  partialJson = pJson as any;

  const debug = await prettier.format(JSON.stringify(fullJson), {
    parser: "json-stringify",
  });
  console.log('debug info', debug);

 const content = await executeMethod(partialJson);

  statusStream.update({
    type: "loading",
    content,
  });
}

const content = await executeMethod(fullJson);
statusStream.update({
  type: "loading",
  data: content
});

statusStream.done();

from ai.

lgrammel avatar lgrammel commented on May 27, 2024

A possible solution could be to introduce a "fixMode" property on streamObject (or something like it) with the following settings:

  • always: always try to fix the JSON. Will lead to incomplete strings etc
  • finished-primitive: only fix when primitive attribute values are finished
  • finished-objects: only fix when objects are complete. this might need refinement, e.g. for array streaming

I want to think more about all possible cases before adding this, since it can become more complex.

Is my understanding of your use case correct?

Use case:

You want to stream an array of objects that contains an alt attribute (or event an array of strings). 
For each finish array object, partialObjectStream should contain a new result, 
but not for any intermediates (that may have partial urls etc).

from ai.

bernaferrari avatar bernaferrari commented on May 27, 2024

Yes. Right now I went lazy and I am with the "I just want to stream the objects that are complete" so there are no surprises anywhere. Like, it might do style="out" instead of style="outlined". Waiting for the whole object to complete is fine for me, while still streaming. The algorithm I did above could be optimized here and there, but it is what I'm doing.

from ai.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.