mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> This introduces a sample that uses function calling with a local model, Nexus Raven V2, in this case using the API, but the same model can be loaded using the Hugging Face Connection, the class here is highly adapted and specific to Nexus including parsing the returned function call value, into FunctionCallContent and then running that. It then returns one or more ChatMessageContents with the function call content and function result content which can then be shared with a OpenAI call. Error handling improvements: * [`python/semantic_kernel/contents/chat_history.py`](diffhunk://#diff-b57a30f97fec1a91bbf6f7d15584473a8f0cf409797bdcd90d8c80bedc670892L266-R267): Improved error logging when parsing prompts as XML fails. * [`python/semantic_kernel/contents/function_result_content.py`](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10): Removed field validator for `result` and allow result to be any value, instead of casting everything to string. [[1]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10) [[2]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L50-R51) [[3]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L63-L69) [[4]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87R92-R115) Minor changes: * [`python/semantic_kernel/core_plugins/math_plugin.py`](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38): Removed some docstrings for clarity. [[1]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38) [[2]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L55-R47) * [`python/semantic_kernel/functions/kernel_function_from_prompt.py`](diffhunk://#diff-eabd53da2ec241cce6f92de85283065e7b478d62cefb52817c5b29ff07e28189L173-R174): Changed the order of condition checks for better readability. * [`python/semantic_kernel/kernel.py`](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10): Added functionality to update kernel arguments in `invoke_stream` method and made `function_name` and `plugin_name` optional in `invoke_prompt` method. [[1]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10) [[2]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR29) [[3]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR119-R120) [[4]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL195-R202) [[5]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL209-R216) Closes #6804 Closes #6944 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
424 lines
20 KiB
Python
424 lines
20 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
import logging
|
|
from collections.abc import AsyncGenerator, AsyncIterable
|
|
from copy import copy
|
|
from typing import TYPE_CHECKING, Any, Literal, TypeVar
|
|
|
|
from semantic_kernel.const import METADATA_EXCEPTION_KEY
|
|
from semantic_kernel.contents.chat_history import ChatHistory
|
|
from semantic_kernel.contents.function_call_content import FunctionCallContent
|
|
from semantic_kernel.contents.function_result_content import FunctionResultContent
|
|
from semantic_kernel.contents.streaming_content_mixin import StreamingContentMixin
|
|
from semantic_kernel.exceptions import (
|
|
FunctionCallInvalidArgumentsException,
|
|
FunctionExecutionException,
|
|
KernelFunctionNotFoundError,
|
|
KernelInvokeException,
|
|
OperationCancelledException,
|
|
TemplateSyntaxError,
|
|
)
|
|
from semantic_kernel.filters.auto_function_invocation.auto_function_invocation_context import (
|
|
AutoFunctionInvocationContext,
|
|
)
|
|
from semantic_kernel.filters.filter_types import FilterTypes
|
|
from semantic_kernel.filters.kernel_filters_extension import (
|
|
KernelFilterExtension,
|
|
_rebuild_auto_function_invocation_context,
|
|
)
|
|
from semantic_kernel.functions.function_result import FunctionResult
|
|
from semantic_kernel.functions.kernel_arguments import KernelArguments
|
|
from semantic_kernel.functions.kernel_function_extension import KernelFunctionExtension
|
|
from semantic_kernel.functions.kernel_function_from_prompt import KernelFunctionFromPrompt
|
|
from semantic_kernel.functions.kernel_plugin import KernelPlugin
|
|
from semantic_kernel.kernel_types import AI_SERVICE_CLIENT_TYPE
|
|
from semantic_kernel.prompt_template.const import KERNEL_TEMPLATE_FORMAT_NAME
|
|
from semantic_kernel.reliability.kernel_reliability_extension import KernelReliabilityExtension
|
|
from semantic_kernel.services.ai_service_selector import AIServiceSelector
|
|
from semantic_kernel.services.kernel_services_extension import KernelServicesExtension
|
|
from semantic_kernel.utils.naming import generate_random_ascii_name
|
|
|
|
if TYPE_CHECKING:
|
|
from semantic_kernel.connectors.ai.function_choice_behavior import (
|
|
FunctionChoiceBehavior,
|
|
)
|
|
from semantic_kernel.functions.kernel_function import KernelFunction
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
logger: logging.Logger = logging.getLogger(__name__)
|
|
|
|
|
|
class Kernel(KernelFilterExtension, KernelFunctionExtension, KernelServicesExtension, KernelReliabilityExtension):
|
|
"""The main Kernel class of Semantic Kernel.
|
|
|
|
This is the main entry point for the Semantic Kernel. It provides the ability to run
|
|
semantic/native functions, and manage plugins, memory, and AI services.
|
|
|
|
Attributes:
|
|
plugins (dict[str, KernelPlugin] | None): The plugins to be used by the kernel
|
|
services (dict[str, AIServiceClientBase]): The services to be used by the kernel
|
|
ai_service_selector (AIServiceSelector): The AI service selector to be used by the kernel
|
|
retry_mechanism (RetryMechanismBase): The retry mechanism to be used by the kernel
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
plugins: KernelPlugin | dict[str, KernelPlugin] | list[KernelPlugin] | None = None,
|
|
services: (
|
|
AI_SERVICE_CLIENT_TYPE | list[AI_SERVICE_CLIENT_TYPE] | dict[str, AI_SERVICE_CLIENT_TYPE] | None
|
|
) = None,
|
|
ai_service_selector: AIServiceSelector | None = None,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
"""Initialize a new instance of the Kernel class.
|
|
|
|
Args:
|
|
plugins (KernelPlugin | dict[str, KernelPlugin] | list[KernelPlugin] | None):
|
|
The plugins to be used by the kernel, will be rewritten to a dict with plugin name as key
|
|
services (AIServiceClientBase | list[AIServiceClientBase] | dict[str, AIServiceClientBase] | None):
|
|
The services to be used by the kernel, will be rewritten to a dict with service_id as key
|
|
ai_service_selector (AIServiceSelector | None):
|
|
The AI service selector to be used by the kernel,
|
|
default is based on order of execution settings.
|
|
**kwargs (Any): Additional fields to be passed to the Kernel model,
|
|
these are limited to retry_mechanism and function_invoking_handlers
|
|
and function_invoked_handlers, the best way to add function_invoking_handlers
|
|
and function_invoked_handlers is to use the add_function_invoking_handler
|
|
and add_function_invoked_handler methods.
|
|
"""
|
|
args = {
|
|
"services": services,
|
|
"plugins": plugins,
|
|
**kwargs,
|
|
}
|
|
if ai_service_selector:
|
|
args["ai_service_selector"] = ai_service_selector
|
|
super().__init__(**args)
|
|
|
|
async def invoke_stream(
|
|
self,
|
|
function: "KernelFunction | None" = None,
|
|
arguments: KernelArguments | None = None,
|
|
function_name: str | None = None,
|
|
plugin_name: str | None = None,
|
|
metadata: dict[str, Any] = {},
|
|
return_function_results: bool = False,
|
|
**kwargs: Any,
|
|
) -> AsyncGenerator[list["StreamingContentMixin"] | FunctionResult | list[FunctionResult], Any]:
|
|
"""Execute one or more stream functions.
|
|
|
|
This will execute the functions in the order they are provided, if a list of functions is provided.
|
|
When multiple functions are provided only the last one is streamed, the rest is executed as a pipeline.
|
|
|
|
Args:
|
|
function (KernelFunction): The function to execute,
|
|
this value has precedence when supplying both this and using function_name and plugin_name,
|
|
if this is none, function_name and plugin_name are used and cannot be None.
|
|
arguments (KernelArguments | None): The arguments to pass to the function(s), optional
|
|
function_name (str | None): The name of the function to execute
|
|
plugin_name (str | None): The name of the plugin to execute
|
|
metadata (dict[str, Any]): The metadata to pass to the function(s)
|
|
return_function_results (bool): If True, the function results are yielded as a list[FunctionResult]
|
|
in addition to the streaming content, otherwise only the streaming content is yielded.
|
|
kwargs (dict[str, Any]): arguments that can be used instead of supplying KernelArguments
|
|
|
|
Yields:
|
|
StreamingContentMixin: The content of the stream of the last function provided.
|
|
"""
|
|
if arguments is None:
|
|
arguments = KernelArguments(**kwargs)
|
|
else:
|
|
arguments.update(kwargs)
|
|
if not function:
|
|
if not function_name or not plugin_name:
|
|
raise KernelFunctionNotFoundError("No function(s) or function- and plugin-name provided")
|
|
function = self.get_function(plugin_name, function_name)
|
|
|
|
function_result: list[list["StreamingContentMixin"] | Any] = []
|
|
|
|
async for stream_message in function.invoke_stream(self, arguments):
|
|
if isinstance(stream_message, FunctionResult) and (
|
|
exception := stream_message.metadata.get(METADATA_EXCEPTION_KEY, None)
|
|
):
|
|
raise KernelInvokeException(
|
|
f"Error occurred while invoking function: '{function.fully_qualified_name}'"
|
|
) from exception
|
|
function_result.append(stream_message)
|
|
yield stream_message
|
|
|
|
if return_function_results:
|
|
output_function_result: list["StreamingContentMixin"] = []
|
|
for result in function_result:
|
|
for choice in result:
|
|
if not isinstance(choice, StreamingContentMixin):
|
|
continue
|
|
if len(output_function_result) <= choice.choice_index:
|
|
output_function_result.append(copy(choice))
|
|
else:
|
|
output_function_result[choice.choice_index] += choice
|
|
yield FunctionResult(function=function.metadata, value=output_function_result)
|
|
|
|
async def invoke(
|
|
self,
|
|
function: "KernelFunction | None" = None,
|
|
arguments: KernelArguments | None = None,
|
|
function_name: str | None = None,
|
|
plugin_name: str | None = None,
|
|
metadata: dict[str, Any] = {},
|
|
**kwargs: Any,
|
|
) -> FunctionResult | None:
|
|
"""Execute a function and return the FunctionResult.
|
|
|
|
Args:
|
|
function (KernelFunction): The function or functions to execute,
|
|
this value has precedence when supplying both this and using function_name and plugin_name,
|
|
if this is none, function_name and plugin_name are used and cannot be None.
|
|
arguments (KernelArguments): The arguments to pass to the function(s), optional
|
|
function_name (str | None): The name of the function to execute
|
|
plugin_name (str | None): The name of the plugin to execute
|
|
metadata (dict[str, Any]): The metadata to pass to the function(s)
|
|
kwargs (dict[str, Any]): arguments that can be used instead of supplying KernelArguments
|
|
|
|
Raises:
|
|
KernelInvokeException: If an error occurs during function invocation
|
|
|
|
"""
|
|
if arguments is None:
|
|
arguments = KernelArguments(**kwargs)
|
|
else:
|
|
arguments.update(kwargs)
|
|
if not function:
|
|
if not function_name or not plugin_name:
|
|
raise KernelFunctionNotFoundError("No function, or function name and plugin name provided")
|
|
function = self.get_function(plugin_name, function_name)
|
|
|
|
try:
|
|
return await function.invoke(kernel=self, arguments=arguments, metadata=metadata)
|
|
except OperationCancelledException as exc:
|
|
logger.info(f"Operation cancelled during function invocation. Message: {exc}")
|
|
return None
|
|
except Exception as exc:
|
|
logger.error(
|
|
"Something went wrong in function invocation. During function invocation:"
|
|
f" '{function.fully_qualified_name}'. Error description: '{exc!s}'"
|
|
)
|
|
raise KernelInvokeException(
|
|
f"Error occurred while invoking function: '{function.fully_qualified_name}'"
|
|
) from exc
|
|
|
|
async def invoke_prompt(
|
|
self,
|
|
prompt: str,
|
|
function_name: str | None = None,
|
|
plugin_name: str | None = None,
|
|
arguments: KernelArguments | None = None,
|
|
template_format: Literal[
|
|
"semantic-kernel",
|
|
"handlebars",
|
|
"jinja2",
|
|
] = KERNEL_TEMPLATE_FORMAT_NAME,
|
|
**kwargs: Any,
|
|
) -> FunctionResult | None:
|
|
"""Invoke a function from the provided prompt.
|
|
|
|
Args:
|
|
prompt (str): The prompt to use
|
|
function_name (str): The name of the function, optional
|
|
plugin_name (str): The name of the plugin, optional
|
|
arguments (KernelArguments | None): The arguments to pass to the function(s), optional
|
|
template_format (str | None): The format of the prompt template
|
|
kwargs (dict[str, Any]): arguments that can be used instead of supplying KernelArguments
|
|
|
|
Returns:
|
|
FunctionResult | list[FunctionResult] | None: The result of the function(s)
|
|
"""
|
|
if not arguments:
|
|
arguments = KernelArguments(**kwargs)
|
|
if not prompt:
|
|
raise TemplateSyntaxError("The prompt is either null or empty.")
|
|
|
|
function = KernelFunctionFromPrompt(
|
|
function_name=function_name or generate_random_ascii_name(),
|
|
plugin_name=plugin_name,
|
|
prompt=prompt,
|
|
template_format=template_format,
|
|
)
|
|
return await self.invoke(function=function, arguments=arguments)
|
|
|
|
async def invoke_prompt_stream(
|
|
self,
|
|
prompt: str,
|
|
function_name: str | None = None,
|
|
plugin_name: str | None = None,
|
|
arguments: KernelArguments | None = None,
|
|
template_format: Literal[
|
|
"semantic-kernel",
|
|
"handlebars",
|
|
"jinja2",
|
|
] = KERNEL_TEMPLATE_FORMAT_NAME,
|
|
return_function_results: bool | None = False,
|
|
**kwargs: Any,
|
|
) -> AsyncIterable[list["StreamingContentMixin"] | FunctionResult | list[FunctionResult]]:
|
|
"""Invoke a function from the provided prompt and stream the results.
|
|
|
|
Args:
|
|
prompt (str): The prompt to use
|
|
function_name (str): The name of the function, optional
|
|
plugin_name (str): The name of the plugin, optional
|
|
arguments (KernelArguments | None): The arguments to pass to the function(s), optional
|
|
template_format (str | None): The format of the prompt template
|
|
return_function_results (bool): If True, the function results are yielded as a list[FunctionResult]
|
|
kwargs (dict[str, Any]): arguments that can be used instead of supplying KernelArguments
|
|
|
|
Returns:
|
|
AsyncIterable[StreamingContentMixin]: The content of the stream of the last function provided.
|
|
"""
|
|
if not arguments:
|
|
arguments = KernelArguments(**kwargs)
|
|
if not prompt:
|
|
raise TemplateSyntaxError("The prompt is either null or empty.")
|
|
|
|
from semantic_kernel.functions.kernel_function_from_prompt import KernelFunctionFromPrompt
|
|
|
|
function = KernelFunctionFromPrompt(
|
|
function_name=function_name or generate_random_ascii_name(),
|
|
plugin_name=plugin_name,
|
|
prompt=prompt,
|
|
template_format=template_format,
|
|
)
|
|
|
|
function_result: list[list["StreamingContentMixin"] | Any] = []
|
|
|
|
async for stream_message in self.invoke_stream(function=function, arguments=arguments):
|
|
if isinstance(stream_message, FunctionResult) and (
|
|
exception := stream_message.metadata.get(METADATA_EXCEPTION_KEY, None)
|
|
):
|
|
raise KernelInvokeException(
|
|
f"Error occurred while invoking function: '{function.fully_qualified_name}'"
|
|
) from exception
|
|
function_result.append(stream_message)
|
|
yield stream_message
|
|
|
|
if return_function_results:
|
|
output_function_result: list["StreamingContentMixin"] = []
|
|
for result in function_result:
|
|
for choice in result:
|
|
if not isinstance(choice, StreamingContentMixin):
|
|
continue
|
|
if len(output_function_result) <= choice.choice_index:
|
|
output_function_result.append(copy(choice))
|
|
else:
|
|
output_function_result[choice.choice_index] += choice
|
|
yield FunctionResult(function=function.metadata, value=output_function_result)
|
|
|
|
async def invoke_function_call(
|
|
self,
|
|
function_call: FunctionCallContent,
|
|
chat_history: ChatHistory,
|
|
arguments: "KernelArguments | None" = None,
|
|
function_call_count: int | None = None,
|
|
request_index: int | None = None,
|
|
function_behavior: "FunctionChoiceBehavior" = None, # type: ignore
|
|
) -> "AutoFunctionInvocationContext | None":
|
|
"""Processes the provided FunctionCallContent and updates the chat history."""
|
|
args_cloned = copy(arguments) if arguments else KernelArguments()
|
|
try:
|
|
parsed_args = function_call.to_kernel_arguments()
|
|
if parsed_args:
|
|
args_cloned.update(parsed_args)
|
|
except (FunctionCallInvalidArgumentsException, TypeError) as exc:
|
|
logger.info(f"Received invalid arguments for function {function_call.name}: {exc}. Trying tool call again.")
|
|
frc = FunctionResultContent.from_function_call_content_and_result(
|
|
function_call_content=function_call,
|
|
result="The tool call arguments are malformed. Arguments must be in JSON format. Please try again.",
|
|
)
|
|
chat_history.add_message(message=frc.to_chat_message_content())
|
|
return None
|
|
|
|
try:
|
|
if function_call.name is None:
|
|
raise FunctionExecutionException("The function name is required.")
|
|
if function_behavior is not None and function_behavior.filters:
|
|
allowed_functions = [
|
|
func.fully_qualified_name for func in self.get_list_of_function_metadata(function_behavior.filters)
|
|
]
|
|
if function_call.name not in allowed_functions:
|
|
raise FunctionExecutionException(
|
|
f"Only functions: {allowed_functions} are allowed, {function_call.name} is not allowed."
|
|
)
|
|
function_to_call = self.get_function(function_call.plugin_name, function_call.function_name)
|
|
except Exception as exc:
|
|
logger.exception(f"The function `{function_call.name}` is not part of the provided functions: {exc}.")
|
|
frc = FunctionResultContent.from_function_call_content_and_result(
|
|
function_call_content=function_call,
|
|
result=(
|
|
f"The tool call with name `{function_call.name}` is not part of the provided tools, "
|
|
"please try again with a supplied tool call name and make sure to validate the name."
|
|
),
|
|
)
|
|
chat_history.add_message(message=frc.to_chat_message_content())
|
|
return None
|
|
|
|
num_required_func_params = len([param for param in function_to_call.parameters if param.is_required])
|
|
if parsed_args is None or len(parsed_args) < num_required_func_params:
|
|
msg = (
|
|
f"There are `{num_required_func_params}` tool call arguments required and "
|
|
f"only `{len(parsed_args) if parsed_args is not None else 0}` received. The required arguments are: "
|
|
f"{[param.name for param in function_to_call.parameters if param.is_required]}. "
|
|
"Please provide the required arguments and try again."
|
|
)
|
|
logger.info(msg)
|
|
frc = FunctionResultContent.from_function_call_content_and_result(
|
|
function_call_content=function_call,
|
|
result=msg,
|
|
)
|
|
chat_history.add_message(message=frc.to_chat_message_content())
|
|
return None
|
|
|
|
logger.info(f"Calling {function_call.name} function with args: {function_call.arguments}")
|
|
|
|
_rebuild_auto_function_invocation_context()
|
|
invocation_context = AutoFunctionInvocationContext(
|
|
function=function_to_call,
|
|
kernel=self,
|
|
arguments=args_cloned,
|
|
chat_history=chat_history,
|
|
function_result=FunctionResult(function=function_to_call.metadata, value=None),
|
|
function_count=function_call_count or 0,
|
|
request_sequence_index=request_index or 0,
|
|
)
|
|
if function_call.index is not None:
|
|
invocation_context.function_sequence_index = function_call.index
|
|
|
|
stack = self.construct_call_stack(
|
|
filter_type=FilterTypes.AUTO_FUNCTION_INVOCATION,
|
|
inner_function=self._inner_auto_function_invoke_handler,
|
|
)
|
|
await stack(invocation_context)
|
|
|
|
if invocation_context.terminate:
|
|
return invocation_context
|
|
|
|
frc = FunctionResultContent.from_function_call_content_and_result(
|
|
function_call_content=function_call, result=invocation_context.function_result
|
|
)
|
|
chat_history.add_message(message=frc.to_chat_message_content())
|
|
return None
|
|
|
|
async def _inner_auto_function_invoke_handler(self, context: AutoFunctionInvocationContext):
|
|
"""Inner auto function invocation handler."""
|
|
try:
|
|
result = await context.function.invoke(context.kernel, context.arguments)
|
|
if result:
|
|
context.function_result = result
|
|
except Exception as exc:
|
|
logger.exception(f"Error invoking function {context.function.fully_qualified_name}: {exc}.")
|
|
value = f"An error occurred while invoking the function {context.function.fully_qualified_name}: {exc}"
|
|
if context.function_result is not None:
|
|
context.function_result.value = value
|
|
else:
|
|
context.function_result = FunctionResult(function=context.function.metadata, value=value)
|
|
return
|