Skip to content

Commit

Permalink
Include chat handling with minimal_assistant (livekit#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidzhao authored Aug 3, 2024
1 parent 4e346c0 commit 9a72ccc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
8 changes: 2 additions & 6 deletions examples/speech-to-text/deepgram_stt.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,14 @@ async def _forward_transcription(
async def entrypoint(ctx: JobContext):
logger.info("starting speech-to-text example")
stt = deepgram.STT()
tasks = []

async def transcribe_track(participant: rtc.RemoteParticipant, track: rtc.Track):
audio_stream = rtc.AudioStream(track)
stt_forwarder = transcription.STTSegmentsForwarder(
room=ctx.room, participant=participant, track=track
)
stt_stream = stt.stream()
stt_task = asyncio.create_task(
_forward_transcription(stt_stream, stt_forwarder)
)
tasks.append(stt_task)
asyncio.create_task(_forward_transcription(stt_stream, stt_forwarder))

async for ev in audio_stream:
stt_stream.push_frame(ev.frame)
Expand All @@ -57,7 +53,7 @@ def on_track_subscribed(
participant: rtc.RemoteParticipant,
):
if track.kind == rtc.TrackKind.KIND_AUDIO:
tasks.append(asyncio.create_task(transcribe_track(participant, track)))
asyncio.create_task(transcribe_track(participant, track))


if __name__ == "__main__":
Expand Down
19 changes: 18 additions & 1 deletion examples/voice-assistant/minimal_assistant.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio

from dotenv import load_dotenv
from livekit import rtc
from livekit.agents import AutoSubscribe, JobContext, WorkerOptions, cli, llm
from livekit.agents.voice_assistant import VoiceAssistant
from livekit.plugins import deepgram, openai, silero
Expand All @@ -19,15 +20,31 @@ async def entrypoint(ctx: JobContext):

await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)

llm_plugin = openai.LLM()
assistant = VoiceAssistant(
vad=silero.VAD.load(),
stt=deepgram.STT(),
llm=openai.LLM(),
llm=llm_plugin,
tts=openai.TTS(),
chat_ctx=initial_ctx,
)
assistant.start(ctx.room)

# listen to incoming chat messages, only required if you'd like the agent to
# answer incoming messages from Chat
chat = rtc.ChatManager(ctx.room)

async def answer_from_text(txt: str):
chat_ctx = assistant.chat_ctx.copy()
chat_ctx.append(role="user", text=txt)
stream = llm_plugin.chat(chat_ctx=chat_ctx)
await assistant.say(stream)

@chat.on("message_received")
def on_chat_received(msg: rtc.ChatMessage):
if msg.message:
asyncio.create_task(answer_from_text(msg.message))

await asyncio.sleep(1)
await assistant.say("Hey, how can I help you today?", allow_interruptions=True)

Expand Down

0 comments on commit 9a72ccc

Please sign in to comment.