Coder Social home page Coder Social logo

prompt.js's Introduction

prompt.js

A simple and flexible node.js prompt loader for OpenAI applications that supports both plain text and TOML formats.

Features

  • Load prompts from .txt and .toml files
  • Support for EJS templating for dynamic content
  • Easy configuration of prompt directory

Installation

npm install --save @themaximalist/prompt.js

Usage

const prompt = require("@themaximalist/prompt.js");

// Configure prompt directory (optional)
prompt.configure({
    promptDir: join(__dirname, "path/to/prompts"),
});

// Load a plain text prompt
// > cat example1.txt
// Hello <%= name %>
const response = prompt.load("example1", { name: "The Maximalist" });
console.log(response); // Hello The Maximalist

// Load a TOML prompt
// > cat example2.toml
// <% for (const chat of chats) { %>
// [[messages]]
// role = "<%= chat.role %>"
// content = """
// <%- chat.content %>
// """
// <% } %>
const messages = prompt.load("example2", {
    chats: [
        { role: "user", content: "Hello, I am a user" },
        { role: "assistant", content: "Hi, how can I help you?" },
    ]
});
console.log(messages); // [{...}, {...}]

API

configure(options)

Configure the prompt loader with custom options.

  • promptDir: A string representing the path to the directory containing the prompt files.

load(promptName, variables)

Load a prompt from a file and return the parsed content.

  • promptName: A string representing the name of the prompt file (without the file extension).
  • variables: An object containing variables to be used when parsing the prompt with EJS.

Error Handling

PromptError is an error class specific to this package, used when a prompt file cannot be found.

try {
    const message = prompt.load("nonexistent_prompt");
} catch (error) {
    if (error instanceof prompt.PromptError) {
        console.error("Prompt not found:", error.message);
    } else {
        throw error;
    }
}

Author

https://themaximalist.com

https://twitter.com/themaximal1st

License

This project is released under the MIT License.

prompt.js's People

Contributors

themaximalist avatar

Watchers

Kostas Georgiou avatar  avatar  avatar

Forkers

ag076810 illraps3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.