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.
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.
You can install GPTyped using NPM or Yarn:
npm install gptyped zod
or
yarn add gptyped zod
Ready to start? Check out the full docs here ?
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 responseexport 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 responseconsole.log(result.tweet) // "Spring is the best season because of the flowers and nature."console.log(result.tags) // ["#spring", "#flowers", "#nature"]
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.
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
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.