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

Define "model" as string literal union type

Open custardcream98 opened this issue 2 years ago • 0 comments

Describe the feature or improvement you're requesting

I am currently trying to post multiple requests using both chat completion and text completion at a same time using Promise.race like this.

const { data } = await Promise.race([
    openai.createChatCompletion({
      model: "gpt-3.5-turbo",
      messages: [
        {
          role: "system",
          content: prompt,
        },
      ],
      temperature: 0.9,
      n: 1,
    }),
    openai.createCompletion({
      model: "text-davinci-003",
      prompt,
      temperature: 0.9,
      n: 1,
      max_tokens: 2000,
    }),
  ]);

Both CreateChatCompletionResponse | CreateCompletionResponse has same key model, but I can't narrow type of response like this.

if (data.model === "gpt-3.5-turbo") {
  const { message } = data.choices[0]; // error!
}

but, if type of model is defined as string literal union type like below, it will resolve the error. (so that it can be distinguished using discreminated union)

type ChatCompletionModel = "gpt-3.5-turbo" | "gpt-3.5-turbo-0301";
interface CreateChatCompletionResponse = {
  model: ChatCompletionModel;
  /* ... */
}

if (data.model === "gpt-3.5-turbo") {
   /* now the type checker knows the type of `data` is `CreateChatCompletionResponse` */
}

I know the type of model should be string because of the fine-tuned models, but if possible, could you please define model type? I can resolve this issue by using custom type guards, but I think it would be nice to have more narrow type of model.

thank you!

Additional context

No response

custardcream98 avatar Mar 09 '23 15:03 custardcream98