All right, here's what the solution looks like.
import { google } from "@ai-sdk/google"import { streamText } from "ai"const model = google("gemini-2.0-flash")const stream = streamText({model,prompt: "Give me the first paragraph of a story about an imaginary planet.",})for await (const chunk of stream.textStream) {process.stdout.write(chunk)}
We have our model
, gemini-2.0-flash
, and we have our prompt
being passed to streamText
. When I run this, we will see the new chunk
.
Xylos was a world painted in hues unknown to human eyes
If I try that again, you'll notice the chunks just popping in piece by piece instead of having to wait for the entire thing.
streamText
is going to be one of the core primitives that we use going forward as we hook things up to the UI and it's much more useful than generateText
in providing a better user experience.
Nice work and I will see you in the next one.