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

@jpbehler/partner

v0.2.7

Published

A friendly and digestible way to using OpenAI Assistants API

Downloads

1,173

Readme

Partner AI

Partner Logo

A human-friendly and digestible way to use OpenAI Assistants API (ugh finally...)


🚀 Features

  • Enables you to create a full-fledged OpenAI Assistant with similar syntax like the ChatCompletion API
  • Admits both Event handlers and await/Promise syntax
  • Intuitive API, just write partner.sendMessage() to send a message and partner.remember() to add a file context
  • Able to store both files and JSON AI Context persistently for later use with partner.remember()
  • Works with a single instruction or multiple instructions as steps for the AI to tackle

📦 Installation

Install the package via npm:

npm install @jpbehler/partner

🧑‍💻 CLI/Command

Just execute this command to start using the interactive command:

npx @jpbehler/partner

🛠️ Usage

Quick Start

import partner from "@jpbehler/partner";
import { input } from "@inquirer/prompts";

let salesman = new partner({
  apiKey:
    "sk-***",
  assistantParams: {
    name: "iPhone Salesman",
    instructions: "You are an iPhone Salesman. Do not offer MacBooks.",
    model: "gpt-4o",
  },
});

const receiveMessage = async (message) => {
  console.log(message);

  const question = await input({
    message: "Question>",
  });
  await salesman.sendMessage(question);
};

//Handle incoming messages async using eventHandlers
salesman.on("message", receiveMessage);
await salesman.start("Let's start!");

//Or just use async/await syntax
//await salesman.start();
//const response = await salesman.sendMessage("Let's start!");

Define functions/code for the Partner to use

import partner from "@jpbehler/partner";
import { input } from "@inquirer/prompts";

//Define a function as a tool the Partner should call whenever asked for a product
const GET_FROM_DB = {
  type: "function",
  function: {
    name: "getProduct",
    description: "Gets stored product information",
    parameters: {
      type: "object",
      properties: {
        _id: {
          type: "string",
          description: "Entry or document ID to be fetched",
        },
        database: {
          type: "string",
          description: "Database to query from",
        },
      },
      required: ["_id", "database"],
    },
  },
};

//Define a Partner that makes use of the above function
let salesman = new partner({
  apiKey:
    "sk-***",

  assistantParams: {
    name: "iPhone Salesman",
    instructions:
      "You are an iPhone Salesman. Do not offer MacBooks. (Please use the provided functions and files to get and recommend products)",
    model: "gpt-4o",
    tools: [GET_FROM_DB, { type: "file_search" }],
  },
});

const receiveMessage = async (message) => {
  console.log(message);

  const question = await input({
    message: "Question>",
  });
  await salesman.sendMessage(question);
};

//Handle GET_FROM_DB function calls
const receiveRequest = async (request) => {
  /* 1. Iterate request.required_action.submit_tool_outputs.tool_calls
     2. Get the function and parameters the AI needs to request
     3. Execute the associated function (eg. getProduct(_id,database))
     4. Respond to the AI request so it can use the information
  */

  await salesman.respondRequest({
    request: request,
    status: "success",
    response: "Ok stored",
  });
};

salesman.on("message", receiveMessage);
salesman.on("request", receiveRequest);

await salesman.start("Let's start");

Define multiple instructions as steps the Partner must resolve

import partner from "@jpbehler/partner";
import { input } from "@inquirer/prompts";

let salesman = new partner([
  {
    apiKey:
      "sk-***",
    assistantParams: {
      name: "Salesman",
      instructions: "You are an iPhone Salesman. Do not offer MacBooks.",
      model: "gpt-4o",
    },
  },
  {
    apiKey:
      "sk-***",
    assistantParams: {
      name: "Cashier",
      instructions:
        "You are a store Cashier, please help the customer with his/her payment.",
      model: "gpt-4o",
    },
  },
]);

const receiveMessage = async (message) => {
  //Check the AI response and go to the next step by sending the "<done>" special code
  if (message.includes("pay")) await salesman.sendMessage("<done>");

  console.log(message);

  const question = await input({
    message: "Question>",
  });
  await salesman.sendMessage(question);
};

//Handle incoming messages async using eventHandlers
salesman.on("message", receiveMessage);
await salesman.start("Let's start!");

🌟 GitHub

If you notice a bug, be welcomed to open an Issue If you just love it, please consider giving it a ⭐ on GitHub!


🧑‍💻 Author

Created with ❤️ & ☕ by J.P. Behler.


🫀 Donate

If you really appreciate it, consider buying me a Coffee. Remember we developers turn coffee into apps 😆