Closed louis030195 closed 1 year ago
Hi! That's seems weird, will take a look :)
@louis030195 It seems like you need to pass a relative URL to the WASM file, as there is a conflict with the exports
field. Will look into it, but in the meantime, the following should work:
// @ts-expect-error
import wasm from "../../../../node_modules/@dqbd/tiktoken/lite/tiktoken_bg.wasm?module";
import model from "@dqbd/tiktoken/encoders/cl100k_base.json";
import { init, Tiktoken } from "@dqbd/tiktoken/lite/init";
export const config = { runtime: "edge" };
async function handler(req: Request) {
await init((imports) => WebAssembly.instantiate(wasm, imports));
const encoding = new Tiktoken(
model.bpe_ranks,
model.special_tokens,
model.pat_str
);
const tokens = encoding.encode("hello world");
encoding.free();
return new Response(`${tokens}`);
}
export default handler;
Update: I believe this is more of an issue of Next.JS, as providing a new exports
for tiktoken_bg.wasm?module
causes an another error in Next.JS (Failed to parse URL...)
@dqbd thanks a lot for the quick response, it works :)
Another possible solution is to enforce the next-middleware-wasm-loader
as the loader for the file.
// @ts-expect-error
import wasm from "!next-middleware-wasm-loader!@dqbd/tiktoken/lite/tiktoken_bg.wasm?module";
import model from "@dqbd/tiktoken/encoders/cl100k_base.json";
import { init, Tiktoken } from "@dqbd/tiktoken/lite/init";
export const config = { runtime: "edge" };
async function handler(req: Request) {
await init((imports) => WebAssembly.instantiate(wasm, imports));
const encoding = new Tiktoken(
model.bpe_ranks,
model.special_tokens,
model.pat_str
);
const tokens = encoding.encode("hello world");
encoding.free();
return new Response(`${tokens}`);
}
export default handler;
Finally, the issue should be fixed in 1.0.4, no need to use relative paths or force a different loader.
Hi, amazing work @dqbd making tiktoken JS compatible :)
I have an issue running it on vercel edge runtime though, been trying different things without success :(
Any idea of whats wrong?