Compare commits

2 Commits

Author SHA1 Message Date
Xin Wang
87e616ab55 提示词添加车辆预先提取和手机号多次拼接输入 2026-02-03 17:33:45 +08:00
Xin Wang
34848dd6a0 add nostream chat example 2026-02-02 18:16:51 +08:00
8 changed files with 4834 additions and 1 deletions

56
docs/chat-stream-mode.md Normal file
View File

@@ -0,0 +1,56 @@
# /chat 流式响应模式说明
## 接口地址
```
POST http://localhost:8000/chat?stream=true
```
## 请求参数
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| sessionId | string | 是 | 会话 ID |
| timeStamp | string | 是 | 时间戳 |
| text | string | 是 | 用户输入文本 |
| stream | bool | 否 | 设为 true 启用流式响应 |
## SSE 事件类型
| 事件类型 | 说明 | 数据格式 |
|----------|------|----------|
| `stage_code` | 阶段状态码 | `{"nextStageCode": "0000", "nextStage": "结束通话"}` |
| `text_delta` | 流式文本片段 | `{"text": "您好..."}` |
| `done` | 流式结束 | `{"status": "completed"}` |
| `error` | 错误信息 | `{"msg": "错误描述", "code": "500"}` |
## 状态码映射
| 状态码 | 含义 |
|--------|------|
| 0000 | 结束通话 |
| 0001 | 转接人工 |
| 0002 | 语义无法识别转接人工 |
| 0003 | 有人伤转接人工 |
| 1001 | 未准备好通话 |
| 1002 | 通话中 |
| 2000 | 进入单车拍照 |
| ... | ... |
## 示例
### 请求
```bash
python examples/stream_chat.py session-001 "发生了交通事故"
```
### 响应
```
Status: 200
--------------------------------------------------
[stage_code] {"nextStageCode": "1002", "nextStage": "通话中"}
[text_delta] {"text": "您好,请问发生了什么情况?"}
[done] {"status": "completed"}
```

55
examples/nostream_chat.py Normal file
View File

@@ -0,0 +1,55 @@
#!/usr/bin/env python3
"""
Simple CLI script to interact with /chat endpoint in non-stream mode.
"""
import asyncio
import aiohttp
import json
import sys
from datetime import datetime
API_BASE_URL = "http://localhost:8000"
async def chat(session_id: str, text: str):
"""Send a non-streaming chat request."""
timestamp = datetime.now().isoformat()
payload = {
"sessionId": session_id,
"timeStamp": timestamp,
"text": text
}
async with aiohttp.ClientSession() as http_session:
async with http_session.post(
f"{API_BASE_URL}/chat",
json=payload,
) as response:
data = await response.json()
print(f"Status: {response.status}")
print("-" * 50)
print(json.dumps(data, indent=2, ensure_ascii=False))
async def main():
if len(sys.argv) < 3:
print("Usage: python nostream_chat.py <session_id> <message>")
print("Example: python nostream_chat.py test-session-123 '发生了交通事故'")
sys.exit(1)
session_id = sys.argv[1]
text = " ".join(sys.argv[2:])
print(f"Session ID: {session_id}")
print(f"Message: {text}")
print("-" * 50)
await chat(session_id, text)
if __name__ == "__main__":
asyncio.run(main())

View File

@@ -1,6 +1,18 @@
#!/usr/bin/env python3
"""
Simple CLI script to interact with /chat endpoint in stream mode.
Stream Chat CLI - 与 /chat 端点进行流式交互的脚本。
用法:
python stream_chat.py <session_id> <消息>
示例:
python stream_chat.py test-001 "发生了交通事故"
输出说明:
- [stage_code]: 阶段状态码,如 {"nextStageCode": "0000", "nextStage": "结束通话"}
- [text_delta]: 流式文本片段
- [done]: 流式结束
- [error]: 错误信息
"""
import asyncio

File diff suppressed because one or more lines are too long