開始使用免費開始

建立搜尋自動完成系統

搜尋自動完成(auto-complete)是常見的 NLP 應用,常見於搜尋引擎與通訊軟體。目標是根據使用者的部分輸入來提供可能的補全建議。你的任務是使用 Hugging Face 的 "text-generation" pipeline,實作一個基礎的自動完成系統,從使用者查詢中產生相關的補全。

本練習屬於課程

Python 的 Natural Language Processing(NLP)

檢視課程

練習說明

  • 使用 "distilgpt2" 模型建立 autocomplete pipeline。
  • 針對給定的 prompt 產生 5 個搜尋查詢 suggestions,且每個建議最多 8 個 token。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

from transformers import pipeline

# Create the pipeline
autocomplete = pipeline(task="____", model="____")

prompt = "Best books to read for"

# Generate search query completions
suggestions = ____

for suggestion in suggestions:
    print(suggestion['generated_text'])
編輯並執行程式碼