複数リクエストの連結

このガイドでは、複数のテキストチャンク/生成にわたって音声のプロソディを維持する方法を紹介します。

ハウツーガイド · ElevenAPI クイックスタートを完了していることを前提としています。

大量のテキストをオーディオに変換すると、チャンクごとにプロソディが急激に変化することがあります。これは、複数の段落やセクションにまたがるテキストを変換する場合に特に顕著です。複数のチャンクにわたって音声のプロソディを維持するには、リクエストスティッチング機能を使用します。

この機能では、すでに生成された内容と今後生成される内容のコンテキストを指定できるため、テキスト全体で一貫した音声とプロソディを維持できます。

リクエストスティッチングはeleven_v3モデルでは利用できません。

以下は、リクエストスティッチングを使用しない例です。

次は、リクエストスティッチングを使用した同じ例です。

リクエストスティッチングの使い方

リクエストスティッチングは、ElevenLabs SDKを使用すると簡単に利用できます。

このガイドは、APIキーとSDKを設定済みであることを前提としています。まだの場合は、先にクイックスタートを完了してください。

1

複数のリクエストをつなげる

使用する言語に応じて、example.pyまたはexample.mtsという新しいファイルを作成し、次のコードを追加します。

import os
from io import BytesIO
from elevenlabs.client import ElevenLabs
from elevenlabs.play import play
from dotenv import load_dotenv
load_dotenv()
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
elevenlabs = ElevenLabs(
api_key=ELEVENLABS_API_KEY,
)
paragraphs = [
"The advent of technology has transformed countless sectors, with education ",
"standing out as one of the most significantly impacted fields.",
"In recent years, educational technology, or EdTech, has revolutionized the way ",
"teachers deliver instruction and students absorb information.",
"From interactive whiteboards to individual tablets loaded with educational software, ",
"technology has opened up new avenues for learning that were previously unimaginable.",
"One of the primary benefits of technology in education is the accessibility it provides.",
]
request_ids = []
audio_buffers = []
for paragraph in paragraphs:
# Usually we get back a stream from the convert function, but with_raw_response is
# used to get the headers from the response
with elevenlabs.text_to_speech.with_raw_response.convert(
text=paragraph,
voice_id="T7QGPtToiqH4S8VlIkMJ",
model_id="eleven_v4",
previous_request_ids=request_ids
) as response:
request_ids.append(response._response.headers.get("request-id"))
# response._response.headers also contains useful information like 'character-cost',
# which shows the cost of the generation in characters.
audio_data = b''.join(chunk for chunk in response.data)
audio_buffers.append(BytesIO(audio_data))
combined_stream = BytesIO(b''.join(buffer.getvalue() for buffer in audio_buffers))
play(combined_stream)
2

コードを実行する

python example.py

結合・スティッチされたオーディオが再生されます。

よくある質問

前のリクエストのリクエストIDをコンディショニングに使用するには、そのリクエストが完全に処理されている必要があります。ストリーミングの場合、レスポンス本文からオーディオを最後まで読み取る必要があります。

違いは、使用するモデル、音声、音声設定によって異なります。

リクエストIDは2時間以内のものである必要があります。

はい。ただし、プライバシー要件がより厳しいエンタープライズユーザーは除きます。

次のステップ