6. 接口与体验
6.1 开放API
API设计
智能客服系统提供RESTful API,支持多种客户端接入:
API架构
基础URL:
https://api.example.com/v1/customer-service
核心接口:
1. 创建对话(Create Chat)
POST /chats
Content-Type: application/json
{
"user_id": "user_123",
"channel": "web",
"metadata": {
"ip": "192.168.1.1",
"user_agent": "Mozilla/5.0..."
}
}
Response:
{
"chat_id": "chat_456",
"status": "active",
"created_at": "2024-01-01T10:00:00Z"
}
2. 发送消息(Send Message)
POST /chats/{chat_id}/messages
Content-Type: application/json
{
"content": "这个产品有什么功能?",
"type": "text"
}
Response:
{
"message_id": "msg_789",
"content": "这个产品具有以下功能:1. 功能A 2. 功能B 3. 功能C",
"type": "text",
"timestamp": "2024-01-01T10:00:05Z",
"confidence": 0.95
}
3. 获取对话历史(Get Chat History)
GET /chats/{chat_id}/messages?limit=50&offset=0
Response:
{
"messages": [
{
"message_id": "msg_001",
"role": "user",
"content": "问题1",
"timestamp": "2024-01-01T10:00:00Z"
},
{
"message_id": "msg_002",
"role": "assistant",
"content": "回答1",
"timestamp": "2024-01-01T10:00:05Z"
}
],
"total": 100,
"limit": 50,
"offset": 0
}
4. 评价对话(Rate Chat)
POST /chats/{chat_id}/rating
Content-Type: application/json
{
"rating": 5,
"comment": "回答很准确,服务很好"
}
Response:
{
"status": "success",
"message": "评价已提交"
}
API设计原则
RESTful设计:
- 使用标准HTTP方法(GET、POST、PUT、DELETE)
- 资源导向的URL设计
- 状态码正确使用(200、201、400、404、500等)
版本管理:
- URL中包含版本号(/v1/、/v2/)
- 向后兼容,旧版本至少支持6个月
- 版本变更文档化
错误处理:
{
"error": {
"code": "INVALID_PARAMETER",
"message": "参数user_id不能为空",
"details": {
"field": "user_id",
"reason": "required"
}
}
}