-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathtts_node_modifications.py
More file actions
76 lines (59 loc) · 2.42 KB
/
tts_node_modifications.py
File metadata and controls
76 lines (59 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
---
title: TTS Node Override
category: pipeline-tts
tags: [pipeline-tts, deepgram, openai, rime]
difficulty: intermediate
description: Shows how to override the default TTS node to do replacements on the output.
demonstrates:
- Using the `tts_node` method to override the default TTS node and add custom logic to do replacements on the output, like replacing "lol" with "<laughs>".
---
"""
import logging
from typing import AsyncIterable
from dotenv import load_dotenv
from livekit.agents import JobContext, JobProcess, AgentServer, cli, Agent, AgentSession, ModelSettings
from livekit.plugins import deepgram, openai, silero, rime
load_dotenv()
logger = logging.getLogger("tts_node")
logger.setLevel(logging.INFO)
class TtsNodeOverrideAgent(Agent):
def __init__(self, vad) -> None:
super().__init__(
instructions="""
You are a helpful assistant communicating through voice.
Feel free to use "lol" in your responses when something is funny.
""",
stt=deepgram.STT(),
llm=openai.LLM(model="gpt-4o"),
tts=rime.TTS(model="arcana"),
vad=vad
)
async def tts_node(self, text: AsyncIterable[str], model_settings: ModelSettings):
"""Modify the TTS output by replacing 'lol' with '<laugh>'."""
async def process_text():
async for chunk in text:
original_chunk = chunk
modified_chunk = chunk.replace("lol", "<laugh>").replace("LOL", "<laugh>")
if original_chunk != modified_chunk:
logger.info(f"TTS original: '{original_chunk}'")
logger.info(f"TTS modified: '{modified_chunk}'")
yield modified_chunk
return Agent.default.tts_node(self, process_text(), model_settings)
async def on_enter(self):
await self.session.say(f"Hi there! Is there anything I can help you with? If you say something funny, I might respond with lol.")
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()
await session.start(
agent=TtsNodeOverrideAgent(vad=ctx.proc.userdata["vad"]),
room=ctx.room
)
await ctx.connect()
if __name__ == "__main__":
cli.run_app(server)