OpenAI (Python Package)

Snippets

テキスト読み上げ

from IPython.display import Audio
from openai import OpenAI
import os

os.environ['OPENAI_API_KEY'] = 'xxxxxx'

client = OpenAI()
mp3_file_path = 'out.mp3'

# https://arxiv.org/abs/2210.03629
with client.audio.speech.with_streaming_response.create(
    model='tts-1-hd', voice='nova', speed=0.75,
    input='While large language models have demonstrated impressive performance'
    ' across tasks in language understanding and interactive decision making,'
    ' their abilities for reasoning and acting have primarily been studied as separate topics.'
) as resp:
    resp.stream_to_file(mp3_file_path)
Audio(mp3_file_path)