Skip to content

Build and run agents you can see, understand and trust.

fix(model): update DashScopeChatModel to use AioMultiModalConversation for async multimodal processing and enhance response type annotations (#1290)

YingchaoX501c909f3acb
diff --git a/src/agentscope/model/_dashscope_model.py b/src/agentscope/model/_dashscope_model.py
index 1da765226..284c714bc 100644
--- a/src/agentscope/model/_dashscope_model.py
+++ b/src/agentscope/model/_dashscope_model.py
@@ -97,7 +97,7 @@ class DashScopeChatModel(ChatModelBase):
for more details.
multimodality (`bool | None`, optional):
Whether to use multimodal conversation API. If `True`,
- it will use `dashscope.MultiModalConversation.call`
+ it will use `dashscope.AioMultiModalConversation.call`
to process multimodal inputs such as images and text. If
`False`, it will use
`dashscope.aigc.generation.AioGeneration.call` to process
@@ -270,7 +270,7 @@ class DashScopeChatModel(ChatModelBase):
or "-vl" in self.model_name
)
):
- response = dashscope.MultiModalConversation.call(
+ response = await dashscope.AioMultiModalConversation.call(
api_key=self.api_key,
**kwargs,
)
@@ -302,6 +302,7 @@ class DashScopeChatModel(ChatModelBase):
start_datetime: datetime,
response: Union[
AsyncGenerator[GenerationResponse, None],
+ AsyncGenerator[MultiModalConversationResponse, None],
Generator[MultiModalConversationResponse, None, None],
],
structured_model: Type[BaseModel] | None = None,
@@ -313,11 +314,12 @@ class DashScopeChatModel(ChatModelBase):
start_datetime (`datetime`):
The start datetime of the response generation.
response (
- `Union[AsyncGenerator[GenerationResponse, None], Generator[ \
- MultiModalConversationResponse, None, None]]`
+ `Union[AsyncGenerator[GenerationResponse, None], \
+ AsyncGenerator[MultiModalConversationResponse, None], \
+ Generator[MultiModalConversationResponse, None, None]]`
):
- DashScope streaming response generator (GenerationResponse or
- MultiModalConversationResponse) to parse.
+ DashScope streaming response (async) generator
+ (GenerationResponse or MultiModalConversationResponse).
structured_model (`Type[BaseModel] | None`, default `None`):
A Pydantic BaseModel class that defines the expected structure
for the model's output.
diff --git a/tests/model_dashscope_test.py b/tests/model_dashscope_test.py
index 39033bf38..e6ce77abb 100644
--- a/tests/model_dashscope_test.py
+++ b/tests/model_dashscope_test.py
@@ -2,7 +2,7 @@
"""Unit tests for DashScope API model class."""
from typing import Any, AsyncGenerator
from unittest.async_case import IsolatedAsyncioTestCase
-from unittest.mock import Mock, patch
+from unittest.mock import AsyncMock, Mock, patch
from http import HTTPStatus
from pydantic import BaseModel
@@ -382,6 +382,32 @@ class TestDashScopeChatModel(IsolatedAsyncioTestCase):
if "schema must be a dict" in str(e):
self.fail("Valid tools schema was rejected")
+ async def test_call_with_multimodal_model(self) -> None:
+ """Test multimodal model uses AioMultiModalConversation (async)."""
+ model = DashScopeChatModel(
+ model_name="qwen-vl-plus",
+ api_key="test_key",
+ stream=False,
+ multimodality=True,
+ )
+ messages = [{"role": "user", "content": "Describe this image."}]
+ mock_response = self._create_mock_response("This is a test image.")
+ with patch(
+ "dashscope.AioMultiModalConversation.call",
+ new_callable=AsyncMock,
+ ) as mock_call:
+ mock_call.return_value = mock_response
+ result = await model(messages)
+ mock_call.assert_called_once()
+ call_kwargs = mock_call.call_args[1]
+ self.assertEqual(call_kwargs["messages"], messages)
+ self.assertEqual(call_kwargs["model"], "qwen-vl-plus")
+ self.assertIsInstance(result, ChatResponse)
+ self.assertEqual(
+ result.content,
+ [TextBlock(type="text", text="This is a test image.")],
+ )
+
async def test_error_handling_scenarios(self) -> None:
"""Test various error handling scenarios."""
model = DashScopeChatModel(