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

@snomiao/zod-chatgpt

v1.0.13

Published

This project demonstrates the use of zod and openai's chatgpt to generate formatted, typed, consistent output:

Downloads

10,666

Readme

Zod as a chatGPT guardrail

This project demonstrates the use of zod and openai's chatgpt to generate formatted, typed, consistent output:

  • Zod is used to create a schema from which the typescript type of the response is infered.
  • Zod's schema is also used to generate a json schema used as context for the llm's prompt.
  • Zod's schema is finally used to validate the response and guarantee the output to the function calling.

To use, remember to provide your openai api key as OPENAI_API_KEY env variable (in a .env file, for instance)

Edit schema and prompt in main.ts

install dependencies and run yarn start

Usage

bun install @snomiao/zod-chatgpt

Example output

Prompt :

"3 pirates talking about their treasure"

Output:

[
  {
    name: 'Captain Jack',
    text: "Arrr, me treasure chest be filled to the brim with gold doubloons and precious jewels! Aye, a true pirate's bounty it be."
  },
  {
    name: 'Blackbeard',
    text: "Ha! Ye scallywags don't know how to truly amass treasure. The real riches be in the form of rare artifacts and artifacts, worth more than any ordinary loot."
  },
  {
    name: 'Anne Bonny',
    text: "Ye two be fools! The real treasure be in the thrill of the chase and the joy of the fight. And let's not forget the power and respect that come with being a feared pirate."
  }
]

Schema (chatGPT generated it for me)

const schema = z.object({
  name: z.string().max(100).optional(),
  email: z.string().email().optional(),
  age: z.number().min(18).max(120).optional(),
  phone: z
    .string()
    .regex(/^\+\d{1,3}\s\d{3}\s\d{3}\s\d{4}$/)
    .optional(),
  address: z
    .object({
      street: z.string().max(100).optional(),
      city: z.string().max(50).optional(),
      state: z.string().max(50).optional(),
      zip: z.string().length(5).optional(),
    })
    .optional(),
  interests: z.array(z.string()).max(5).optional(),
  employment: z
    .object({
      companyName: z.string().max(100).optional(),
      jobTitle: z.string().max(100).optional(),
      salary: z.number().min(0).optional(),
      startDate: z
        .string()
        .regex(/^\d{4}-\d{2}-\d{2}$/)
        .optional(),
      endDate: z
        .string()
        .regex(/^\d{4}-\d{2}-\d{2}$/)
        .optional(),
    })
    .optional(),
  education: z
    .array(
      z.object({
        institutionName: z.string().max(100).optional(),
        degree: z.string().max(50).optional(),
        fieldOfStudy: z.string().max(50).optional(),
        graduationDate: z
          .string()
          .regex(/^\d{4}-\d{2}-\d{2}$/)
          .optional(),
      })
    )
    .max(5)
    .optional(),
  skills: z
    .array(
      z.object({
        name: z.string().max(50).optional(),
        rating: z.number().min(1).max(10).optional(),
      })
    )
    .max(10)
    .optional(),
});

Prompt:

A pirate talking about her treasure

Result:

{
  name: 'Sally',
  age: 28,
  email: '[email protected]',
  phone: '+1 555 123 4567',
  address: {
    street: '123 Main St',
    city: 'Tortuga',
    state: 'Caribbean',
    zip: '12345'
  },
  interests: [ 'sailing', 'rum', 'map reading' ],
  employment: {
    companyName: "Blackbeard's Fleet",
    jobTitle: 'First Mate',
    salary: 50000,
    startDate: '2015-06-01',
    endDate: '2020-12-31'
  },
  education: [
    {
      institutionName: 'Pirate University',
      degree: 'Bachelor of Piracy',
      fieldOfStudy: 'Navigational Studies',
      graduationDate: '2015-05-31'
    }
  ],
  skills: [
    { name: 'Navigation', rating: 9 },
    { name: 'Sword Fighting', rating: 8 },
    { name: 'Rum Drinking', rating: 10 }
  ]
}

Schema

const schema = z.object({
  sentiment: z.enum(["positive", "negative", "neutral"]),
  confidenceScore: z.number({
    description: "How confident is the LLM on the sentiments it analyses",
  }),
  sourceExample: z.string({
    description: "excerpt of the text where the sentiment was found",
  }),
});

Prompt (chatgpt generated) :

Analyze this text:
Get ready to witness the power of computer engineering! We're about to blow this project out of the water with some serious skills. Let's show everyone what we're made of and bring home the win. I'm feeling pumped up and ready to conquer any challenge that comes our way. Let's do this!

Result:

{
  sentiment: 'positive',
  confidenceScore: 0.95,
  sourceExample: "I'm feeling pumped up and ready to conquer any challenge that comes our way."
}

Schema

const schema = z.array(
  z.object({
    command: z.enum(["switchOn", "switchOff"]),
    room: z.enum(["livingRoom", "kitchen", "room1", "room2"]),
  })
);

Prompt :

const prompt = "I am going to the kitchen";

Result:

[
  { command: 'switchOff', room: 'livingRoom' },
  { command: 'switchOff', room: 'room1' },
  { command: 'switchOff', room: 'room2' },
  { command: 'switchOn', room: 'kitchen' }
]

Updates

2024-07-16 release as package