Comments (3)
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.
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 etcfinished-primitive
: only fix when primitive attribute values are finishedfinished-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.
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)
- ai/rsc and middleware header size failing
- Feature: useChat for rsc HOT 2
- Components rendered by tools will be immediately overwritten by text, causing the execution of generated UI to fail.
- Add ability to stream additional data immediately in `StreamData`
- Only plain objects can be passed to Client Components from Server Components HOT 11
- Add stop helper to useAssistant HOT 4
- Request to review changes for Vercel deployment HOT 3
- DOMException [InvalidCharacterError]: Invalid character HOT 1
- `streamObject` alternating between `undefined` and the response HOT 8
- aistate messages dissapear?
- 'useUIState' is not exported from 'ai/rsc' HOT 5
- Tool calls do not work as expected HOT 14
- No errors thrown even if there is no response HOT 3
- streamObject works on local machines, but not when deployed to vercel (pro user) HOT 11
- fail type inference at createStreamableValue HOT 3
- Error: action is not a function HOT 4
- AI State not updated during iterations
- Client side not working Next:JS HOT 5
- Demo streamUI with a custom provider HOT 1
- web-llm integration HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ai.