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

Improving documentation about the use of api vs importing function directly #22

Open
nahumzs opened this issue Jul 2, 2023 · 2 comments

Comments

@nahumzs
Copy link

nahumzs commented Jul 2, 2023

From the documentation seems possible to import functions directly from gpt-tokenizer as:

import { encodeChat, isWithinTokenLimit } from "gpt-tokenizer/esm/main"

But doing it and running that code will create an exception on the GptEncoding since this inside of the execution will be undefined.


on clk100_base.js:

const api = GptEncoding.getEncodingApi('cl100k_base', () => convertTokenBytePairEncodingFromTuples(encoder));
const { decode, decodeAsyncGenerator, decodeGenerator, encode, encodeGenerator, isWithinTokenLimit, encodeChat, encodeChatGenerator, } = api;
export { decode, decodeAsyncGenerator, decodeGenerator, encode, encodeChat, encodeChatGenerator, encodeGenerator, isWithinTokenLimit, };

export default api;

all destructure functions will not hold the correct reference to it's scope, not really know why, but executing them without using api (the reference) will throw an exception and it is hard to debug.

The following code works

import api from "gpt-tokenizer/esm/main"
import { ChatMessage } from "gpt-tokenizer/esm/GptEncoding"

 api.modelName = 'YourModelName' // gpt-4
 const chatTokens = api.encodeChat(historyChat, "gpt-4")
 const currentTokenCount = chatTokens.length
 const withinTokenLimit = api.isWithinTokenLimit(historyChat, tokenLimit)

Should we rework the documentation so it is clear that you should use the GptEncoding instance instead of the isolated functions? do you want me to open a PR?

Btw I'm using vite, and I'm not sure this could also be a bundling issue.

@mh739025250
Copy link

I face the same problem. Any work on it now?

@nahumzs
Copy link
Author

nahumzs commented Jul 13, 2023

I face the same problem. Any work on it now?

Do you mean, if it has been change on the documentation? Or not working for you, cause if you use it as I describe will work

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

2 participants