1.1.9 • Published 1 year ago

chat.dev-consumer v1.1.9

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Installation and usage

https://www.npmjs.com/package/chat.dev-consumer

A Promise-based Typescript-first consumer for https://chat.dev

The Consumer ChatDevClient is a javascript, promise-based, Typescript-first class that simplifies the creation, validation and interaction of your code with out chat.dev API.


Installation

To install, run

npm install chat.dev-consumer

Configuration

You can retrieve a key for the consumer at https://chat.dev/settings.\ These keys start with cd-sk-*****.

Create a new instance in your code in one of the following two ways:

  1. Set the key only once and reuse it (at a module level):

    import { ChatDevClient } from "chat.dev-consumer";
    
    ChatDevClient.init("MY_SECRET_KEY"); // the key is set "globally"
    const client1 = new ChatDevClient(); // will use MY_SECRET_KEY
    const client2 = new ChatDevClient(); // will use MY_SECRET_KEY
  2. Set the key once per instance

    import { ChatDevClient } from "chat.dev-consumer";
    
    const client1 = new ChatDevClient("MY_SECRET_KEY"); // will use MY_SECRET_KEY
    const client2 = new ChatDevClient(); // throws error!

Usage

To start, you must create a bot and pass it to the client. The bot is structured like IBotData, which can be imported from the consumer:

  • Bot with APIs

    import { IBotData } from "chat.dev-consumer";
    // A bot with APIs
    const bot: IBotData = {
            name: "my-bot",
            openAIKey: "sk-MY_OPENAI_API_KEY",
            apis: [
                {
                    description: "this api gets examples from the internet", // required for the bot to understand when to use this api!
                    method: "GET",
                    endpoint: "https://api.example.com/v1",
                    params: [
                        {name: "userId", value: "the ID of the user", isValue: false}, // description
                        {name: "type", value: "inactive", isValue: true} // value
                    ],
                    jsonBody: [],
                    dataBody: [],
                    headers: [],
                    authorization: [],
                    cert: "",
                }
            ],
        };
  • Bot without APIs

    import { IBotData } from "chat.dev-consumer";
    // A bot without APIs. They can be added later
    const bot: IBotData = {
            name: "my-bot",
            openAIKey: "sk-MY_OPENAI_API_KEY",
            apis: [],
        };

Now the bot can be added to the client:

import { ChatDevClient, IBotData } from "chat.dev-consumer";

const client = new ChatDevClient("MY_SECRET_KEY");
client.setBot(bot);

You can still add APIs to it after that:

import { ChatDevClient, IBotApi } from "chat.dev-consumer";

const client = new ChatDevClient("MY_SECRET_KEY");
client.setBot(bot);

const api: IBotApi = {
    description: "calculates the number of kittens in a given location",
    method: "GET",
    endpoint: "https://api.example.com/kitten-calculator",
    params: [
        {name: "place_name", value: "the place where we want to find the quantity of kittens", isValue: false}, // description
        {name: "distance_from_place_name", value: "20km", isValue: true} // value
    ],
    jsonBody: [],
    dataBody: [],
    headers: [],
    authorization: [],
    cert: ""
};

client.setApi(api);
client.setApi(api); // can't be added twice -> the validation will indicate why.

If the API has been added already, an error will be thrown.

The bot and api will be validated when calling their respective setBot() and setApi() functions. If they are invalid, an error object will be returned.

Validation

Cautious validation

To avoid unexpected issues, we can validate the data before passing it to our client:

import { ChatDevClient, IBotApi, IBotData, ZodValidationResult } from "chat.dev-consumer";

const client = new ChatDevClient("MY_SECRET_KEY");
const bot: IBotData = { ... }
const validateBot: ZodValidationResult<IBotData> = client.validateBot(bot);

if (!validateBot.success) {
    // handle the error
    console.log(validateBot.errors);
} else {
    // bot is valid, and can be added to client!
    client.setBot(bot);
}

const api: IBotApi = { ... };

const validateApi: ZodValidationResult<IBotApi> = client.validateApi(api);

if (!validateApi.success) {
    // handle the error
    console.log(validateApi.errors);
} else {
    // api is valid, and can be added to the bot!
    client.setApi(api);
}

client.setApi(api);

In-time validation

The data is also validated when we set a Bot or add an API:

import { ChatDevClient, IBotApi, IBotData, ZodValidationResult } from "chat.dev-consumer";

const client = new ChatDevClient("MY_SECRET_KEY");
const bot: IBotData = { ... }

const botAdded: ZodValidationResult<IBotData> = client.setBot(bot);

if (!botAdded.success) {
    // handle the error
    console.log(validateBot.errors);
} else {
    // bot is valid, and HAS BEEN added to client!
}

Interaction

Once a bot has been set, and we are happy with its data (check client.getBot() if you want to retrieve its value) we can start sending requests

import { ChatDevClient, IBotApi, IBotData, ZodValidationResult, IInteractionConsumerResponse } from "chat.dev-consumer";

const client = new ChatDevClient("MY_SECRET_KEY");
client.setBot({ ... });

// Send an interaction - wrap in an async function!
const response: IInteractionConsumerResponse = 
    await client.sendInteraction("How many kittens are there in New York City?");

// Use the result
console.log(
    response.prompt.question,
    response.prompt.answer,
    response.prompt.privateDebugInfo
);

// Send another interaction:
const followupResponse: IInteractionConsumerResponse = 
    await client.sendInteraction("What about in San Francisco?");

// Use the result
console.log(followupResponse.prompt.answer);

Notes

The interaction history (i.e. the list of previous question/answer pairs) is automatically handled by the client. You don't have to do anything about it. If we want to retrieve the history, the function sendInteraction("..."), which returns type Promise<IInteractionConsumerResponse> will contain the history under: IInteractionConsumerResponse["history"].

Functions

Function namegiven typereturn typeexample argument
static init(KEY)sets the key globally for all instancesstring
setBot(bot)If valid, sets the Bot in the client. If invalid, returns the Bot's errors.IBotDataInputZodValidationResult <IBotData>{name: "", openAIKey: "sk-...", apis: []}
validateBot(bot)Validates a given Bot. No side effectsIBotDataInputZodValidationResult <IBotData>{name: "", openAIKey: "sk-..."}
setApi(api)If valid, adds the API to the client. If invalid, returns the API's errors.IBotApiInputZodValidationResult <IBotApi>{description: "", endpoint: "https://..."}
validateApi(api)Validates a given API. No side effectsIBotApiInputZodValidationResult <IBotApi>{description: "", endpoint: "https://...", params: [{name: "q", value: "search term"}] }
getBot()Returns the bot, if it's set, or throws an error.IBotData
validateParam(param)Validates a given API's parameter (headers, authentication, body, jsonBody, params)IBotDataParameterInputZodValidationResult <IBotDataParameter>{"name": "q", value: "search term", isValue: false}
sendInteraction(prompt)Sends an interaction to the server and returns the answerstringPromise <IInteractionConsumerResponse>"How many kittens are there in Paris?"
1.1.1

1 year ago

1.1.0

1 year ago

1.0.39

1 year ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.0.38

1 year ago

1.0.37

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.33

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago