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-
871def test_audio_error_field ():
882 from zhipuai .types .audio .audio_speech_chunk import AudioSpeechChunk , AudioError , AudioSpeechChoice , AudioSpeechDelta
893
@@ -110,7 +24,7 @@ def test_audio_error_field():
11024 assert chunk .error .message == "Internal Error"
11125
11226 # 检查序列化
113- as_dict = chunk .dict ()
27+ as_dict = chunk .model_dump ()
11428 assert as_dict ["error" ]["code" ] == "500"
11529 assert as_dict ["error" ]["message" ] == "Internal Error"
11630 print ("test_audio_error_field passed." )
0 commit comments