-
Notifications
You must be signed in to change notification settings - Fork 38
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
encodeChatGenerator undefined? #15
encodeChatGenerator undefined? #15
Comments
Also facing this problem. The error occurs when I execute the following code: import { encodeChat } from "gpt-tokenizer";
const messages = [ /* Valid chat */ ];
console.log(encodeChat(messages, "gpt-3.5-turbo")); Here's the full error:
|
I'm also facing the same issue. |
I have the same issue. |
Same issue. |
@zz98 This problem is related to the way you're importing and using the encodeChat method from the gpt-tokenizer module. import { encodeChat } from 'gpt-tokenizer' You're importing encodeChat as a detached function from the original object (the module). Therefore, you won't have access to the context or properties of the original object. However, when you use the import statement as follows: import tokenizer from 'gpt-tokenizer'
tokenizer.encodeChat([{ ... }]) You're importing the entire original object. This allows you to access the properties and methods of the original object.
suggests that this is undefined or not what's expected in this.encodeChatGenerator(chat, model). In JavaScript, when a method is called detached from its object, this becomes undefined (in strict mode). Hence, calling the detached encodeChat method results in an error. However, when you call encodeChat in the form of tokenizer.encodeChat, the encodeChat method is bound to the tokenizer object. Therefore, this refers to the tokenizer object, and no error occurs. To resolve this issue, you should import the whole tokenizer and call encodeChat as a method of tokenizer. This way, this will correctly refer to tokenizer, and the error should not occur. (this answer written and translate by gpt-4.) |
@wgot Thanks for the explanation! I just had the same issue. However, it seems the underlying problem is that class functions really shouldn't be exported like that. Even the author itself has mistaken the behavior of their own code, because the usage example in the main import {
encode,
encodeChat,
decode,
isWithinTokenLimit,
encodeGenerator,
decodeGenerator,
decodeAsyncGenerator,
} from 'gpt-tokenizer' |
Same issue. Documentation needs to be updated as well. |
Still doesn't work: const tokenizer = require('gpt-tokenizer') I get this error:
|
Same issue const tokenizer = require('gpt-tokenizer') this.specialTokenMapping is undefined when the encodeChatGenerator function is called in cjs.
|
Solved like this for cjs |
Same issue, return [...this.encodeChatGenerator(chat, model)].flat(); |
this fixed my issue! Thanks! |
@niieani this is a valid point, please adjust the documentation for |
Apologies folks, the documentation was co-written by ChatGPT and I've missed this when doing a manual review. 😄 |
Wasn't a documentation issue after all, just forgot to bind the functions. Should be fixed in next version. |
🎉 This issue has been resolved in version 2.1.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
encodeChat error TypeError: Cannot read properties of undefined (reading 'encodeChatGenerator')
at encodeChat
node_modules/.pnpm/gpt-tokenizer@2.1.1/node_modules/gpt-tokenizer/cjs/GptEncoding.js:141:25
The text was updated successfully, but these errors were encountered: