Spaces:
Running

isold's picture
update
a1867e5
|
Raw History Blame Contribute Delete
7.56 kB

MeloonChatBI RESTful API 接口规范文档


1. 全局约定与服务基础

  • 基础路径:/api/v1
  • 协议格式:application/json; charset=utf-8
  • 本地服务地址:
    • 前端开发代理:http://localhost:3002(请求自动代理到后端)
    • 后端网关:http://127.0.0.1:8000
  • 认证方式:
    • 请求头携带 Authorization: Bearer <access_token>。
  • 敏感安全原则:
    • 所有返回的数据源对象中,数据库密码字段一律脱敏,不输出明文。

2. 用户认证与工作区偏好 (/api/v1/auth, /api/v1/profile)

2.1 注册账号

  • 端点:POST /api/v1/auth/register
  • 说明:在统一 users 表创建账号,同时在 chatbi_profile 记录默认工作区名称。
  • 请求体:
{
  "username": "analyst_david",
  "email": "david@company.com",
  "password": "SecurePassword123!",
  "default_workspace_name": "集团经营分析工作区"
}
  • **成功响应 (200 OK)**:
{
  "success": true,
  "user": {
    "id": "18f2d591-62bc-448e-9764-77a3d13264ec",
    "username": "analyst_david",
    "email": "david@company.com",
    "role": "student"
  },
  "access_token": "eyJhbGciOiJIUzI1NiIsIn...",
  "token_type": "bearer"
}

2.2 用户登录

  • 端点:POST /api/v1/auth/login
  • 请求体:
{
  "identifier": "david@company.com",
  "password": "SecurePassword123!"
}
  • **成功响应 (200 OK)**:返回用户基本资料与 JWT Token。

2.3 获取与修改工作区偏好

  • 获取偏好:GET /api/v1/profile
  • 响应:
{
  "user_id": "18f2d591-62bc-448e-9764-77a3d13264ec",
  "default_workspace_name": "集团经营分析工作区",
  "default_data_source_id": 1,
  "settings": {
    "default_chart_type": "bar",
    "theme": "dark"
  }
}
  • 修改偏好:PUT /api/v1/profile
{
  "default_workspace_name": "战略运营数据中心",
  "default_data_source_id": 2
}

3. 数据源管理接口 (/api/v1/datasources)

3.1 获取所有配置的数据源

  • 端点:GET /api/v1/datasources/
  • **成功响应 (200 OK)**:
[
  {
    "id": 1,
    "name": "生产订单库 (PostgreSQL)",
    "type": "postgres",
    "host": "pg-prod.internal.net",
    "port": 5432,
    "database_name": "order_db",
    "username": "readonly_user",
    "status": "active",
    "created_at": "2026-09-20T10:00:00Z"
  }
]

3.2 测试数据库连接可用性

  • 端点:POST /api/v1/datasources/test-connection
  • 说明:在未保存前探测数据库网络可达性与认证正确性。
  • 请求体:
{
  "type": "postgres",
  "host": "pg-prod.internal.net",
  "port": 5432,
  "database_name": "order_db",
  "username": "readonly_user",
  "password": "StrongPassword123!"
}
  • **成功响应 (200 OK)**:
{
  "success": true,
  "message": "数据库连接测试成功,连接延迟 12ms",
  "server_version": "PostgreSQL 16.2"
}

3.3 新增数据源连接

  • 端点:POST /api/v1/datasources/
  • 请求体:包含连接凭据字段,保存后返回包含 ID 的数据源摘要。

3.4 上传本地表格文件 (Excel / CSV)

  • 端点:POST /api/v1/datasources/upload
  • 请求格式:multipart/form-data
    • file: 选中的 .xlsx 或 .csv 文件
    • name: 数据源名称(如“2026Q1市场部投放报表”)
  • **成功响应 (200 OK)**:
{
  "id": 5,
  "name": "2026Q1市场部投放报表",
  "type": "excel",
  "status": "active",
  "tables_extracted": ["Sheet1"],
  "total_rows": 12500
}

4. 智能对话与问数核心接口 (/api/v1/conversations)

4.1 创建问数会话

  • 端点:POST /api/v1/conversations/
  • 请求体:
{
  "title": "2026年Q1各品类营收分析",
  "data_source_id": 1
}
  • **成功响应 (201 Created)**:
{
  "id": 101,
  "title": "2026年Q1各品类营收分析",
  "data_source_id": 1,
  "created_at": "2026-10-02T16:00:00Z"
}

4.2 向数据源发送自然语言提问 (Ask Question)

  • 端点:POST /api/v1/conversations/{id}/messages
  • 说明:核心问数接口,触发 LLM Text-to-SQL、执行查询、图表匹配与洞察总结。
  • 请求体:
{
  "content": "查询上个月各个产品类别的总销售额,并找出前 5 名",
  "data_source_id": 1
}
  • **成功响应 (200 OK)**:
{
  "id": 2001,
  "conversation_id": 101,
  "role": "assistant",
  "content": "已为您统计上月销售额前 5 名的产品类别。其中【消费电子】以 428.5 万元居首位,其次是【智能家居】与【个护健康】。",
  "generated_sql": "SELECT category_name, SUM(amount) AS total_sales FROM orders WHERE order_date >= '2026-09-01' AND order_date < '2026-10-01' GROUP BY category_name ORDER BY total_sales DESC LIMIT 5;",
  "execution_result": [
    { "category_name": "消费电子", "total_sales": 4285000 },
    { "category_name": "智能家居", "total_sales": 2890000 },
    { "category_name": "个护健康", "total_sales": 1750000 },
    { "category_name": "户外运动", "total_sales": 1320000 },
    { "category_name": "办公文具", "total_sales": 890000 }
  ],
  "chart_type": "bar",
  "chart_config": {
    "x_axis": "category_name",
    "y_axis": "total_sales",
    "title": "上月产品品类销售额 TOP 5"
  },
  "created_at": "2026-10-02T16:00:05Z"
}

4.3 获取指定会话的历史消息

  • 端点:GET /api/v1/conversations/{id}/messages

5. 实体知识图谱接口 (/api/v1/kg)

5.1 获取数据源实体拓扑图谱

  • 端点:GET /api/v1/kg/schema/{data_source_id}
  • 说明:返回适合前端力导向网络渲染的节点与连接边。
  • **成功响应 (200 OK)**:
{
  "nodes": [
    { "id": "orders", "name": "订单主表", "group": "fact", "columns_count": 12 },
    { "id": "customers", "name": "客户资料表", "group": "dimension", "columns_count": 8 },
    { "id": "products", "name": "商品目录表", "group": "dimension", "columns_count": 15 }
  ],
  "links": [
    { "source": "orders", "target": "customers", "relation": "placed_by", "foreign_key": "customer_id" },
    { "source": "orders", "target": "products", "relation": "contains_product", "foreign_key": "product_id" }
  ]
}

5.2 触发知识图谱增量同步

  • 端点:POST /api/v1/kg/sync/{data_source_id}

6. 自定义仪表盘接口 (/api/v1/dashboards)

6.1 获取用户仪表盘列表

  • 端点:GET /api/v1/dashboards/
  • **成功响应 (200 OK)**:
[
  {
    "id": 1,
    "title": "公司核心经营大盘",
    "description": "监控关键营收指标与各事业部开销",
    "cards_count": 6,
    "updated_at": "2026-10-02T12:00:00Z"
  }
]

6.2 保存图表卡片至仪表盘

  • 端点:POST /api/v1/dashboards/{id}/cards
  • 请求体:
{
  "title": "品类销售额 TOP 5",
  "chart_type": "bar",
  "generated_sql": "SELECT ...",
  "data_source_id": 1,
  "chart_config": { ... }
}

7. 元数据管理接口 (/api/v1/metadata)

7.1 读取数据源完整元数据

  • 端点:GET /api/v1/metadata/{data_source_id}

7.2 更新字段业务批注与同义词

  • 端点:PUT /api/v1/metadata/columns/{column_id}
  • 请求体:
{
  "business_name": "实付金额 (元)",
  "description": "扣减各类优惠券与积分抵扣后的实付总额",
  "synonyms": ["实收金额", "客付金额", "净销售额"]
}