优化图片格式处理,统一将'jpg'格式转换为'jpeg',新增标准化和MIME类型转换功能,以确保API兼容性和提高代码可读性(最主要的还是为了gemini)
This commit is contained in:
committed by
Windpicker-owo
parent
e6976e4e50
commit
1e785a117d
@@ -20,6 +20,29 @@ from ..payload_content.tool_option import ToolOption, ToolParam, ToolCall
|
||||
logger = get_logger("AioHTTP-Gemini客户端")
|
||||
|
||||
|
||||
def _format_to_mime_type(image_format: str) -> str:
|
||||
"""
|
||||
将图片格式转换为正确的MIME类型
|
||||
|
||||
Args:
|
||||
image_format (str): 图片格式 (如 'jpg', 'png' 等)
|
||||
|
||||
Returns:
|
||||
str: 对应的MIME类型
|
||||
"""
|
||||
format_mapping = {
|
||||
"jpg": "image/jpeg",
|
||||
"jpeg": "image/jpeg",
|
||||
"png": "image/png",
|
||||
"webp": "image/webp",
|
||||
"gif": "image/gif",
|
||||
"heic": "image/heic",
|
||||
"heif": "image/heif"
|
||||
}
|
||||
|
||||
return format_mapping.get(image_format.lower(), f"image/{image_format.lower()}")
|
||||
|
||||
|
||||
def _convert_messages(messages: list[Message]) -> tuple[list[dict], list[str] | None]:
|
||||
"""
|
||||
转换消息格式 - 将消息转换为Gemini REST API所需的格式
|
||||
@@ -46,7 +69,7 @@ def _convert_messages(messages: list[Message]) -> tuple[list[dict], list[str] |
|
||||
if isinstance(item, tuple): # (format, base64_data)
|
||||
parts.append({
|
||||
"inline_data": {
|
||||
"mime_type": f"image/{item[0].lower()}",
|
||||
"mime_type": _format_to_mime_type(item[0]),
|
||||
"data": item[1]
|
||||
}
|
||||
})
|
||||
|
||||
@@ -61,6 +61,25 @@ def _convert_messages(
|
||||
:param messages: 消息列表
|
||||
:return: 转换后的消息列表(和可能存在的system消息)
|
||||
"""
|
||||
|
||||
def _get_correct_mime_type(image_format: str) -> str:
|
||||
"""
|
||||
获取正确的MIME类型,修复jpg到jpeg的映射问题
|
||||
:param image_format: 图片格式
|
||||
:return: 正确的MIME类型
|
||||
"""
|
||||
# 标准化格式名称,解决jpg/jpeg兼容性问题
|
||||
format_mapping = {
|
||||
"jpg": "jpeg",
|
||||
"jpeg": "jpeg",
|
||||
"png": "png",
|
||||
"webp": "webp",
|
||||
"heic": "heic",
|
||||
"heif": "heif",
|
||||
"gif": "gif"
|
||||
}
|
||||
normalized_format = format_mapping.get(image_format.lower(), image_format.lower())
|
||||
return f"image/{normalized_format}"
|
||||
|
||||
def _convert_message_item(message: Message) -> Content:
|
||||
"""
|
||||
@@ -84,7 +103,7 @@ def _convert_messages(
|
||||
if isinstance(item, tuple):
|
||||
image_format = "jpeg" if item[0].lower() == "jpg" else item[0].lower()
|
||||
content.append(
|
||||
Part.from_bytes(data=base64.b64decode(item[1]), mime_type=f"image/{image_format}")
|
||||
Part.from_bytes(data=base64.b64decode(item[1]), mime_type=_get_correct_mime_type(item[0]))
|
||||
)
|
||||
elif isinstance(item, str):
|
||||
content.append(Part.from_text(text=item))
|
||||
|
||||
Reference in New Issue
Block a user