加入防護欄
系統訊息最常見的用途之一是加入防護欄(guardrails),也就是對模型輸出加上限制。
在這個練習中,你要對模型輸出設下限制,避免產生與語言無關的學習計畫,因為你的系統正聚焦在該領域。你也會為請求此類學習計畫的使用者設計一段自訂訊息,讓他們理解這項變更。
本練習屬於課程
Working with the OpenAI API
練習說明
- 完成聊天請求,提供
sys_msg中的系統訊息,並以包含與語言無關技能(例如溜直排輪)的使用者訊息進行測試。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
client = OpenAI(api_key="")
sys_msg = """You are a study planning assistant that creates plans for learning new skills.
If these skills are non related to languages, return the message:
'Apologies, to focus on languages, we no longer create learning plans on other topics.'
"""
# Create a request to the Chat Completions endpoint
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "____", "content": ____},
{"role": "user", "content": "Help me learn to ____."}
]
)
print(response.choices[0].message.content)