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

gpt-persona

v0.4.1

Published

A small library for managing different "personas" for OpenAI Chat API.

Downloads

138

Readme

gpt-persona

npm GitHub

A small library in TypeScript for managing different "personas" for OpenAI Chat API.

Use the Persona class to manage chat context and trim history without losing memories of the persona itself.

npm install gpt-persona

This library includes three features, with Persona being the main feature.

  • A class Persona for managing different personas for OpenAI Chat API.
  • A thin wrapper OpenAI around OpenAI Chat API.
    • You may use other libraries such as openai, but this wrapper supports streaming.
  • A small chat application called gpt-chat to quickly test with different personas.

This library is not ready for production, but you may use this to experiment with a chatbot.

Example

import {Persona, OpenAI} from 'gpt-persona';

const persona = new Persona("You end every sentence with '-desu' or '-nya'.");

const api = new OpenAI(process.env.OPENAI_API_KEY);

// Method 1: provide the API object
console.log(await persona.respond(api, "Good morning!"));
// Example response: "Good morning desu!"

// Method 2: use the API directly (with this, you can use other API libraries)
persona.pushMessage("Good afternoon!");
const response = await api.chatCompletion(persona.getAPIMessages());
persona.pushResponse(response);
console.log(response);
// Example response: "Good afternoon nya!"

gpt-chat

usage: gpt-chat [-h] [-k KEY] [-p PERSONA_FILE]

A simple client for OpenAI's Chat API.

optional arguments:
  -h, --help            show this help message and exit
  -k KEY, --key KEY     API key for OpenAI. If not provided, then the environment variable `OPENAI_API_KEY` will be used.
  -p PERSONA_FILE, --persona PERSONA_FILE
                        A path for the persona JSON/text file.

You can use following commands:

  • /quit: Quit chatting.
  • /persona: Override the persona.
  • /instruction: Override the instruction.
  • /reset: Resets the history and persona.
  • /clear: Resets the history. Retains persona and instructions.
  • /save: Save current persona and history.

Start your message with // if you want to put / at the beginning.

API

Persona

A Persona represents a chat agent. It holds memories of who it is and past conversations.

The constructor accepts either the persona (Persona#persona) or an object (see Persona#toJSON).

import {Persona} from 'gpt-persona';

const persona = new Persona("... enter detailed persona for this chat agent ...");

Persona#persona

Every request to the OpenAI API will be started with this. Default role is system.

// Could be a string, a `Message`, or a list of those.
persona.persona = "You are a cat.";

Persona#instructions

Every request to the OpenAI API will be ended with this. Default role is system.

It's usually better to provide instructions as Persona#persona and leave Persona#instructions empty.

Persona#token_count

# of tokens for current context (== this.persona_token_count + this.history_token_count + this.instruction_token_count).

Persona#max_context_token_count

Maximum allowed # of tokens for the context.

async Persona#respond(api, message, options, deltaCallback)

Create a response given a message; it automatically rollbacks upon an error, so even when an invocation throws an error, the persona can be reused safely.

  • api
  • message
  • options (optional)
    • An optional object with following optional fields.
    • options.request_params
      • Same parameters for OpenAI's API, but all fields are optional.
    • options.additional_instructions
      • Additional instructions to be provided.
      • Consider it as one-time persona.instruction.
    • options.freeze_history
      • History will be not modified when this is set to true.
    • options.condenser: (persona: Persona) => void
      • The way the history is condensed can be customized. (See Persona#condense() for a bit more details.)
    • options.isEphemeral: (messages: Message[]) => boolean
      • Given the persona's responses, if this function returns true, then the history will not be updated.
  • deltaCallback: (delta: string) => void (optional)
    • An optional function, if provided, will be called for every incremental message.

Here's an example with streaming:

import { stdout } from 'node:process';
import { Persona, OpenAI } from "../index.js";

const api = new OpenAI(process.env.OPENAI_API_KEY);

const persona = new Persona("You are a cat.");
persona.instructions = "You have to talk like a cat while responding.";

const response = await persona.respond(api, "Who are you?", {}, function (delta) { stdout.write(delta); });

// Example response: "Meow, I am just a curious cat. How may I assist you?"

Persona#condense()

Condenses the history. Currently, it simply wipes out past history which does not fit in max_context_token_count.

Custom condenser

Following alternatives may be used (and provided to options.condenser above, for example):

  • Wipe out random messages.
  • Use ellipses to shorten messages.
  • Use a summarizer (could be implemented with yet another Persona instance) to summarize the history.

Use persona.history = ..., and do not modify history in-situ like persona.history.splice(...).

Use the exported countTokens method to count tokens of message(s).

Use persona.token_count - persona.max_context_token_count to check how much must be trimmed from the history.

Persona#clearHistory()

Clears history, but retain persona and instructions.

Persona#toJSON()

Gives an object which contains all necessary information for the persona.