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 😄
98 lines
3.1 KiB
Python
98 lines
3.1 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
import asyncio
|
|
|
|
from dotenv import dotenv_values
|
|
|
|
import semantic_kernel as sk
|
|
from semantic_kernel.connectors.ai.open_ai import (
|
|
AzureTextCompletion,
|
|
AzureTextEmbedding,
|
|
)
|
|
from semantic_kernel.connectors.memory.azure_cognitive_search import (
|
|
AzureCognitiveSearchMemoryStore,
|
|
)
|
|
from semantic_kernel.memory.semantic_text_memory import SemanticTextMemory
|
|
|
|
COLLECTION_NAME = "acs-index-sample"
|
|
|
|
|
|
async def populate_memory(memory: SemanticTextMemory) -> None:
|
|
# Add some documents to the ACS semantic memory
|
|
await memory.save_information(COLLECTION_NAME, id="info1", text="My name is Andrea")
|
|
await memory.save_information(COLLECTION_NAME, id="info2", text="I currently work as a tour guide")
|
|
await memory.save_information(COLLECTION_NAME, id="info3", text="I've been living in Seattle since 2005")
|
|
await memory.save_information(
|
|
COLLECTION_NAME,
|
|
id="info4",
|
|
text="I visited France and Italy five times since 2015",
|
|
)
|
|
await memory.save_information(COLLECTION_NAME, id="info5", text="My family is from New York")
|
|
|
|
|
|
async def search_acs_memory_questions(memory: SemanticTextMemory) -> None:
|
|
questions = [
|
|
"what's my name",
|
|
"where do I live?",
|
|
"where's my family from?",
|
|
"where have I traveled?",
|
|
"what do I do for work",
|
|
]
|
|
|
|
for question in questions:
|
|
print(f"Question: {question}")
|
|
result = await memory.search(COLLECTION_NAME, question)
|
|
print(f"Answer: {result[0].text}\n")
|
|
|
|
|
|
async def main() -> None:
|
|
kernel = sk.Kernel()
|
|
|
|
config = dotenv_values(".env")
|
|
|
|
AZURE_COGNITIVE_SEARCH_ENDPOINT = config["AZURE_COGNITIVE_SEARCH_ENDPOINT"]
|
|
AZURE_COGNITIVE_SEARCH_ADMIN_KEY = config["AZURE_COGNITIVE_SEARCH_ADMIN_KEY"]
|
|
AZURE_OPENAI_API_KEY = config["AZURE_OPENAI_API_KEY"]
|
|
AZURE_OPENAI_ENDPOINT = config["AZURE_OPENAI_ENDPOINT"]
|
|
vector_size = 1536
|
|
|
|
# Setting up OpenAI services for text completion and text embedding
|
|
text_complete_service_id = "dv"
|
|
kernel.add_service(
|
|
AzureTextCompletion(
|
|
service_id=text_complete_service_id,
|
|
deployment_name="text-embedding-ada-002",
|
|
endpoint=AZURE_OPENAI_ENDPOINT,
|
|
api_key=AZURE_OPENAI_API_KEY,
|
|
),
|
|
)
|
|
embedding_service_id = ("ada",)
|
|
embedding_gen = AzureTextEmbedding(
|
|
service_id=embedding_service_id,
|
|
deployment_name="text-embedding-ada-002",
|
|
endpoint=AZURE_OPENAI_ENDPOINT,
|
|
api_key=AZURE_OPENAI_API_KEY,
|
|
)
|
|
kernel.add_service(
|
|
embedding_gen,
|
|
)
|
|
|
|
acs_connector = AzureCognitiveSearchMemoryStore(
|
|
vector_size, AZURE_COGNITIVE_SEARCH_ENDPOINT, AZURE_COGNITIVE_SEARCH_ADMIN_KEY
|
|
)
|
|
|
|
memory = SemanticTextMemory(storage=acs_connector, embeddings_generator=embedding_gen)
|
|
kernel.add_plugin(sk.core_plugins.TextMemoryPlugin(memory), "TextMemoryPlugin")
|
|
|
|
print("Populating memory...")
|
|
await populate_memory(kernel)
|
|
|
|
print("Asking questions... (manually)")
|
|
await search_acs_memory_questions(kernel)
|
|
|
|
await acs_connector.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|