
audio_string = "" for chunk in completion: if chunk.choices: if hasattr(chunk.choices[0].delta, "audio"): try: audio_string += chunk.choices[0].delta.audio["data"] except Exception as e: print(chunk.choices[0].delta.audio["transcript"]) else: print(chunk.usage) wav_bytes = base64.b64decode(audio_string) audio_np = np.frombuffer(wav_bytes, dtype=np.int16) sf.write("audio_assistant_py.wav", audio_np, samplerate=24000) 第 7 、8 行:为什么把 print(chunk.choices[0].delta.audio["transcript"]) 放在 Exception 后面输出?
1 lisisi OP ``` if "data" in chunk.choices[0].delta.audio: audio_string += chunk.choices[0].delta.audio["data"] elif "transcript" in chunk.choices[0].delta.audio: print(chunk.choices[0].delta.audio["transcript"]) ``` |
2 NoOneNoBody 163 天前 except 内的 print 只是取代原有的出错信息,让代码可以继续运行,意义不大,有些人甚至只给个 pass 如果这段代码是别人写的,这个 print 只是随便打个断点信息,你可以改成你能理解的,或者输出到 log 方便以后查找 |
3 guanzhangzhang 163 天前 可能是偷懒选择一个随便思考的解决方式 |