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.
-->
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 😄
92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
import asyncio
|
|
|
|
import semantic_kernel as sk
|
|
import semantic_kernel.connectors.ai.google_palm as sk_gp
|
|
from semantic_kernel.contents.chat_history import ChatHistory
|
|
from semantic_kernel.prompt_template.input_variable import InputVariable
|
|
|
|
"""
|
|
System messages prime the assistant with different personalities or behaviors.
|
|
The system message is added to the prompt template, and a chat history can be
|
|
added as well to provide further context.
|
|
A system message can only be used once at the start of the conversation, and
|
|
conversation history persists with the instance of GooglePalmChatCompletion. To
|
|
overwrite the system message and start a new conversation, you must create a new
|
|
instance of GooglePalmChatCompletion.
|
|
Sometimes, PaLM struggles to use the information in the prompt template. In this
|
|
case, it is recommended to experiment with the messages in the prompt template
|
|
or ask different questions.
|
|
"""
|
|
|
|
system_message = """
|
|
You are a chat bot. Your name is Blackbeard
|
|
and you speak in the style of a swashbuckling
|
|
pirate. You reply with brief, to-the-point answers
|
|
with no elaboration. Your full name is Captain
|
|
Bartholomew "Blackbeard" Thorne.
|
|
"""
|
|
|
|
kernel = sk.Kernel()
|
|
api_key = sk.google_palm_settings_from_dot_env()
|
|
service_id = "models/chat-bison-001"
|
|
palm_chat_completion = sk_gp.GooglePalmChatCompletion(service_id, api_key)
|
|
kernel.add_service(palm_chat_completion)
|
|
|
|
req_settings = kernel.get_service(service_id).get_prompt_execution_settings_class()(service_id=service_id)
|
|
req_settings.max_tokens = 2000
|
|
req_settings.temperature = 0.7
|
|
req_settings.top_p = 0.8
|
|
|
|
prompt_template_config = sk.PromptTemplateConfig(
|
|
template="{{$user_input}}",
|
|
name="chat",
|
|
template_format="semantic-kernel",
|
|
input_variables=[
|
|
InputVariable(name="user_input", description="The user input", is_required=True),
|
|
InputVariable(name="chat_history", description="The history of the conversation", is_required=True),
|
|
],
|
|
execution_settings=req_settings,
|
|
)
|
|
|
|
chat_func = kernel.add_function(
|
|
plugin_name="PiratePlugin", function_name="Chat", prompt_template_config=prompt_template_config
|
|
)
|
|
|
|
chat_history = ChatHistory()
|
|
chat_history.add_system_message(system_message)
|
|
chat_history.add_user_message("Hi there, who are you?")
|
|
chat_history.add_assistant_message("I am Blackbeard.")
|
|
|
|
|
|
async def chat() -> bool:
|
|
try:
|
|
user_input = input("User:> ")
|
|
except KeyboardInterrupt:
|
|
print("\n\nExiting chat...")
|
|
return False
|
|
except EOFError:
|
|
print("\n\nExiting chat...")
|
|
return False
|
|
|
|
if user_input == "exit":
|
|
print("\n\nExiting chat...")
|
|
return False
|
|
|
|
answer = await kernel.invoke(chat_func, user_input=user_input, chat_history=chat_history)
|
|
print(f"Blackbeard:> {answer}")
|
|
chat_history.add_user_message(user_input)
|
|
chat_history.add_assistant_message(str(answer))
|
|
return True
|
|
|
|
|
|
async def main() -> None:
|
|
chatting = True
|
|
while chatting:
|
|
chatting = await chat()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|