# # Copyright (c) 2024–2025, Daily # # SPDX-License-Identifier: BSD 2-Clause License # import os from datetime import datetime from dotenv import load_dotenv from loguru import logger # import logging from pipecat.adapters.schemas.function_schema import FunctionSchema from pipecat.adapters.schemas.tools_schema import ToolsSchema from pipecat.audio.vad.silero import SileroVADAnalyzer from pipecat.audio.vad.vad_analyzer import VADParams from pipecat.pipeline.pipeline import Pipeline from pipecat.pipeline.runner import PipelineRunner from pipecat.pipeline.task import PipelineParams, PipelineTask from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext from pipecat.services.aws_nova_sonic import AWSNovaSonicLLMService from pipecat.transports.base_transport import TransportParams from pipecat.transports.network.small_webrtc import SmallWebRTCTransport from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection # Load environment variables load_dotenv(override=True) # logging.basicConfig( # level=logging.DEBUG, # format='%(asctime)s - %(levelname)s - %(message)s' # ) async def fetch_weather_from_api(function_name, tool_call_id, args, llm, context, result_callback): temperature = 75 if args["format"] == "fahrenheit" else 24 await result_callback( { "conditions": "nice", "temperature": temperature, "format": args["format"], "timestamp": datetime.now().strftime("%Y%m%d_%H%M%S"), } ) weather_function = FunctionSchema( name="get_current_weather", description="Get the current weather", properties={ "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA", }, "format": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "The temperature unit to use. Infer this from the users location.", }, }, required=["location", "format"], ) # Create tools schema tools = ToolsSchema(standard_tools=[weather_function]) async def run_bot(webrtc_connection: SmallWebRTCConnection): logger.info(f"Starting bot") # Initialize the SmallWebRTCTransport with the connection transport = SmallWebRTCTransport( webrtc_connection=webrtc_connection, params=TransportParams( audio_in_enabled=True, audio_in_sample_rate=16000, audio_out_enabled=True, camera_in_enabled=False, vad_enabled=True, vad_audio_passthrough=True, vad_analyzer=SileroVADAnalyzer(params=VADParams(stop_secs=0.8)), ), ) # Specify initial system instruction # TODO: looks like Nova Sonic can't handle new lines? system_instruction = ( "You are a friendly assistant. The user and you will engage in a spoken dialog " "exchanging the transcripts of a natural real-time conversation. Keep your responses short, " "generally two or three sentences for chatty scenarios." ) # Create the AWS Nova Sonic LLM service llm = AWSNovaSonicLLMService( secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"), access_key_id=os.getenv("AWS_ACCESS_KEY_ID"), region=os.getenv("AWS_REGION"), voice_id="tiffany", # matthew, tiffany, amy # you could choose to pass instruction here rather than via context # instruction=system_instruction # you could choose to pass tools here rather than via context # tools=tools ) # Register function for function calls # you can either register a single function for all function calls, or specific functions # llm.register_function(None, fetch_weather_from_api) llm.register_function("get_current_weather", fetch_weather_from_api) # Set up context and context management. # AWSNovaSonicService will adapt OpenAI LLM context objects with standard message format to # what's expected by Nova Sonic. # TODO: since we can't trigger a response upon joining, this isn't particularly useful context = OpenAILLMContext( messages=[ {"role": "system", "content": f"{system_instruction}"}, { "role": "user", "content": "Say hello!", }, ], tools=tools, ) context_aggregator = llm.create_context_aggregator(context) # Build the pipeline pipeline = Pipeline( [ transport.input(), context_aggregator.user(), llm, transport.output(), context_aggregator.assistant(), ] ) # Configure the pipeline task task = PipelineTask( pipeline, params=PipelineParams( allow_interruptions=True, enable_metrics=True, enable_usage_metrics=True, ), ) # Handle client connection event @transport.event_handler("on_client_connected") async def on_client_connected(transport, client): logger.info(f"Client connected") # Kick off the conversation. await task.queue_frames([context_aggregator.user().get_context_frame()]) # Handle client disconnection events @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() # Run the pipeline runner = PipelineRunner(handle_sigint=False) await runner.run(task) if __name__ == "__main__": from run import main main()