角色陪伴
智能导游
英语学习
在线教育
# 安装最新版本 pip install zai-sdk # 或指定版本 pip install zai-sdk==0.0.1
import zai print(zai.__version__)
import wave import base64 from zai import ZhipuAiClient def save_audio_as_wav(audio_data, filepath): """保存音频数据为WAV文件(模型返回的语音用)""" with wave.open(filepath, 'wb') as wav_file: wav_file.setnchannels(1) wav_file.setsampwidth(2) wav_file.setframerate(44100) wav_file.writeframes(audio_data) print(f"Audio saved to {filepath}") def get_base64_from_wav(wav_path): """将WAV文件转为Base64编码字符串""" with open(wav_path, "rb") as f: audio_bytes = f.read() return base64.b64encode(audio_bytes).decode("utf-8") client = ZhipuAiClient(api_key="your_api_key") # 请填写您自己的APIKey input_wav_path = "your_voice.wav" # 你的WAV文件路径 base64_voice = get_base64_from_wav(input_wav_path) response = client.chat.completions.create( model="glm-4-voice", messages=[ { "role": "user", "content": [ { "type": "text", "text": "你好,这是我的语音输入测试,请慢速复述一遍" }, { "type": "input_audio", "input_audio": { "data": base64_voice, "format": "wav" } } ] } ], stream=False ) print(response.choices[0].message.content) # 解析并保存模型返回的语音 try: audio_data = response.choices[0].message.audio['data'] decoded_data = base64.b64decode(audio_data) save_audio_as_wav(decoded_data, "output.wav") except Exception as e: print("处理音频失败:", e)
Was this page helpful?