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

chatgpt-helper

v0.2.1

Published

A thin layer of abstraction over chatGPT chat completion api and its function calling capabilities

Downloads

17

Readme

ChatGPT Helper

A thin layer of abstraction over chatGPT chat completion api and its function calling capabilities

Features

  • Use Zod schema to define the structure of the output
  • Create automatic agents for chatGPT in a very simple way
  • Nice abstraction over chatGPT function calling flow

Getting Started

npm i chatgpt-helper zod openai

usage

The library provides two ways to interact with ChatGPT

  1. extractDataWithPrompt - extract structured data using a schema and prompt (simple way)
import { extractDataWithPrompt } from 'chatgpt-helper';
import { Configuration, OpenAIApi } from 'openai';
import { z } from 'zod';

const api = new OpenAIApi(
  new Configuration({
    apiKey: '<openai key>',
  })
);

//Zod schema describing the way we want the data to be structured
const movieSchema = z.object({
  name: z.string().describe('Name of the movie'), //describe method provides way to provide hints to chatGPT
  year: z.number().describe('Year the movie came out'),
  cast: z
    .array(
      z.object({
        name: z.string().describe('Name of the actor'),
        character: z
          .string()
          .describe('Name of the character the actor played'),
      })
    )
    .describe('List of cast members sorted by their runtime'),
  genres: z.array(z.string()).describe('List of genres associated with'),
  runTime: z.string().describe('Runtime of the movie'),
});

extractDataWithPrompt({
  api: api,
  prompt: `Details of movie Big Fish`,
  schema: movieSchema,
}).then(({ data }) => {
  console.log(JSON.parse(data));
});

// Output
// {
//   "name": "Big Fish",
//   "year": 2003,
//   "cast": [
//     {
//       "name": "Ewan McGregor",
//       "character": "Edward Bloom (Young)"
//     },
//     {
//       "name": "Albert Finney",
//       "character": "Edward Bloom (Senior)"
//     },
//     {
//       "name": "Billy Crudup",
//       "character": "Will Bloom"
//     },
//     {
//       "name": "Jessica Lange",
//       "character": "Sandra Bloom"
//     },
//     {
//       "name": "Helena Bonham Carter",
//       "character": "Jenny (Young) / The Witch"
//     }
//   ],
//   "genres": [
//     "Adventure",
//     "Drama",
//     "Fantasy"
//   ],
//   "runTime": "2h 5min"
// }
  1. runWithToolsUntilComplete - create plugins for chatGPT on demand and allow chatGPT to call necessary tool as needed

:warning: Be very careful on using this: With this utility you are putting chatGPT in charge of calling the custom tools you provide.

import { Tools, runWithToolsUntilComplete } from 'chatgpt-helper';
import { Configuration, OpenAIApi } from 'openai';
import { z } from 'zod';

const api = new OpenAIApi(
  new Configuration({
    apiKey: '<openai key>',
  })
);

//Need to create zod schema if arguments needed for the tool
const timeForFoodSchema = z.object({
  name: z.string().describe('name of the food item'),
});

//Tool definitions and implementations
const tools = new Tools()
  .addTool({
    name: 'currentTime',
    purpose: 'Getting the current time',
    implementation: () => {
      return { currentTime: new Date().toLocaleTimeString() };
    },
  })
  .addTool({
    name: 'timeForFood',
    purpose:
      'Gives an approximate time taken for food to be ready when ordered in a hotel',
    argSchema: timeForFoodSchema,
    implementation: (arg: z.infer<typeof timeForFoodSchema>) => {
      return { time: '30', unit: 'minutes' };
    },
  });

runWithToolsUntilComplete({
  api: api,
  prompt:
    'I am Planning to have pizza from my favourite restaurant now. By what time that pizza would be completely digested?',
  tools,
}).then((r) => {
  console.log(r.lastMessage);
});

//Output

// {
//  role: 'assistant',
//  content: 'If you order pizza now, it will take 
// approximately 30 minutes for the pizza to be ready.
// So, by 1:03 AM, the pizza should be completely digested. 
// Please note that digestion time can vary depending on 
// variousfactors such as metabolism, individual health 
//  conditions, and other factors.'
// }