Skip to content

Commit 54237cc

Browse files
author
wangjiaju
committed
Update _convert_message
1 parent c1e5984 commit 54237cc

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ dependencies = [
3535
"psycopg2-binary>=2.9.10", # For PostgreSQL database (short term memory)
3636
"pymysql>=1.1.1", # For MySQL database (short term memory)
3737
"opensearch-py==2.8.0",
38+
"filetype>=1.2.0",
3839
]
3940

4041
[project.scripts]

veadk/runner.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from veadk.memory.short_term_memory import ShortTermMemory
3535
from veadk.types import MediaMessage
3636
from veadk.utils.logger import get_logger
37-
from veadk.utils.misc import formatted_timestamp, read_png_to_bytes
37+
from veadk.utils.misc import formatted_timestamp, read_file_to_bytes
3838

3939
logger = get_logger(__name__)
4040

@@ -105,9 +105,20 @@ def _convert_messages(
105105
if isinstance(messages, str):
106106
_messages = [types.Content(role="user", parts=[types.Part(text=messages)])]
107107
elif isinstance(messages, MediaMessage):
108-
assert messages.media.endswith(".png"), (
109-
"The MediaMessage only supports PNG format file for now."
108+
import filetype
109+
110+
file_data = read_file_to_bytes(messages.media)
111+
112+
kind = filetype.guess(file_data)
113+
if kind is None:
114+
raise ValueError("Unsupported or unknown file type.")
115+
116+
mime_type = kind.mime
117+
118+
assert mime_type.startswith(("image/", "video/")), (
119+
f"Unsupported media type: {mime_type}"
110120
)
121+
111122
_messages = [
112123
types.Content(
113124
role="user",
@@ -116,8 +127,8 @@ def _convert_messages(
116127
types.Part(
117128
inline_data=Blob(
118129
display_name=messages.media,
119-
data=read_png_to_bytes(messages.media),
120-
mime_type="image/png",
130+
data=file_data,
131+
mime_type=mime_type,
121132
)
122133
),
123134
],

veadk/tools/builtin_tools/generate_image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from veadk.config import getenv
2929
from veadk.consts import DEFAULT_IMAGE_GENERATE_MODEL_NAME, DEFAULT_MODEL_AGENT_API_BASE
3030
from veadk.utils.logger import get_logger
31-
from veadk.utils.misc import formatted_timestamp, read_png_to_bytes
31+
from veadk.utils.misc import formatted_timestamp, read_file_to_bytes
3232
from veadk.version import VERSION
3333

3434
logger = get_logger(__name__)
@@ -299,7 +299,7 @@ async def image_generate(
299299
artifact=Part(
300300
inline_data=Blob(
301301
display_name=filename,
302-
data=read_png_to_bytes(image_tos_url),
302+
data=read_file_to_bytes(image_tos_url),
303303
mime_type=mimetypes.guess_type(image_tos_url)[0],
304304
)
305305
),

veadk/utils/misc.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,14 @@ def formatted_timestamp() -> str:
3636
return time.strftime("%Y%m%d%H%M%S", time.localtime())
3737

3838

39-
def read_png_to_bytes(png_path: str) -> bytes:
40-
# Determine whether it is a local file or a network file
41-
if png_path.startswith(("http://", "https://")):
42-
# Network file: Download via URL and return bytes
43-
response = requests.get(png_path)
44-
response.raise_for_status() # Check if the HTTP request is successful
39+
def read_file_to_bytes(file_path: str) -> bytes:
40+
if file_path.startswith(("http://", "https://")):
41+
response = requests.get(file_path)
42+
response.raise_for_status()
4543
return response.content
4644
else:
47-
# Local file
48-
with open(png_path, "rb") as f:
49-
data = f.read()
50-
return data
45+
with open(file_path, "rb") as f:
46+
return f.read()
5147

5248

5349
def load_module_from_file(module_name: str, file_path: str) -> types.ModuleType:

0 commit comments

Comments
 (0)