文本生成
Claude 适合生成、总结、改写、分类、抽取和多轮问答。接入时统一使用 Messages API:
text
https://xikapi.com/v1/messages基础文本生成
bash
curl https://xikapi.com/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: $XIKAPI_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 800,
"messages": [
{
"role": "user",
"content": "Write a product description in fewer than 100 words"
}
]
}'长文本总结
长文本总结适合合同、会议纪要、报告、客服记录和知识库文档。
json
{
"model": "claude-sonnet-4-5",
"max_tokens": 1200,
"system": "You are a document summarization assistant. Preserve key facts, amounts, dates, responsible parties, and risks.",
"messages": [
{
"role": "user",
"content": "Summarize the following contract content: ..."
}
]
}改写与润色
改写任务建议明确目标语气、长度和保留信息。
text
Rewrite the following support reply to be more polite and clear. Preserve the refund rules and processing timeline, and do not promise anything not stated.分类与抽取
分类和抽取适合客服工单、反馈归因、内容审核、订单备注识别等场景。
json
{
"model": "claude-sonnet-4-5",
"max_tokens": 500,
"temperature": 0,
"messages": [
{
"role": "user",
"content": "Classify the text as complaint, inquiry, or praise. Text: I paid, but the order has not arrived."
}
]
}多轮问答
多轮问答需要服务端维护必要历史。历史越长,成本越高,也越容易引入无关上下文。
json
{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"messages": [
{ "role": "user", "content": "I want to build a document Q&A system" },
{ "role": "assistant", "content": "Start with document chunking and vector retrieval." },
{ "role": "user", "content": "What should I consider when chunking?" }
]
}控制输出长度
可以同时用 max_tokens 和提示词约束输出长度。
text
Answer in no more than five bullet points, each under 30 words.生产环境建议:
- 短回复使用较小
max_tokens - 总结和分析任务给足输出空间
- 对必须短输出的场景增加服务端截断或校验
控制语气和格式
把稳定要求放在 system 中,把本轮任务放在 messages 中。
json
{
"system": "You are an enterprise support assistant. Be polite and concise, and do not invent policies.",
"messages": [
{
"role": "user",
"content": "The user says a top-up has not arrived. Draft a support reply."
}
]
}常见 Prompt 模板
文章生成:
text
Write an article about the topic with these requirements:
1. Start with a title
2. Use three sections
3. Keep the tone professional but not exaggerated
4. Do not use unverified data合同总结:
text
Summarize the contract using this structure:
- Parties
- Scope of services
- Payment terms
- Breach liability
- Risks requiring manual review字段抽取:
text
Extract name, phone number, order ID, and issue type from the text. Use null for unknown fields.最佳实践
- 把稳定角色和边界放入
system - 对分类、抽取等任务降低
temperature - 输出 JSON 时配合服务端解析和校验
- 长文档先切分,再总结或检索
- 不要把 API Key 暴露在浏览器前端