From 9f6c5182ddab8fe8690f4c1bc1164e5793f690d6 Mon Sep 17 00:00:00 2001 From: zhengweijun Date: Thu, 24 Jul 2025 16:39:16 +0800 Subject: [PATCH 1/3] chore: update version to 2.1.5.a1 and refactor AudioError class to inherit from BaseModel --- pyproject.toml | 2 +- tests/unit_tests/test_audio.py | 116 ++++++++++++++++++++++ zhipuai/__version__.py | 2 +- zhipuai/types/audio/audio_speech_chunk.py | 2 +- 4 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 tests/unit_tests/test_audio.py diff --git a/pyproject.toml b/pyproject.toml index 940194b..6fada2b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "zhipuai" -version = "2.1.5.20250724" +version = "2.1.5.a1" description = "A SDK library for accessing big model apis from ZhipuAI" authors = ["Zhipu AI"] readme = "README.md" diff --git a/tests/unit_tests/test_audio.py b/tests/unit_tests/test_audio.py new file mode 100644 index 0000000..639e5f1 --- /dev/null +++ b/tests/unit_tests/test_audio.py @@ -0,0 +1,116 @@ +import base64 +import json +import logging +import logging.config +from pathlib import Path + +import zhipuai +from zhipuai import ZhipuAI + + +def test_audio_speech(logging_conf): + logging.config.dictConfig(logging_conf) # type: ignore + client = ZhipuAI() # 填写您自己的APIKey + try: + speech_file_path = Path(__file__).parent / 'speech.wav' + response = client.audio.speech( + model='cogtts', + input='你好呀,欢迎来到智谱开放平台', + voice='tongtong', + stream=False, + response_format='wav', + ) + response.stream_to_file(speech_file_path) + + except zhipuai.core._errors.APIRequestFailedError as err: + print(err) + except zhipuai.core._errors.APIInternalError as err: + print(err) + except zhipuai.core._errors.APIStatusError as err: + print(err) + +def test_audio_speech_streaming(logging_conf): + logging.config.dictConfig(logging_conf) # type: ignore + client = ZhipuAI() # 填写您自己的APIKey + try: + response = client.audio.speech( + model='cogtts', + input='你好呀,欢迎来到智谱开放平台', + voice='tongtong', + stream=True, + response_format='wav', + ) + with open("output.pcm", "wb") as f: + for item in response: + choice = item.choices[0] + index = choice.index + finish_reason = choice.finish_reason + audio_delta = choice.delta.content + if finish_reason is not None: + break + f.write(base64.b64decode(audio_delta)) + print(f"{index}.finish_reason = {finish_reason}, audio_delta = {len(audio_delta)}") + + except zhipuai.core._errors.APIRequestFailedError as err: + print(err) + except zhipuai.core._errors.APIInternalError as err: + print(err) + except zhipuai.core._errors.APIStatusError as err: + print(err) + except Exception as e: + print(e) + + +def test_audio_customization(logging_conf): + logging.config.dictConfig(logging_conf) + client = ZhipuAI() # 填写您自己的APIKey + with open(Path(__file__).parent / 'speech.wav', 'rb') as file: + try: + speech_file_path = Path(__file__).parent / 'speech.wav' + response = client.audio.customization( + model='cogtts', + input='你好呀,欢迎来到智谱开放平台', + voice_text='这是一条测试用例', + voice_data=file, + response_format='wav', + ) + response.stream_to_file(speech_file_path) + + except zhipuai.core._errors.APIRequestFailedError as err: + print(err) + except zhipuai.core._errors.APIInternalError as err: + print(err) + except zhipuai.core._errors.APIStatusError as err: + print(err) + + +def test_audio_error_field(): + from zhipuai.types.audio.audio_speech_chunk import AudioSpeechChunk, AudioError, AudioSpeechChoice, AudioSpeechDelta + + # 构造一个 AudioError + error = AudioError(code="500", message="Internal Error") + + # 构造一个完整的 AudioSpeechChunk + chunk = AudioSpeechChunk( + choices=[ + AudioSpeechChoice( + delta=AudioSpeechDelta(content="audio", role="system"), + finish_reason="error", + index=0 + ) + ], + request_id="req_2", + created=123456, + error=error + ) + + # 检查 error 字段是否为 AudioError 实例 + assert isinstance(chunk.error, AudioError) + assert chunk.error.code == "500" + assert chunk.error.message == "Internal Error" + + # 检查序列化 + as_dict = chunk.dict() + assert as_dict["error"]["code"] == "500" + assert as_dict["error"]["message"] == "Internal Error" + print("test_audio_error_field passed.") diff --git a/zhipuai/__version__.py b/zhipuai/__version__.py index 355f8d5..e40c152 100644 --- a/zhipuai/__version__.py +++ b/zhipuai/__version__.py @@ -1 +1 @@ -__version__ = 'v2.1.5.20250724' \ No newline at end of file +__version__ = 'v2.1.5.a1' \ No newline at end of file diff --git a/zhipuai/types/audio/audio_speech_chunk.py b/zhipuai/types/audio/audio_speech_chunk.py index 7788d9d..3d18c3b 100644 --- a/zhipuai/types/audio/audio_speech_chunk.py +++ b/zhipuai/types/audio/audio_speech_chunk.py @@ -20,7 +20,7 @@ class AudioSpeechChoice(BaseModel): finish_reason: Optional[str] = None index: int -class AudioError: +class AudioError(BaseModel): code: Optional[str] = None message: Optional[str] = None From 7a4b6dd751bb55b70b99090be24abe0bbb1a79d9 Mon Sep 17 00:00:00 2001 From: zhengweijun Date: Thu, 24 Jul 2025 16:44:31 +0800 Subject: [PATCH 2/3] fix test --- tests/unit_tests/test_audio.py | 88 +--------------------------------- 1 file changed, 1 insertion(+), 87 deletions(-) diff --git a/tests/unit_tests/test_audio.py b/tests/unit_tests/test_audio.py index 639e5f1..904a273 100644 --- a/tests/unit_tests/test_audio.py +++ b/tests/unit_tests/test_audio.py @@ -1,89 +1,3 @@ -import base64 -import json -import logging -import logging.config -from pathlib import Path - -import zhipuai -from zhipuai import ZhipuAI - - -def test_audio_speech(logging_conf): - logging.config.dictConfig(logging_conf) # type: ignore - client = ZhipuAI() # 填写您自己的APIKey - try: - speech_file_path = Path(__file__).parent / 'speech.wav' - response = client.audio.speech( - model='cogtts', - input='你好呀,欢迎来到智谱开放平台', - voice='tongtong', - stream=False, - response_format='wav', - ) - response.stream_to_file(speech_file_path) - - except zhipuai.core._errors.APIRequestFailedError as err: - print(err) - except zhipuai.core._errors.APIInternalError as err: - print(err) - except zhipuai.core._errors.APIStatusError as err: - print(err) - -def test_audio_speech_streaming(logging_conf): - logging.config.dictConfig(logging_conf) # type: ignore - client = ZhipuAI() # 填写您自己的APIKey - try: - response = client.audio.speech( - model='cogtts', - input='你好呀,欢迎来到智谱开放平台', - voice='tongtong', - stream=True, - response_format='wav', - ) - with open("output.pcm", "wb") as f: - for item in response: - choice = item.choices[0] - index = choice.index - finish_reason = choice.finish_reason - audio_delta = choice.delta.content - if finish_reason is not None: - break - f.write(base64.b64decode(audio_delta)) - print(f"{index}.finish_reason = {finish_reason}, audio_delta = {len(audio_delta)}") - - except zhipuai.core._errors.APIRequestFailedError as err: - print(err) - except zhipuai.core._errors.APIInternalError as err: - print(err) - except zhipuai.core._errors.APIStatusError as err: - print(err) - except Exception as e: - print(e) - - -def test_audio_customization(logging_conf): - logging.config.dictConfig(logging_conf) - client = ZhipuAI() # 填写您自己的APIKey - with open(Path(__file__).parent / 'speech.wav', 'rb') as file: - try: - speech_file_path = Path(__file__).parent / 'speech.wav' - response = client.audio.customization( - model='cogtts', - input='你好呀,欢迎来到智谱开放平台', - voice_text='这是一条测试用例', - voice_data=file, - response_format='wav', - ) - response.stream_to_file(speech_file_path) - - except zhipuai.core._errors.APIRequestFailedError as err: - print(err) - except zhipuai.core._errors.APIInternalError as err: - print(err) - except zhipuai.core._errors.APIStatusError as err: - print(err) - - def test_audio_error_field(): from zhipuai.types.audio.audio_speech_chunk import AudioSpeechChunk, AudioError, AudioSpeechChoice, AudioSpeechDelta @@ -110,7 +24,7 @@ def test_audio_error_field(): assert chunk.error.message == "Internal Error" # 检查序列化 - as_dict = chunk.dict() + as_dict = chunk.model_dump() assert as_dict["error"]["code"] == "500" assert as_dict["error"]["message"] == "Internal Error" print("test_audio_error_field passed.") From 1263270542bf9ac70046e72a26c8018cbe9245a0 Mon Sep 17 00:00:00 2001 From: zhengweijun Date: Thu, 24 Jul 2025 17:10:34 +0800 Subject: [PATCH 3/3] chore: update version to 2.1.5.20250725 in pyproject.toml and __version__.py --- pyproject.toml | 2 +- zhipuai/__version__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6fada2b..17dc36d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "zhipuai" -version = "2.1.5.a1" +version = "2.1.5.20250725" description = "A SDK library for accessing big model apis from ZhipuAI" authors = ["Zhipu AI"] readme = "README.md" diff --git a/zhipuai/__version__.py b/zhipuai/__version__.py index e40c152..593664b 100644 --- a/zhipuai/__version__.py +++ b/zhipuai/__version__.py @@ -1 +1 @@ -__version__ = 'v2.1.5.a1' \ No newline at end of file +__version__ = 'v2.1.5.20250725' \ No newline at end of file