开始使用免费开始使用

多轮对话

让我们扩展旅行聊天机器人,使用户可以对模型的初始推荐作出回应。您将再次使用 Conversation 类,但这一次,您会多次调用模型,观察它如何处理先前的信息。

作为提醒,以下是 Conversation 类中的方法:

  • __init__(self, llm: Llama, system_prompt='', history=[])
  • create_completion(self, user_prompt='')

本练习是课程的一部分

使用 Llama 3

查看课程

练习说明

  • 先请求一次旅行推荐,在模型首次回复后,再提供一个后续请求。

交互式实操练习

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

chatbot = Conversation(llm, system_prompt="You are a travel expert that recommends a travel destination based on a prompt. Return the location name only as 'City, Country'.")

# Ask for the initial travel recommendation
first_recommendation = chatbot.____("Recommend a Spanish-speaking city.")
print(first_recommendation)

# Add an additional request to update the recommendation
second_recommendation = chatbot.____("A different city in the same country")
print(second_recommendation)
编辑并运行代码