LLMService: only do handle function inspection once

This commit is contained in:
Aleix Conchillo Flaqué
2025-08-03 09:59:14 -07:00
parent d6d39fc873
commit 3a72e94d0c

View File

@@ -25,7 +25,6 @@ from loguru import logger
from pipecat.adapters.base_llm_adapter import BaseLLMAdapter from pipecat.adapters.base_llm_adapter import BaseLLMAdapter
from pipecat.adapters.schemas.direct_function import DirectFunction, DirectFunctionWrapper from pipecat.adapters.schemas.direct_function import DirectFunction, DirectFunctionWrapper
from pipecat.adapters.schemas.function_schema import FunctionSchema
from pipecat.adapters.services.open_ai_adapter import OpenAILLMAdapter from pipecat.adapters.services.open_ai_adapter import OpenAILLMAdapter
from pipecat.frames.frames import ( from pipecat.frames.frames import (
CancelFrame, CancelFrame,
@@ -108,6 +107,7 @@ class FunctionCallRegistryItem:
function_name: Optional[str] function_name: Optional[str]
handler: FunctionCallHandler | "DirectFunctionWrapper" handler: FunctionCallHandler | "DirectFunctionWrapper"
cancel_on_interruption: bool cancel_on_interruption: bool
handler_deprecated: bool
@dataclass @dataclass
@@ -282,12 +282,25 @@ class LLMService(AIService):
cancel_on_interruption: Whether to cancel this function call when an cancel_on_interruption: Whether to cancel this function call when an
interruption occurs. Defaults to True. interruption occurs. Defaults to True.
""" """
signature = inspect.signature(handler)
handler_deprecated = len(signature.parameters) > 1
if handler_deprecated:
import warnings
with warnings.catch_warnings():
warnings.simplefilter("always")
warnings.warn(
"Function calls with parameters `(function_name, tool_call_id, arguments, llm, context, result_callback)` are deprecated, use a single `FunctionCallParams` parameter instead.",
DeprecationWarning,
)
# Registering a function with the function_name set to None will run # Registering a function with the function_name set to None will run
# that handler for all functions # that handler for all functions
self._functions[function_name] = FunctionCallRegistryItem( self._functions[function_name] = FunctionCallRegistryItem(
function_name=function_name, function_name=function_name,
handler=handler, handler=handler,
cancel_on_interruption=cancel_on_interruption, cancel_on_interruption=cancel_on_interruption,
handler_deprecated=handler_deprecated,
) )
# Start callbacks are now deprecated. # Start callbacks are now deprecated.
@@ -325,6 +338,7 @@ class LLMService(AIService):
function_name=wrapper.name, function_name=wrapper.name,
handler=wrapper, handler=wrapper,
cancel_on_interruption=cancel_on_interruption, cancel_on_interruption=cancel_on_interruption,
handler_deprecated=False,
) )
def unregister_function(self, function_name: Optional[str]): def unregister_function(self, function_name: Optional[str]):
@@ -552,17 +566,7 @@ class LLMService(AIService):
) )
else: else:
# Handler is a FunctionCallHandler # Handler is a FunctionCallHandler
signature = inspect.signature(item.handler) if item.handler_deprecated:
if len(signature.parameters) > 1:
import warnings
with warnings.catch_warnings():
warnings.simplefilter("always")
warnings.warn(
"Function calls with parameters `(function_name, tool_call_id, arguments, llm, context, result_callback)` are deprecated, use a single `FunctionCallParams` parameter instead.",
DeprecationWarning,
)
await item.handler( await item.handler(
runner_item.function_name, runner_item.function_name,
runner_item.tool_call_id, runner_item.tool_call_id,