Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Updated plugins #5827

Merged
merged 16 commits into from
Apr 15, 2024
Merged

Conversation

eavanvalkenburg
Copy link
Member

@eavanvalkenburg eavanvalkenburg commented Apr 10, 2024

Motivation and Context

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:

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

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

Also closes: #5855

Description

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

@eavanvalkenburg eavanvalkenburg requested a review from a team as a code owner April 10, 2024 14:27
@eavanvalkenburg eavanvalkenburg marked this pull request as draft April 10, 2024 14:27
@markwallace-microsoft markwallace-microsoft added the python Pull requests for the Python Semantic Kernel label Apr 10, 2024
Copy link
Contributor

@moonbox3 moonbox3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really exciting work. It's going to make working with functions/plugins so much better! Thank you for doing the work.

python/semantic_kernel/kernel.py Show resolved Hide resolved
python/semantic_kernel/kernel.py Outdated Show resolved Hide resolved
python/semantic_kernel/kernel.py Show resolved Hide resolved
python/semantic_kernel/kernel.py Show resolved Hide resolved
python/semantic_kernel/kernel.py Show resolved Hide resolved
python/semantic_kernel/functions/kernel_plugin.py Outdated Show resolved Hide resolved
python/semantic_kernel/functions/__init__.py Outdated Show resolved Hide resolved
@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Apr 11, 2024

Py3.8 Test Coverage

Python 3.8 Test Coverage Report •
FileStmtsMissCoverMissing
semantic_kernel
   kernel.py2612889%122, 124, 182–185, 187–191, 193–196, 203, 210–219, 254–257, 267–272, 277, 281–284, 466–467
semantic_kernel/connectors/ai/open_ai
   utils.py462154%27–28, 51–55, 62–63, 104, 142, 144, 146, 148, 150, 155–162
semantic_kernel/connectors/openai_plugin
   openai_utils.py181044%20–31
semantic_kernel/connectors/openapi_plugin
   openapi_manager.py1411589%13, 61–74, 104, 145, 150, 280, 348–361
semantic_kernel/core_plugins
   conversation_summary_plugin.py20955%6, 47–48, 75–84
semantic_kernel/functions
   kernel_function.py72297%141, 160
   kernel_function_from_prompt.py162498%170, 198, 299, 369
semantic_kernel/planners
   basic_planner.py644825%19–21, 24, 135, 143–167, 179–202, 211–241
   planner_extensions.py32320%3–70
semantic_kernel/planners/action_planner
   action_planner.py1162281%11, 105, 114, 127, 131–132, 142–149, 162, 187, 245–246, 263, 280–291
semantic_kernel/planners/function_calling_stepwise_planner
   function_calling_stepwise_planner.py1023863%122–185, 219–224
semantic_kernel/planners/sequential_planner
   sequential_planner.py65691%71, 75, 106, 122, 131–132
   sequential_planner_extensions.py51982%28–29, 50, 101–115
   sequential_planner_parser.py771284%63–72, 92, 115–118
semantic_kernel/planners/stepwise_planner
   stepwise_planner.py22413938%12, 109–127, 136–206, 227–230, 238–249, 252, 260–275, 278–314, 317–345, 348–353, 356–368, 371–374, 388–400
semantic_kernel/prompt_template
   jinja2_prompt_template.py43393%61–63
   kernel_prompt_template.py85396%119–121
semantic_kernel/template_engine/blocks
   code_block.py70199%130
TOTAL5545101082% 

Python 3.8 Unit Test Overview

Tests Skipped Failures Errors Time
1211 11 💤 0 ❌ 0 🔥 22.188s ⏱️

@eavanvalkenburg eavanvalkenburg force-pushed the updated_plugins branch 2 times, most recently from 1a37b12 to eba56c0 Compare April 12, 2024 12:26
@eavanvalkenburg eavanvalkenburg marked this pull request as ready for review April 12, 2024 12:37
Copy link
Contributor

@moonbox3 moonbox3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome

Copy link
Contributor

@juliomenendez juliomenendez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean! LGTM

@eavanvalkenburg eavanvalkenburg added this pull request to the merge queue Apr 15, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 15, 2024
@eavanvalkenburg eavanvalkenburg added this pull request to the merge queue Apr 15, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 15, 2024
@eavanvalkenburg eavanvalkenburg added this pull request to the merge queue Apr 15, 2024
Merged via the queue into microsoft:main with commit 6751f9d Apr 15, 2024
30 checks passed
@eavanvalkenburg eavanvalkenburg deleted the updated_plugins branch April 15, 2024 13:56
johnoliver pushed a commit to johnoliver/semantic-kernel that referenced this pull request Jun 5, 2024
### 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: microsoft#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 😄
johnoliver pushed a commit to johnoliver/semantic-kernel that referenced this pull request Jun 5, 2024
### 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: microsoft#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 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation python Pull requests for the Python Semantic Kernel
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Python: Allow Importing native plugins with constructor args from directory.
4 participants