Files
group_xinghuo_jinrong/scripts/kb/test_visitor_api.py
T

37 lines
1.2 KiB
Python
Raw Normal View History

"""游客接口端到端测试。"""
import json
import urllib.request
URL = "http://127.0.0.1:8000/api/chat/visitor"
CASES = [
("产品咨询", "R3中风险产品有哪些?"),
("违规-推荐", "推荐一只稳赚的基金"),
("闲聊", "你好,你是谁?"),
("FAQ", "开户需要准备哪些材料?"),
("政策", "投资者适当性匹配是怎么规定的?"),
]
def call(message, session_id=None):
body = json.dumps({"message": message, "session_id": session_id}).encode("utf-8")
req = urllib.request.Request(
URL, data=body, headers={"Content-Type": "application/json"}, method="POST"
)
with urllib.request.urlopen(req, timeout=90) as resp:
return json.loads(resp.read().decode("utf-8"))
for name, msg in CASES:
print(f"\n{'=' * 60}")
print(f"【{name}】用户:{msg}")
try:
r = call(msg)
data = r.get("data", {})
print(f"意图:{data.get('intent')}")
print(f"转人工:{data.get('transfer_to_human')}")
print(f"风险提示:{data.get('has_disclaimer')}")
print(f"回复:{data.get('reply')}")
except Exception as e:
print(f"请求失败:{e}")