Files
semantic-kernel/python/semantic_kernel/planners/planner_extensions.py
T
Eduard van Valkenburg fbb26b5cf6 Python: further improvements to checks and docs in dev_setup (#6445)
### 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.
-->
Added a section on documentation to the DEV_SETUP
Added a check for the copyright row to pre-commit
Closes #5629

### 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 😄
2024-05-31 13:06:41 +00:00

71 lines
2.9 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
import logging
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.functions.kernel_function_metadata import KernelFunctionMetadata
from semantic_kernel.kernel import Kernel
from semantic_kernel.planners.planner_options import PlannerOptions
logger: logging.Logger = logging.getLogger(__name__)
# NOTE these extensions will be used going forward by the FunctionCallingStepWisePlanner and others.
# The other sequential planner extensions will be left in right now for as long as we support that planner
# and will be removed once we deprecate it.
class PlannerFunctionExtension:
@staticmethod
def to_manual_string(function: KernelFunctionMetadata):
"""Convert the function to a string that can be used in the manual."""
inputs = [
f" - {parameter.name}: {parameter.description}"
+ (f" (default value: {parameter.default_value})" if parameter.default_value else "")
for parameter in function.parameters
]
inputs = "\n".join(inputs)
return f"{function.fully_qualified_name}:\n description: {function.description}\n inputs:\n {inputs}"
@staticmethod
def to_embedding_string(function: KernelFunctionMetadata):
"""Convert the function to a string that can be used as an embedding."""
inputs = "\n".join([f" - {parameter.name}: {parameter.description}" for parameter in function.parameters])
return f"{function.name}:\n description: {function.description}\n inputs:\n{inputs}"
class PlannerKernelExtension:
PLANNER_MEMORY_COLLECTION_NAME = " Planning.KernelFunctionManual"
PLAN_KERNEL_FUNCTIONS_ARE_REMEMBERED = "Planning.KernelFunctionsAreRemembered"
@staticmethod
async def get_functions_manual(
kernel: "Kernel",
arguments: KernelArguments,
options: PlannerOptions = None,
) -> str:
"""Get the string of the function."""
options = options or PlannerOptions()
if options.get_available_functions is None:
functions = await PlannerKernelExtension.get_available_functions(kernel, arguments, options)
else:
functions = await options.get_available_functions(options)
return "\n\n".join([PlannerFunctionExtension.to_manual_string(func) for func in functions])
@staticmethod
async def get_available_functions(
kernel: Kernel,
arguments: KernelArguments,
options: PlannerOptions,
):
"""Get the available functions for the kernel."""
excluded_plugins = options.excluded_plugins or []
excluded_functions = options.excluded_functions or []
return [
func
for func in kernel.get_list_of_function_metadata()
if (func.plugin_name not in excluded_plugins and func.name not in excluded_functions)
]