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

ts-prompt

v0.3.1

Published

Typesafe prompt construction for LLM inference with zod-parsed results

Downloads

1,204

Readme

ts-prompt

ts-prompt is a simple typescript library for constructing typesafe prompts for LLMs. Its inputs (template arguments), and outputs (zod-parsed results) are guaranteed to be correctly typed so that code changes and prompt changes cannot get out of sync. The patterns in this library were extracted from SchoolAI's large, working codebase and agentic system. It is provider-agnostic, but works well with OpenAI.

Note that it is not possible to take advantage of template placeholders if prompts are stored in a database--the prompts must be stored in the code itself so that typescript's powerful engine can extract placeholders in the prompt template and create type consistency across the codebase.

Example

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY! })

const mkPrompt = initPromptBuilder<
  ChatCompletionCreateParamsNonStreaming,
  ChatRequest
>({ messages: [], model: 'gpt-3.5-turbo', stream: false, })

const requestCourseMetadata = mkPrompt({
  template: `
    You are an educational consultant. Extract the course or lesson name,
    subject, duration, key topics, and target audience. If information is
    not available, do not make up details--instead, report as null (or
    empty array if appropriate).

    Record your findings in the natural language {{language}}.
  `,
  respondWithJson(openai,
    z.object({
      name: z.string().nullable()
        .describe('The name of the course or lesson.'),
      subject: z.string().nullable()
        .describe('The subject of the course or lesson.'),
      duration: z.string().nullable()
        .describe('How long the course or lesson is, e.g. hours, days.'),
      keyTopics: z.array(z.string())
        .describe('The key topics covered in the course or lesson.'),
      targetAudience: z.string().nullable()
        .describe('The target audience for the course or lesson.'),
    })
  )
})

// Note: the async `requestCourseMetadata` function above will require a
// `language` template arg to be passed in, enforced by typescript. It
// also enforces that the response from the LLM is a JSON object with the
// correct shape and types, parsed by the zod schema provided. The result
// is guaranteed to be typed correctly (or an error will be thrown).

// Request the AI to provide a response
const details = await requestCourseMetadata({
  request: {
    messages: [
      {
        role: 'user',
        content: `
          The kindergarten class will be learning about the life cycle of a
          butterfly. The topic will cover the different stages from egg, to
          caterpillar, to chrysalis, and finally to butterfly. The lesson
          will include hands-on activities such as observing live caterpillars
          and creating butterfly crafts. The target audience for this lesson
          is young children aged 4-6 years old.
        `
      }
    ],
  },
  templateArgs: { language: 'English' },
  // optional:
  // config: { temperature: 0.8 },
})

// Note: in this example, the user message will be appended by default to
// the system message, before it is sent to the LLM for inference. But
// this is flexible--you can arrange or rearrange the timeline however
// you need. See the `respondWithJson` function for more info.

console.log(details)
// {
//   name: "The Life Cycle of a Butterfly",
//   subject: "Biology",
//   duration: "1 hour",
//   keyTopics: ["egg", "caterpillar", "chrysalis", "butterfly", "hands-on activities"],
//   targetAudience: "young children aged 4-6 years old",
// }

How to Use ts-prompt

ts-prompt is very flexible around what inference engine or LLM it uses, how it logs information, and what kind of model config it uses. In order to have this much flexibility, the first thing you need to create is a function that builds prompts, e.g. mkPrompt or mkImagePrompt (you can name it what you like):

import { OpenAI } from 'openai';
import { initPromptBuilder } from 'ts-prompt';

// Initialize OpenAI client with API key
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY! });

// Initialize the `mkImagePrompt` function with default configuration
const mkImagePrompt = initPromptBuilder<ImageGenerateParams, string>({
  prompt: '',
  model: 'dall-e-2',
  size: '256x256',
  response_format: 'url',
})

// now use `mkImagePrompt` to define a typesafe, specific image prompt:
const requestGenerateIcon = mkImagePrompt(
  `
  Create a beautiful, flat color image suitable for iconography.
  Make it in the style of '{{style}}'.
`,
  respondWithImage(openai, 'url'),
)

// finally, use the `requestGenerateIcon` function to request an image:
const images = await requestGenerateIcon({
  templateArgs: { style: 'absurdism' },
  request: 'a red apple',
})

You can create your own respondWithImage function if you want to use a different inference engine, or if you need special logging, tracking, retry logic, etc.

Other functions specific to OpenAI, such as respondWithJson, respondWithText, etc., are easy to change out with your own app-specific types or logging requirements. See src/openai/index.ts.

Tests

ts-prompt comes with a test suite of unit tests and a handful of integrtion tests. The integration tests call out to an OpenAI API endpoint and require a valid OPENAI_API_KEY to be set in the environment. To run the tests, use the following command:

$ pnpm test