examples: update with single FunctionCallParams parameter

This commit is contained in:
Aleix Conchillo Flaqué
2025-04-25 09:00:43 -07:00
parent 944bc23135
commit 4df6444832
36 changed files with 268 additions and 292 deletions

View File

@@ -29,7 +29,7 @@ from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
from pipecat.processors.filters.function_filter import FunctionFilter
from pipecat.processors.frame_processor import FrameDirection, FrameProcessor
from pipecat.services.cartesia.tts import CartesiaTTSService
from pipecat.services.llm_service import LLMService
from pipecat.services.llm_service import FunctionCallParams, LLMService
from pipecat.services.openai.llm import OpenAILLMService
from pipecat.transports.services.daily import DailyDialinSettings, DailyParams, DailyTransport
@@ -220,12 +220,7 @@ async def main(
async def terminate_call(
task: PipelineTask, # Pipeline task reference
function_name,
tool_call_id,
args,
llm: LLMService,
context: OpenAILLMContext,
result_callback,
params: FunctionCallParams,
):
"""Function the bot can call to terminate the call."""
# Create a message to add
@@ -237,16 +232,9 @@ async def main(
await task.queue_frames([LLMMessagesFrame(messages)])
# Then end the call
await llm.queue_frame(EndTaskFrame(), FrameDirection.UPSTREAM)
await params.llm.queue_frame(EndTaskFrame(), FrameDirection.UPSTREAM)
async def dial_operator(
function_name: str,
tool_call_id: str,
args: dict,
llm: LLMService,
context: dict,
result_callback: callable,
):
async def dial_operator(params: FunctionCallParams):
"""Function the bot can call to dial an operator."""
dialout_setting = session_manager.call_flow_state.get_current_dialout_setting()
if call_config_manager.get_transfer_mode() == "dialout":
@@ -306,9 +294,7 @@ async def main(
llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"))
# Register functions with the LLM
llm.register_function(
"terminate_call", lambda *args, **kwargs: terminate_call(task, *args, **kwargs)
)
llm.register_function("terminate_call", lambda params: terminate_call(task, params))
llm.register_function("dial_operator", dial_operator)
# Initialize LLM context and aggregator

View File

@@ -22,7 +22,7 @@ from pipecat.pipeline.task import PipelineParams, PipelineTask
from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
from pipecat.processors.frame_processor import FrameDirection
from pipecat.services.cartesia.tts import CartesiaTTSService
from pipecat.services.llm_service import LLMService
from pipecat.services.llm_service import FunctionCallParams
from pipecat.services.openai.llm import OpenAILLMService
from pipecat.transports.services.daily import DailyDialinSettings, DailyParams, DailyTransport
@@ -99,16 +99,14 @@ async def main(
# ------------ FUNCTION DEFINITIONS ------------
async def terminate_call(
function_name, tool_call_id, args, llm: LLMService, context, result_callback
):
async def terminate_call(params: FunctionCallParams):
"""Function the bot can call to terminate the call upon completion of a voicemail message."""
if session_manager:
# Mark that the call was terminated by the bot
session_manager.call_flow_state.set_call_terminated()
# Then end the call
await llm.queue_frame(EndTaskFrame(), FrameDirection.UPSTREAM)
await params.llm.queue_frame(EndTaskFrame(), FrameDirection.UPSTREAM)
# Define function schemas for tools
terminate_call_function = FunctionSchema(

View File

@@ -22,7 +22,7 @@ from pipecat.pipeline.task import PipelineParams, PipelineTask
from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
from pipecat.processors.frame_processor import FrameDirection
from pipecat.services.cartesia.tts import CartesiaTTSService
from pipecat.services.llm_service import LLMService
from pipecat.services.llm_service import FunctionCallParams
from pipecat.services.openai.llm import OpenAILLMService
from pipecat.transports.services.daily import DailyParams, DailyTransport
@@ -77,11 +77,9 @@ async def main(
# ------------ FUNCTION DEFINITIONS ------------
async def terminate_call(
function_name, tool_call_id, args, llm: LLMService, context, result_callback
):
async def terminate_call(params: FunctionCallParams):
"""Function the bot can call to terminate the call upon completion of a voicemail message."""
await llm.queue_frame(EndTaskFrame(), FrameDirection.UPSTREAM)
await params.llm.queue_frame(EndTaskFrame(), FrameDirection.UPSTREAM)
# Define function schemas for tools
terminate_call_function = FunctionSchema(

View File

@@ -32,7 +32,7 @@ from pipecat.services.cartesia.tts import CartesiaTTSService
from pipecat.services.deepgram.stt import DeepgramSTTService
from pipecat.services.google.google import GoogleLLMContext
from pipecat.services.google.llm import GoogleLLMService
from pipecat.services.llm_service import LLMService # Base LLM service class
from pipecat.services.llm_service import FunctionCallParams
from pipecat.transports.services.daily import (
DailyParams,
DailyTransport,
@@ -98,35 +98,19 @@ class FunctionHandlers:
self.session_manager = session_manager
self.prompt = None # Can be set externally
async def voicemail_response(
self,
function_name,
tool_call_id,
args,
llm: LLMService,
context,
result_callback,
):
async def voicemail_response(self, params: FunctionCallParams):
"""Function the bot can call to leave a voicemail message."""
message = """You are Chatbot leaving a voicemail message. Say EXACTLY this message and then terminate the call:
'Hello, this is a message for Pipecat example user. This is Chatbot. Please call back on 123-456-7891. Thank you.'"""
await result_callback(message)
await params.result_callback(message)
async def human_conversation(
self,
function_name,
tool_call_id,
args,
llm: LLMService,
context,
result_callback,
):
async def human_conversation(self, params: FunctionCallParams):
"""Function called when bot detects it's talking to a human."""
# Update state to indicate human was detected
self.session_manager.call_flow_state.set_human_detected()
await llm.push_frame(StopTaskFrame(), FrameDirection.UPSTREAM)
await params.llm.push_frame(StopTaskFrame(), FrameDirection.UPSTREAM)
# ------------ MAIN FUNCTION ------------
@@ -182,12 +166,7 @@ async def main(
# ------------ FUNCTION DEFINITIONS ------------
async def terminate_call(
function_name,
tool_call_id,
args,
llm: LLMService,
context,
result_callback,
params: FunctionCallParams,
session_manager=None,
):
"""Function the bot can call to terminate the call."""
@@ -195,7 +174,7 @@ async def main(
# Set call terminated flag in the session manager
session_manager.call_flow_state.set_call_terminated()
await llm.queue_frame(EndTaskFrame(), FrameDirection.UPSTREAM)
await params.llm.queue_frame(EndTaskFrame(), FrameDirection.UPSTREAM)
# ------------ VOICEMAIL DETECTION PHASE SETUP ------------
@@ -275,7 +254,7 @@ async def main(
"switch_to_human_conversation", handlers.human_conversation
)
voicemail_detection_llm.register_function(
"terminate_call", functools.partial(terminate_call, session_manager=session_manager)
"terminate_call", lambda params: terminate_call(params, session_manager)
)
# Set up audio collector for handling audio input