mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-23 03:19:41 +00:00
调整为code判断
This commit is contained in:
parent
7d9f47b0d1
commit
6589f7f481
2 changed files with 21 additions and 10 deletions
|
|
@ -166,13 +166,21 @@ async def _search_outcrawl(api_key: str, query: str, max_results: int) -> str:
|
|||
logger.bind(tag=TAG).debug(f"Outcrawl搜索响应 | status: {response.status_code}")
|
||||
|
||||
if response.status_code != 200:
|
||||
# 错误信封为 {"code": "...", "message": "...", "details": {}},
|
||||
# code 是官方承诺的封闭枚举
|
||||
code = data.get("code", "") if isinstance(data, dict) else ""
|
||||
detail = data.get("message", "") if isinstance(data, dict) else ""
|
||||
logger.bind(tag=TAG).error(
|
||||
f"Outcrawl搜索失败 | status: {response.status_code} | detail: {detail}"
|
||||
f"Outcrawl搜索失败 | status: {response.status_code} | code: {code} | detail: {detail}"
|
||||
)
|
||||
if response.status_code == 402:
|
||||
return "联网搜索额度已用完,请检查Outcrawl账户余额。"
|
||||
return "联网搜索请求失败,请检查API Key是否正确。"
|
||||
if code == "quota_exceeded":
|
||||
# 该code覆盖两种情况:每分钟请求数限流,或月度credits用尽
|
||||
return "联网搜索被限流或额度已用完,请稍后重试或检查Outcrawl账户余额。"
|
||||
if code == "unauthorized":
|
||||
return "联网搜索请求失败,请检查API Key是否正确。"
|
||||
# 其余code(bad_request、no_capacity、search_refused、internal等)
|
||||
# 多为输入问题或服务端暂时性故障,统一引导重试
|
||||
return "联网搜索请求失败,请稍后重试。"
|
||||
|
||||
hits = data.get("hits", [])
|
||||
if not hits:
|
||||
|
|
|
|||
|
|
@ -95,15 +95,18 @@ async def test_outcrawl_empty_hits(fake_http):
|
|||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("status_code", "message"),
|
||||
("code", "message"),
|
||||
[
|
||||
(401, "联网搜索请求失败,请检查API Key是否正确。"),
|
||||
(402, "联网搜索额度已用完,请检查Outcrawl账户余额。"),
|
||||
(429, "联网搜索请求失败,请检查API Key是否正确。"),
|
||||
("unauthorized", "联网搜索请求失败,请检查API Key是否正确。"),
|
||||
("quota_exceeded", "联网搜索被限流或额度已用完,请稍后重试或检查Outcrawl账户余额。"),
|
||||
("bad_request", "联网搜索请求失败,请稍后重试。"),
|
||||
("internal", "联网搜索请求失败,请稍后重试。"),
|
||||
],
|
||||
)
|
||||
async def test_outcrawl_error_status(fake_http, status_code, message):
|
||||
fake_http.response = FakeResponse(status_code=status_code, payload={"message": "denied"})
|
||||
async def test_outcrawl_error_code(fake_http, code, message):
|
||||
fake_http.response = FakeResponse(
|
||||
status_code=429, payload={"code": code, "message": "denied", "details": {}}
|
||||
)
|
||||
|
||||
assert await web_search_module._search_outcrawl("oc-key", "q", 3) == message
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue