Skip to content

Commit 6de86b1

Browse files
author
wangjiaju
committed
ConvertMessage: change png to files. Runner.run: return final response.
1 parent d55ad50 commit 6de86b1

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
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: 21 additions & 6 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,23 @@ 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+
# 读取文件数据
111+
file_data = read_file_to_bytes(messages.media)
112+
113+
# 自动识别 MIME 类型
114+
kind = filetype.guess(file_data)
115+
if kind is None:
116+
raise ValueError("Unsupported or unknown file type.")
117+
118+
mime_type = kind.mime
119+
120+
# 检查是否为图片或视频
121+
assert mime_type.startswith(("image/", "video/")), (
122+
f"Unsupported media type: {mime_type}"
110123
)
124+
111125
_messages = [
112126
types.Content(
113127
role="user",
@@ -116,8 +130,8 @@ def _convert_messages(
116130
types.Part(
117131
inline_data=Blob(
118132
display_name=messages.media,
119-
data=read_png_to_bytes(messages.media),
120-
mime_type="image/png",
133+
data=file_data,
134+
mime_type=mime_type,
121135
)
122136
),
123137
],
@@ -271,7 +285,8 @@ async def run(
271285
and event.content.parts[0].text is not None
272286
and len(event.content.parts[0].text.strip()) > 0
273287
):
274-
final_output += event.content.parts[0].text
288+
final_output = event.content.parts[0].text
289+
logger.debug(f"Output: {final_output}")
275290
except LlmCallsLimitExceededError as e:
276291
logger.warning(f"Max number of llm calls limit exceeded: {e}")
277292
final_output = ""

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)