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

Langchain中用HuggingFace替换OpenAiEmbeddings遇错误求助

解决方案

错误1:HuggingFaceInferenceEmbeddings is not a constructor

HuggingFaceInferenceEmbeddings并非@huggingface/inference包的成员,它是LangChain封装的专属组件,必须从LangChain官方包导入:

  • 若使用LangChain v0.1+版本:从@langchain/community/embeddings/hf导入
  • 若使用旧版LangChain:从langchain/embeddings导入

错误2:__init__() got an unexpected keyword argument 'pooling_mode_weightedmean_tokens'

该问题由版本不兼容或模型参数配置错误导致,按以下步骤解决:

  1. 更新兼容依赖
    安装最新稳定版的LangChain及相关依赖:
    npm install @langchain/core @langchain/community @huggingface/inference --save
    
  2. 适配Instructor模型参数
    hkunlp/instructor-large是指令型嵌入模型,必须指定查询和文档的指令参数,否则会触发内部参数错误。修正后的代码:
    const { HuggingFaceInferenceEmbeddings } = require("@langchain/community/embeddings/hf");
    
    const embeddings = new HuggingFaceInferenceEmbeddings({
      apiKey: process.env.HUGGINGFACEHUB_API_KEY,
      model: "hkunlp/instructor-large",
      // 添加Instructor模型必需的指令参数
      queryInstruction: "Represent the query for retrieval:",
      documentInstruction: "Represent the document for retrieval:",
    });
    
    vectorStore = await HNSWLib.load(
      VECTOR_STORE_PATH,
      embeddings
    );
    
  3. 校验版本匹配度
    若仍报错,检查@huggingface/inference版本是否与当前LangChain版本兼容,可参考LangChain官方文档的依赖说明调整。

额外说明

无需更换向量数据库库,问题根源在嵌入模型的导入与配置环节,与HNSWLib无关。

内容的提问来源于stack exchange,提问作者user42141

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 21:20:01