# # Copyright (c) 2024–2025, Daily # # SPDX-License-Identifier: BSD 2-Clause License # import argparse import os from dotenv import load_dotenv from loguru import logger from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter from pipecat.adapters.schemas.function_schema import FunctionSchema from pipecat.adapters.schemas.tools_schema import AdapterType, ToolsSchema from pipecat.audio.vad.silero import SileroVADAnalyzer from pipecat.frames.frames import TTSSpeakFrame 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.cartesia.tts import CartesiaTTSService from pipecat.services.deepgram.stt import DeepgramSTTService from pipecat.services.gemini_multimodal_live.gemini import GeminiMultimodalLiveLLMService from pipecat.services.llm_service import FunctionCallParams from pipecat.services.openai.llm import OpenAILLMService from pipecat.transports.base_transport import BaseTransport, TransportParams from pipecat.transports.services.daily import DailyParams from pipecat.utils.time import time_now_iso8601 from pipecat.utils.tracing.setup import setup_tracing load_dotenv(override=True) IS_TRACING_ENABLED = bool(os.getenv("ENABLE_TRACING")) # Initialize tracing if enabled if IS_TRACING_ENABLED: # Create the exporter otlp_exporter = OTLPSpanExporter() # Set up tracing with the exporter setup_tracing( service_name="pipecat-demo", exporter=otlp_exporter, console_export=bool(os.getenv("OTEL_CONSOLE_EXPORT")), ) logger.info("OpenTelemetry tracing initialized") # 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, vad_analyzer=SileroVADAnalyzer(), ), "twilio": lambda: TransportParams( audio_in_enabled=True, audio_out_enabled=True, vad_analyzer=SileroVADAnalyzer(), ), "webrtc": lambda: TransportParams( audio_in_enabled=True, audio_out_enabled=True, vad_analyzer=SileroVADAnalyzer(), ), } async def fetch_weather_from_api(params: FunctionCallParams): temperature = 75 if params.arguments["format"] == "fahrenheit" else 24 await params.result_callback( { "conditions": "nice", "temperature": temperature, "format": params.arguments["format"], "timestamp": time_now_iso8601(), } ) system_instruction = """ You are a helpful assistant who can answer questions and use tools. You have a tool called "get_current_weather" that can be used to get the current weather. If the user asks for the weather, call this function. """ async def run_example(transport: BaseTransport, _: argparse.Namespace, handle_sigint: bool): logger.info(f"Starting bot") 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 user's location.", }, }, required=["location", "format"], ) search_tool = {"google_search": {}} tools = ToolsSchema( standard_tools=[weather_function], custom_tools={AdapterType.GEMINI: [search_tool]} ) llm = GeminiMultimodalLiveLLMService( api_key=os.getenv("GOOGLE_API_KEY"), system_instruction=system_instruction, tools=tools, ) llm.register_function("get_current_weather", fetch_weather_from_api) context = OpenAILLMContext( [{"role": "user", "content": "Say hello."}], ) context_aggregator = llm.create_context_aggregator(context) pipeline = Pipeline( [ transport.input(), context_aggregator.user(), llm, transport.output(), context_aggregator.assistant(), ] ) task = PipelineTask( pipeline, params=PipelineParams( allow_interruptions=True, enable_metrics=True, enable_usage_metrics=True, ), enable_tracing=IS_TRACING_ENABLED, # Optionally, add a conversation ID to track the conversation # conversation_id="8df26cc1-6db0-4a7a-9930-1e037c8f1fa2", ) @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()]) @transport.event_handler("on_client_disconnected") async def on_client_disconnected(transport, client): logger.info(f"Client disconnected") await task.cancel() runner = PipelineRunner(handle_sigint=handle_sigint) await runner.run(task) if __name__ == "__main__": from pipecat.examples.run import main main(run_example, transport_params=transport_params)