Files
semantic-kernel/python/semantic_kernel/planners/sequential_planner/sequential_planner_parser.py
T
Eduard van Valkenburg 6751f9d819 Python: Updated plugins (#5827)
### 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.
-->

The loading of plugins and functions into Kernel was not fully
consistent and clear, this is now simplified.

Adding a plugin can now be done in two ways:
- multiple: `kernel.add_plugins[plugin1, plugin2]`
- single plugin: `kernel.add_plugin(plugin)`
- if the plugin here is a class, with methods that have the
kernel_function decorator then that is parsed into a plugin

Adding a function can now be done in three ways:
- multiple: `kernel.add_functions([func1, func2]` or
`kernel.add_function({'func1': func1})`
- single: `kernel.add_function('plugin_name', func1)`
- from a prompt: `kernel.add_function(function_name='func1',
prompt='test prompt', ...)`

In other words, all the different methods that were available have been
simplified, these methods also return the created plugin (or updated
plugin for the `add_functions` method. The `add_function` (singular)
method has a parameter `return_plugin` to control whether you get the
created or updated plugin, instead of the created function.

**An important callout:**

One big internal change that this introduces is that a function that
gets added to a plugin (new or existing) is automatically copied, the
metadata is deep-copied and the plugin_name in the metadata is updated,
so this sequence is valid now:

```python
@kernel_function(name='test')
def f(...):
   etc

func = KernelFunctionFromMethod(method=f)
func2 = kernel.add_function('plugin', func)
assert func != func2
```

Also closes: #5855

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Removed KernelPluginCollection
Updated KernelPlugin
   added from_... method to load from different sources
Updated KernelFunctionFromPrompt
   added from_yaml and from_directory methods.

Added and updated tests to 99% coverage of KernelPlugin and
KernelFunctionFromPrompt

### 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
- [ ] I didn't break anyone 😄
2024-04-15 13:51:59 +00:00

119 lines
4.9 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
import re
from typing import Callable, Optional, Tuple
from defusedxml import ElementTree as ET
from semantic_kernel.exceptions import PlannerInvalidPlanError
from semantic_kernel.exceptions.kernel_exceptions import KernelFunctionNotFoundError, KernelPluginNotFoundError
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.functions.kernel_function import KernelFunction
from semantic_kernel.kernel import Kernel
from semantic_kernel.planners.plan import Plan
# Constants
GOAL_TAG = "goal"
SOLUTION_TAG = "plan"
FUNCTION_TAG = "function."
SET_CONTEXT_VARIABLE_TAG = "setContextVariable"
APPEND_TO_RESULT_TAG = "appendToResult"
class SequentialPlanParser:
@staticmethod
def to_plan_from_xml(
xml_string: str,
goal: str,
kernel: Kernel,
get_plugin_function: Optional[Callable[[str, str], Optional[KernelFunction]]] = None,
allow_missing_functions: bool = False,
):
xml_string = "<xml>" + xml_string + "</xml>"
try:
xml_doc = ET.fromstring(xml_string)
except ET.ParseError:
# Attempt to parse <plan> out of it
plan_regex = re.compile(r"<plan\b[^>]*>(.*?)</plan>", re.DOTALL)
match = plan_regex.search(xml_string)
if match:
plan_xml = match.group(0)
try:
xml_doc = ET.fromstring("<xml>" + plan_xml + "</xml>")
except ET.ParseError:
raise PlannerInvalidPlanError(f"Failed to parse plan xml strings: '{xml_string}' or '{plan_xml}'")
else:
raise PlannerInvalidPlanError(f"Failed to parse plan xml string: '{xml_string}'")
solution = xml_doc.findall(".//" + SOLUTION_TAG)
plan = Plan.from_goal(goal)
for solution_node in solution:
for child_node in solution_node:
if (
child_node.tag == "#text"
or child_node.tag == "#comment"
or (not child_node.tag.startswith(FUNCTION_TAG))
):
continue
plugin_function_name = child_node.tag.split(FUNCTION_TAG)[1]
if get_plugin_function:
try:
func = get_plugin_function(
*SequentialPlanParser.get_plugin_function_names(plugin_function_name)
)
except Exception as exc:
if allow_missing_functions:
plan.add_steps([Plan.from_goal(plugin_function_name)])
continue
else:
raise PlannerInvalidPlanError(f"Failed to find function '{plugin_function_name}'.") from exc
else:
try:
func = kernel.get_function_from_fully_qualified_function_name(plugin_function_name)
except (KernelFunctionNotFoundError, KernelPluginNotFoundError) as exc:
if allow_missing_functions:
plan.add_steps([Plan.from_goal(plugin_function_name)])
continue
else:
raise PlannerInvalidPlanError(
f"Failed to find function '{plugin_function_name}'.",
) from exc
plan_step = Plan.from_function(func)
function_variables = KernelArguments()
function_outputs = []
function_results = []
for p in func.metadata.parameters:
function_variables[p.name] = p.default_value
for attr in child_node.attrib:
if attr == SET_CONTEXT_VARIABLE_TAG:
function_outputs.append(child_node.attrib[attr])
elif attr == APPEND_TO_RESULT_TAG:
function_outputs.append(child_node.attrib[attr])
function_results.append(child_node.attrib[attr])
else:
function_variables[attr] = child_node.attrib[attr]
plan_step._outputs = function_outputs
plan_step._parameters = function_variables
for result in function_results:
plan._outputs.append(result)
plan.add_steps([plan_step])
return plan
@staticmethod
def get_plugin_function_names(plugin_function_name: str) -> Tuple[str, str]:
plugin_function_name_parts = plugin_function_name.split("-")
plugin_name = plugin_function_name_parts[0] if len(plugin_function_name_parts) > 0 else ""
function_name = plugin_function_name_parts[1] if len(plugin_function_name_parts) > 1 else plugin_function_name
return plugin_name, function_name