使用 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)
IDE - Visual Studio 代码
警告如下 -
** DeprecationWarning:由于错误,此方法实际上并不传输响应内容,应使用
.with_streaming_response.method()
response.stream_to_file("歌曲.mp3")**
有人可以帮忙吗?
我尝试检查不同的论坛,但找不到与stream_to_file相关的错误。
我正在使用Python 3.12
我遇到了同样的问题,但这解决了它。删除行
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)