Skip to content
Merged
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
55 changes: 55 additions & 0 deletions dashscope/api_entities/dashscope_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class DashScopeAPIResponse(DictMixin):
message: str
output: Any
usage: Any
headers: Dict[Any, Any]

def __init__(
self,
Expand All @@ -100,6 +101,7 @@ def __init__(
message: str = "",
output: Any = None,
usage: Any = None,
headers: Dict[Any, Any] = None,
**kwargs,
):
super().__init__(
Expand All @@ -109,9 +111,32 @@ def __init__(
message=message,
output=output,
usage=usage,
headers=headers,
**kwargs,
)

def __repr__(self):
data = {
"status_code": self.status_code,
"request_id": self.request_id,
"code": self.code,
"message": self.message,
"output": self.output,
"usage": self.usage,
}
return f"{type(self).__name__}({data})"
Comment thread
mose-zm marked this conversation as resolved.

def __str__(self):
data = {
"status_code": self.status_code,
"request_id": self.request_id,
"code": self.code,
"message": self.message,
"output": self.output,
"usage": self.usage,
}
return json.dumps(data, ensure_ascii=False)
Comment thread
mose-zm marked this conversation as resolved.


class Role:
USER = "user"
Expand Down Expand Up @@ -235,6 +260,7 @@ def __init__(
class GenerationResponse(DashScopeAPIResponse):
output: GenerationOutput
usage: GenerationUsage
headers: Dict[Any, Any]

@staticmethod
def from_api_response(api_response: DashScopeAPIResponse):
Expand All @@ -250,13 +276,15 @@ def from_api_response(api_response: DashScopeAPIResponse):
message=api_response.message,
output=GenerationOutput(**api_response.output),
usage=GenerationUsage(**usage),
headers=api_response.headers,
)
else:
return GenerationResponse(
status_code=api_response.status_code,
request_id=api_response.request_id,
code=api_response.code,
message=api_response.message,
headers=api_response.headers,
)


Expand Down Expand Up @@ -316,6 +344,7 @@ def __init__(
class MultiModalConversationResponse(DashScopeAPIResponse):
output: MultiModalConversationOutput
usage: MultiModalConversationUsage
headers: Dict[Any, Any]

@staticmethod
def from_api_response(api_response: DashScopeAPIResponse):
Expand All @@ -331,13 +360,15 @@ def from_api_response(api_response: DashScopeAPIResponse):
message=api_response.message,
output=MultiModalConversationOutput(**api_response.output),
usage=MultiModalConversationUsage(**usage),
headers=api_response.headers,
)
else:
return MultiModalConversationResponse(
status_code=api_response.status_code,
request_id=api_response.request_id,
code=api_response.code,
message=api_response.message,
headers=api_response.headers,
)


Expand Down Expand Up @@ -365,6 +396,7 @@ def __init__(self, **kwargs):
class TranscriptionResponse(DashScopeAPIResponse):
output: TranscriptionOutput
usage: TranscriptionUsage
headers: Dict[Any, Any]

@staticmethod
def from_api_response(api_response: DashScopeAPIResponse):
Expand All @@ -383,6 +415,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
message=api_response.message,
output=output,
usage=usage,
headers=api_response.headers,
)

else:
Expand All @@ -391,6 +424,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
request_id=api_response.request_id,
code=api_response.code,
message=api_response.message,
headers=api_response.headers,
)


Expand All @@ -414,6 +448,7 @@ def __init__(self, duration: int = 0, **kwargs):
class RecognitionResponse(DashScopeAPIResponse):
output: RecognitionOutput
usage: RecognitionUsage
headers: Dict[Any, Any]

@staticmethod
def from_api_response(api_response: DashScopeAPIResponse):
Expand All @@ -433,6 +468,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
message=api_response.message,
output=output,
usage=usage,
headers=api_response.headers,
)

else:
Expand All @@ -441,6 +477,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
request_id=api_response.request_id,
code=api_response.code,
message=api_response.message,
headers=api_response.headers,
)

@staticmethod
Expand Down Expand Up @@ -478,6 +515,7 @@ def __init__(self, characters: int = 0, **kwargs):
class SpeechSynthesisResponse(DashScopeAPIResponse):
output: SpeechSynthesisOutput
usage: SpeechSynthesisUsage
headers: Dict[Any, Any]

@staticmethod
def from_api_response(api_response: DashScopeAPIResponse):
Expand All @@ -496,6 +534,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
message=api_response.message,
output=output,
usage=usage,
headers=api_response.headers,
)

else:
Expand All @@ -504,6 +543,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
request_id=api_response.request_id,
code=api_response.code,
message=api_response.message,
headers=api_response.headers,
)


Expand Down Expand Up @@ -597,6 +637,7 @@ def __init__(
class ImageSynthesisResponse(DashScopeAPIResponse):
output: ImageSynthesisOutput
usage: ImageSynthesisUsage
headers: Dict[Any, Any]

@staticmethod
def from_api_response(api_response: DashScopeAPIResponse):
Expand All @@ -615,6 +656,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
message=api_response.message,
output=output,
usage=usage,
headers=api_response.headers,
)

else:
Expand All @@ -623,13 +665,15 @@ def from_api_response(api_response: DashScopeAPIResponse):
request_id=api_response.request_id,
code=api_response.code,
message=api_response.message,
headers=api_response.headers,
)


