Skip to content

Getting Started - with TypeScript/JavaScript

Setup

Install required dependencies:

bash
npm install aisbreaker-api-js aisbreaker-core-nodejs
bash
npm install aisbreaker-api-js aisbreaker-core-browserjs

Set an API Key in your environment, if required by your desired service, and only for NodeJS apps:

bash
# set AIsBreaker API Key:
export AISBREAKER_API_KEY="aisbreaker_123abc..."
# or set OpenAI API Key:
export AISBREAKER_API_KEY="sk-123abc..."

In a browser webapps read the API Key from an authentication flow or from configuration instead. But only use AIsBreaker API Keys to never expose your AI service API key to webapp users.

Initialize the AIsBreaker Client API

Import required libraries:

TypeScript
import { api } from 'aisbreaker-api-js'
JavaScript
import { api } from 'aisbreaker-api-js';

Select your desired service and define respective Service Properties - mainly the serviceId:

TypeScript
const serviceProps: api.AIsServiceProps = {
  //serviceId: 'chat:dummy',

  serviceId: "chat:openai.com",

  //serviceId: "chat:huggingface.co/microsoft/DialoGPT-large",

  //serviceId: "chat:huggingface.co/<HF-ACCOUNT>/<HF-MODEL>",

  //serviceId: "aisbreaker:mirror",
  //forward2ServiceProps: {
  //  "serviceId": "chat:echo"
  //},
}
JavaScript
const serviceProps = {
  //serviceId: 'chat:dummy',

  serviceId: "chat:openai.com",

  //serviceId: "chat:huggingface.co/microsoft/DialoGPT-large",

  //serviceId: "chat:huggingface.co/<HF-ACCOUNT>/<HF-MODEL>",

  //serviceId: "aisbreaker:mirror",
  //forward2ServiceProps: {
  //  "serviceId": "chat:echo"
  //},
};

Read the API Key from your environment, if required by your desired service:

TypeScript
const auth: api.Auth = {
  secret: process.env.AISBREAKER_API_KEY || process.env.OPENAI_API_KEY || "",
}
// or (if no API is required by your desired service):
const auth = undefined
JavaScript
const auth = {
  secret: process.env.AISBREAKER_API_KEY || process.env.OPENAI_API_KEY || "",
};
// or (if no API is required by your desired service):
const auth = undefined;
TypeScript
// In a browser webapps read the API Key
// from an authentication flow or from configuration instead.
//
// But only use AIsBreaker API Keys
// to never expose your AI service API key to webapp users!!!
const auth: api.Auth = {
  secret: "...",
}
// or (if no API is required by your desired service):
const auth = undefined
JavaScript
// In a browser webapps read the API Key
// from an authentication flow or from configuration instead.
//
// But only use AIsBreaker API Keys
// to never expose your AI service API key to webapp users!!!
const auth = {
  secret: "...",
};
// or (if no API is required by your desired service):
const auth = undefined;

Select the AIsBreaker server to use and get access to the API:

TypeScript
// set AIsBreaker Server URL:
const url = "https://api.demo.aisbreaker.org"
// get API access:
const aisService: api.AIsService = api.AIsBreaker.getAIsService(url, serviceProps, auth)
JavaScript
// set AIsBreaker Server URL:
const url = "https://api.demo.aisbreaker.org";
// get API access:
const aisService = api.AIsBreaker.getAIsService(url, serviceProps, auth);

Use the AIsBreaker Client API

Most API functions work asynchronously. It's a good idea to use async + await to make your code easier to read and to write.

Below we implement a simple conversation example in an async function: 1st request/question/prompt + 1st response/answer + 2nd request/question/prompt + 2nd response/answer.

TypeScript
// 1st request/question/prompt
const request1 = "What is an AI?"
// process
const response1 = await aisService.process({
    inputs: [ {
        text: {
            role: 'user',
            content: request1,
        },
    } ],
})
// 1st response/answer
console.log(`response1: ${response1.outputs[0].text.content}`)

// 2nd request/question/prompt
const prompt2 = `
    If your previous answer in English, then translate your answser to German.
    If your previous answer was not English, then translate your answser to English.
    Only provide the translated text.
    `
// process, include conversation state from previous request
const response2 = await aisService.process({
    inputs: [ {
        text: {
            role: 'system',
            content: prompt2,
        },
    } ],
    conversationState: response1.conversationState,
})
// 2nd response/answer
console.log(`response2: ${response2.outputs[0].text.content}`)
JavaScript
// 1st request/question/prompt
const request1 = "What is an AI?";
// process
const response1 = await aisService.process({
    inputs: [ {
        text: {
            role: 'user',
            content: request1,
        },
    } ],
});
// 1st response/answer
console.log(`response1: ${response1.outputs[0].text.content}`);

// 2nd request/question/prompt
const prompt2 = `
    If your previous answer in English, then translate your answser to German.
    If your previous answer was not English, then translate your answser to English.
    Only provide the translated text.
    `;
// process, include conversation state from previous request
const response2 = await aisService.process({
    inputs: [ {
        text: {
            role: 'system',
            content: prompt2,
        },
    } ],
    conversationState: response1.conversationState,
});
// 2nd response/answer
console.log(`response2: ${response2.outputs[0].text.content}`);

Details:

Finally, we need to call the action function:

TypeScript
# at buttom of the file:
action()
JavaScript
# at buttom of the file:
action();
TypeScript
# usually called by an event handler
# (details depend on your webapp framework):
action()
JavaScript
# usually called by an event handler
# (details depend on your webapp framework):
action();

Examples

Some example apps are available:

Released under the MIT License.