添加防护栏
系统消息最常见的用途之一是添加防护栏,用于对模型输出施加限制。
在本练习中,您将对模型输出加上限制,禁止生成与语言无关的学习计划,因为您的系统正开始在这一领域聚焦。您还将为请求此类学习计划的用户设计一条自定义消息,以便他们理解这一变更。
本练习是课程的一部分
在 Python 中使用 DeepSeek
练习说明
- 完成聊天请求,将系统消息放入
sys_msg,并用包含与语言无关技能(例如轮滑)的用户消息进行测试。
交互式实操练习
通过完成这段示例代码来试试这个练习。
client = OpenAI(api_key="", base_url="https://api.together.xyz/v1")
sys_msg = """You are a study planning assistant that creates plans for learning new skills, returning only the structured plan.
If these skills are non related to languages, return only the message:
'Apologies, to focus on languages, we no longer create learning plans on other topics.'
"""
# Create a request to the chat model
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V4-Pro",
messages=[
{"role": "____", "content": ____},
{"role": "user", "content": "Help me learn to ____."}
]
)
print(response.choices[0].message.content)