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

'TextEncoder' is not defined - MongoDB Functions #91

Open
georgeherby opened this issue Feb 11, 2024 · 1 comment
Open

'TextEncoder' is not defined - MongoDB Functions #91

georgeherby opened this issue Feb 11, 2024 · 1 comment

Comments

@georgeherby
Copy link

Sadly due to MongoDB limitations see here TextEncoder and TextDecoder.

This is the error I get

{"message":"'TextEncoder' is not defined","name":"ReferenceError"}

Not sure if there is a workaround e.g. passing in an override or if using https://www.npmjs.com/package/util would help?

I tried to bodge overriding global, but Mongo stop you doing that...

if (typeof TextEncoder === "undefined") {
  global.TextEncoder = class TextEncoder {
    encode(str) {
      const utf8 = unescape(encodeURIComponent(str));
      const result = new Uint8Array(utf8.length);
      for (let i = 0; i < utf8.length; i++) {
        result[i] = utf8.charCodeAt(i);
      }
      return result;
    }
  };
}

So I am at an impasse and can't use the library in this instance

This is a code snippet (can't share the whole function)

import { encodingForModel } from 'js-tiktoken';

function getTokenToken(content, model) {
  const enc = encodingForModel(model);
  const tokens = enc.encode(content);
  return tokens.length;
}

@georgeherby
Copy link
Author

Just to say i have forked and published a version where it works with the utils dep.

No idea if its compatible with all scenarios, I cap raise an MR if of interest

https://github.com/georgeherby/tiktoken-mongodb

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

1 participant