botisan-ai / gpt3-tokenizer

Isomorphic JavaScript/TypeScript Tokenizer for GPT-3 and Codex Models by OpenAI.
MIT License
171 stars 19 forks source link

GPT3Tokenizer is not a constructor #12

Closed rlin415 closed 1 year ago

rlin415 commented 1 year ago

Hi, I get an error when trying to instantiate GPT3Tokenizer.

const tokenizer = new GPT3Tokenizer({ type: 'gpt3' });
                  ^

TypeError: GPT3Tokenizer is not a constructor

gpt3-tokenizer v1.1.4 Node.js v18.12.1

lhr0909 commented 1 year ago

Make sure when you import, you are using the right import methods.

For TypeScript:

import GPT3Tokenizer from 'gpt3-tokenizer';

For NodeJS using require, you need to access the default field:

const GPT3Tokenizer = require('gpt3-tokenizer').default;

If you take a look at the projects that uses this lib, you can find how other people are importing it. Hope this helps!

andrewschmidt-a commented 1 year ago

In ES Module you have to do GPT3Tokenizer.default even if using an import statement

so the call to fix the OP's issue would be: const tokenizer = new GPT3Tokenizer.default({ type: 'gpt3' });