Skip to content

Commit 9f6c518

Browse files
author
zhengweijun
committed
chore: update version to 2.1.5.a1 and refactor AudioError class to inherit from BaseModel
1 parent b76f044 commit 9f6c518

File tree

4 files changed

+119
-3
lines changed

4 files changed

+119
-3
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.20250724"
3+
version = "2.1.5.a1"
44
description = "A SDK library for accessing big model apis from ZhipuAI"
55
authors = ["Zhipu AI"]
66
readme = "README.md"

tests/unit_tests/test_audio.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import base64
2+
import json
3+
import logging
4+
import logging.config
5+
from pathlib import Path
6+
7+
import zhipuai
8+
from zhipuai import ZhipuAI
9+
10+
11+
def test_audio_speech(logging_conf):
12+
logging.config.dictConfig(logging_conf) # type: ignore
13+
client = ZhipuAI() # 填写您自己的APIKey
14+
try:
15+
speech_file_path = Path(__file__).parent / 'speech.wav'
16+
response = client.audio.speech(
17+
model='cogtts',
18+
input='你好呀,欢迎来到智谱开放平台',
19+
voice='tongtong',
20+
stream=False,
21+
response_format='wav',
22+
)
23+
response.stream_to_file(speech_file_path)
24+
25+
except zhipuai.core._errors.APIRequestFailedError as err:
26+
print(err)
27+
except zhipuai.core._errors.APIInternalError as err:
28+
print(err)
29+
except zhipuai.core._errors.APIStatusError as err:
30+
print(err)
31+
32+
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)
62+
63+
64+
def test_audio_customization(logging_conf):
65+
logging.config.dictConfig(logging_conf)
66+
client = ZhipuAI() # 填写您自己的APIKey
67+
with open(Path(__file__).parent / 'speech.wav', 'rb') as file:
68+
try:
69+
speech_file_path = Path(__file__).parent / 'speech.wav'
70+
response = client.audio.customization(
71+
model='cogtts',
72+
input='你好呀,欢迎来到智谱开放平台',
73+
voice_text='这是一条测试用例',
74+
voice_data=file,
75+
response_format='wav',
76+
)
77+
response.stream_to_file(speech_file_path)
78+
79+
except zhipuai.core._errors.APIRequestFailedError as err:
80+
print(err)
81+
except zhipuai.core._errors.APIInternalError as err:
82+
print(err)
83+
except zhipuai.core._errors.APIStatusError as err:
84+
print(err)
85+
86+
87+
def test_audio_error_field():
88+
from zhipuai.types.audio.audio_speech_chunk import AudioSpeechChunk, AudioError, AudioSpeechChoice, AudioSpeechDelta
89+
90+
# 构造一个 AudioError
91+
error = AudioError(code="500", message="Internal Error")
92+
93+
# 构造一个完整的 AudioSpeechChunk
94+
chunk = AudioSpeechChunk(
95+
choices=[
96+
AudioSpeechChoice(
97+
delta=AudioSpeechDelta(content="audio", role="system"),
98+
finish_reason="error",
99+
index=0
100+
)
101+
],
102+
request_id="req_2",
103+
created=123456,
104+
error=error
105+
)
106+
107+
# 检查 error 字段是否为 AudioError 实例
108+
assert isinstance(chunk.error, AudioError)
109+
assert chunk.error.code == "500"
110+
assert chunk.error.message == "Internal Error"
111+
112+
# 检查序列化
113+
as_dict = chunk.dict()
114+
assert as_dict["error"]["code"] == "500"
115+
assert as_dict["error"]["message"] == "Internal Error"
116+
print("test_audio_error_field passed.")

zhipuai/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = 'v2.1.5.20250724'
1+
__version__ = 'v2.1.5.a1'

zhipuai/types/audio/audio_speech_chunk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class AudioSpeechChoice(BaseModel):
2020
finish_reason: Optional[str] = None
2121
index: int
2222

23-
class AudioError:
23+
class AudioError(BaseModel):
2424
code: Optional[str] = None
2525
message: Optional[str] = None
2626

0 commit comments

Comments
 (0)