examples(foundational): support multiple transports

This commit is contained in:
Aleix Conchillo Flaqué
2025-05-24 21:00:24 -07:00
parent ecf878e14d
commit 2cdfaa0a82
128 changed files with 3282 additions and 2716 deletions

View File

@@ -10,6 +10,7 @@ import os
from dotenv import load_dotenv
from loguru import logger
from run import get_transport_client_id, maybe_capture_participant_video
from pipecat.adapters.schemas.function_schema import FunctionSchema
from pipecat.adapters.schemas.tools_schema import ToolsSchema
@@ -23,15 +24,14 @@ from pipecat.services.cartesia.tts import CartesiaTTSService
from pipecat.services.deepgram.stt import DeepgramSTTService
from pipecat.services.google.llm import GoogleLLMService
from pipecat.services.llm_service import FunctionCallParams
from pipecat.transports.base_transport import TransportParams
from pipecat.transports.network.small_webrtc import SmallWebRTCTransport
from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection
from pipecat.transports.base_transport import BaseTransport, TransportParams
from pipecat.transports.services.daily import DailyParams
load_dotenv(override=True)
# Global variable to store the peer connection ID
webrtc_peer_id = ""
# Global variable to store the client ID
client_id = ""
async def get_weather(params: FunctionCallParams):
@@ -42,11 +42,11 @@ async def get_weather(params: FunctionCallParams):
async def get_image(params: FunctionCallParams):
question = params.arguments["question"]
logger.debug(f"Requesting image with user_id={webrtc_peer_id}, question={question}")
logger.debug(f"Requesting image with user_id={client_id}, question={question}")
# Request the image frame
await params.llm.request_image_frame(
user_id=webrtc_peer_id,
user_id=client_id,
function_name=params.function_name,
tool_call_id=params.tool_call_id,
text_content=question,
@@ -61,21 +61,27 @@ async def get_image(params: FunctionCallParams):
)
async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespace):
global webrtc_peer_id
webrtc_peer_id = webrtc_connection.pc_id
# 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 = {
"daily": lambda: DailyParams(
audio_in_enabled=True,
audio_out_enabled=True,
video_in_enabled=True,
vad_analyzer=SileroVADAnalyzer(),
),
"webrtc": lambda: TransportParams(
audio_in_enabled=True,
audio_out_enabled=True,
video_in_enabled=True,
vad_analyzer=SileroVADAnalyzer(),
),
}
logger.info(f"Starting bot with peer_id: {webrtc_peer_id}")
transport = SmallWebRTCTransport(
webrtc_connection=webrtc_connection,
params=TransportParams(
audio_in_enabled=True,
audio_out_enabled=True,
video_in_enabled=True,
vad_analyzer=SileroVADAnalyzer(),
),
)
async def run_example(transport: BaseTransport, _: argparse.Namespace):
logger.info(f"Starting bot")
stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY"))
@@ -167,12 +173,19 @@ indicate you should use the get_image tool are:
@transport.event_handler("on_client_connected")
async def on_client_connected(transport, client):
logger.info(f"Client connected: {client}")
await maybe_capture_participant_video(transport, client)
global client_id
client_id = get_transport_client_id(transport, client)
# Kick off the conversation.
await task.queue_frames([context_aggregator.user().get_context_frame()])
@transport.event_handler("on_client_disconnected")
async def on_client_disconnected(transport, client):
logger.info(f"Client disconnected")
await task.cancel()
@transport.event_handler("on_client_closed")
async def on_client_closed(transport, client):
@@ -187,4 +200,4 @@ indicate you should use the get_image tool are:
if __name__ == "__main__":
from run import main
main()
main(run_example, transport_params=transport_params)