API 概述
用于与 Potato 进行编程集成的 HTTP API 参考文档。
API 参考文档
Potato 提供 HTTP API 端点,用于与自定义前端、自动化脚本或外部系统进行集成。
概述
基础 URL
text
http://localhost:8000
认证
大多数端点需要通过登录端点建立活跃会话,并通过 cookie 维持。
对于编程访问:
- 使用浏览器中的会话 cookie
- 使用调试模式并自动创建用户
- 以编程方式实现登录流程
响应格式
所有 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}示例:完整标注流程
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- 内部服务器错误
延伸阅读
有关实现细节,请参阅源代码文档。