zod-stream
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

zod-stream

Define structured response models for OpenAI or Anyscale completions using Zod schemas and enable partial streaming of that json so that it can be used safely and right away.

Installation

with pnpm

$ pnpm add zod-stream zod openai

with npm

$ npm install zod-stream zod openai

with bun

$ bun add zod-stream zod openai

Basic Usage

Creating an endpoint that calls OpenAI with a defined response model. (Next.js app router, route handler example)

/api/get-stream

  import { OAIStream } from "zod-stream/OAIStream"
  import { withResponseModel } from "zod-stream/response-model"

  import OpenAI from "openai"
  import { z } from "zod"

  const oai = new OpenAI({
    apiKey: process.env["OPENAI_API_KEY"] ?? undefined,
    organization: process.env["OPENAI_ORG_ID"] ?? undefined
  })

export async function POST(request: Request) {
  const { messages } = await request.json()

  const params = withResponseModel({
    response_model: { schema: z.object({ content: z.string() }), name: "Content response" },
    params: {
      messages,
      model: "gpt-4"
    },
    mode: "TOOLS"
  })

  const extractionStream = await oai.chat.completions.create({
    ...params,
    stream: true
  })

  return new Response(
    OAIStream({
      res: extractionStream
    })
  )
}

Consuming the structured stream elsewhere, maybe in the browser.

  const client = new ZodStream()

  const stream = await client.create({
    completionPromise: async () => {
      const response = fetch("/api/get-stream", {
        body: JSON.stringify({ messages: [] }),
        method: "POST"
      })

      return response.body
    },
    response_model: { // should match model expected to be returned by the completion.
      schema: z.object({
        content: z.string()
      })
    }
  })

  for await (const chunk of extractionStream) {
    console.log(chunk) // safe to parse partial json
  }

Package Sidebar

Install

npm i zod-stream

Weekly Downloads

5,601

Version

1.0.3

License

MIT

Unpacked Size

101 kB

Total Files

8

Last publish

Collaborators

  • dimitrikennedy