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

如何在Haystack v2.0(haystack-ai)中构建自然语言推理管道

在Haystack v2.0(haystack-ai)中实现自然语言推理管道

步骤1:安装依赖

首先确保安装必要的包:

pip install haystack-ai haystack-integrations

步骤2:实现代码示例

Haystack v2.0对组件结构和导入路径做了调整,以下是对应实现:

from haystack import Pipeline
from haystack_integrations.components.classifiers.hugging_face import HuggingFaceTextClassifier

# 初始化自然语言推理分类器
classifier = HuggingFaceTextClassifier(
    model_name_or_path="roberta-large-mnli",  # 替换为你的NLI模型
    task="text-classification",
    labels=["entailment", "contradiction", "neutral"]
)

# 构建管道
classifier_pipeline = Pipeline()
classifier_pipeline.add_component(name="nli_classifier", component=classifier)

# 定义前提和假设
premise = "The sun rises in the east and sets in the west."
hypothesis = "The sun rises in the east."

# 运行管道
result = classifier_pipeline.run(
    data={"nli_classifier": {"text": premise, "text_pair": hypothesis}}
)

# 输出结果
print(result["nli_classifier"]["labels"])
print(result["nli_classifier"]["scores"])

关键变化说明

  • 组件导入路径:HuggingFace文本分类器现在位于haystack_integrations.components.classifiers.hugging_face下,而非旧版本的haystack.nodes
  • 管道组件添加:使用add_component方法(修正了旧代码中的拼写错误add_cmponent)
  • 运行参数传递:v2.0的run方法通过data参数明确指定组件对应的输入,结构更清晰

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 20:12:07