update SmallWebRTCTransport text examples with new run_example

This commit is contained in:
vipyne
2025-05-28 10:16:27 -05:00
parent 575b97ba60
commit 821da723c0
2 changed files with 159 additions and 149 deletions

View File

@@ -7,6 +7,7 @@
import argparse import argparse
import os import os
import aiohttp
from dotenv import load_dotenv from dotenv import load_dotenv
from loguru import logger from loguru import logger
@@ -28,9 +29,8 @@ from pipecat.processors.frameworks.rtvi import (
) )
from pipecat.services.openai import OpenAIContextAggregatorPair from pipecat.services.openai import OpenAIContextAggregatorPair
from pipecat.services.openai.llm import OpenAILLMService from pipecat.services.openai.llm import OpenAILLMService
from pipecat.transports.base_transport import TransportParams from pipecat.transports.base_transport import BaseTransport, TransportParams
from pipecat.transports.network.small_webrtc import SmallWebRTCTransport from pipecat.transports.services.daily import DailyParams
from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection
load_dotenv(override=True) load_dotenv(override=True)
@@ -71,87 +71,92 @@ def create_action_llm_append_to_messages(context_aggregator: OpenAIContextAggreg
return action_llm_append_to_messages return action_llm_append_to_messages
async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespace): # We store functions so objects (e.g. SileroVADAnalyzer) don't get
# instantiated. The function will be called when the desired transport gets
# selected.
transport_params = {
"webrtc": lambda: TransportParams(),
}
async def run_example(transport: BaseTransport, _: argparse.Namespace, handle_sigint: bool):
logger.info(f"Starting bot") logger.info(f"Starting bot")
transport = SmallWebRTCTransport( # Create an HTTP session for API calls
webrtc_connection=webrtc_connection, async with aiohttp.ClientSession() as session:
params=TransportParams(), llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"))
)
llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY")) messages = [
{
messages = [ "role": "system",
{ "content": "You are a helpful LLM in a WebRTC call. Your goal is to demonstrate your capabilities in a succinct way. Respond to what the user said in a creative and helpful way.",
"role": "system", },
"content": "You are a helpful LLM in a WebRTC call. Your goal is to demonstrate your capabilities in a succinct way. Respond to what the user said in a creative and helpful way.",
},
]
context = OpenAILLMContext(messages)
context_aggregator = llm.create_context_aggregator(context)
action_llm_append_to_messages = create_action_llm_append_to_messages(context_aggregator)
rtvi = RTVIProcessor(config=RTVIConfig(config=[]))
rtvi.register_action(action_llm_append_to_messages)
pipeline = Pipeline(
[
transport.input(),
rtvi,
context_aggregator.user(),
llm,
transport.output(),
context_aggregator.assistant(),
] ]
)
task = PipelineTask( context = OpenAILLMContext(messages)
pipeline, context_aggregator = llm.create_context_aggregator(context)
params=PipelineParams(
allow_interruptions=True,
enable_metrics=True,
),
observers=[RTVIObserver(rtvi)],
)
@rtvi.event_handler("on_client_ready") action_llm_append_to_messages = create_action_llm_append_to_messages(context_aggregator)
async def on_client_ready(rtvi): rtvi = RTVIProcessor(config=RTVIConfig(config=[]))
logger.info("Pipecat client ready.") rtvi.register_action(action_llm_append_to_messages)
await rtvi.set_bot_ready()
# This block is frontend UI specific pipeline = Pipeline(
# These messages are intended for small webrtc UI to only handle text [
# https://github.com/pipecat-ai/small-webrtc-prebuilt transport.input(),
messages = { rtvi,
"show_text_container": True, context_aggregator.user(),
"show_video_container": False, llm,
"show_debug_container": False, transport.output(),
} context_aggregator.assistant(),
rtvi_frame = RTVIServerMessageFrame(data=messages) ]
await task.queue_frames([rtvi_frame]) )
@transport.event_handler("on_client_connected") task = PipelineTask(
async def on_client_connected(transport, client): pipeline,
logger.info(f"Client connected: {client}") params=PipelineParams(
# Kick off the conversation. allow_interruptions=True,
await task.queue_frames([context_aggregator.user().get_context_frame()]) enable_metrics=True,
),
observers=[RTVIObserver(rtvi)],
)
@transport.event_handler("on_client_disconnected") @rtvi.event_handler("on_client_ready")
async def on_client_disconnected(transport, client): async def on_client_ready(rtvi):
logger.info(f"Client disconnected") logger.info("Pipecat client ready.")
await rtvi.set_bot_ready()
@transport.event_handler("on_client_closed") # This block is frontend UI specific
async def on_client_closed(transport, client): # These messages are intended for small webrtc UI to only handle text
logger.info(f"Client closed connection") # https://github.com/pipecat-ai/small-webrtc-prebuilt
await task.cancel() messages = {
"show_text_container": True,
"show_video_container": False,
"show_debug_container": False,
}
rtvi_frame = RTVIServerMessageFrame(data=messages)
await task.queue_frames([rtvi_frame])
runner = PipelineRunner(handle_sigint=False) @transport.event_handler("on_client_connected")
async def on_client_connected(transport, client):
logger.info(f"Client connected: {client}")
# Kick off the conversation.
await task.queue_frames([context_aggregator.user().get_context_frame()])
await runner.run(task) @transport.event_handler("on_client_disconnected")
async def on_client_disconnected(transport, client):
logger.info(f"Client disconnected")
@transport.event_handler("on_client_closed")
async def on_client_closed(transport, client):
logger.info(f"Client closed connection")
await task.cancel()
runner = PipelineRunner(handle_sigint=False)
await runner.run(task)
if __name__ == "__main__": if __name__ == "__main__":
from run import main from run import main
main() main(run_example, transport_params=transport_params)

