|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from typing import TYPE_CHECKING, Callable, Literal |
| 5 | + |
| 6 | +from pydantic.dataclasses import dataclass |
| 7 | + |
| 8 | +from semantic_kernel.functions.kernel_function_metadata import KernelFunctionMetadata |
| 9 | +from semantic_kernel.kernel_pydantic import KernelBaseModel |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings |
| 13 | + from semantic_kernel.kernel import Kernel |
| 14 | + |
| 15 | +DEFAULT_MAX_AUTO_INVOKE_ATTEMPTS = 5 |
| 16 | + |
| 17 | + |
| 18 | +@dataclass |
| 19 | +class FunctionCallConfiguration: |
| 20 | + """Class that holds the configured functions for function calling.""" |
| 21 | + |
| 22 | + available_functions: list["KernelFunctionMetadata"] | None = None |
| 23 | + required_functions: list["KernelFunctionMetadata"] | None = None |
| 24 | + |
| 25 | + |
| 26 | +class FunctionCallBehavior(KernelBaseModel): |
| 27 | + """Class that controls function calling behavior. |
| 28 | +
|
| 29 | + Args: |
| 30 | + enable_kernel_functions (bool): Enable kernel functions. |
| 31 | + max_auto_invoke_attempts (int): The maximum number of auto invoke attempts. |
| 32 | +
|
| 33 | + Attributes: |
| 34 | + enable_kernel_functions (bool): Enable kernel functions. |
| 35 | + max_auto_invoke_attempts (int): The maximum number of auto invoke attempts. |
| 36 | +
|
| 37 | + Properties: |
| 38 | + auto_invoke_kernel_functions: Check if the kernel functions should be auto-invoked. |
| 39 | + Determined as max_auto_invoke_attempts > 0. |
| 40 | +
|
| 41 | + Methods: |
| 42 | + configure: Configures the settings for the function call behavior, |
| 43 | + the default version in this class, does nothing, use subclasses for different behaviors. |
| 44 | +
|
| 45 | + Class methods: |
| 46 | + AutoInvokeKernelFunctions: Returns KernelFunctions class with auto_invoke enabled, all functions. |
| 47 | + EnableKernelFunctions: Returns KernelFunctions class with auto_invoke disabled, all functions. |
| 48 | + EnableFunctions: Set the enable kernel functions flag, filtered functions, auto_invoke optional. |
| 49 | + RequiredFunction: Set the required function flag, auto_invoke optional. |
| 50 | +
|
| 51 | + """ |
| 52 | + |
| 53 | + enable_kernel_functions: bool = True |
| 54 | + max_auto_invoke_attempts: int = DEFAULT_MAX_AUTO_INVOKE_ATTEMPTS |
| 55 | + |
| 56 | + @property |
| 57 | + def auto_invoke_kernel_functions(self): |
| 58 | + """Check if the kernel functions should be auto-invoked.""" |
| 59 | + return self.max_auto_invoke_attempts > 0 |
| 60 | + |
| 61 | + @auto_invoke_kernel_functions.setter |
| 62 | + def auto_invoke_kernel_functions(self, value: bool): |
| 63 | + """Set the auto_invoke_kernel_functions flag.""" |
| 64 | + if not value: |
| 65 | + self.max_auto_invoke_attempts = 0 |
| 66 | + else: |
| 67 | + if self.max_auto_invoke_attempts == 0: |
| 68 | + self.max_auto_invoke_attempts = DEFAULT_MAX_AUTO_INVOKE_ATTEMPTS |
| 69 | + |
| 70 | + def configure( |
| 71 | + self, |
| 72 | + kernel: "Kernel", |
| 73 | + update_settings_callback: Callable[..., None], |
| 74 | + settings: "PromptExecutionSettings", |
| 75 | + ) -> None: |
| 76 | + """Configures the settings for the function call behavior. |
| 77 | +
|
| 78 | + Using the base ToolCallBehavior means that you manually have to set tool_choice and tools. |
| 79 | +
|
| 80 | + For different behaviors, use the subclasses of ToolCallBehavior: |
| 81 | + KernelFunctions (all functions in the Kernel) |
| 82 | + EnabledFunctions (filtered set of functions from the Kernel) |
| 83 | + RequiredFunction (a single function) |
| 84 | +
|
| 85 | + By default the update_settings_callback is called with FunctionCallConfiguration, |
| 86 | + which contains a list of available functions or a list of required functions, it also |
| 87 | + takes the PromptExecutionSettings object. |
| 88 | +
|
| 89 | + It should update the prompt execution settings with the available functions or required functions. |
| 90 | +
|
| 91 | + Alternatively you can override this class and add your own logic in the configure method. |
| 92 | + """ |
| 93 | + return |
| 94 | + |
| 95 | + @classmethod |
| 96 | + def AutoInvokeKernelFunctions(cls) -> "KernelFunctions": |
| 97 | + """Returns KernelFunctions class with auto_invoke enabled.""" |
| 98 | + return KernelFunctions(max_auto_invoke_attempts=DEFAULT_MAX_AUTO_INVOKE_ATTEMPTS) |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def EnableKernelFunctions(cls) -> "KernelFunctions": |
| 102 | + """Returns KernelFunctions class with auto_invoke disabled. |
| 103 | +
|
| 104 | + Function calls are enabled in this case, just not invoked. |
| 105 | + """ |
| 106 | + return KernelFunctions(max_auto_invoke_attempts=0) |
| 107 | + |
| 108 | + @classmethod |
| 109 | + def EnableFunctions( |
| 110 | + cls, |
| 111 | + auto_invoke: bool = False, |
| 112 | + *, |
| 113 | + filters: dict[ |
| 114 | + Literal["excluded_plugins", "included_plugins", "excluded_functions", "included_functions"], list[str] |
| 115 | + ], |
| 116 | + ) -> "EnabledFunctions": |
| 117 | + """Set the enable kernel functions flag.""" |
| 118 | + return EnabledFunctions( |
| 119 | + filters=filters, max_auto_invoke_attempts=DEFAULT_MAX_AUTO_INVOKE_ATTEMPTS if auto_invoke else 0 |
| 120 | + ) |
| 121 | + |
| 122 | + @classmethod |
| 123 | + def RequiredFunction( |
| 124 | + cls, |
| 125 | + auto_invoke: bool = False, |
| 126 | + *, |
| 127 | + function_fully_qualified_name: str, |
| 128 | + ) -> "RequiredFunction": |
| 129 | + """Set the required function flag.""" |
| 130 | + return RequiredFunction( |
| 131 | + function_fully_qualified_name=function_fully_qualified_name, |
| 132 | + max_auto_invoke_attempts=1 if auto_invoke else 0, |
| 133 | + ) |
| 134 | + |
| 135 | + |
| 136 | +class KernelFunctions(FunctionCallBehavior): |
| 137 | + """Function call behavior for making all kernel functions available for tool calls.""" |
| 138 | + |
| 139 | + def configure( |
| 140 | + self, |
| 141 | + kernel: "Kernel", |
| 142 | + update_settings_callback: Callable[..., None], |
| 143 | + settings: "PromptExecutionSettings", |
| 144 | + ) -> None: |
| 145 | + """Set the options for the tool call behavior in the settings.""" |
| 146 | + if self.enable_kernel_functions: |
| 147 | + update_settings_callback( |
| 148 | + FunctionCallConfiguration(available_functions=kernel.get_full_list_of_function_metadata()), settings |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +class EnabledFunctions(FunctionCallBehavior): |
| 153 | + """Function call behavior for making a filtered set of functions available for tool calls.""" |
| 154 | + |
| 155 | + filters: dict[ |
| 156 | + Literal["excluded_plugins", "included_plugins", "excluded_functions", "included_functions"], list[str] |
| 157 | + ] |
| 158 | + |
| 159 | + def configure( |
| 160 | + self, |
| 161 | + kernel: "Kernel", |
| 162 | + update_settings_callback: Callable[..., None], |
| 163 | + settings: "PromptExecutionSettings", |
| 164 | + ) -> None: |
| 165 | + """Set the options for the tool call behavior in the settings.""" |
| 166 | + if self.enable_kernel_functions: |
| 167 | + update_settings_callback( |
| 168 | + FunctionCallConfiguration(available_functions=kernel.get_list_of_function_metadata(self.filters)), |
| 169 | + settings, |
| 170 | + ) |
| 171 | + |
| 172 | + |
| 173 | +class RequiredFunction(FunctionCallBehavior): |
| 174 | + """Function call behavior for making a single function available for tool calls.""" |
| 175 | + |
| 176 | + function_fully_qualified_name: str |
| 177 | + |
| 178 | + def configure( |
| 179 | + self, |
| 180 | + kernel: "Kernel", |
| 181 | + update_settings_callback: Callable[..., None], |
| 182 | + settings: "PromptExecutionSettings", |
| 183 | + ) -> None: |
| 184 | + """Set the options for the tool call behavior in the settings.""" |
| 185 | + if not self.enable_kernel_functions: |
| 186 | + return |
| 187 | + # since using this always calls this single function, we do not want to allow repeated calls |
| 188 | + # TODO: reevaluate when other models support function calling then OpenAI. |
| 189 | + if self.max_auto_invoke_attempts > 1: |
| 190 | + self.max_auto_invoke_attempts = 1 |
| 191 | + update_settings_callback( |
| 192 | + FunctionCallConfiguration( |
| 193 | + required_functions=kernel.get_list_of_function_metadata( |
| 194 | + {"included_functions": [self.function_fully_qualified_name]} |
| 195 | + ) |
| 196 | + ), |
| 197 | + settings, |
| 198 | + ) |
0 commit comments