You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用huggingface.js调用textGeneration时出现Error fetching blob错误求助

解决Hugging Face.js textGeneration的"An error occurred while fetching the blob"错误

问题背景

之前基于OpenAI GPT API搭建的项目,转用huggingface.js API时,将原postChatGPTMessage替换为postHFMessage函数后,调用出现Error: An error occurred while fetching the blob错误。

原代码

const postHFMessage = async (contextMessage, conversation) => {
    // build the conversation for context
    const { textGeneration } = await import('@huggingface/inference');
    
    // Prepare the payload for the text generation request
    const payload = {
        inputs: contextMessage,
        parameters: {
            max_length: 100,
            temperature: 0.1,
            // Add any other parameters as needed
            },
        };

    // Call the textGeneration function with your payload
    try {
        const response = await textGeneration({
            accessToken: process.env.HUGGINGFACE_API_KEY,
            model: USED_MODEL,
            inputs: payload.inputs,
            parameters: payload.parameters,
        });
        if (response) {
            const responseData = await response.json();
            console.log('Received response:', responseData);
            return response;
        }
        else {
            console.log("no response from textGeneration");
        }
    }
    catch(error) {
        console.log("Could not get response from textGeneration: ", error);
    }
};

报错信息

Could not get response from textGeneration:  Error: An error occurred while fetching the blob
    at request (file:///Users/o*******/node_modules/@huggingface/inference/dist/index.js:207:11)

解决方案

这个错误既不需要把参数转为Blob,也不是权限问题,核心是错误处理了textGeneration的返回值:huggingface.js的textGeneration方法内部已经完成了响应解析,直接返回JSON格式的结果,不需要再调用.json()方法。

修改后的代码

const postHFMessage = async (contextMessage, conversation) => {
    const { textGeneration } = await import('@huggingface/inference');
    
    const payload = {
        inputs: contextMessage,
        parameters: {
            max_length: 100,
            temperature: 0.1,
        },
    };

    try {
        // textGeneration直接返回解析后的响应数据,无需再调用.json()
        const responseData = await textGeneration({
            accessToken: process.env.HUGGINGFACE_API_KEY,
            model: USED_MODEL,
            inputs: payload.inputs,
            parameters: payload.parameters,
        });
        console.log('Received response:', responseData);
        return responseData;
    } catch(error) {
        console.log("Could not get response from textGeneration: ", error);
    }
};

额外验证点

  • 确认USED_MODEL是支持文本生成任务的模型(如gpt2、distilgpt2、meta-llama/Llama-2-7b-chat-hf等),且你的API密钥拥有该模型的访问权限
  • 免费API密钥有调用次数与速率限制,若超出额度也会引发异常,可登录Hugging Face账户查看使用情况

内容的提问来源于stack exchange,提问作者Omer Sela Rothenberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 06:35:57