Skip to content

Commit 85530c4

Browse files
author
yuhongxiao
committed
improve code
1 parent 725ac16 commit 85530c4

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

src/zai/api_resource/audio/audio.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import TYPE_CHECKING, Mapping, Optional, cast
44

55
import httpx
6-
from httpx import stream
76

87
from zai.core import (
98
NOT_GIVEN,
@@ -23,8 +22,8 @@
2322
from zai.types.sensitive_word_check import SensitiveWordCheckRequest
2423

2524
from .transcriptions import Transcriptions
26-
from ...core import StreamResponse
27-
from ...types.audio import AudioSpeechChunk
25+
from zai.core._streaming import StreamResponse
26+
from zai.types.audio import AudioSpeechChunk
2827

2928
if TYPE_CHECKING:
3029
from zai._client import ZaiClient
@@ -85,7 +84,6 @@ def speech(
8584
'voice': voice,
8685
'response_format': response_format,
8786
'encode_format': encode_format,
88-
'sensitive_word_check': sensitive_word_check,
8987
'request_id': request_id,
9088
'user_id': user_id,
9189
'speed': speed,
@@ -98,6 +96,8 @@ def speech(
9896
body=maybe_transform(body, AudioSpeechParams),
9997
options=make_request_options(extra_headers=extra_headers, extra_body=extra_body, timeout=timeout),
10098
cast_type=HttpxBinaryResponseContent,
99+
stream=stream or False,
100+
stream_cls=StreamResponse[AudioSpeechChunk]
101101
)
102102

103103
def customization(

src/zai/types/audio/audio_speech_params.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ class AudioSpeechParams(TypedDict, total=False):
2929
sensitive_word_check: Optional[SensitiveWordCheckRequest]
3030
request_id: str
3131
user_id: str
32+
encode_format: str
33+
speed: float
34+
volume: float
35+
stream: bool

tests/integration_tests/test_audio.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
import zai
77
from zai import ZaiClient
88

9-
from src.zai import ZhipuAiClient
9+
from zai import ZhipuAiClient
1010

1111

1212
def test_audio_speech(logging_conf):
1313
logging.config.dictConfig(logging_conf) # type: ignore
14-
client = ZhipuAiClient(base_url='https://open.bigmodel.cn/api/paas/v4', api_key='adf953faf621426da79103110eb41473.3FCVJZcTaq0Q7i3W') # Fill in your own API Key
14+
client = ZhipuAiClient() # Fill in your own API Key
1515
try:
1616
speech_file_path = Path(__file__).parent / 'asr1.pcm'
1717
response = client.audio.speech(
@@ -20,7 +20,7 @@ def test_audio_speech(logging_conf):
2020
voice='female',
2121
response_format='pcm',
2222
encode_format='base64',
23-
stream=False,
23+
stream=True,
2424
speed=1.0,
2525
volume=1.0,
2626
)
@@ -29,9 +29,9 @@ def test_audio_speech(logging_conf):
2929
choice = item.choices[0]
3030
index = choice.index
3131
finish_reason = choice.finish_reason
32-
audio_delta = choice.delta.content
33-
if finish_reason is not None:
32+
if choice.delta is None:
3433
break
34+
audio_delta = choice.delta.content
3535
f.write(base64.b64decode(audio_delta))
3636

3737
except zai.core._errors.APIRequestFailedError as err:

0 commit comments

Comments
 (0)