Skip to content

Typescript SDK for building better chatbot experiences

License

Notifications You must be signed in to change notification settings

commonbaseapp/commonbase-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

author
Brandon Meyerowitz
Jul 26, 2023
d3c3a3b · Jul 26, 2023

History

42 Commits
Apr 24, 2023
Apr 6, 2023
Jul 26, 2023
May 12, 2023
Apr 12, 2023
Apr 5, 2023
Feb 18, 2023
Mar 14, 2023
Feb 18, 2023
Feb 18, 2023
May 12, 2023
Apr 12, 2023
Jul 26, 2023
Mar 14, 2023
Apr 5, 2023
Apr 12, 2023
Apr 12, 2023
Apr 5, 2023
Jul 26, 2023

Repository files navigation

Commonbase Typescript SDK for building LLM integrations faster and easier

Commonbase allows developers to integrate with any popular LLM API provider without needing to change any code. The SDK helps with collecting data and feedback from the users and helps you fine-tune models for your specific use case.

Installation

$ npm install @commonbase/sdk

Usage

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

const client = new Client({
  projectId: "xxx-xxx-xxx-xxx-xxx",
});

const completionResult = await client.createCompletion({
  variables: {
    user_name: "Alice",
    project_name: "my-galaxy",
  },
});

console.log(completionResult.bestResult);

Drop-in replacement for OpenAI API

// the openai package can be replaced with the @commonbase/sdk/openai package
// import { Configuration, OpenAIApi } from "openai";
import { Configuration, OpenAIApi } from "@commonbase/sdk/openai";

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

const completion = await openai.createCompletion({
  model: "text-davinci-003",
  // when using the openai drop-in replacement, the prompt is passed to 
  // the OpenAI API as-is and the prompt CMS + templating is not used
  prompt: "Hello World, my name is Alice!",
  // you need to add your project id to be able to track 
  // the usage and responses of the OpenAI requests
  projectId: "xxx-xxx-xxx-xxx-xxx",
});
console.log(completion.data.choices[0].text);

Chat

The Chat API is dependent on the WebSocket API, if you are intending to use it from a Node (or other) environment you have to make a compatible WebSocket implementation globally available like websockets/ws.

const chatClient = new ChatClient({ projectId: "xxx-xxx-xxx-xxx-xxx" });
const stream = chatClient.send("Hey Bot");
while (true) {
  const { done, value } = await stream.next();
  if (done) {
    break;
  }
  console.log(value);
}

In browsers that support async iteration of streams (currently only Firefox), you can just iterate over the stream

for await (const chunk of stream) {
  console.log(chunk);
}