Merge pull request #869 from ligen131/lg/fix_image_format

fix: 发送正确的图片格式而不是默认的 `image/jpeg`
This commit is contained in:
Junyan Qin 2024-08-24 15:47:55 +08:00 committed by GitHub
commit 077e77eee5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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