@dataclass(init=False)
class VideoSynthesisResponse(DashScopeAPIResponse):
output: VideoSynthesisOutput
usage: VideoSynthesisUsage
headers: Dict[Any, Any]

@staticmethod
def from_api_response(api_response: DashScopeAPIResponse):
Expand All @@ -648,6 +692,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
message=api_response.message,
output=output,
usage=usage,
headers=api_response.headers,
)

else:
Expand All @@ -656,6 +701,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
request_id=api_response.request_id,
code=api_response.code,
message=api_response.message,
headers=api_response.headers,
)


Expand Down Expand Up @@ -705,6 +751,7 @@ def __init__(self, total_tokens=None, **kwargs):
class ReRankResponse(DashScopeAPIResponse):
output: ReRankOutput
usage: GenerationUsage
headers: Dict[Any, Any]

@staticmethod
def from_api_response(api_response: DashScopeAPIResponse):
Expand All @@ -720,13 +767,15 @@ def from_api_response(api_response: DashScopeAPIResponse):
message=api_response.message,
output=ReRankOutput(**api_response.output),
usage=ReRankUsage(**usage),
headers=api_response.headers,
)
else:
return ReRankResponse(
status_code=api_response.status_code,
request_id=api_response.request_id,
code=api_response.code,
message=api_response.message,
headers=api_response.headers,
)


Expand Down Expand Up @@ -777,6 +826,7 @@ def __init__(
class TextToSpeechResponse(DashScopeAPIResponse):
output: TextToSpeechOutput
usage: MultiModalConversationUsage
headers: Dict[Any, Any]

@staticmethod
def from_api_response(api_response: DashScopeAPIResponse):
Expand All @@ -792,13 +842,15 @@ def from_api_response(api_response: DashScopeAPIResponse):
message=api_response.message,
output=TextToSpeechOutput(**api_response.output),
usage=MultiModalConversationUsage(**usage),
headers=api_response.headers,
)
else:
return TextToSpeechResponse(
status_code=api_response.status_code,
request_id=api_response.request_id,
code=api_response.code,
message=api_response.message,
headers=api_response.headers,
)


Expand Down Expand Up @@ -858,6 +910,7 @@ def __init__(
class ImageGenerationResponse(DashScopeAPIResponse):
output: ImageGenerationOutput
usage: ImageGenerationUsage
headers: Dict[Any, Any]

@staticmethod
def from_api_response(api_response: DashScopeAPIResponse):
Expand All @@ -873,11 +926,13 @@ def from_api_response(api_response: DashScopeAPIResponse):
message=api_response.message,
output=ImageGenerationOutput(**api_response.output),
usage=ImageGenerationUsage(**usage),
headers=api_response.headers,
)
else:
return ImageGenerationResponse(
status_code=api_response.status_code,
request_id=api_response.request_id,
code=api_response.code,
message=api_response.message,
headers=api_response.headers,
)
11 changes: 11 additions & 0 deletions dashscope/api_entities/http_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s
response: aiohttp.ClientResponse,
):
request_id = ""
headers = dict(response.headers)
if (
response.status == HTTPStatus.OK
and self.stream
Expand All @@ -291,6 +292,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
code="Unknown",
message=data,
headers=headers,
)
continue
if is_error:
Expand All @@ -299,6 +301,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s
status_code=status_code,
code=msg["code"],
message=msg["message"],
headers=headers,
)
else:
if self.encryption and self.encryption.is_valid():
Expand All @@ -308,6 +311,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s
status_code=HTTPStatus.OK,
output=output,
usage=usage,
headers=headers,
)
elif (
response.status == HTTPStatus.OK
Expand All @@ -329,6 +333,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s
request_id=request_id,
status_code=HTTPStatus.OK,
output=output,
headers=headers,
)
elif response.status == HTTPStatus.OK:
json_content = await response.json()
Expand Down Expand Up @@ -356,6 +361,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s
status_code=HTTPStatus.OK,
output=output,
usage=usage,
headers=headers,
)
else:
yield await _handle_aiohttp_failed_response(response)
Expand All @@ -365,6 +371,7 @@ def _handle_response( # pylint: disable=too-many-branches
response: requests.Response,
):
request_id = ""
headers = dict(response.headers)
if (
response.status_code == HTTPStatus.OK
and self.stream
Expand Down Expand Up @@ -395,6 +402,7 @@ def _handle_response( # pylint: disable=too-many-branches
output=None,
code="Unknown",
message=data,
headers=headers,
)
continue
if is_error:
Expand All @@ -406,6 +414,7 @@ def _handle_response( # pylint: disable=too-many-branches
if "code" in msg
else None, # noqa E501
message=msg["message"] if "message" in msg else None,
headers=headers,
) # noqa E501
else:
if self.flattened_output:
Expand All @@ -418,6 +427,7 @@ def _handle_response( # pylint: disable=too-many-branches
status_code=HTTPStatus.OK,
output=output,
usage=usage,
headers=headers,
)
elif response.status_code == HTTPStatus.OK:
json_content = response.json()
Expand All @@ -442,6 +452,7 @@ def _handle_response( # pylint: disable=too-many-branches
status_code=HTTPStatus.OK,
output=output,
usage=usage,
headers=headers,
)
else:
yield _handle_http_failed_response(response)
Expand Down
Loading
Loading