From d515a810735ad09cb69473e48df8ae12fcade912 Mon Sep 17 00:00:00 2001 From: filipi87 Date: Wed, 1 Apr 2026 15:31:32 -0300 Subject: [PATCH 1/2] Updating the Anthropic example to use async function calls. --- .../{anthropic.py => anthropic-example.py} | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) rename examples/function-calling/{anthropic.py => anthropic-example.py} (89%) diff --git a/examples/function-calling/anthropic.py b/examples/function-calling/anthropic-example.py similarity index 89% rename from examples/function-calling/anthropic.py rename to examples/function-calling/anthropic-example.py index b8bb4eac6..e4b9fca01 100644 --- a/examples/function-calling/anthropic.py +++ b/examples/function-calling/anthropic-example.py @@ -4,7 +4,7 @@ # SPDX-License-Identifier: BSD 2-Clause License # - +import asyncio import os from dotenv import load_dotenv @@ -35,9 +35,10 @@ from pipecat.transports.websocket.fastapi import FastAPIWebsocketParams load_dotenv(override=True) -async def get_weather(params: FunctionCallParams): - location = params.arguments["location"] - await params.result_callback(f"The weather in {location} is currently 72 degrees and sunny.") +async def fetch_weather_from_api(params: FunctionCallParams): + # Simulate a long-running API call, so we can test async function calls. + await asyncio.sleep(20) + await params.result_callback({"conditions": "nice", "temperature": "75"}) async def fetch_restaurant_recommendation(params: FunctionCallParams): @@ -80,11 +81,20 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments): system_instruction="You are a helpful assistant in a voice conversation. Your responses will be spoken aloud, so avoid emojis, bullet points, or other formatting that can't be spoken. Respond to what the user said in a creative, helpful, and brief way.", ), ) - llm.register_function("get_weather", get_weather) + + # You can also register a function_name of None to get all functions + # sent to the same callback with an additional function_name parameter. + llm.register_function( + "get_current_weather", + fetch_weather_from_api, + cancel_on_interruption=False, + is_async=True, + timeout_secs=30, + ) llm.register_function("get_restaurant_recommendation", fetch_restaurant_recommendation) weather_function = FunctionSchema( - name="get_weather", + name="get_current_weather", description="Get the current weather", properties={ "location": { From 59092fe4fe4bf8a38941010940bb1ee28246c09a Mon Sep 17 00:00:00 2001 From: filipi87 Date: Wed, 1 Apr 2026 15:42:50 -0300 Subject: [PATCH 2/2] Renaming the examples to match main. --- .../{anthropic-example.py => function-calling-anthropic.py} | 0 .../{openai-example.py => function-calling-openai.py} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename examples/function-calling/{anthropic-example.py => function-calling-anthropic.py} (100%) rename examples/function-calling/{openai-example.py => function-calling-openai.py} (100%) diff --git a/examples/function-calling/anthropic-example.py b/examples/function-calling/function-calling-anthropic.py similarity index 100% rename from examples/function-calling/anthropic-example.py rename to examples/function-calling/function-calling-anthropic.py diff --git a/examples/function-calling/openai-example.py b/examples/function-calling/function-calling-openai.py similarity index 100% rename from examples/function-calling/openai-example.py rename to examples/function-calling/function-calling-openai.py