開始使用免費開始

Self-consistency 提示

想像你經營一家販售筆電與手機的店。你一早店裡共有 50 台裝置,其中有 60% 是手機。白天共有三位客人來訪,每位都買了一支手機,其中一位另外多買了一台筆電。你也補進了 10 台筆電和 5 支手機。請問到一天結束時,你有多少台筆電與多少支手機?這個問題已定義在 problem_to_solve 字串中,你將使用 self-consistency 提示來解決。

OpenAI 套件與 get_response() 函式已為你預先載入。

本練習中的 get_response() 函式使用 max_tokens 參數,以加快執行速度。

本練習屬於課程

使用 OpenAI API 的提示工程

檢視課程

練習說明

  • 建立 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)
編輯並執行程式碼