開始使用免費開始

用範例對話提供脈絡

假設有一家名為 MyPersonalDelivery 的外送服務,能為各式物品提供多元的配送選項。你想建立一個客服聊天機器人,協助顧客處理各種需求。為了達成這個目標,你會提供來自先前對話的 context_questioncontext_answer,內容關於該公司可配送的物品,接著你會用新的使用者提示來測試模型是否能辨識這段脈絡。

OpenAI 套件、context_questioncontext_answer 字串都已為你預先載入。

本練習屬於課程

使用 OpenAI API 的提示工程

檢視課程

練習說明

  • 定義一個 system_prompt,說明聊天機器人的目的,並引導它以溫和的方式回答問題。
  • 使用 system_promptcontext_questioncontext_answer,設計一段對話,讓聊天機器人能以此作為脈絡來回應新的使用者提問。

動手互動練習

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

client = OpenAI(api_key="")

# Define the system prompt
system_prompt = "____"

context_question = "What types of items can be delivered using MyPersonalDelivery?"
context_answer = "We deliver everything from everyday essentials such as groceries, medications, and documents to larger items like electronics, clothing, and furniture. However, please note that we currently do not offer delivery for hazardous materials or extremely fragile items requiring special handling."

# Add the context to the model
response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "____", "content": ____},
            {"role": "____", "content": ____},
            {"role": "____", "content": ____ },
            {"role": "user", "content": "Do you deliver furniture?"}])
response = response.choices[0].message.content
print(response)
編輯並執行程式碼