Files
wehub-resource-sync ba4be087d5
Create PR to main with cherry-pick from release / cherry-pick (push) Failing after 0s
CICD NeMo / pre-flight (push) Failing after 0s
CICD NeMo / configure (push) Has been skipped
Build, validate, and release Neural Modules / pre-flight (push) Failing after 1s
CICD NeMo / code-linting (push) Has been skipped
Build, validate, and release Neural Modules / release (push) Has been skipped
Build, validate, and release Neural Modules / release-summary (push) Has been cancelled
CICD NeMo / cicd-test-container-build (push) Has been cancelled
CICD NeMo / cicd-import-tests (push) Has been cancelled
CICD NeMo / L0_Setup_Test_Data_And_Models (push) Has been cancelled
CICD NeMo / cicd-main-unit-tests (push) Has been cancelled
CICD NeMo / cicd-main-speech (push) Has been cancelled
CICD NeMo / Nemo_CICD_Test (push) Has been cancelled
CICD NeMo / Coverage (e2e) (push) Has been cancelled
CICD NeMo / Coverage (unit-test) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
CICD NeMo / cicd-wait-in-queue (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:28:58 +08:00

107 lines
4.0 KiB
Python

# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from loguru import logger
from pipecat.adapters.schemas.direct_function import DirectFunction
from pipecat.adapters.schemas.tools_schema import ToolsSchema
from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
from pipecat.services.openai.llm import OpenAILLMService
class ToolCallingMixin:
"""
A mixin class for tool calling.
Subclasses must implement the `setup_tool_calling` method to register all available tools
using `self.register_direct_function()`. Then the `__init__` method of the subclass should
call the `setup_tool_calling` method to register the tools.
"""
def setup_tool_calling(self):
"""
Setup the tool calling mixin by registering all available tools using self.register_direct_function().
"""
raise NotImplementedError(
"Subclasses must implement this method to register all available functions "
"using self.register_direct_function()"
)
def register_direct_function(self, function_name: str, function: DirectFunction):
"""
Register a direct function to be called by the LLM.
Args:
function_name: The name of the function to register.
function: The direct function to register.
"""
if not hasattr(self, "direct_functions"):
self.direct_functions = {}
logger.info(
f"[{self.__class__.__name__}] Registering direct function name {function_name} to "
f"{function.__module__ + '.' + function.__qualname__}"
)
self.direct_functions[function_name] = function
@property
def available_tools(self) -> dict[str, DirectFunction]:
"""
Return a dictionary of available tools, where the key is the tool name and the value is the direct function.
"""
tools = {}
if not hasattr(self, "direct_functions"):
return tools
for function_name, function in self.direct_functions.items():
tools[function_name] = function
return tools
def register_direct_tools_to_llm(
*,
llm: OpenAILLMService,
context: OpenAILLMContext,
tool_mixins: list[ToolCallingMixin] = [],
tools: list[DirectFunction] = [],
cancel_on_interruption: bool = True,
) -> None:
"""
Register direct tools to the LLM.
Args:
llm: The LLM service to use.
context: The LLM context to use.
tools: The list of tools (instances of either `DirectFunction` or `ToolCallingMixin`) to use.
"""
all_tools = []
for tool in tool_mixins:
if not isinstance(tool, ToolCallingMixin):
logger.warning(f"Tool {tool.__class__.__name__} is not a ToolCallingMixin, skipping.")
continue
for function_name, function in tool.available_tools.items():
logger.info(f"Registering direct function {function_name} from {tool.__class__.__name__}")
all_tools.append(function)
for tool in tools:
logger.info(f"Registering direct function: {tool.__module__ + '.' + tool.__qualname__}")
all_tools.append(tool)
if not all_tools:
logger.warning("No direct tools provided.")
return
else:
logger.info(f"Registering {len(all_tools)} direct tools to the LLM.")
tools_schema = ToolsSchema(standard_tools=all_tools)
context.set_tools(tools_schema)
for tool in all_tools:
llm.register_direct_function(tool, cancel_on_interruption=cancel_on_interruption)