openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

Stream nor ChatCompletionStream types not exported

Open edgar0011 opened this issue 1 year ago • 3 comments

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • [X] This is an issue with the Node library

Describe the bug

One of openai.chat.completions.create return types is ApiPromise<Stream<OpenAI.Chat.ChatCompletionChunk>> yet the Stream is not the intrinsic Nodejs Stream, and not exported from openai.

To Reproduce

try to set type of awaited or in Promise return value from openai.chat.completions.create()

Code snippets

import OpenAI from 'openai'

import { config } from 'server/config'
import { errorHandler } from 'server/helpers'

const openai = new OpenAI({
  apiKey: config?.OPENAI_API_KEY as string,
})

export const createText = async(prompt: string): Promise<OpenAI.Chat.ChatCompletion | null> => {
  try {
    const params: OpenAI.Chat.ChatCompletionCreateParams = {
      messages: [{ role: 'user', content: prompt }],
      model: 'gpt-3.5-turbo',
      stream: true,
    }

    const options: OpenAI.RequestOptions = {}

    const chatCompletion: OpenAI.Stream<OpenAI.Chat.ChatCompletionChunk> = await openai.chat.completions.create(params, options)

    return chatCompletion
  } catch (error: unknown) {
    console.error(error)
    errorHandler(error)
    return null
  }
}

OS

any

Node version

18.13.0

Library version

4.20.1

edgar0011 avatar Dec 10 '23 10:12 edgar0011

Right now you can

import { Stream } from 'openai/streaming';
import { ChatCompletionStream } from 'openai/lib/ChatCompletionStream';

We'll take a look at adding them to the top-level export.

rattrayalex avatar Dec 11 '23 07:12 rattrayalex

This is actually good, IMO, but is this documented?

edgar0011 avatar Dec 11 '23 12:12 edgar0011

Thanks! We do need better docs on this. What would you like to see in those docs?

rattrayalex avatar Dec 11 '23 19:12 rattrayalex