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

gptyped

v1.0.5

Published

Utility for making structured requests and get type safe responses from LLMs. Like using JSON for OpenAI GPT.

Downloads

16

Readme

GPTyped Logo

NPM Version NPM MIT License

GPTyped

Welcome to GPTyped! 🎉 A small, but powerful NPM package that lets you interact with OpenAI's GPT language model in a type-safe way. With GPTyped, you can send objects as prompts and receive structured responses from LLM AIs in a breeze, whether you're running it on the Web or on the Server. 💻

Features

  • ✨ Sends TypeScript objects as prompts and receives TypeScript objects as responses.
  • 💪 Fully customizable with interceptors to alter requests or responses at any time.
  • 🤖 Comes pre-loaded with an OpenAI client for easy integration with GPT.
  • 🛠️ Validates returned objects schemas using Zod.
  • 💬 Support for common prompt patterns, like memory and metaprompts.

Installation

You can install GPTyped using NPM or Yarn:

npm install gptyped zod

or

yarn add gptyped zod

Basic Usage

Using GPTyped is simple. Here's an example of how you can send objects as prompts and receive type safe objects responses:

import { OpenAiClientBuilder, PrompterForObjectBuilder } from "gptyped"
import { z } from "zod"

// A Zod schema describing the type of the AI response
export const TweetSchema = z.object({
  tweet: z.string().min(1),
  tags: z.array(z.string()).min(3),
})
type Tweet = z.infer<typeof TweetSchema>

const gpTypedOpenAiClient = new OpenAiClientBuilder("YOUR_OPEN_AI_SECRET_KEY").build()

const prompterForObject = new PrompterForObjectBuilder(gpTypedOpenAiClient, TweetSchema, {
  tweet: "A tweet about the topic. Maximum of 140 characters.",
  tags: "3 hashtags about the tweet.",
}).build()

//  Use an input object to make a request to the OpenAI API. The response will be type safe.
const result = await prompterForObject.send<Tweet>({
  topic: "Why is spring the best season?",
})

// Access the Tweet type safe response
console.log(result.tweet) // "Spring is the best season because of the flowers and nature."
console.log(result.tags) // ["#spring", "#flowers", "#nature"]

Note: About structured responses and generative AI

LLM's reponses are not deterministic. This means that the exact same prompt can result in different responses by the AI. Having this in mind, it's impossible to guarantee if the response will be a valid data structure or not. GPTyped will validate the response against the schema you provided using Zod and return the response if it's valid. If the response is not valid, GPTyped will throw an error.

It's recommended to retry the request at least a couple of times whenever you encounter a type error, as usually subsequent requests will return a valid response. With OpenAI's GPT, you can also try lowering the temperature parameter of the AI to make it more predictable.

Contributing

Contributions are welcome! Feel free to open issues or pull requests for bug fixes, feature requests, or improvements. Please take a look at some planned improvements where you can jump in:

  • Automatic retry on failed requests
  • Clients for other popular LLM APIs
  • Support for Markdown request/responses
  • Support for CSV request/responses
  • Support for other popular data formats

License

GPTyped is open-source software released under the MIT License. Feel free to use, modify, and distribute it as per the terms of the license.