Installation and usage

https://www.npmjs.com/package/@-anarchy-/chat

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 @-anarchy-/chat

(comes bundled with Typescript!)

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 "@-anarchy-/chat";
    
    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 "@-anarchy-/chat";
    
    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

  • Bot without APIs

Now the bot can be added to the client:

You can still add APIs to it after that:

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:

In-time validation

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

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

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 name
given type
return type
example argument

static init(KEY)

sets the key globally for all instances

string

setBot(bot)

If valid, sets the Bot in the client. If invalid, returns the Bot's errors.

IBotDataInput

ZodValidationResult <IBotData>

{name: "", llmKey: "sk-...", apis: []}

validateBot(bot)

Validates a given Bot. No side effects

IBotDataInput

ZodValidationResult <IBotData>

{name: "", llmKey: "sk-..."}

setApi(api)

If valid, adds the API to the client. If invalid, returns the API's errors.

IBotApiInput

ZodValidationResult <IBotApi>

{description: "", endpoint: "https://..."}

validateApi(api)

Validates a given API. No side effects

IBotApiInput

ZodValidationResult <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)

IBotDataParameterInput

ZodValidationResult <IBotDataParameter>

{"name": "q", value: "search term", valueType: "description"}

sendInteraction(prompt)

Sends an interaction to the server and returns the answer

string

Promise <IInteractionConsumerResponse>

"How many kittens are there in Paris?"

Last updated