Compare commits
2 Commits
fastgpt-py
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
87e616ab55 | ||
|
|
34848dd6a0 |
56
docs/chat-stream-mode.md
Normal file
56
docs/chat-stream-mode.md
Normal 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
55
examples/nostream_chat.py
Normal 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())
|
||||||
@@ -1,6 +1,18 @@
|
|||||||
#!/usr/bin/env python3
|
#!/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
|
import asyncio
|
||||||
|
|||||||
4710
workflow/20251108/事故信息采集20251108.json
Normal file
4710
workflow/20251108/事故信息采集20251108.json
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user