Skip to content

Add support for previous text in elevenlabs http processor #1590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
20 changes: 20 additions & 0 deletions src/pipecat/services/elevenlabs/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ class InputParams(BaseModel):
style: Optional[float] = None
use_speaker_boost: Optional[bool] = None
speed: Optional[float] = None
context: Optional[List[dict]] = None
"""Optionally provide a context for previous_text parameter for "context-aware" TTS resulting in more consistant TTS output"""
context_max_previous_text: int = 3
"""The max number of previous assistant messages that will be used for the previous_text parameter"""

def __init__(
self,
Expand Down Expand Up @@ -529,6 +533,22 @@ async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
if self._voice_settings:
payload["voice_settings"] = self._voice_settings

if self._params.context:
# Get the previous assistant messages
previous_assistant_messages = []
if self._params.context is not None:
previous_assistant_messages = [
msg.get("content")
for msg in self._params.context
if msg.get("role") == "assistant" and isinstance(msg.get("content"), str)
]
previous_assistant_messages = previous_assistant_messages[
-self._params.context_max_previous_text :
]

if len(previous_assistant_messages) > 0:
payload["previous_text"] = " ".join(previous_assistant_messages)

language = self._settings["language"]
if self._model_name in ELEVENLABS_MULTILINGUAL_MODELS and language:
payload["language_code"] = language
Expand Down