python“Stream_to_file”中的方法不起作用

Roh*_*rey 5 text-to-speech python-3.x openai-api

使用 OpenAI API 及以下是我的简单代码,但收到“steam_to_file”方法的弃用警告。

代码 -

from openai import OpenAI
from pathlib import Path

client = OpenAI(
  api_key=os.getenv("OPENAI_API_KEY"),
 )

speech_file_path = Path(__file__).parent / "speech.mp3"
response = client.audio.speech.create(
  model="tts-1",
  voice="alloy",
  input= '''I see skies of blue and clouds of white
            The bright blessed days, the dark sacred nights
            And I think to myself
            What a wonderful world
         '''
)
response.stream_to_file(speech_file_path)
Run Code Online (Sandbox Code Playgroud)

IDE——Visual Studio 代码

警告如下 -

** DeprecationWarning:由于错误,此方法实际上并不传输响应内容,.with_streaming_response.method()应使用 response.stream_to_file("song.mp3")**

有人可以帮忙吗?

我尝试检查不同的论坛,但找不到与stream_to_file相关的错误。

我正在使用Python 3.12

小智 -1

我有同样的问题,但这解决了它。删除行
speech_file_path = Path(__file__).parent / "speech.mp3"并写入file_name = "speech.mp3".

response = openai.audio.speech.create(
  model="tts-1",
  voice=shimmer,
  input="The quick brown fox jumped over the lazy dog."
)

file_name = "speech.mp3"
response.stream_to_file(file_name)
Run Code Online (Sandbox Code Playgroud)