Last updated for npm package version 0.15.2

The Commonbase npm package supports Node.js v18 and later. It also includes TypeScript support.

This package is meant for server-side usage only. Using it in the browser will expose your API Key and Project ID. With this information, anyone can make requests to Commonbase on your behalf.

Installation

Install Commonbase from npm using your preferred package manager:

npm add @commonbase/sdk

Usage

A Project ID and API Key are required for all Commonbase requests. You can find your project ID and generate an API key in the Commonbase Dashboard.

To create a completion, configure a Client with your API Key and provide your Project ID and prompt to createCompletion.

Text Completion

To create a basic text completion, use createCompletion with a prompt:

import { Client } from "@commonbase/sdk";

const client = new Client({
  apiKey: "API_KEY",
});

const completion = await client.createCompletion({
  projectId: "PROJECT_ID",
  prompt: "Hello!",
});

console.log(completion.bestChoice.text);

completion.bestChoice returns the most relevant completion. completion.choices contains the full list of choices returned by the LLM:

for (const choice of completion.choices) {
  console.log(choice.text);
}

Chat Completion

To create a chat completion, use createChatCompletion with a list of messages.

import { Client } from "@commonbase/sdk";

const client = new Client({
  apiKey: "API_KEY",
});

const completion = await client.createChatCompletion({
  projectId: "PROJECT_ID",
  messages: [
    {
      role: "system",
      content: "You are an assistant who helps users with tech problems.",
    },
    { role: "user", content: "My internet isn't working." },
    { role: "assistant", content: "Have you tried restarting your router?" },
    { role: "user", content: "Yes I've tried that." },
  ],
});

console.log(completion.bestChoice.text);

Variables

You can also create a completion by providing variables and a template prompt.

import { Client } from "@commonbase/sdk";

const client = new Client({
  apiKey: "API_KEY",
});

const completion = await client.createCompletion({
  projectId: "PROJECT_ID",
  prompt: "A new user {{user_name}} just signed up with {{email}}. Say hello!"
  variables: {
    user_name: "Alice",
    email: "alice@example.com"
  }
});

console.log(completion.bestChoice.text);

Embeddings

An embedding is a vector (list) of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness.

To learn more about embeddings, see:

Commonbase supports embeddings through OpenAI’s text-embedding-ada-002 model:

import { Client } from "@commonbase/sdk";

const client = new Client({
  apiKey: "API_KEY",
});

const embeddings = await client.createEmbedding({
  input: "Your text string",
  projectId: "PROJECT_ID",
});

console.log(embeddings.data);

Streaming

To stream the completion response as it is generated, use streamChatCompletion.

Streaming is useful when you want to display a completion “as it is typed” for a chatbot-like experience.

For longer completions, streaming lets you avoid hanging while you wait for a completed response from createChatCompletion.

import { Client } from "@commonbase/sdk";

const client = new Client({
  apiKey: "API_KEY",
});

const completionStream = await client.streamChatCompletion({
  projectId: "PROJECT_ID",
  messages: [
    {
      role: "user",
      content: "Write me a short essay about artificial intelligence please.",
    },
  ],
});

for await (const completion of completionStream) {
  if (completion.completed) {
    process.stdout.write("\n\ndone");
  }
  process.stdout.write(completion.bestChoice.text);
}

Usage

To get the costs and token counts of the request and response, use showUsage: true parameter in the request body.

import { Client } from "@commonbase/sdk";

const client = new Client({
  apiKey: "API_KEY",
});

const completionStream = await client.streamChatCompletion({
  projectId: "PROJECT_ID",
  messages: [
    {
      role: "user",
      content: "Write me a short essay about artificial intelligence please.",
    },
  ],
  showUsage: true,
});