Getting started · Run a completion2 / 5
  1. 01
  2. 03
  3. 04
  4. 05

Run a completion

Example on GitHub(packages/sdk/examples/quickstart.ts)

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.

Put it to the test

  1. Build a history array with one user message asking the model to explain quantum computing in one sentence.
  2. Call completion({ modelId, history, stream: true }) and store the result.
  3. Iterate result.tokenStream and write each token to stdout.
index.ts

$ Run your code to see results

$