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

prompting

v0.2.0

Published

A prompt engineering library for Node.js and TypeScript

Downloads

6

Readme

prompting

npm version

A batteries-included, model-agnostic prompt engineering library for Node.js and TypeScript.

Build dynamic, reusable prompts that output structured data that's ready to use in your application or API. Compatible with all text-based generative language models such as OpenAI GPT.

Features

  • Intuitive, flexible Prompt builder
  • Reusable prompt templates with variables
  • Validated output in JSON or CSV
  • Model-agnostic, extensible generation API
  • Serializable to database and files

Installation

To install prompting, use npm:

npm install prompting

Examples

Simple text prompt

import {Prompt} from 'prompting';

const prompt = Prompt().text('What is your favorite animal?')

console.log(prompt.toString(); // 'What is your favorite animal?'

Using template variables and default values

import {Prompt} from 'prompting';

const prompt = Prompt()
  .text('What is your favorite {{topic}}?')
  .defaults({topic: 'animal'});

prompt.toString(); // 'What is your favorite animal?'
prompt.vars({topic: 'color'}).toString(); // 'What is your favorite color?'

Generating prompt responses

The library also contains a flexible Generator class for generating responses to a Prompt. For convenience, the Generator.prompt() method creates a new prompt that is bound to the Generator instance and can be invoked by calling generate().

Here's an example using the OpenAIGenerator:

import {OpenAIGenerator} from 'prompting';

const gpt = new OpenAIGenerator({apiKey: 'my_api_key'});

const prompt = gpt.prompt().text('What is your favorite {{topic}}?');

const result = await prompt.generate({topic: 'color'});

The generate method returns a Promise that resolves to the model's response for the prompt.

Structured JSON data with validation

To output a structured object and validate the result automatically, construct your prompt using the schema method. The Prompt class leverages the power of JSON Schema and the battle-tested validation library ajv to validate the response.

const prompt = Prompt()
  .text('List {{num}} books by the author {{author}}.')
  .defaults({num: 3})
  .schema({
    type: 'array',
    items: {
      type: 'object',
      properties: {
        title: {type: 'string'},
        year: {type: 'string'},
      },
      required: ['title', 'year'],
    },
  });

const result = await prompt.generate({author: 'George Orwell'});

The generate method returns a Promise that resolves to the model's response if it matches the schema, or rejects with a validation error if the model's response doesn't match the schema.

TypeScript Support

The library supports strongly typed prompts, arguments, and return types when used with TypeScript. The Prompt class supports generics to specify the expected arguments and return type.

Here's an example:

import {Prompt} from 'prompting';

type BookVars = {author: string};
type Book = {title: string, year: string};

const prompt = Prompt<BookVars, Book>()
  .text('What is the most popular book by {{author}}?')
  .schema({
    type: 'object',
    properties: {
      title: {type: 'string'},
      year: {type: 'string'},
    },
    required: ['title', 'year'],
  });

const result: Book = await prompt.generate({author: 'George Orwell'});

In this example, the generate method takes an argument of type BookVars and returns a Promise that resolves to a Book object, or rejects with a validation error if the model fails to generate a valid response.

Prompt API

| Method | Description | Usage | --- | --- | --- | Prompt(options?: PromptOptions) | Creates a new instance of the Prompt class. | Prompt() | text(template: string) | Sets the text template for the prompt. | prompt.text('What is your favorite {{topic}}?') | defaults(defaults: object) | Sets default values for the variables in the text template. | prompt.defaults({topic: 'animal'}) | schema(schema: object) | Sets the JSON schema for validating the generated result. | prompt.schema({type: 'string'}) | generate(vars?: object) | Generates the final prompt text by replacing variables in the template, then executes the generator to get the AI response. | prompt.generate({color: 'red'}) | vars(vars: object) | Returns a copy of the Prompt with variables preset but does not generate the result, e.g. in order to call toString | prompt.vars({topic: 'animal'}) | using(generator: Generator) | Sets the generator for the prompt so that generate can be called. | prompt.using(generator) | toString() | Returns the final prompt text by replacing variables in the template. | prompt.toString() | toJSON() | Returns the prompt as a JSON object, useful for serializing to a file or database. | prompt.toJSON()

PromptOptions

| Property | Type | Description | --- | --- | --- | text | string | The text template for the prompt. | defaults | object | Default values for the variables in the text template. | schema | object | The JSON schema for validating the generated result. | generator | Generator | The generator instance to use for executing the prompt.

Contributing

Contributions to prompting are welcome! To contribute, please fork the repository and make your changes, then submit a pull request.