diff --git a/python/semantic_kernel/functions/kernel_function.py b/python/semantic_kernel/functions/kernel_function.py index 15f2ab34c6b7..d6386480d31d 100644 --- a/python/semantic_kernel/functions/kernel_function.py +++ b/python/semantic_kernel/functions/kernel_function.py @@ -138,7 +138,28 @@ def from_prompt( "PromptExecutionSettings | Sequence[PromptExecutionSettings] | Mapping[str, PromptExecutionSettings] | None" ) = None, ) -> "KernelFunctionFromPrompt": - """Create a new instance of the KernelFunctionFromPrompt class.""" + """Create a new instance of the KernelFunctionFromPrompt class. + + Example: + Create a prompt-based function and register it with the kernel: + + .. code-block:: python + + from semantic_kernel import Kernel + from semantic_kernel.functions import KernelFunction + + kernel = Kernel() + func = KernelFunction.from_prompt( + function_name="summarize", + plugin_name="WriterPlugin", + description="Summarizes the provided text in one sentence.", + prompt="Summarize the following in one sentence: {{$input}}", + ) + kernel.add_function(plugin_name="WriterPlugin", function=func) + print(func.name) # summarize + print(func.plugin_name) # WriterPlugin + print(func.is_prompt) # True + """ from semantic_kernel.functions.kernel_function_from_prompt import KernelFunctionFromPrompt return KernelFunctionFromPrompt( @@ -248,13 +269,43 @@ async def invoke( Args: kernel (Kernel): The kernel - arguments (KernelArguments): The Kernel arguments - metadata (Dict[str, Any]): Additional metadata. + arguments (KernelArguments | None): The Kernel arguments. Optional; defaults to None, + in which case arguments are built from kwargs. + metadata (dict[str, Any] | None): Additional metadata. Optional; defaults to None. kwargs (Any): Additional keyword arguments that will be added to the KernelArguments. Returns: - FunctionResult: The result of the function + FunctionResult | None: The result of the function, or None if no result is produced. + + Example: + Invoke a prompt function with arguments: + + .. code-block:: python + + import asyncio + from semantic_kernel import Kernel + from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion + from semantic_kernel.functions import KernelArguments, KernelFunction + + + async def main(): + kernel = Kernel() + # Requires OPENAI_API_KEY env var (or pass api_key explicitly). + kernel.add_service(OpenAIChatCompletion(ai_model_id="gpt-4o-mini", service_id="default")) + func = KernelFunction.from_prompt( + function_name="summarize", + plugin_name="WriterPlugin", + prompt="Summarize the following in one sentence: {{$input}}", + ) + result = await func.invoke( + kernel=kernel, + arguments=KernelArguments(input="Azure API Management is a fully managed gateway service."), + ) + print(result) + + + asyncio.run(main()) """ if arguments is None: arguments = KernelArguments(**kwargs)