构建 Q&A 管道
在上一个练习中,您使用 PyPDF 从多页文档中提取了文本,并将其整理为一个字符串。
现在,您将使用 Hugging Face 构建一个问答(question-answering)管道,从文档中检索特定答案。
pipeline 模块和 document_text 均已为您预加载。
本练习是课程的一部分
使用 Hugging Face
练习说明
- 使用正确的任务类型和模型初始化一个问答管道。
- 将问题和
document_text传入该管道。 - 打印结果以查看答案。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Load the question-answering pipeline
qa_pipeline = pipeline(task="____", model="distilbert-base-cased-distilled-squad")
question = "What is the notice period for resignation?"
# Get the answer from the QA pipeline
result = qa_pipeline(question=____, context=____)
# Print the answer
print(f"Answer: {result['____']}")