建立 Q&A pipeline
在前一個練習中,我們使用 PyPDF 從多頁文件擷取文字,並整理成單一字串。
現在,你要使用 Hugging Face 建立問答(question-answering)pipeline,從文件中擷取特定答案。
pipeline 模組與 document_text 都已為你預先載入。
本練習屬於課程
善用 Hugging Face
練習說明
- 以正確的任務與模型初始化問答(question-answering)pipeline。
- 將問題與
document_text傳入 pipeline。 - 列印結果以查看答案。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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['____']}")