System Prompt 与多轮对话
System Prompt
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(api_key="your-api-key", base_url="https://www.cheapertoken.work")
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
system="你是一个专业的Python编程助手,回答简洁准确。",
messages=[
{"role": "user", "content": "如何读取一个文件?"}
]
)
print(response.content[0].text)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://www.cheapertoken.work" });
const response = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
system: "你是一个专业的Python编程助手,回答简洁准确。",
messages: [{ role: "user", content: "如何读取一个文件?" }],
});
console.log(response.content[0].text);
curl https://www.cheapertoken.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"system": "你是一个专业的Python编程助手,回答简洁准确。",
"messages": [{"role": "user", "content": "如何读取一个文件?"}]
}'
多轮对话
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(api_key="your-api-key", base_url="https://www.cheapertoken.work")
messages = []
def chat(user_input):
messages.append({"role": "user", "content": user_input})
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
system="你是一个友好的助手。",
messages=messages
)
reply = response.content[0].text
messages.append({"role": "assistant", "content": reply})
return reply
print(chat("我叫小明"))
print(chat("你还记得我叫什么吗?"))
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://www.cheapertoken.work" });
const messages = [];
async function chat(userInput) {
messages.push({ role: "user", content: userInput });
const response = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
system: "你是一个友好的助手。",
messages,
});
const reply = response.content[0].text;
messages.push({ role: "assistant", content: reply });
return reply;
}
console.log(await chat("我叫小明"));
console.log(await chat("你还记得我叫什么吗?"));
# 第一轮
curl https://www.cheapertoken.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"system": "你是一个友好的助手。",
"messages": [{"role": "user", "content": "我叫小明"}]
}'
# 第二轮(携带历史)
curl https://www.cheapertoken.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"system": "你是一个友好的助手。",
"messages": [
{"role": "user", "content": "我叫小明"},
{"role": "assistant", "content": "你好,小明!很高兴认识你。"},
{"role": "user", "content": "你还记得我叫什么吗?"}
]
}'
多模态内容块
system 可以是字符串或内容块数组:
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(api_key="your-api-key", base_url="https://www.cheapertoken.work")
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
system=[
{"type": "text", "text": "你是代码助手。"},
{"type": "text", "text": "始终提供可运行的代码示例。"}
],
messages=[{"role": "user", "content": "写一个快速排序"}]
)
print(response.content[0].text)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://www.cheapertoken.work" });
const response = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
system: [
{ type: "text", text: "你是代码助手。" },
{ type: "text", text: "始终提供可运行的代码示例。" },
],
messages: [{ role: "user", content: "写一个快速排序" }],
});
console.log(response.content[0].text);
curl https://www.cheapertoken.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"system": [
{"type": "text", "text": "你是代码助手。"},
{"type": "text", "text": "始终提供可运行的代码示例。"}
],
"messages": [{"role": "user", "content": "写一个快速排序"}]
}'