Skip to content

Commit

Permalink
support local image files for dashscope qwen-vl
Browse files Browse the repository at this point in the history
  • Loading branch information
JianxinMa committed Apr 16, 2024
1 parent e84b103 commit 3f51838
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions qwen_agent/llm/qwenvl_dashscope.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import copy
import os
import re
from http import HTTPStatus
from pprint import pformat
from typing import Dict, Iterator, List, Optional
Expand Down Expand Up @@ -35,6 +37,7 @@ def _chat_stream(
if delta_stream:
raise NotImplementedError

messages = _format_local_files(messages)
messages = [msg.model_dump() for msg in messages]
logger.debug(f'*{pformat(messages, indent=2)}*')
response = dashscope.MultiModalConversation.call(
Expand All @@ -54,6 +57,7 @@ def _chat_no_stream(
self,
messages: List[Message],
) -> List[Message]:
messages = _format_local_files(messages)
messages = [msg.model_dump() for msg in messages]
logger.debug(f'*{pformat(messages, indent=2)}*')
response = dashscope.MultiModalConversation.call(
Expand All @@ -77,6 +81,29 @@ def _postprocess_messages(self, messages: List[Message],
return messages


# DashScope Qwen-VL requires the following format for local files:
# Linux & Mac: file:///home/images/test.png
# Windows: file://D:/images/abc.png
def _format_local_files(messages: List[Message]) -> List[Message]:
messages = copy.deepcopy(messages)
for msg in messages:
if isinstance(msg.content, list):
for item in msg.content:
if item.image:
fname = item.image
if not fname.startswith((
'http://',
'https://',
'file://',
)):
if fname.startswith('~'):
fname = os.path.expanduser(fname)
if re.match(r'^[A-Za-z]:\\', fname):
fname = fname.replace('\\', '/')
item.image = fname
return messages


def _extract_vl_response(response) -> List[Message]:
output = response.output.choices[0].message
text_content = []
Expand Down

0 comments on commit 3f51838

Please sign in to comment.