Skip to content

文章 API ​

文章 API 提供文章生成、编辑、删除等功能。

生成文章 ​

接口地址 ​

POST /api/articles/generate

请求头 ​

Authorization: Bearer YOUR_API_KEY
Content-Type: application/json

请求参数 ​

参数类型必填说明
titlestring是文章标题
requirementstring是创作要求
modelstring否AI 模型:deepseek、kimi、moonshot(默认:deepseek)
stylestring否写作风格:professional、casual、humor(默认:professional)
lengthstring否内容长度:short、medium、long(默认:medium)
referencestring否参考内容(文本或文件 URL)
temperaturenumber否温度:0.0-1.0(默认:0.7)
max_lengthnumber否最大长度: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_idstring是文章 ID
requirementstring是优化要求

请求示例 ​

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

查询参数 ​

参数类型必填说明
pagenumber否页码(默认:1)
page_sizenumber否每页数量(默认:20)
statusstring否状态:draft、published(默认:all)
keywordstring否搜索关键词

响应示例 ​

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文章已被删除
2003Token 余额不足
2004AI 模型不可用
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 消耗:

  1. 精简要求: 避免冗余描述
  2. 合理设置长度: 不要设置过长
  3. 多轮优化: 逐步完善,避免一次性生成
  4. 选择合适模型: 根据需求选择性价比高的模型

基于 AI 技术驱动的内容创作平台