diff --git a/examples/voice-pipeline-agent/function_calling_weather.py b/examples/voice-pipeline-agent/function_calling_weather.py index 7f1ba5fa5..88358e419 100644 --- a/examples/voice-pipeline-agent/function_calling_weather.py +++ b/examples/voice-pipeline-agent/function_calling_weather.py @@ -39,18 +39,25 @@ async def get_weather( # that it might take awhile: # Option 1: you can use .say filler message immediately after the call is triggered # Option 2: you can prompt the agent to return a text response when it's making a function call - call_ctx = AgentCallContext.get_current() - filler_messages = [ - "Let me check the weather in {location} for you.", - "Let me see what the weather is like in {location} right now.", - # LLM will complete this sentence if it is added to the end of the chat context - "The current weather in {location} is ", - ] - message = random.choice(filler_messages).format(location=location) - - # NOTE: set add_to_chat_ctx=True will add the message to the end - # of the chat context of the function call for answer synthesis - speech_handle = await call_ctx.agent.say(message, add_to_chat_ctx=True) # noqa: F841 + agent = AgentCallContext.get_current().agent + + if ( + not agent.chat_ctx.messages + or agent.chat_ctx.messages[-1].role != "assistant" + ): + # skip if assistant already said something + filler_messages = [ + "Let me check the weather in {location} for you.", + "Let me see what the weather is like in {location} right now.", + # LLM will complete this sentence if it is added to the end of the chat context + "The current weather in {location} is ", + ] + message = random.choice(filler_messages).format(location=location) + logger.info(f"saying filler message: {message}") + + # NOTE: set add_to_chat_ctx=True will add the message to the end + # of the chat context of the function call for answer synthesis + speech_handle = await agent.say(message, add_to_chat_ctx=True) # noqa: F841 logger.info(f"getting weather for {location}") url = f"https://wttr.in/{urllib.parse.quote(location)}?format=%C+%t"