Skip to content

Commit

Permalink
Merge pull request #869 from ligen131/lg/fix_image_format
Browse files Browse the repository at this point in the history
fix: 发送正确的图片格式而不是默认的 `image/jpeg`
  • Loading branch information
RockChinQ authored Aug 24, 2024
2 parents 2f092f4 + b51ca06 commit 077e77e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
5 changes: 3 additions & 2 deletions pkg/provider/modelmgr/apis/anthropicmsgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ async def call(

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
Expand Down
6 changes: 2 additions & 4 deletions pkg/provider/modelmgr/apis/chatcmpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,5 @@ async def get_base64_str(
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}"
4 changes: 2 additions & 2 deletions pkg/provider/modelmgr/apis/ollamachat.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ async def get_base64_str(
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}"
13 changes: 9 additions & 4 deletions pkg/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

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)
Expand All @@ -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

0 comments on commit 077e77e

Please sign in to comment.