開始使用免費開始

加入防護欄

你正在開發一個為造訪羅馬的旅客提供建議的聊天機器人。需求是將主題侷限在餐飲、景點、歷史,以及在市區可做的事。對於任何其他主題,聊天機器人都應道歉,並回覆「Apologies, but I am not allowed to discuss this topic.」。

本練習屬於課程

使用 OpenAI API 開發 AI 系統

檢視課程

練習說明

  • 以提供的 user_request 撰寫一則 user 訊息,並撰寫一則 system 訊息,指示模型先評估問題:若屬允許主題就提供回覆;否則回覆訊息:「Apologies, but I am not allowed to discuss this topic.」。
  • 印出回應。

動手互動練習

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

client = OpenAI(api_key="")

user_request = "Can you recommend a good restaurant in Berlin?"

# Write the system and user message
messages = ____

response = client.chat.completions.create(
    model="gpt-4o-mini", messages=messages
)

# Print the response
____
編輯並執行程式碼