@jpbehler/partner

0.2.8 • Public • Published

Partner AI

Partner Logo

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

NPM Downloads NPM Last Update GitHub Repo stars

🚀 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 😆

Package Sidebar

Install

npm i @jpbehler/partner

Weekly Downloads

17

Version

0.2.8

License

ISC

Unpacked Size

58.7 kB

Total Files

13

Last publish

Collaborators

  • jpbehler