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

retort-js

v0.0.3-dev.8

Published

Intuitive, production-ready prompt chaining in Javascript

Downloads

504

Readme

RetortJS

NPM License

Intuitive prompt chaining in Javascript. Designed for production.

Usage

module.exports = require('retort-js').retort(async ($) => {
  $.system`You are 'Retorter', an AI that responds in a quick & witty manner.`;
  
  await $.user.input({query: "Ask a question: "});

  await $.assistant.generation();
});

Refer to the examples section below for more details.

Getting Started

Installation

To install via NPM, run:

npm i retort-js

Give Retort a try

The quickest way to try Retort is in our playground:

RetortJS Playground

This will open a development project with an example script you can modify, and a UI where you can run your script and see the output of each message in the conversation. You will need to add your own OpenAI API key.


How to run your first Retort script

API key setup

[!NOTE] Currently only setup to use with OpenAI's API.

Add your OpenAI API key to the following environment variable:

OPENAI_API_KEY=<your key>

Setup 'retort' directory

Create a retort directory within your project to store your Retort scripts. This isn't required but is recommended as @retort-js/dev-server will search for scripts here.

Create first file

Create a new example.rt.js file and copy and paste the following code:

module.exports = require('retort-js').retort(async ($) => {
  $.system`You are 'Retorter', an AI that responds in a quick & witty manner.`;

  await $.user.input({query: "Ask a question: "});

  await $.assistant.generation();
});

Run the script

You can see the output of the conversation directly in your console by running the following command:

node retort/example.rt.js

This will show you each message in the conversation so you can easily see what happens at each step of the conversation.

What's going on here?

module.exports = require('retort-js').retort(async ($) => { ... }) imports the Retort library, defines a Retort script with the retort() function, and exports it. You don't need to import/export like this, but it's recommended for simplicity. In the script we are passing a simple instruction to the system and then waiting for the user to input their message before finally awaiting the response from the LLM.

The $

This is a RetortConversation. It keeps track of all the messages in a script and exposes methods for creating a message for each role type (system, user and assistant), and allows you to build a chain of prompts.

These can be called with a string or with a tagged template literal:

// valid
$.system("Write a haiku about Large Language Models.");

// also valid
$.system`Write a haiku about Large Language Models.`;

In your terminal you should see the initial system message. You can now enter your own message for the assistant to reply to. Once you've entered your message the assistant.generation() function will interface with the OpenAI API and return the response.

You can now see exactly what happened each step of the way.

The RetortConversation object also exposes various settings such as the model, temperature and topP.

retort()

The retort function creates a Retort object. This object represents a conversation that can be run. The function takes a chatFunction as an argument, which is a function that defines the conversation (ie, your script).

The chatFunction takes a RetortConversation ($) as an argument. It can also take any number of additional arguments but we don't need those for this example.

The retort function then returns a Retort object that includes a unique ID, a _run function, and a retortType property. You can then call this _run function on the exported Retort object wherever you're using it in your codebase. It allows you to run the file as either a script or a module:

const retorter = retort(async ($) => {/* script goes here */});

// as a script
retorter._run();

// as a retort module
retort(async ($) => {
  await $.run(retorter);
});

Examples

A script that keeps a conversation open with an LLM until the user is ready to end the conversation:

module.exports = require("retort-js").retort(async $ => {
  $.system`
  You are 'Retorter', an AI that responds in a quick & witty manner.
  If the user wishes to end the conversation, say "DONE" in all caps.
  `;

  let reply;

  while (!reply?.content.includes("DONE")) {
    await $.user.input();
    reply = await $.assistant.generation();
  }
});

The script will wait for your input into the console before calling the assistant.generation() function.


Passing parameters to a conversation:

module.exports = require("retort-js").retort(async ($, { age }) => {
  $.user`A birthday message for someone ${age} years old`;

  await $.assistant.generation();
});

A script that imports and runs another script:

// script.rt.js
module.exports = require("retort-js").retort(async ($) => {
  const imported = await $.run(require("./example-2.rt.js"));
  
  // use the imported conversation here
});

If you're using ESM:

import { retort } from "retort-js";

const retorter = retort(async ($) => {
  $.system`You are 'Retorter', an AI that responds in a quick & witty manner.`;

  await $.user.input();

  await $.assistant.generation();
});

retorter._run(); // run the conversation

RetortConversation Properties

| Property | Description | | ----------------- |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | id | A unique identifier for the conversation. | | chat | A reference to the conversation itself. | | messagePromises | An array of messages or promises of messages in the conversation. | | settings | An object containing settings for the conversation, including the model used, temperature, and topP. | | model | A getter and setter for the model used in the conversation. | | temperature | A getter and setter for the temperature setting of the conversation. | | topP | A getter and setter for the topP setting of the conversation. | | messages | A getter for the messages in the conversation. Throws an error if any message promises have not yet resolved. | | user | An agent representing the user in the conversation. | | input | A getter for the user's input in the conversation. A getter for the user's input in the conversation. It takes an object {query: string} as an argument. This defines what input query the user will see in the console. | | assistant | An agent representing the assistant in the conversation. | | system | An agent representing the system in the conversation. | | generation | A getter for the assistant's generation in the conversation. | | prompt | A getter for the user's prompt in the conversation. | | run | A function that runs a conversation. It takes a Retort object as an argument. |

Join our Community

Join us in the RetortJS Discord to share your experiences, ask questions, contribute, and get help from the creators.