Skip to content

Commit

Permalink
avoid duplicate say in function call example (#1317)
Browse files Browse the repository at this point in the history
  • Loading branch information
longcw authored Dec 31, 2024
1 parent b7f2895 commit bcbe7dd
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions examples/voice-pipeline-agent/function_calling_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit bcbe7dd

Please sign in to comment.