文章 API
文章 API 提供文章生成、编辑、删除等功能。
生成文章
接口地址
POST /api/articles/generate请求头
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| title | string | 是 | 文章标题 |
| requirement | string | 是 | 创作要求 |
| model | string | 否 | AI 模型:deepseek、kimi、moonshot(默认:deepseek) |
| style | string | 否 | 写作风格:professional、casual、humor(默认:professional) |
| length | string | 否 | 内容长度:short、medium、long(默认:medium) |
| reference | string | 否 | 参考内容(文本或文件 URL) |
| temperature | number | 否 | 温度:0.0-1.0(默认:0.7) |
| max_length | number | 否 | 最大长度:Token 数量(默认:2000) |
请求示例
json
{
"title": "AI 技术发展趋势",
"requirement": "重点介绍大语言模型的应用,字数 1500 字左右",
"model": "deepseek",
"style": "professional",
"length": "medium",
"temperature": 0.7
}响应示例
json
{
"code": 200,
"msg": "生成成功",
"data": {
"article_id": "abc123",
"title": "AI 技术发展趋势",
"content": "大语言模型(LLM)是当前 AI 领域最热门的技术...",
"tokens_used": 1250,
"created_at": "2026-02-09T10:30:00Z"
}
}优化文章
接口地址
POST /api/articles/optimize请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| article_id | string | 是 | 文章 ID |
| requirement | string | 是 | 优化要求 |
请求示例
json
{
"article_id": "abc123",
"requirement": "第三段太长,请精简到 200 字以内"
}响应示例
json
{
"code": 200,
"msg": "优化成功",
"data": {
"article_id": "abc123",
"optimized_content": "大语言模型(LLM)是当前 AI 领域最热门的技术...",
"tokens_used": 150,
"optimized_at": "2026-02-09T10:35:00Z"
}
}获取文章列表
接口地址
GET /api/articles查询参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| page | number | 否 | 页码(默认:1) |
| page_size | number | 否 | 每页数量(默认:20) |
| status | string | 否 | 状态:draft、published(默认:all) |
| keyword | string | 否 | 搜索关键词 |
响应示例
json
{
"code": 200,
"msg": "获取成功",
"data": {
"total": 100,
"page": 1,
"page_size": 20,
"articles": [
{
"article_id": "abc123",
"title": "AI 技术发展趋势",
"status": "draft",
"created_at": "2026-02-09T10:30:00Z",
"updated_at": "2026-02-09T10:35:00Z"
}
]
}
}获取文章详情
接口地址
GET /api/articles/:article_id响应示例
json
{
"code": 200,
"msg": "获取成功",
"data": {
"article_id": "abc123",
"title": "AI 技术发展趋势",
"content": "大语言模型(LLM)是当前 AI 领域最热门的技术...",
"style": "professional",
"model": "deepseek",
"tokens_used": 1250,
"created_at": "2026-02-09T10:30:00Z",
"updated_at": "2026-02-09T10:35:00Z"
}
}删除文章
接口地址
DELETE /api/articles/:article_id响应示例
json
{
"code": 200,
"msg": "删除成功",
"data": null
}错误码
| 错误码 | 说明 |
|---|---|
| 2001 | 文章不存在 |
| 2002 | 文章已被删除 |
| 2003 | Token 余额不足 |
| 2004 | AI 模型不可用 |
| 2005 | 生成超时 |
最佳实践
流式生成
使用流式生成提高用户体验:
javascript
const response = await fetch('/api/articles/generate', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(params)
})
const reader = response.body.getReader()
const decoder = new TextDecoder()
while (true) {
const { done, value } = await reader.read()
if (done) break
const text = decoder.decode(value)
console.log(text) // 实时输出
}错误重试
实现指数退避重试:
javascript
async function generateWithRetry(params, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
return await generateArticle(params)
} catch (error) {
if (i === maxRetries - 1) throw error
await sleep(Math.pow(2, i) * 1000) // 指数退避
}
}
}成本优化
降低 Token 消耗:
- 精简要求: 避免冗余描述
- 合理设置长度: 不要设置过长
- 多轮优化: 逐步完善,避免一次性生成
- 选择合适模型: 根据需求选择性价比高的模型
