添加护栏
系统消息最常见的用途之一是添加护栏,也就是对模型输出施加限制。
在本练习中,您将对模型输出加上限制,阻止生成与语言无关的学习计划,因为您的系统正在该领域形成定位。对于请求此类学习计划的用户,您将设计一条自定义消息,让他们理解这一变更。
本练习是课程的一部分
使用 OpenAI API
练习说明
- 完成对话请求,将系统消息提供为
sys_msg,并用包含与语言无关技能的用户消息进行测试,例如 rollerskating。
交互式实操练习
通过完成这段示例代码来试试这个练习。
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)