Skip to content

Commit 538fe6e

Browse files
author
yuhongxiao
committed
新增音频合成流式返回
1 parent c795403 commit 538fe6e

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ build-backend = "poetry.core.masonry.api"
8484
#
8585
# https://github.com/tophat/syrupy
8686
# --snapshot-warn-unused Prints a warning on unused snapshots rather than fail the test suite.
87-
addopts = "--strict-markers --strict-config --durations=5 --snapshot-warn-unused -svv"
87+
addopts = "--strict-markers --strict-config --durations=5 -svv"
8888
# Registering custom markers.
8989
# https://docs.pytest.org/en/7.1.x/example/markers.html#registering-markers
9090
markers = [
9191
"requires: mark tests as requiring a specific library",
9292
"scheduled: mark tests to run in scheduled testing",
9393
"compile: mark placeholder test used to compile integration tests without running them"
9494
]
95-
asyncio_mode = "auto"
95+
# asyncio_mode = "auto"
9696

9797

9898
# https://python-poetry.org/docs/repositories/

tests/integration_tests/test_audio.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_audio_speech(logging_conf):
1414
response = client.audio.speech(
1515
model='cogtts',
1616
input='你好呀,欢迎来到智谱开放平台',
17-
voice='female',
17+
voice='tongtong',
1818
stream=False,
1919
response_format='wav',
2020
)
@@ -27,6 +27,27 @@ def test_audio_speech(logging_conf):
2727
except zhipuai.core._errors.APIStatusError as err:
2828
print(err)
2929

30+
def test_audio_speech_streaming(logging_conf):
31+
logging.config.dictConfig(logging_conf) # type: ignore
32+
client = ZhipuAI() # 填写您自己的APIKey
33+
try:
34+
response = client.audio.speech(
35+
model='cogtts',
36+
input='你好呀,欢迎来到智谱开放平台',
37+
voice='tongtong',
38+
stream=True,
39+
response_format='wav',
40+
)
41+
for item in response:
42+
print(item)
43+
44+
except zhipuai.core._errors.APIRequestFailedError as err:
45+
print(err)
46+
except zhipuai.core._errors.APIInternalError as err:
47+
print(err)
48+
except zhipuai.core._errors.APIStatusError as err:
49+
print(err)
50+
3051

3152
def test_audio_customization(logging_conf):
3253
logging.config.dictConfig(logging_conf)

zhipuai/api_resource/audio/audio.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from zhipuai.types.audio import AudioSpeechParams
1010
from ...types.audio import audio_customization_param
1111

12-
from zhipuai.core import BaseAPI, maybe_transform
12+
from zhipuai.core import BaseAPI, maybe_transform, StreamResponse
1313
from zhipuai.core import NOT_GIVEN, Body, Headers, NotGiven, FileTypes
1414
from zhipuai.core import _legacy_response
1515

@@ -20,6 +20,7 @@
2020
make_request_options,
2121
)
2222
from zhipuai.core import deepcopy_minimal
23+
from ...types.audio.audio_speech_chunk import AudioSpeechChunk
2324

2425
if TYPE_CHECKING:
2526
from zhipuai._client import ZhipuAI
@@ -50,7 +51,7 @@ def speech(
5051
extra_headers: Headers | None = None,
5152
extra_body: Body | None = None,
5253
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
53-
) -> _legacy_response.HttpxBinaryResponseContent:
54+
) -> _legacy_response.HttpxBinaryResponseContent | StreamResponse[AudioSpeechChunk]:
5455
body = deepcopy_minimal(
5556
{
5657
"model": model,
@@ -65,11 +66,13 @@ def speech(
6566
)
6667
return self._post(
6768
"/audio/speech",
68-
body=maybe_transform(body, AudioSpeechParams),
69+
body=body,
6970
options=make_request_options(
7071
extra_headers=extra_headers, extra_body=extra_body, timeout=timeout
7172
),
72-
cast_type=_legacy_response.HttpxBinaryResponseContent
73+
cast_type=_legacy_response.HttpxBinaryResponseContent,
74+
stream= stream or False,
75+
stream_cls=StreamResponse[AudioSpeechChunk]
7376
)
7477

7578
def customization(
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import List, Optional, Dict, Any
2+
3+
from ...core import BaseModel
4+
5+
__all__ = [
6+
"AudioSpeechChunk",
7+
"AudioError",
8+
"AudioSpeechChoice",
9+
"AudioSpeechDelta"
10+
]
11+
12+
13+
class AudioSpeechDelta(BaseModel):
14+
content: Optional[str] = None
15+
role: Optional[str] = None
16+
17+
18+
class AudioSpeechChoice(BaseModel):
19+
delta: AudioSpeechDelta
20+
finish_reason: Optional[str] = None
21+
index: int
22+
23+
class AudioError:
24+
code: Optional[str] = None
25+
message: Optional[str] = None
26+
27+
28+
class AudioSpeechChunk(BaseModel):
29+
choices: List[AudioSpeechChoice]
30+
request_id: Optional[str] = None
31+
created: Optional[int] = None
32+
error: Optional[AudioError] = None

0 commit comments

Comments
 (0)