-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplaying_audio.py
66 lines (52 loc) · 2.04 KB
/
playing_audio.py
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
import logging
from pathlib import Path
import wave
from dotenv import load_dotenv
from livekit.agents import JobContext, WorkerOptions, cli
from livekit.agents.llm import function_tool
from livekit.agents.voice import Agent, AgentSession, RunContext
from livekit.plugins import deepgram, openai, silero
from livekit import rtc
logger = logging.getLogger("function-calling")
logger.setLevel(logging.INFO)
load_dotenv(dotenv_path=Path(__file__).parent.parent / '.env')
class FunctionAgent(Agent):
def __init__(self) -> None:
super().__init__(
instructions="""
You are a helpful assistant communicating through voice. Don't use any unpronouncable characters.
If asked to play audio, use the `play_audio_file` function.
""",
stt=deepgram.STT(),
llm=openai.LLM(model="gpt-4o"),
tts=openai.TTS(),
vad=silero.VAD.load()
)
@function_tool
async def play_audio_file(self, context: RunContext):
audio_path = Path(__file__).parent / "audio.wav"
with wave.open(str(audio_path), 'rb') as wav_file:
num_channels = wav_file.getnchannels()
sample_rate = wav_file.getframerate()
frames = wav_file.readframes(wav_file.getnframes())
audio_frame = rtc.AudioFrame(
data=frames,
sample_rate=sample_rate,
num_channels=num_channels,
samples_per_channel=wav_file.getnframes()
)
async def audio_generator():
yield audio_frame
await self.session.say("Playing audio file", audio=audio_generator())
return None, "I've played the audio file for you."
async def on_enter(self):
self.session.generate_reply()
async def entrypoint(ctx: JobContext):
await ctx.connect()
session = AgentSession()
await session.start(
agent=FunctionAgent(),
room=ctx.room
)
if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))