Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work on front-end in Next.js projects #32

Open
Johnrobmiller opened this issue Feb 9, 2023 · 6 comments
Open

Does not work on front-end in Next.js projects #32

Johnrobmiller opened this issue Feb 9, 2023 · 6 comments

Comments

@Johnrobmiller
Copy link

Johnrobmiller commented Feb 9, 2023

Even after adding fs via yarn add fs, I still get this error. ):
Screenshot:
image

@Clueed
Copy link

Clueed commented Feb 11, 2023

Yes, this is a node module. fs and path are not available in the browser.

You can get around that fairly easily. vocab.bpe needs be saved as a json.

const bpe_file = fs.readFileSync(path.join(__dirname, './vocab.bpe'), 'utf-8');
fs.writeFileSync('./vocab_bpe.json', JSON.stringify(bpe_file))

Then the two jsons can be loaded directly.

//const fs = require('fs')
//const path = require('path');

//const encoder = JSON.parse(fs.readFileSync(path.join(__dirname, './encoder.json')));
//const bpe_file = fs.readFileSync(path.join(__dirname, './vocab.bpe'), 'utf-8');

const encoder = require('./encoder.json')
const bpe_file = require('./vocab_bpe.json')

Modifying the module itself didn't work for me. I get errors around process not being defined in node_module/path even through it's no longer using path and doesn't have any dependencies that do. Coping the code with the modification above into react with import { encode } from './gpt-3-encoder-react/Encoder' works perfectly fine. Tested on react 18.2.0.

Zip with both jsons and modified code.


There are actually two pull requests that address this issue: #33 and #30. I think this issue can be closed and one of the two should be merged.

@nem035
Copy link

nem035 commented Feb 28, 2023

If interested, a temporary fix would be to use https://www.npmjs.com/package/@nem035/gpt-3-encoder until the maintainers merge #33

npm i @nem035/gpt-3-encoder

@syonfox
Copy link

syonfox commented Mar 8, 2023

YOu can probably use the browser version from my 1.4 release. This removed all node dependencies by including data as js files. THis can be used directly or in the browserified version.

I have just pushed a new update it should be getting pretty stable. let me know if it works for you

https://syonfox.github.io/GPT-3-Encoder/browser.html

@niieani
Copy link

niieani commented Apr 16, 2023

You can also use my fork gpt-tokenizer. See the playground here that works fully in the browser.

@ruckc
Copy link

ruckc commented May 4, 2023

@syonfox - getting this in a frontend svelte project with the latest release - 1.1.4.

p.js:16 ReferenceError: __dirname is not defined
    at node_modules/.pnpm/[email protected]/node_modules/gpt-3-encoder/Encoder.js (Encoder.js:5:54)
    at __require (chunk-ENY474MH.js?v=4b935346:5:50)
    at node_modules/.pnpm/[email protected]/node_modules/gpt-3-encoder/index.js (index.js:1:28)
    at __require (chunk-ENY474MH.js?v=4b935346:5:50)
    at index.js:6:1

@syonfox
Copy link

syonfox commented May 5, 2023

is that not built in node if your in the browser environment you will need node_modules/gptoken/browser.js copy to relevant location in build

just include this as a regular file and use let c = gptoken.count(str)

also npm install gptoken

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants