Now that we have a modelId, it's time to ask the model to say something.
completion() takes a modelId and a history (an array of messages), and gives us back a result we can either await fully or stream token by token. We pass stream: true so we can watch the tokens arrive.
The history array is a list of { role, content } messages, like a chat log. We're starting with one user message, but later lessons will add assistant and system turns.
A history array with one user message would look like this:
const history = [
{ role: "user", content: "Explain quantum computing in one sentence." },
];Now we wrap that history in a completion() call like so:
const result = completion({ modelId, history, stream: true });Finally, we drain the token stream token-by-token using process.stdout.write:
for await (const token of result.tokenStream) {
process.stdout.write(token);
}Note: when the stream finishes, the result is also available as a single string via
await result.text. We'll use that in later lessons.
history array with one user message asking the model to explain quantum computing in one sentence.completion({ modelId, history, stream: true }) and store the result.result.tokenStream and write each token to stdout.$ Run your code to see results
$