mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 11:19:56 +00:00
### Motivation and Context As we move toward v1, it is necessary to remove the single kernel memory attribute so that memory connectors can be handled via plugins. This change also refactors the `import_plugin` method name to be similar with dotnet as `import_plugin_from_object`, and introduces a simple `kernel.invoke_prompt` method. <!-- 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. --> ### Description Updates are: - The memory connector is now passed to the `SemanticTextMemory` class, along with the embedding generator. This allows the user to add memory to multiple plugins/services as needed. Closes #4632 - Kernel Examples and Notebooks were updated to reflect these changes. Now using Service Enum for all notebooks. Closes #4557 - There were planners using memory related to the kernel, to store the functions, and many of these planners are non-existent in dotnet's v1 SDK. Python has pending work to update the planner/remove deprecated planners before getting to v1. - A Kernel method called `invoke_prompt` was introduced to invoke a simple prompt, and return the `FunctionResult` when complete. - All unit/integration tests are passing. <!-- 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 - [ ] I didn't break anyone 😄
117 lines
3.9 KiB
Python
117 lines
3.9 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
import asyncio
|
|
|
|
import semantic_kernel as sk
|
|
import semantic_kernel.connectors.ai.open_ai as sk_oai
|
|
from semantic_kernel.core_plugins.text_memory_plugin import TextMemoryPlugin
|
|
from semantic_kernel.memory.semantic_text_memory import SemanticTextMemory
|
|
from semantic_kernel.prompt_template.prompt_template_config import PromptTemplateConfig
|
|
|
|
collection_id = "generic"
|
|
|
|
|
|
async def populate_memory(memory: SemanticTextMemory) -> None:
|
|
# Add some documents to the semantic memory
|
|
await memory.save_information(collection=collection_id, id="info1", text="Your budget for 2024 is $100,000")
|
|
await memory.save_information(collection=collection_id, id="info2", text="Your savings from 2023 are $50,000")
|
|
await memory.save_information(collection=collection_id, id="info3", text="Your investments are $80,000")
|
|
|
|
|
|
async def search_memory_examples(memory: SemanticTextMemory) -> None:
|
|
questions = ["What is my budget for 2024?", "What are my savings from 2023?", "What are my investments?"]
|
|
|
|
for question in questions:
|
|
print(f"Question: {question}")
|
|
result = await memory.search(collection_id, question)
|
|
print(f"Answer: {result[0].text}\n")
|
|
|
|
|
|
async def setup_chat_with_memory(
|
|
kernel: sk.Kernel,
|
|
service_id: str,
|
|
) -> sk.KernelFunction:
|
|
prompt = """
|
|
ChatBot can have a conversation with you about any topic.
|
|
It can give explicit instructions or say 'I don't know' if
|
|
it does not have an answer.
|
|
|
|
Information about me, from previous conversations:
|
|
- {{recall 'budget by year'}} What is my budget for 2024?
|
|
- {{recall 'savings from previous year'}} What are my savings from 2023?
|
|
- {{recall 'investments'}} What are my investments?
|
|
|
|
{{$request}}
|
|
""".strip()
|
|
|
|
prompt_template_config = PromptTemplateConfig(
|
|
template=prompt,
|
|
execution_settings={
|
|
service_id: kernel.get_service(service_id).get_prompt_execution_settings_class()(service_id=service_id)
|
|
},
|
|
)
|
|
|
|
chat_func = kernel.create_function_from_prompt(prompt_template_config=prompt_template_config)
|
|
|
|
return chat_func
|
|
|
|
|
|
async def chat(kernel: sk.Kernel, chat_func: sk.KernelFunction) -> 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, request=user_input)
|
|
|
|
print(f"ChatBot:> {answer}")
|
|
return True
|
|
|
|
|
|
async def main() -> None:
|
|
kernel = sk.Kernel()
|
|
|
|
api_key, org_id = sk.openai_settings_from_dot_env()
|
|
service_id = "chat-gpt"
|
|
kernel.add_service(
|
|
sk_oai.OpenAIChatCompletion(service_id=service_id, ai_model_id="gpt-3.5-turbo", api_key=api_key, org_id=org_id)
|
|
)
|
|
embedding_gen = sk_oai.OpenAITextEmbedding(
|
|
service_id="ada", ai_model_id="text-embedding-ada-002", api_key=api_key, org_id=org_id
|
|
)
|
|
kernel.add_service(embedding_gen)
|
|
|
|
memory = SemanticTextMemory(storage=sk.memory.VolatileMemoryStore(), embeddings_generator=embedding_gen)
|
|
kernel.import_plugin_from_object(TextMemoryPlugin(memory), "TextMemoryPlugin")
|
|
|
|
print("Populating memory...")
|
|
await populate_memory(memory)
|
|
|
|
print("Asking questions... (manually)")
|
|
await search_memory_examples(memory)
|
|
|
|
print("Setting up a chat (with memory!)")
|
|
chat_func = await setup_chat_with_memory(kernel, service_id)
|
|
|
|
print("Begin chatting (type 'exit' to exit):\n")
|
|
print(
|
|
"Welcome to the chat bot!\
|
|
\n Type 'exit' to exit.\
|
|
\n Try asking a question about your finances (i.e. \"talk to me about my finances\")."
|
|
)
|
|
chatting = True
|
|
while chatting:
|
|
chatting = await chat(kernel, chat_func)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|