|
| 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.") |
0 commit comments