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 stream in.
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.
Question 1 of 3
What does completion() return when you pass stream: true?
Question 2 of 3
Which of the following is a valid role value in a history message?
Question 3 of 3
How can you get the completion's full text after the stream ends, instead of assembling it token by token?
Run your code, check your answer, or ask a question. It all shows up here.