Thi's avatar
HomeAboutNotesBlogTopicsToolsReading
About|My sketches |Cooking |Cafe icon Support Thi
💌 [email protected]

Migrate OpenAI Node API from v3 to v4

Anh-Thi Dinh
draft
API & ServicesWeb Dev
Left aside
⚠️
This is a quick & dirty draft, for me only!
v3 to v4 Migration Guide · openai/openai-node · Discussion #217
v4 is a complete rewrite of the SDK. To see what's new, see the release notes. Installation First, update your package.json to specify v4: "openai": "^4.0.0" and run npm ins...
https://github.com/openai/openai-node/discussions/217
👆 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.
👉 Create completion (Legacy) vs new version.

Return

The big impact is that they remove data before every response in the old version.

Types

☝
For types, you should dive into the node_modules/openai and search for it!

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!
About|My sketches |Cooking |Cafe icon Support Thi
💌 [email protected]
1// old
2chatCompletion.data.choices[0].message
3
4// new
5chatCompletion.choices[0].message
1// v3
2import { ChatCompletionResponseMessage } from 'openai';
3let message: ChatCompletionResponseMessage;
4
5// v4
6import OpenAI from 'openai';
7let message: OpenAI.ChatCompletionMessage;