開始使用免費開始

產生文字

LLM 具備多種能力,其中以文字產生最受歡迎。

你需要針對變數 text 中的顧客評論產生回覆;它包含你先前看過的 Riverview Hotel 的同一則顧客評論。

pipeline 模組已為你載入。

本練習屬於課程

Python 的 LLM 入門

檢視課程

練習說明

  • 建立 generator pipeline,並指定適合用於產生文字的任務。
  • 完成 prompt:在 f-string 中加入 textresponse
  • 完成模型的 pipeline 設定:將最大長度設為 150 個權杖(tokens),並把 pad_token_id 設為序列結尾權杖。

動手互動練習

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

# Instantiate the pipeline
generator = pipeline(____, model="gpt2")

response = "Dear valued customer, I am glad to hear you had a good stay with us."

# Complete the prompt
prompt = f"Customer review:\n{____}\n\nHotel reponse to the customer:\n{____}"

# Complete the model pipeline
outputs = generator(prompt, ____, pad_token_id=____, truncation=True)

print(outputs[0]["generated_text"])
編輯並執行程式碼