Closed Andision closed 11 months ago
Hi @Andision, could you please share a reproducible repository? Thanks 😄
Hi @Andision, could you please share a reproducible repository? Thanks 😄
Thank you for your reply! Here is the repo and branch I am working on. You can open it in Gitpod and use yarn install && yarn run dev
to dev it.
I see now, the issue is caused by importing @dqbd/tiktoken
via "use client"
directive. Will need to investigate further, but it does seem like a bug related to RSC webpack plugin.
There are various possible workarounds which you may consider when using experimental appDir
:
@dqbd/tiktoken
only in API routes and create a fetch request. That should be well supported.tiktoken_bg.wasm
from node_modules/@dqbd/tiktoken
to a public folder and use @dqbd/tiktoken/init
to load via fetch"use client";
import { type Tiktoken, encoding_for_model, init } from "@dqbd/tiktoken/init";
import { useEffect, useState } from "react";
export default function ClientComponent() {
const [encoder, setEncoder] = useState<Tiktoken>();
useEffect(() => {
fetch("/tiktoken_bg.wasm")
.then(async (wasm) => {
const buffer = await wasm.arrayBuffer();
await init((imports) => WebAssembly.instantiate(buffer, imports));
setEncoder(encoding_for_model("gpt-4"));
})
.catch((e) => {
console.error(e);
});
});
const [input, setInput] = useState("hello world");
const tokens = encoder?.encode(input);
}
Please note that it might be a better idea to feth tiktoken_bg.wasm
only once.
I see now, the issue is caused by importing
@dqbd/tiktoken
via"use client"
directive. Will need to investigate further, but it does seem like a bug related to RSC webpack plugin.There are various possible workarounds which you may consider when using experimental
appDir
:
- Use
@dqbd/tiktoken
only in API routes and create a fetch request. That should be well supported.- Copy
tiktoken_bg.wasm
fromnode_modules/@dqbd/tiktoken
to a public folder and use@dqbd/tiktoken/init
to load via fetch"use client"; import { type Tiktoken, encoding_for_model, init } from "@dqbd/tiktoken/init"; import { useEffect, useState } from "react"; export default function ClientComponent() { const [encoder, setEncoder] = useState<Tiktoken>(); useEffect(() => { fetch("/tiktoken_bg.wasm") .then(async (wasm) => { const buffer = await wasm.arrayBuffer(); await init((imports) => WebAssembly.instantiate(buffer, imports)); setEncoder(encoding_for_model("gpt-4")); }) .catch((e) => { console.error(e); }); }); const [input, setInput] = useState("hello world"); const tokens = encoder?.encode(input); }
Please note that it might be a better idea to feth
tiktoken_bg.wasm
only once.
but with move this file to a static folder manually , everytime that the package is updated we need to copy it again, is there a better way to fetch it?
Hello! Haven't gotten around to research deeper into RSC, but I think js-tiktoken
should suffice for now! Closing, let me know if you have any more issues.
I an using
"@dqbd/tiktoken": "^1.0.6",
withNext.js 13.2.4
like this.I got an error like this
I found when I comment these lines, it will be fine. I searched on Internet and I think this issue may be related to this question but I have no idea how to fix it.