feat: 上游错误日志 + ModelArts错误→OpenAI格式转换
- 非200响应记录WARNING日志(含响应体前500字节) - 华为云ModelArts错误格式(error_code/error_msg)转为OpenAI标准格式 - 解决客户端'压缩上下文'报错时错误格式不兼容问题 - 根因: prompt超196608 tokens触发ModelArts.81001
This commit is contained in:
@@ -337,6 +337,14 @@ def proxy(subpath):
|
|||||||
if k.lower() not in skip_headers:
|
if k.lower() not in skip_headers:
|
||||||
response_headers.append((k, v))
|
response_headers.append((k, v))
|
||||||
|
|
||||||
|
# 记录上游非200响应
|
||||||
|
if resp.status_code != 200:
|
||||||
|
try:
|
||||||
|
_err_peek = resp.content[:500]
|
||||||
|
logger.warning(f"上游返回 {resp.status_code}: {_err_peek.decode('utf-8', errors='replace')}")
|
||||||
|
except Exception:
|
||||||
|
logger.warning(f"上游返回 {resp.status_code}")
|
||||||
|
|
||||||
# SSE 流式转发
|
# SSE 流式转发
|
||||||
content_type = resp.headers.get('Content-Type', '')
|
content_type = resp.headers.get('Content-Type', '')
|
||||||
if 'text/event-stream' in content_type or resp.headers.get('Transfer-Encoding', '') == 'chunked':
|
if 'text/event-stream' in content_type or resp.headers.get('Transfer-Encoding', '') == 'chunked':
|
||||||
@@ -358,6 +366,26 @@ def proxy(subpath):
|
|||||||
# 非流式响应:读取完整内容
|
# 非流式响应:读取完整内容
|
||||||
content = resp.content
|
content = resp.content
|
||||||
resp.close()
|
resp.close()
|
||||||
|
|
||||||
|
# 华为云 ModelArts 错误 → OpenAI 标准格式
|
||||||
|
if resp.status_code >= 400:
|
||||||
|
try:
|
||||||
|
import json as _jj
|
||||||
|
err = _jj.loads(content)
|
||||||
|
if 'error_code' in err and 'error_msg' in err:
|
||||||
|
openai_err = {
|
||||||
|
"error": {
|
||||||
|
"message": err.get('error_msg', ''),
|
||||||
|
"type": err.get('error', {}).get('type', 'server_error'),
|
||||||
|
"code": err.get('error_code', ''),
|
||||||
|
"param": None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
content = _jj.dumps(openai_err).encode('utf-8')
|
||||||
|
response_headers = [(k, v) for k, v in response_headers if k.lower() != 'content-length']
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
return Response(
|
return Response(
|
||||||
content,
|
content,
|
||||||
status=resp.status_code,
|
status=resp.status_code,
|
||||||
|
|||||||
Reference in New Issue
Block a user