使用@ tensorflow模型/qna时出错:TypeError:context.trim不是函数

xzlaal3s  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(150)

我正在开发一款React应用,该应用使用TensorFlow QnA模型根据用户输入生成学习卡片。我实现了一个表单,用户可以在其中输入问题,提交后,应用应使用QnA模型生成答案,并创建包含问题和答案的新学习卡片。
但是,当我尝试提交表单时,出现以下错误:"类型错误:context.trim不是一个函数"。经过一些调查,我确定这个错误发生在QnA模型的process函数中,该函数由findAnswers函数调用。
以下是表单提交和QnA模型使用的相关代码:

import * as qna from "@tensorflow-models/qna";
import * as tf from "@tensorflow/tfjs";

//...

const [qnaModel, setQnAModel] = useState(null);

//...

const handleInputChange = (event) => {
    setInput(event.target.value.toString());
  };

// ...

 const handleInputSubmit = async (event) => {
    event.preventDefault();
    if (qnaModel) {
      // Make sure that the `context` parameter is a string
      let context = String(input).trim();

      // model => ready
      console.log(typeof context);
      const answers = await qnaModel.findAnswers(context, 1);
      console.log(answers[0].question); 
      console.log(answers[0].answer); 

      setCards([
        ...cards,
        { question: answers[0].question, answer: answers[0].answer },
      ]);
    } else {
      // The qnaModel is not ready yet, so you should not try to use it
      console.error("qnaModel is not ready yet");
    }
  };

我已经通过将上下文变量的类型记录到控制台来验证它确实是一个字符串。但是,错误仍然发生。
有没有其他人遇到这个错误或有任何想法如何修复它?任何帮助将不胜感激!
先谢了。

46qrfjad

46qrfjad1#

根据documentation,您看到该错误的原因是您没有向findAnswers传递正确的参数。

const answers = await model.findAnswers(question, passage);

其中第一个参数是要问的问题,第二个参数是问题所基于的段落,现在只传递数字1作为段落。
该文档提供了以下示例:

const passage = "Google LLC is an American multinational technology company that specializes in Internet-related services and products, which include online advertising technologies, search engine, cloud computing, software, and hardware. It is considered one of the Big Four technology companies, alongside Amazon, Apple, and Facebook. Google was founded in September 1998 by Larry Page and Sergey Brin while they were Ph.D. students at Stanford University in California. Together they own about 14 percent of its shares and control 56 percent of the stockholder voting power through supervoting stock. They incorporated Google as a California privately held company on September 4, 1998, in California. Google was then reincorporated in Delaware on October 22, 2002. An initial public offering (IPO) took place on August 19, 2004, and Google moved to its headquarters in Mountain View, California, nicknamed the Googleplex. In August 2015, Google announced plans to reorganize its various interests as a conglomerate called Alphabet Inc. Google is Alphabet's leading subsidiary and will continue to be the umbrella company for Alphabet's Internet interests. Sundar Pichai was appointed CEO of Google, replacing Larry Page who became the CEO of Alphabet."
const question = "Who is the CEO of Google?"
const model = await qna.load();
const answers = await model.findAnswers(question, passage);
console.log(answers);

还要注意的是,您试图访问答案的格式(answers[0].question)与您将得到的结果不一致。上面的文档示例记录了以下结果:

[{
  text: "Sundar Pichai",
  startIndex: 1143,
  endIndex: 1156,
  score: 0.8380282521247864
}]

根据编写代码的方式,如果要访问答案,可以使用answers[0].text

相关问题