Skip to content

Commit 9b7e51e

Browse files
committed
support modifying the prefix of base64.
1 parent 86a9a81 commit 9b7e51e

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

oxygent/oxy/llms/base_llm.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class BaseLLM(Oxy):
7979
default=2 * 1024 * 1024,
8080
description="Maximum non-media file size (bytes) for base64 embedding.",
8181
)
82+
base64_image_prefix: str = Field(default="data:image", description="")
83+
base64_video_prefix: str = Field(default="data:video", description="")
8284
is_disable_system_prompt: bool = Field(default=False)
8385

8486
async def _get_messages(self, oxy_request: OxyRequest):
@@ -170,11 +172,15 @@ async def _get_messages(self, oxy_request: OxyRequest):
170172

171173
if item_type == "image_url":
172174
item[item_type]["url"] = await image_to_base64(
173-
item[item_type]["url"], self.max_image_pixels
175+
item[item_type]["url"],
176+
self.max_image_pixels,
177+
self.base64_image_prefix,
174178
)
175179
elif item_type == "video_url":
176180
item[item_type]["url"] = await video_to_base64(
177-
item[item_type]["url"], self.max_video_size
181+
item[item_type]["url"],
182+
self.max_video_size,
183+
self.base64_video_prefix,
178184
)
179185
else:
180186
logger.warning(

oxygent/utils/common_utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ async def source_to_bytes(source: str):
9696
return await f.read()
9797

9898

99-
async def image_to_base64(source: str, max_image_pixels: int = 10000000) -> str:
99+
async def image_to_base64(
100+
source: str, max_image_pixels: int = 10000000, base64_prefix="data:image"
101+
) -> str:
100102
image_bytes = await source_to_bytes(source)
101103

102104
def process_image(image_bytes):
@@ -118,16 +120,18 @@ def process_image(image_bytes):
118120
return output.getvalue()
119121

120122
image_bytes = await asyncio.to_thread(process_image, image_bytes)
121-
return f"data:image;base64,{base64.b64encode(image_bytes).decode('utf-8')}"
123+
return f"{base64_prefix};base64,{base64.b64encode(image_bytes).decode('utf-8')}"
122124

123125

124126
# 512 * 1024 * 1024 bytes == 512MB
125-
async def video_to_base64(source: str, max_video_size: int = 512 * 1024 * 1024) -> str:
127+
async def video_to_base64(
128+
source: str, max_video_size: int = 512 * 1024 * 1024, base64_prefix="data:video"
129+
) -> str:
126130
video_bytes = await source_to_bytes(source)
127131
if len(video_bytes) > max_video_size:
128132
return source
129133
else:
130-
return f"data:video;base64,{base64.b64encode(video_bytes).decode('utf-8')}"
134+
return f"{base64_prefix};base64,{base64.b64encode(video_bytes).decode('utf-8')}"
131135

132136

133137
async def table_to_base64(source: str, max_table_size: int = 50 * 1024 * 1024) -> str:

0 commit comments

Comments
 (0)