huntabyte / chatty

A chatbot application build with OpenAI's ChatGPT API.
https://chatty-ecru.vercel.app
189 stars 112 forks source link

`tokenizer.ts` doesn't need a conditional import if you're using Vite's default bundler #13

Open inspiredlabs opened 1 year ago

inspiredlabs commented 1 year ago

tokenizer.ts doesn't need a conditional import if you're using Vite (npm run dev), but it is necessary when using (npm run build --debug && npm run preview).

So, why does tokenizer.ts require this conditional check to import GPT3TokenizerImport?

The code below (from the GitHub issue mentioned in the video), checks if you're using CommonJS or not.

const GPT3Tokenizer: typeof GPT3TokenizerImport =
 typeof GPT3TokenizerImport === 'function'
 ? GPT3TokenizerImport
 : (GPT3TokenizerImport as any).default

However – the following Just Works™️ when you're in npm run dev in Vite because it can handle mixed import syntax from ES6 module imports and CommonJS:

// $lib/tokenizer.ts
import GPT3Tokenizer from 'gpt3-tokenizer';

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

export function getTokens(input: string): number {
  const tokens = tokenizer.encode(input);
  return tokens.text.length;
}

I hope this helps.

inspiredlabs commented 1 year ago

Rich Harris on the state of commonJS, see this: https://youtu.be/AOXq89h8saI?t=298

inspiredlabs commented 1 month ago

And this: https://youtu.be/xCeYmdukOKI?t=541