Getting Started

Quickstart

Install

Recommend using pnpm for installation.

npm i ai-stream-sdk

Basic Usage

For example, in a Next.js project, combined with OpenAI

app/api/chat/route.ts
import { OpenAIStream } from 'ai-stream-sdk'
import OpenAI from 'openai'
 
const openai = new OpenAI({
  apiKey: 'sk-*****',
})
 
export const runtime = 'edge'
 
export async function POST(request: Request) {
  const response = await openai.chat.completions.create({
    model: 'gpt-3.5-turbo',
    messages: [{ role: 'user', content: 'Say this is a test' }],
    stream: true,
  })
 
  const stream = OpenAIStream(response, {
    onStart: () => {
      console.log('onStart')
    },
    onCompletion: (completion) => {
      console.log(completion, 'completion')
    },
  })
 
  return new Response(stream)
}