Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dashscope/api_entities/api_request_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ def get_batch_binary_data(self) -> bytes: # type: ignore[return]

def _only_parameters(self) -> str:
obj = {"model": self.model, "parameters": self.parameters, "input": {}}
parameters = self.parameters.copy()
if "raw_input" in parameters:
obj["input"] = parameters.pop("raw_input")
Comment thread
songguocola marked this conversation as resolved.
if self.task is not None:
obj["task"] = self.task
if self.task_group is not None:
Expand Down
7 changes: 7 additions & 0 deletions dashscope/audio/asr/transcription.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,20 @@ def wait(
task: Union[str, TranscriptionResponse], # type: ignore[override]
api_key: str = None,
workspace: str = None,
wait_timeout: int = -1,
**kwargs,
) -> TranscriptionResponse:
"""Poll task until the final results of transcription is obtained.

Args:
task (Union[str, TranscriptionResponse]): The task_id or
response including task_id returned from async_call().
api_key (str, optional): The api_key. Defaults to None.
workspace (str): The dashscope workspace id.
wait_timeout (int, optional): The timeout for waiting.
Defaults to -1.That means no timeout.
If set to a value > 0, the task does not complete
within this time, a timeout error response will be returned.

Returns:
TranscriptionResponse: The result of batch transcription.
Expand All @@ -183,6 +189,7 @@ def wait(
task,
api_key=api_key,
workspace=workspace,
wait_timeout=wait_timeout,
**kwargs,
)
return TranscriptionResponse.from_api_response(response)
Expand Down
50 changes: 50 additions & 0 deletions dashscope/client/base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ async def wait(
task: Union[str, DashScopeAPIResponse],
api_key: str = None,
workspace: str = None,
wait_timeout: int = -1,
**kwargs,
) -> DashScopeAPIResponse:
"""Wait for async task completion and return task result.
Expand All @@ -199,6 +200,12 @@ async def wait(
task (Union[str, DashScopeAPIResponse]): The task_id, or
async_call response.
api_key (str, optional): The api_key. Defaults to None.
workspace (str, optional): The dashscope workspace id.
wait_timeout (int, optional): The maximum seconds to wait
for the task to complete. Default is -1, which means no
timeout. When set to a value > 0, if the task does not
complete within this time, a timeout error response will
be returned.

Returns:
DashScopeAPIResponse: The async task information.
Expand All @@ -208,6 +215,7 @@ async def wait(
max_wait_seconds = 5
increment_steps = 3
step = 0
start_time = time.time()
while True:
step += 1
# we start by querying once every second, and double
Expand All @@ -217,6 +225,21 @@ async def wait(
# (server side return immediately when ready)
if wait_seconds < max_wait_seconds and step % increment_steps == 0:
wait_seconds = min(wait_seconds * 2, max_wait_seconds)
if 0 < wait_timeout <= (time.time() - start_time):
logger.warning(
"Wait task: %s timeout after %s seconds.",
task_id,
wait_timeout,
)
return DashScopeAPIResponse(
request_id=task_id,
status_code=HTTPStatus.REQUEST_TIMEOUT,
code="WaitTaskTimeout",
message=(
f"Wait task: {task_id} timeout after "
f"{wait_timeout} seconds."
),
)
rsp = await cls._get(
task_id,
api_key,
Expand Down Expand Up @@ -606,10 +629,14 @@ def call(
workspace=workspace,
**kwargs,
)
wait_timeout = -1
if "wait_timeout" in kwargs:
wait_timeout = kwargs.pop("wait_timeout")
Comment on lines +632 to +634

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In BaseAsyncApi.call, wait_timeout is popped from kwargs after cls.async_call has already been invoked. Because kwargs is passed to cls.async_call before being popped, wait_timeout is still sent to the server as an unexpected request parameter.

To fix this, wait_timeout should be popped from kwargs before calling cls.async_call:

    @classmethod
    def call(
        cls,
        *args,
        api_key: str = None,
        workspace: str = None,
        **kwargs,
    ) -> DashScopeAPIResponse:
        """Call service and get result."""
        wait_timeout = kwargs.pop("wait_timeout", -1)
        task_response = cls.async_call(
            *args,
            api_key=api_key,
            workspace=workspace,
            **kwargs,
        )
        response = cls.wait(
            task_response,
            api_key=api_key,
            workspace=workspace,
            wait_timeout=wait_timeout,
        )
        return response

response = cls.wait(
task_response,
api_key=api_key,
workspace=workspace,
wait_timeout=wait_timeout,
)
return response

Expand Down Expand Up @@ -767,6 +794,7 @@ def wait(
task: Union[str, DashScopeAPIResponse],
api_key: str = None,
workspace: str = None,
wait_timeout: int = -1,
**kwargs,
) -> DashScopeAPIResponse:
"""Wait for async task completion and return task result.
Expand All @@ -775,6 +803,12 @@ def wait(
task (Union[str, DashScopeAPIResponse]): The task_id, or
async_call response.
api_key (str, optional): The api_key. Defaults to None.
workspace (str, optional): The dashscope workspace id.
wait_timeout (int, optional): The maximum seconds to wait
for the task to complete. Default is -1, which means no
timeout. When set to a value > 0, if the task does not
complete within this time, a timeout error response will
be returned.

Returns:
DashScopeAPIResponse: The async task information.
Expand All @@ -784,6 +818,7 @@ def wait(
max_wait_seconds = 5
increment_steps = 3
step = 0
start_time = time.time()
while True:
step += 1
# we start by querying once every second, and double
Expand All @@ -794,6 +829,21 @@ def wait(
# (server side return immediately when ready)
if wait_seconds < max_wait_seconds and step % increment_steps == 0:
wait_seconds = min(wait_seconds * 2, max_wait_seconds)
if 0 < wait_timeout <= (time.time() - start_time):
logger.warning(
"Wait task: %s timeout after %s seconds.",
task_id,
wait_timeout,
)
return DashScopeAPIResponse(
request_id=task_id,
status_code=HTTPStatus.REQUEST_TIMEOUT,
code="WaitTaskTimeout",
message=(
f"Wait task: {task_id} timeout after "
f"{wait_timeout} seconds."
),
)
rsp = cls._get(task_id, api_key, workspace=workspace, **kwargs)
if rsp.status_code == HTTPStatus.OK:
if rsp.output is None:
Expand Down
Loading