Migrate OpenAI Node API from v3 to v4

Anh-Thi Dinh
draft
⚠️
This is a quick & dirty draft, for me only!
👆 It’s written for the first release of v4. It may be outdated for the later subversions of v4. For example, when I write this note, I’m using v4.19.0 and there are incomparabilities in the guide. This guide isn’t good for typescript problems too.
This note doesn’t re-write the ones already given in the official guide.

Return

The big impact is that they remove data before every response in the old version.
1// old
2chatCompletion.data.choices[0].message
3
4// new
5chatCompletion.choices[0].message

Types

For types, you should dive into the node_modules/openai and search for it!
1// v3
2import { ChatCompletionResponseMessage } from 'openai';
3let message: ChatCompletionResponseMessage;
4
5// v4
6import OpenAI from 'openai';
7let message: OpenAI.ChatCompletionMessage;

Function calling

🚨
An important note for ones who are going to use function calling. They changes the return format!
If you want to use Parallel Function Calling (multiple functions detected are integrated in the repsonse, for the old versions, there is only first one function will be integrated), you have to update openai to v4.
You have to use model gpt-3.5-turbo-0613 (or later) or gpt-4-1106-preview (or later) to use the parallel function calling!
⚠️
With the same request, you may obtain different responses for the parallel function calling! Sometimes there is only 1 function, sometimes there are more than 1. So, don’t be supprise!