跳转到内容

API 概述

用于与 Potato 进行编程集成的 HTTP API 参考文档。

Potato 提供 HTTP API 端点,用于与自定义前端、自动化脚本或外部系统进行集成。

概述

基础 URL

text
http://localhost:8000

认证

大多数端点需要通过登录端点建立活跃会话,并通过 cookie 维持。

对于编程访问:

  1. 使用浏览器中的会话 cookie
  2. 使用调试模式并自动创建用户
  3. 以编程方式实现登录流程

响应格式

所有 API 端点返回 JSON 响应。错误响应包含一个 error 字段。

会话管理

登录

http
POST /auth
Content-Type: application/x-www-form-urlencoded
 
username=your_username&password=your_password

检查会话

http
GET /api/current_instance

响应:

json
{
  "instance_id": "item_001",
  "current_index": 0,
  "total_instances": 100
}

注销

http
POST /logout

标注端点

获取当前实例

http
GET /api/current_instance

返回用户当前标注实例的信息。

获取实例内容

http
GET /api/spans/{instance_id}

返回文本内容和现有的片段标注。

响应:

json
{
  "instance_id": "item_001",
  "text": "The quick brown fox jumps over the lazy dog.",
  "spans": [
    {
      "id": "span_123",
      "schema": "entities",
      "label": "ANIMAL",
      "start": 16,
      "end": 19,
      "text": "fox",
      "color": "#ff6b6b"
    }
  ]
}

获取标注

http
GET /get_annotations?instance_id={instance_id}

返回特定实例的所有标注。

提交标注

http
POST /updateinstance
Content-Type: application/json
 
{
  "instance_id": "item_001",
  "annotations": {
    "sentiment:positive": "1"
  },
  "span_annotations": [
    {
      "schema": "entities",
      "name": "PERSON",
      "start": 0,
      "end": 5,
      "value": "true"
    }
  ]
}

响应(成功):

json
{
  "status": "success",
  "processing_time_ms": 15
}

响应(含质量控制):

json
{
  "status": "success",
  "qc_result": {
    "type": "gold_standard",
    "correct": false,
    "gold_label": {"sentiment": "positive"}
  }
}

跳转到实例

http
POST /go_to
Content-Type: application/json
 
{"go_to": "item_005"}

或通过操作:

json
{"action": "next_instance"}

模式信息

获取标注模式

http
GET /api/schemas

获取标签颜色

http
GET /api/colors

获取关键词高亮

http
GET /api/keyword_highlights/{instance_id}

管理员 API 端点

管理员端点需要管理员权限。

健康检查

http
GET /admin/health

仪表板概览

http
GET /admin/api/overview

响应:

json
{
  "total_items": 1000,
  "total_annotations": 5000,
  "total_users": 25,
  "completion_rate": 0.45
}

获取标注者

http
GET /admin/api/annotators

获取实例

http
GET /admin/api/instances?status=completed&limit=100

获取配置

http
GET /admin/api/config

更新配置

http
POST /admin/api/config
Content-Type: application/json
 
{"setting_name": "value"}

质量控制 API

质量控制指标

http
GET /admin/api/quality_control

返回注意力检查和金标准统计数据。

一致性指标

http
GET /admin/api/agreement

返回按模式分类的 Krippendorff's alpha 值。

AI 助手 API

获取 AI 建议

http
GET /get_ai_suggestion?instance_id={instance_id}

获取 AI 助手帮助

http
GET /api/ai_assistant?instance_id={instance_id}&schema={schema_name}

模型训练 API

v2.14.0 新增

设置 model_training.enabled: true 后,Potato 会注册 26 个训练操作,OpenAPI 规范列出了每一个。管理员路由位于 /admin/training 之下,需要管理员权限,可以来自管理员会话,也可以来自 X-API-Key 请求头。浏览器发出的 POST 和 DELETE 请求必须来自同一来源。

路由用途
GET /admin/training训练页面
GET /admin/training/api/trainers每个训练器,以及该训练器处理的方案类型和媒体、许可证和是否已安装
GET /admin/training/api/schemas研究的方案,以及适合每个方案的训练器
GET /admin/training/api/status当前或最近一次运行,包括进度、指标、警告、回写报告和队列
GET /admin/training/api/runs运行历史(?limit=,默认 50)
POST /admin/training/api/runs开始一次运行
POST /admin/training/api/runs/<run_id>/cancel取消正在进行的运行或排队等待的运行
DELETE /admin/training/api/runs/<run_id>/predictions撤回一次运行的预标签

用一个训练器和一组方案开始一次运行:

bash
curl -X POST -H "X-API-Key: $KEY" -H "Content-Type: application/json" \
  http://localhost:8000/admin/training/api/runs \
  -d '{"trainer": "sklearn-text", "schemas": ["sentiment"]}'

运行开始时响应为 200,运行排在另一次运行之后等待时响应为 202 并带 queued: true。被拒绝的启动请求返回 400、409 或 429,原因写在 error 中。远程训练代理使用 /api/training 之下的另一组路由,用 Authorization: Bearer <token> 认证。模型训练配置列出了每条路由的请求体和状态码。

示例:完整标注流程

python
import requests
 
BASE_URL = "http://localhost:8000"
session = requests.Session()
 
# 1. Login
session.post(f"{BASE_URL}/auth", data={
    "username": "annotator1",
    "password": "password123"
})
 
# 2. Get current instance
response = session.get(f"{BASE_URL}/api/current_instance")
instance_id = response.json()["instance_id"]
 
# 3. Get instance content
response = session.get(f"{BASE_URL}/api/spans/{instance_id}")
print(f"Text: {response.json()['text']}")
 
# 4. Submit annotation
session.post(f"{BASE_URL}/updateinstance", json={
    "instance_id": instance_id,
    "annotations": {"sentiment:positive": "1"}
})
 
# 5. Navigate to next instance
session.post(f"{BASE_URL}/go_to", json={"action": "next_instance"})

示例:管理员监控

python
import requests
 
BASE_URL = "http://localhost:8000"
 
# Get overview
overview = requests.get(f"{BASE_URL}/admin/api/overview").json()
print(f"Progress: {overview['completion_rate']*100:.1f}%")
 
# Get agreement metrics
agreement = requests.get(f"{BASE_URL}/admin/api/agreement").json()
print(f"Agreement: alpha = {agreement['overall']['average_krippendorff_alpha']:.3f}")

错误响应

所有端点可能返回错误响应:

json
{"error": "Description of the error"}

常见 HTTP 状态码:

  • 400 - 请求错误(无效参数)
  • 401 - 未授权(无会话)
  • 403 - 禁止访问(权限不足)
  • 404 - 未找到
  • 500 - 内部服务器错误

延伸阅读

有关实现细节,请参阅源代码文档。