开始使用免费开始使用

文本生成

LLM 具备多种能力,其中最常见之一就是文本生成。

您需要针对变量 text 中的客户评论生成一段回复;其中包含您之前见过的 Riverview Hotel 的同一条评论。

pipeline 模块已为您加载。

本练习是课程的一部分

Python 中的 LLM 入门

查看课程

练习说明

  • 实例化 generator pipeline,并为文本生成指定合适的任务。
  • 在 f-string 中把 textresponse 都包含进来,完成 prompt
  • 完成模型 pipeline:将最大长度设为 150 个 token,并把 pad_token_id 设为序列结束 token。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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"])
编辑并运行代码