全面更换orjson
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import asyncio
|
||||
import json
|
||||
import orjson
|
||||
import io
|
||||
from typing import Callable, Any, Coroutine, Optional
|
||||
import aiohttp
|
||||
@@ -200,7 +200,7 @@ class AiohttpGeminiStreamParser:
|
||||
if chunk_text == "[DONE]":
|
||||
return
|
||||
|
||||
chunk_data = json.loads(chunk_text)
|
||||
chunk_data = orjson.loads(chunk_text)
|
||||
|
||||
# 解析候选项
|
||||
if "candidates" in chunk_data and chunk_data["candidates"]:
|
||||
@@ -231,7 +231,7 @@ class AiohttpGeminiStreamParser:
|
||||
usage.get("totalTokenCount", 0)
|
||||
)
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
except orjson.JSONDecodeError as e:
|
||||
logger.warning(f"解析流式数据块失败: {e}, 数据: {chunk_text}")
|
||||
except Exception as e:
|
||||
logger.error(f"处理流式数据块时出错: {e}")
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import asyncio
|
||||
import io
|
||||
import json
|
||||
import orjson
|
||||
import re
|
||||
import base64
|
||||
from collections.abc import Iterable
|
||||
@@ -218,13 +218,13 @@ def _build_stream_api_resp(
|
||||
raw_arg_data = arguments_buffer.getvalue()
|
||||
arguments_buffer.close()
|
||||
try:
|
||||
arguments = json.loads(repair_json(raw_arg_data))
|
||||
arguments = orjson.loads(repair_json(raw_arg_data))
|
||||
if not isinstance(arguments, dict):
|
||||
raise RespParseException(
|
||||
None,
|
||||
f"响应解析失败,工具调用参数无法解析为字典类型。工具调用参数原始响应:\n{raw_arg_data}",
|
||||
)
|
||||
except json.JSONDecodeError as e:
|
||||
except orjson.JSONDecodeError as e:
|
||||
raise RespParseException(
|
||||
None,
|
||||
f"响应解析失败,无法解析工具调用参数。工具调用参数原始响应:{raw_arg_data}",
|
||||
@@ -357,11 +357,11 @@ def _default_normal_response_parser(
|
||||
api_response.tool_calls = []
|
||||
for call in message_part.tool_calls:
|
||||
try:
|
||||
arguments = json.loads(repair_json(call.function.arguments))
|
||||
arguments = orjson.loads(repair_json(call.function.arguments))
|
||||
if not isinstance(arguments, dict):
|
||||
raise RespParseException(resp, "响应解析失败,工具调用参数无法解析为字典类型")
|
||||
api_response.tool_calls.append(ToolCall(call.id, call.function.name, arguments))
|
||||
except json.JSONDecodeError as e:
|
||||
except orjson.JSONDecodeError as e:
|
||||
raise RespParseException(resp, "响应解析失败,无法解析工具调用参数") from e
|
||||
|
||||
# 提取Usage信息
|
||||
|
||||
Reference in New Issue
Block a user