开始使用免费开始使用

自洽提示词(Self-consistency prompts)

设想您经营一家同时销售笔记本电脑和手机的店铺。一天开始时店里共有 50 台设备,其中 60% 是手机。当天有三位顾客来访,每位各买了一部手机,其中一位另外又买了一台笔记本电脑。您还补充了库存:新增 10 台笔记本电脑和 5 部手机。到一天结束时,您共有多少台笔记本电脑和多少部手机?该问题已写在字符串 problem_to_solve 中,您将使用自洽提示词来求解。

已为您预先加载 OpenAI 包和 get_response() 函数。

本练习中的 get_response() 函数使用了 max_tokens 参数,以加快运行速度。

本练习是课程的一部分

使用 OpenAI API 的 Prompt Engineering

查看课程

练习说明

  • 创建 self_consistency_instruction,让模型由「三位专家」进行推理,并用「少数服从多数」的多数投票来合并结果。
  • 通过合并 self_consistency_instructionproblem_to_solve 来创建最终的 prompt

交互式实操练习

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

client = OpenAI(api_key="")

# Create the self_consistency instruction
self_consistency_instruction = ____

# Create the problem to solve
problem_to_solve = "If you own a store that sells laptops and mobile phones. You start your day with 50 devices in the store, out of which 60% are mobile phones. Throughout the day, three clients visited the store, each of them bought one mobile phone, and one of them bought additionally a laptop. Also, you added to your collection 10 laptops and 5 mobile phones. How many laptops and mobile phones do you have by the end of the day?"

# Create the final prompt
prompt = ____

response = get_response(prompt)
print(response)
编辑并运行代码