diff --git a/pyproject.toml b/pyproject.toml index b593b81..492c99e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "zhipuai" -version = "2.1.5.20250726" +version = "2.1.5.20250801" description = "A SDK library for accessing big model apis from ZhipuAI" authors = ["Zhipu AI"] readme = "README.md" diff --git a/tests/integration_tests/test_audio.py b/tests/integration_tests/test_audio.py index 2ef05d6..8140a66 100644 --- a/tests/integration_tests/test_audio.py +++ b/tests/integration_tests/test_audio.py @@ -30,35 +30,35 @@ def test_audio_speech(logging_conf): 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) + logging.config.dictConfig(logging_conf) # type: ignore + client = ZhipuAI() # 填写您自己的APIKey + try: + response = client.audio.speech( + model='cogtts', + input='你好呀,欢迎来到智谱开放平台', + voice='tongtong', + stream=True, + response_format='mp3', + encode_format='hex' + ) + with open("output.mp3", "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(bytes.fromhex(audio_delta)) + print(f"audio delta: {audio_delta[:64]}..., 长度:{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): diff --git a/zhipuai/api_resource/audio/audio.py b/zhipuai/api_resource/audio/audio.py index d916048..4181b2b 100644 --- a/zhipuai/api_resource/audio/audio.py +++ b/zhipuai/api_resource/audio/audio.py @@ -51,6 +51,7 @@ def speech( extra_headers: Headers | None = None, extra_body: Body | None = None, timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, + encode_format: str, ) -> _legacy_response.HttpxBinaryResponseContent | StreamResponse[AudioSpeechChunk]: body = deepcopy_minimal( { @@ -61,7 +62,8 @@ def speech( "response_format": response_format, "sensitive_word_check": sensitive_word_check, "request_id": request_id, - "user_id": user_id + "user_id": user_id, + "encode_format": encode_format } ) return self._post(