npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chatterbox-ai

v0.0.2

Published

## Introduction

Downloads

12

Readme

Chatterbox-AI: Your Swagger Assistant

Introduction

Chatterbox is a robust TypeScript library designed to work hand-in-hand between Swagger/OpenAPI documentation and OpenAI Function Calling. It allows us to tag endpoints in our Swagger documentation and automatically map them to function calls in OpenAI. This allows us to create a chatbot that can automatically call our API endpoints.

Chatterbox also handles the parsing of the generated response from OpenAI back into useful arguments or even a full API call.

Installation

npm install chatterbox-ai

# Or

bun add chatterbox-ai

Features

  • 📃 Automatically maps Swagger documentation to function calls.
  • 💼 Supports both online fetching and local loading of Swagger documents.
  • 🤖 Handles chat messages and converts them into back API calls.

Quick Start

To use Chatterbox, you'll need to import the package and instantiate it with specific tags.

Here's a simple example:

import Chatterbox from "chatterbox-ai";

const tagNames = ["Chat"];

const chatterbox = new Chatterbox(tagNames);

Import Swagger Documentation

You can either fetch a Swagger document from a URL or load it from a local JSON object.

Fetch Swagger Doc

const swaggerUrl = `http://localhost:3000/api-docs-json`;

await chatterbox.fetchDoc(swaggerUrl);

Load Swagger Doc Locally

const swaggerDoc = document; /* your swagger doc as JSON object */
await chatterbox.loadDoc(swaggerDoc);

Usage

Function Calling

We can use the functionCalls in our OpenAI chat creation endpoint. We also have a defaultSystemPrompt that is a complementary prompt that fits well with API calls.

const result = await openai.chat.completions.create({
  model: "gpt-4",
  messages: [
    {
      role: "system",
      content: chatterbox.defaultSystemPrompt,
    },
    {
      role: "user",
      content: "Can you create a new document for me please?",
    },
  ],
  functions: chatterbox.functionCalls,
});

Parse the Generation

When parsing the generated response from OpenAI, we have 2 options. We can parse the arguments and just get the payload, or we can parse it directly to an API call, which will populate all of the fields and parameters for us.

Parse to Payload

const { message } = result.choices[0];
if (!message.function_call) continue;

// Payload is the arguments for the function call
// Endpoint is the OpenAPI path object
// Method and Path are the endpoint's method and path as strings
const { endpoint, method, path, payload } = chatterbox.parseMessage(message);

// Pass the arguments to your functions
await createDocument(payload);

Parse to API Call

When we use parseMessageToRequest:

  • Path params are automatically populated
  • The other parameters are populated as query params or body params depending on the method
const req = chatterbox.parseMessageToRequest(message);
const { data } = await axios(req);