如何在React Webpack JS中使用WASM Tiktoken

构建问题是由于wasm未能实例化或未导入wasm引起的。

wasm.__wbindgen_add_to_stack_pointer 不是一个函数


回答:

为了在React中使用tiktoken wasm以获得更好的性能,请按照以下步骤进行webpack 5的配置。

在webpack配置中添加以下覆盖。你也可以直接从js cdn使用wasm。如果你想使用cdn,可以跳过这一步:

module.exports = (config, env) => {  config.experiments = {    asyncWebAssembly: true,    layers: true,  };}

使用配置覆盖来使用tiktoken:

import { get_encoding, init } from 'tiktoken/init';import wasm from 'tiktoken/tiktoken_bg.wasm';  let tikInit = false;  const countTokens= (str) =>{    if (!tikInit) {    tikInit = true;      const wasmFile = await fetch(wasm);      const buffer = await wasmFile.arrayBuffer();      await init((imports) => WebAssembly.instantiate(buffer, imports));    }    const e = get_encoding('cl100k_base');    const t = e.encode(str);    e.free();    return t.length;  }

使用js cdn来获取wasm并在不使用配置覆盖的情况下使用tiktoken:

import { get_encoding, init } from 'tiktoken/init';import wasm from 'tiktoken/tiktoken_bg.wasm';  let tikInit = false;  const countTokens= (str) =>{    if (!tikInit) {    tikInit = true;}   const wasmFile = await fetch(      'https://cdn.jsdelivr.net/npm/[email protected]/tiktoken_bg.wasm'    );       const buffer = await wasmFile.arrayBuffer();      await init((imports) => WebAssembly.instantiate(buffer, imports));    }    const e = get_encoding('cl100k_base');    const t = e.encode(str);    e.free();    return t.length;  }

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注