系统提示词
本课属于 Coddy AI Engineering 学习旅程的“在代码中调用模型”单元。第 3 课,共 22 课。
系统提示词用于设定模型在整个对话中的身份及其遵循的规则。它并不是 messages 中的一轮对话:它位于独立的 system 参数中,模型赋予它的权重高于用户输入的任何内容。
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=100,
system="You are Rex, a museum guide. Answer in one word.",
messages=[{"role": "user", "content": "What is the capital of Spain?"}],
)
print(response.content[0].text)这会打印 Madrid。
产品使用系统提示主要用于三件事:人设(名称和语气)、规则(长度、格式、拒绝回答的内容)以及模型需要的事实(例如营业时间)。用户无法修改它,因为你的代码会在每次调用时写入它。替代模型遵循 coddy_ai.py 顶部列出的规则;而真实模型则会遵循用任何词语编写的指令。
挑战
简单通过系统提示(system prompt)给咖啡馆的助手设置名称、已知事实和结束语。
- 初始代码会读取助手的名称和一个问题。
CAFE_FACTS保存了已知事实。 - 构建系统提示:
You are <name>, the assistant of Green Leaf Cafe.、一个空格、CAFE_FACTS、一个空格以及End every reply with "Have a green day!" - 发送该问题作为唯一的消息,并附带该系统提示、
model="claude-opus-5-5"和max_tokens=200。 - 打印
Answer:和回复内容。
例如,当输入为 Mia 和 Who are you? 时,程序打印:
Answer: I am Mia. Have a green day!亲手试试
import coddy_ai as anthropic # 生产环境中:import anthropic
client = anthropic.Anthropic()
CAFE_FACTS = "The cafe opens at 8 am and closes at 4 pm. Oat milk costs 50 cents extra. Every pastry is baked here each morning."
name = input()
question = input()
# 构建 system prompt,将其与问题一起发送,并输出 "Answer: " 和回复
这节课包含一个小测验。开始学习即可作答,并记录你的进度。
在代码中调用模型中的所有课
自己练习: Python 在线编译器