Skip to content

Commit d1bf260

Browse files
iceAndFireisFailedyuhongxiao
andauthored
feat: 20250801 tts mp3 (#101)
Co-authored-by: yuhongxiao <[email protected]>
1 parent fa496db commit d1bf260

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "zhipuai"
3-
version = "2.1.5.20250726"
3+
version = "2.1.5.20250801"
44
description = "A SDK library for accessing big model apis from ZhipuAI"
55
authors = ["Zhipu AI"]
66
readme = "README.md"

tests/integration_tests/test_audio.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,35 @@ def test_audio_speech(logging_conf):
3030
print(err)
3131

3232
def test_audio_speech_streaming(logging_conf):
33-
logging.config.dictConfig(logging_conf) # type: ignore
34-
client = ZhipuAI() # 填写您自己的APIKey
35-
try:
36-
response = client.audio.speech(
37-
model='cogtts',
38-
input='你好呀,欢迎来到智谱开放平台',
39-
voice='tongtong',
40-
stream=True,
41-
response_format='wav',
42-
)
43-
with open("output.pcm", "wb") as f:
44-
for item in response:
45-
choice = item.choices[0]
46-
index = choice.index
47-
finish_reason = choice.finish_reason
48-
audio_delta = choice.delta.content
49-
if finish_reason is not None:
50-
break
51-
f.write(base64.b64decode(audio_delta))
52-
print(f"{index}.finish_reason = {finish_reason}, audio_delta = {len(audio_delta)}")
53-
54-
except zhipuai.core._errors.APIRequestFailedError as err:
55-
print(err)
56-
except zhipuai.core._errors.APIInternalError as err:
57-
print(err)
58-
except zhipuai.core._errors.APIStatusError as err:
59-
print(err)
60-
except Exception as e:
61-
print(e)
33+
logging.config.dictConfig(logging_conf) # type: ignore
34+
client = ZhipuAI() # 填写您自己的APIKey
35+
try:
36+
response = client.audio.speech(
37+
model='cogtts',
38+
input='你好呀,欢迎来到智谱开放平台',
39+
voice='tongtong',
40+
stream=True,
41+
response_format='mp3',
42+
encode_format='hex'
43+
)
44+
with open("output.mp3", "wb") as f:
45+
for item in response:
46+
choice = item.choices[0]
47+
index = choice.index
48+
finish_reason = choice.finish_reason
49+
audio_delta = choice.delta.content
50+
if finish_reason is not None:
51+
break
52+
f.write(bytes.fromhex(audio_delta))
53+
print(f"audio delta: {audio_delta[:64]}..., 长度:{len(audio_delta)}")
54+
except zhipuai.core._errors.APIRequestFailedError as err:
55+
print(err)
56+
except zhipuai.core._errors.APIInternalError as err:
57+
print(err)
58+
except zhipuai.core._errors.APIStatusError as err:
59+
print(err)
60+
except Exception as e:
61+
print(e)
6262

6363

6464
def test_audio_customization(logging_conf):

zhipuai/api_resource/audio/audio.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def speech(
5151
extra_headers: Headers | None = None,
5252
extra_body: Body | None = None,
5353
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
54+
encode_format: str,
5455
) -> _legacy_response.HttpxBinaryResponseContent | StreamResponse[AudioSpeechChunk]:
5556
body = deepcopy_minimal(
5657
{
@@ -61,7 +62,8 @@ def speech(
6162
"response_format": response_format,
6263
"sensitive_word_check": sensitive_word_check,
6364
"request_id": request_id,
64-
"user_id": user_id
65+
"user_id": user_id,
66+
"encode_format": encode_format
6567
}
6668
)
6769
return self._post(

0 commit comments

Comments
 (0)