-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: add Telnyx STT and TTS plugins #4665
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
Open
fmv1992
wants to merge
2
commits into
livekit:main
Choose a base branch
from
team-telnyx:add_telnyx_vendor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+731
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| """ | ||
| Telnyx Voice Agent Example. | ||
|
|
||
| This example demonstrates a voice agent using: | ||
| - Telnyx STT (Speech-to-Text) | ||
| - Telnyx TTS (Text-to-Speech) | ||
| - OpenAI LLM (GPT-4.1-mini) | ||
|
|
||
| Usage: | ||
| export TELNYX_API_KEY='your_telnyx_api_key' | ||
| export OPENAI_API_KEY='your_openai_api_key' | ||
| export LIVEKIT_URL='wss://your-livekit-server' | ||
| export LIVEKIT_API_KEY='your_livekit_api_key' | ||
| export LIVEKIT_API_SECRET='your_livekit_api_secret' | ||
|
|
||
| python telnyx_voice_agent.py dev # Development mode with hot reload | ||
| python telnyx_voice_agent.py console # Terminal mode (no server needed) | ||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| from livekit.agents import ( | ||
| Agent, | ||
| AgentServer, | ||
| AgentSession, | ||
| JobContext, | ||
| JobProcess, | ||
| RunContext, | ||
| cli, | ||
| metrics, | ||
| room_io, | ||
| ) | ||
| from livekit.agents.llm import function_tool | ||
| from livekit.plugins import openai, silero, telnyx | ||
|
|
||
| logger = logging.getLogger("telnyx-voice-agent") | ||
|
|
||
| load_dotenv() | ||
|
|
||
|
|
||
| class TelnyxVoiceAgent(Agent): | ||
| def __init__(self) -> None: | ||
| super().__init__( | ||
| instructions=( | ||
| "You are a helpful voice assistant powered by Telnyx. " | ||
| "Keep your responses concise and conversational. " | ||
| "Do not use emojis, asterisks, or markdown in your responses. " | ||
| "You are friendly and professional." | ||
| ), | ||
| ) | ||
|
|
||
| async def on_enter(self): | ||
| self.session.generate_reply(allow_interruptions=False) | ||
|
|
||
| @function_tool | ||
| async def get_current_time(self, context: RunContext): | ||
| """Called when the user asks for the current time.""" | ||
| import datetime | ||
|
|
||
| now = datetime.datetime.now() | ||
| return f"The current time is {now.strftime('%I:%M %p')}." | ||
|
|
||
| @function_tool | ||
| async def lookup_weather(self, context: RunContext, location: str): | ||
| """Called when the user asks about the weather. | ||
|
|
||
| Args: | ||
| location: The city or location to get weather for. | ||
| """ | ||
| logger.info(f"Looking up weather for {location}") | ||
| return f"The weather in {location} is sunny with a temperature of 72 degrees Fahrenheit." | ||
|
|
||
|
|
||
| server = AgentServer() | ||
|
|
||
|
|
||
| def prewarm(proc: JobProcess): | ||
| proc.userdata["vad"] = silero.VAD.load() | ||
|
|
||
|
|
||
| server.setup_fnc = prewarm | ||
|
|
||
|
|
||
| @server.rtc_session() | ||
| async def entrypoint(ctx: JobContext): | ||
| ctx.log_context_fields = { | ||
| "room": ctx.room.name, | ||
| } | ||
|
|
||
| session = AgentSession( | ||
| stt=telnyx.STT( | ||
| language="en", | ||
| transcription_engine="telnyx", | ||
| ), | ||
| llm=openai.LLM(model="gpt-4.1-mini"), | ||
| tts=telnyx.TTS( | ||
| voice="Telnyx.NaturalHD.astra", | ||
| ), | ||
| vad=ctx.proc.userdata["vad"], | ||
| ) | ||
|
|
||
| usage_collector = metrics.UsageCollector() | ||
|
|
||
| @session.on("metrics_collected") | ||
| def _on_metrics_collected(ev): | ||
| metrics.log_metrics(ev.metrics) | ||
| usage_collector.collect(ev.metrics) | ||
|
|
||
| async def log_usage(): | ||
| summary = usage_collector.get_summary() | ||
| logger.info(f"Usage: {summary}") | ||
|
|
||
| ctx.add_shutdown_callback(log_usage) | ||
|
|
||
| await session.start( | ||
| agent=TelnyxVoiceAgent(), | ||
| room=ctx.room, | ||
| room_options=room_io.RoomOptions(), | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| cli.run_app(server) |
18 changes: 18 additions & 0 deletions
18
livekit-plugins/livekit-plugins-telnyx/livekit/plugins/telnyx/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| from .stt import STT | ||
| from .tts import TTS | ||
| from .version import __version__ | ||
|
|
||
| __all__ = ["STT", "TTS", "__version__"] | ||
|
|
||
|
|
||
| from livekit.agents import Plugin | ||
|
|
||
| from .log import logger | ||
|
|
||
|
|
||
| class TelnyxPlugin(Plugin): | ||
| def __init__(self) -> None: | ||
| super().__init__(__name__, __version__, __package__, logger) | ||
|
|
||
|
|
||
| Plugin.register_plugin(TelnyxPlugin()) | ||
36 changes: 36 additions & 0 deletions
36
livekit-plugins/livekit-plugins-telnyx/livekit/plugins/telnyx/common.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
|
|
||
| import aiohttp | ||
|
|
||
| API_BASE_URL = "wss://api.telnyx.com/v2" | ||
| TTS_ENDPOINT = f"{API_BASE_URL}/text-to-speech/speech" | ||
| STT_ENDPOINT = f"{API_BASE_URL}/speech-to-text/transcription" | ||
|
|
||
| SAMPLE_RATE = 16000 | ||
| NUM_CHANNELS = 1 | ||
|
|
||
|
|
||
| def get_api_key(api_key: str | None = None) -> str: | ||
| resolved_key = api_key or os.environ.get("TELNYX_API_KEY") | ||
| if not resolved_key: | ||
| raise ValueError("Telnyx API key required. Set TELNYX_API_KEY or provide api_key.") | ||
| return resolved_key | ||
|
|
||
|
|
||
| class SessionManager: | ||
| def __init__(self, http_session: aiohttp.ClientSession | None = None) -> None: | ||
| self._session = http_session | ||
| self._owns_session = False | ||
|
|
||
| def ensure_session(self) -> aiohttp.ClientSession: | ||
| if not self._session: | ||
| self._session = aiohttp.ClientSession() | ||
| self._owns_session = True | ||
| return self._session | ||
|
|
||
| async def close(self) -> None: | ||
| if self._owns_session and self._session: | ||
| await self._session.close() | ||
| self._session = None |
3 changes: 3 additions & 0 deletions
3
livekit-plugins/livekit-plugins-telnyx/livekit/plugins/telnyx/log.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import logging | ||
|
|
||
| logger = logging.getLogger("livekit.plugins.telnyx") |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: livekit/agents
Length of output: 3507
🏁 Script executed:
Repository: livekit/agents
Length of output: 3205
🏁 Script executed:
Repository: livekit/agents
Length of output: 475
Add plugin registration following the established pattern.
The telnyx plugin is missing the standard plugin registration that all other plugins implement. Based on the consistent pattern in deepgram and ultravox, add the following after the
__all__definition:Plugin registration pattern
This ensures proper plugin registration with the LiveKit agents framework, consistent with the plugin system design.
🤖 Prompt for AI Agents