add nostream chat example

This commit is contained in:
Xin Wang
2026-02-02 18:16:51 +08:00
parent 30306addee
commit 34848dd6a0

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())