View File

@@ -7,6 +7,7 @@
import argparse import argparse
import os import os
import aiohttp
from dotenv import load_dotenv from dotenv import load_dotenv
from loguru import logger from loguru import logger
@@ -31,9 +32,8 @@ from pipecat.services.cartesia.tts import CartesiaTTSService
from pipecat.services.deepgram.stt import DeepgramSTTService from pipecat.services.deepgram.stt import DeepgramSTTService
from pipecat.services.openai import OpenAIContextAggregatorPair from pipecat.services.openai import OpenAIContextAggregatorPair
from pipecat.services.openai.llm import OpenAILLMService from pipecat.services.openai.llm import OpenAILLMService
from pipecat.transports.base_transport import TransportParams from pipecat.transports.base_transport import BaseTransport, TransportParams
from pipecat.transports.network.small_webrtc import SmallWebRTCTransport from pipecat.transports.services.daily import DailyParams
from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection
load_dotenv(override=True) load_dotenv(override=True)
@@ -77,98 +77,103 @@ def create_action_llm_append_to_messages(context_aggregator: OpenAIContextAggreg
return action_llm_append_to_messages return action_llm_append_to_messages
async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespace): # We store functions so objects (e.g. SileroVADAnalyzer) don't get
# instantiated. The function will be called when the desired transport gets
# selected.
transport_params = {
"webrtc": lambda: TransportParams(
audio_in_enabled=True,
audio_out_enabled=True,
vad_analyzer=SileroVADAnalyzer(),
),
}
async def run_example(transport: BaseTransport, _: argparse.Namespace, handle_sigint: bool):
logger.info(f"Starting bot") logger.info(f"Starting bot")
transport = SmallWebRTCTransport( # Create an HTTP session for API calls
webrtc_connection=webrtc_connection, async with aiohttp.ClientSession() as session:
params=TransportParams( stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY"))
audio_in_enabled=True,
audio_out_enabled=True,
vad_analyzer=SileroVADAnalyzer(),
),
)
stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY")) llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"))
llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY")) tts = CartesiaTTSService(
api_key=os.getenv("CARTESIA_API_KEY"), voice_id="71a7ad14-091c-4e8e-a314-022ece01c121"
)
tts = CartesiaTTSService( messages = [
api_key=os.getenv("CARTESIA_API_KEY"), voice_id="71a7ad14-091c-4e8e-a314-022ece01c121" {
) "role": "system",
"content": "You are a helpful LLM in a WebRTC call. Your goal is to demonstrate your capabilities in a succinct way. Respond to what the user says in a creative and helpful way. Explain to the User they can speak or type text to communicate with you.",
messages = [ },
{
"role": "system",
"content": "You are a helpful LLM in a WebRTC call. Your goal is to demonstrate your capabilities in a succinct way. Respond to what the user says in a creative and helpful way. Explain to the User they can speak or type text to communicate with you.",
},
]
context = OpenAILLMContext(messages)
context_aggregator = llm.create_context_aggregator(context)
action_llm_append_to_messages = create_action_llm_append_to_messages(context_aggregator)
rtvi = RTVIProcessor(config=RTVIConfig(config=[]))
rtvi.register_action(action_llm_append_to_messages)
pipeline = Pipeline(
[
transport.input(),
rtvi,
stt,
context_aggregator.user(),
llm,
tts,
transport.output(),
context_aggregator.assistant(),
] ]
)
task = PipelineTask( context = OpenAILLMContext(messages)
pipeline, context_aggregator = llm.create_context_aggregator(context)
params=PipelineParams(
allow_interruptions=True,
enable_metrics=True,
),
observers=[RTVIObserver(rtvi)],
)
@rtvi.event_handler("on_client_ready") action_llm_append_to_messages = create_action_llm_append_to_messages(context_aggregator)
async def on_client_ready(rtvi): rtvi = RTVIProcessor(config=RTVIConfig(config=[]))
logger.info("Pipecat client ready.") rtvi.register_action(action_llm_append_to_messages)
await rtvi.set_bot_ready()
# This block is frontend UI specific pipeline = Pipeline(
# These messages are intended for small webrtc UI to only handle text [
# https://github.com/pipecat-ai/small-webrtc-prebuilt transport.input(),
messages = { rtvi,
"show_text_container": True, stt,
"show_debug_container": False, context_aggregator.user(),
} llm,
rtvi_frame = RTVIServerMessageFrame(data=messages) tts,
await task.queue_frames([rtvi_frame]) transport.output(),
context_aggregator.assistant(),
]
)
@transport.event_handler("on_client_connected") task = PipelineTask(
async def on_client_connected(transport, client): pipeline,
logger.info(f"Client connected: {client}") params=PipelineParams(
# Kick off the conversation. allow_interruptions=True,
await task.queue_frames([context_aggregator.user().get_context_frame()]) enable_metrics=True,
),
observers=[RTVIObserver(rtvi)],
)
@transport.event_handler("on_client_disconnected") @rtvi.event_handler("on_client_ready")
async def on_client_disconnected(transport, client): async def on_client_ready(rtvi):
logger.info(f"Client disconnected") logger.info("Pipecat client ready.")
await rtvi.set_bot_ready()
@transport.event_handler("on_client_closed") # This block is frontend UI specific
async def on_client_closed(transport, client): # These messages are intended for small webrtc UI to only handle text
logger.info(f"Client closed connection") # https://github.com/pipecat-ai/small-webrtc-prebuilt
await task.cancel() messages = {
"show_text_container": True,
"show_debug_container": False,
}
rtvi_frame = RTVIServerMessageFrame(data=messages)
await task.queue_frames([rtvi_frame])
runner = PipelineRunner(handle_sigint=False) @transport.event_handler("on_client_connected")
async def on_client_connected(transport, client):
logger.info(f"Client connected: {client}")
# Kick off the conversation.
await task.queue_frames([context_aggregator.user().get_context_frame()])
await runner.run(task) @transport.event_handler("on_client_disconnected")
async def on_client_disconnected(transport, client):
logger.info(f"Client disconnected")
@transport.event_handler("on_client_closed")
async def on_client_closed(transport, client):
logger.info(f"Client closed connection")
await task.cancel()
runner = PipelineRunner(handle_sigint=False)
await runner.run(task)
if __name__ == "__main__": if __name__ == "__main__":
from run import main from run import main
main() main(run_example, transport_params=transport_params)