fix: 发送正确的图片格式而不是默认的 image/jpeg

This commit is contained in:
ligen131 2024-08-19 00:00:29 +08:00
parent 2f092f4a87
commit b51ca06c7c
4 changed files with 16 additions and 12 deletions

View File

@ -72,12 +72,13 @@ class AnthropicMessages(api.LLMAPIRequester):
for i, ce in enumerate(m.content):
if ce.type == "image_url":
base64_image, image_format = await image.qq_image_url_to_base64(ce.image_url.url)
alter_image_ele = {
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": await image.qq_image_url_to_base64(ce.image_url.url)
"media_type": f"image/{image_format}",
"data": base64_image
}
}
msg_dict["content"][i] = alter_image_ele

View File

@ -136,7 +136,5 @@ class OpenAIChatCompletions(api.LLMAPIRequester):
self,
original_url: str,
) -> str:
base64_image = await image.qq_image_url_to_base64(original_url)
return f"data:image/jpeg;base64,{base64_image}"
base64_image, image_format = await image.qq_image_url_to_base64(original_url)
return f"data:image/{image_format};base64,{base64_image}"

View File

@ -101,5 +101,5 @@ class OllamaChatCompletions(api.LLMAPIRequester):
self,
original_url: str,
) -> str:
base64_image: str = await image.qq_image_url_to_base64(original_url)
return f"data:image/jpeg;base64,{base64_image}"
base64_image, image_format = await image.qq_image_url_to_base64(original_url)
return f"data:image/{image_format};base64,{base64_image}"

View File

@ -8,14 +8,14 @@ import aiohttp
async def qq_image_url_to_base64(
image_url: str
) -> str:
"""将QQ图片URL转为base64
) -> typing.Tuple[str, str]:
"""将QQ图片URL转为base64,并返回图片格式
Args:
image_url (str): QQ图片URL
Returns:
str: base64编码
typing.Tuple[str, str]: base64编码和图片格式
"""
parsed = urlparse(image_url)
query = parse_qs(parsed.query)
@ -35,7 +35,12 @@ async def qq_image_url_to_base64(
) as resp:
resp.raise_for_status() # 检查HTTP错误
file_bytes = await resp.read()
content_type = resp.headers.get('Content-Type')
if not content_type or not content_type.startswith('image/'):
image_format = 'jpeg'
else:
image_format = content_type.split('/')[-1]
base64_str = base64.b64encode(file_bytes).decode()
return base64_str
return base64_str, image_format