mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 11:19:56 +00:00
### Motivation and Context Fix planner notebook link to plan.py file <!-- 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 Fix planner notebook link to plan.py file. - Closes #9041 <!-- 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 - [X] I didn't break anyone 😄
18 KiB
18 KiB
In [ ]:
# Note: if using a virtual environment, do not run this cell
%pip install -U semantic-kernel
from semantic_kernel import __version__
__version__In [ ]:
# Make sure paths are correct for the imports
import os
import sys
notebook_dir = os.path.abspath("")
parent_dir = os.path.dirname(notebook_dir)
grandparent_dir = os.path.dirname(parent_dir)
sys.path.append(grandparent_dir)In [ ]:
from services import Service
from samples.service_settings import ServiceSettings
service_settings = ServiceSettings.create()
# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)
selectedService = (
Service.AzureOpenAI
if service_settings.global_llm_service is None
else Service(service_settings.global_llm_service.lower())
)
print(f"Using service type: {selectedService}")In [ ]:
from semantic_kernel.contents.chat_history import ChatHistory # noqa: F401
from semantic_kernel.functions.kernel_arguments import KernelArguments # noqa: F401
from semantic_kernel.prompt_template.input_variable import InputVariable # noqa: F401In [ ]:
ask = """
Tomorrow is Valentine's day. I need to come up with a few short poems.
She likes Shakespeare so write using his style. She speaks French so write it in French.
Convert the text to uppercase."""In [ ]:
from semantic_kernel.connectors.ai.open_ai import OpenAIChatPromptExecutionSettings
from semantic_kernel.core_plugins.text_plugin import TextPlugin
from semantic_kernel.functions.kernel_function_from_prompt import KernelFunctionFromPrompt
from semantic_kernel.kernel import Kernel
kernel = Kernel()
service_id = None
if selectedService == Service.OpenAI:
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
service_id = "default"
kernel.add_service(
OpenAIChatCompletion(
service_id=service_id,
),
)
elif selectedService == Service.AzureOpenAI:
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
service_id = "default"
kernel.add_service(
AzureChatCompletion(
service_id=service_id,
),
)
plugins_directory = "../../../prompt_template_samples/"
summarize_plugin = kernel.add_plugin(plugin_name="SummarizePlugin", parent_directory=plugins_directory)
writer_plugin = kernel.add_plugin(
plugin_name="WriterPlugin",
parent_directory=plugins_directory,
)
text_plugin = kernel.add_plugin(plugin=TextPlugin(), plugin_name="TextPlugin")
shakespeare_func = KernelFunctionFromPrompt(
function_name="Shakespeare",
plugin_name="WriterPlugin",
prompt="""
{{$input}}
Rewrite the above in the style of Shakespeare.
""",
prompt_execution_settings=OpenAIChatPromptExecutionSettings(
service_id=service_id,
max_tokens=2000,
temperature=0.8,
),
description="Rewrite the input in the style of Shakespeare.",
)
kernel.add_function(plugin_name="WriterPlugin", function=shakespeare_func)
for plugin_name, plugin in kernel.plugins.items():
for function_name, function in plugin.functions.items():
print(f"Plugin: {plugin_name}, Function: {function_name}")In [ ]:
from semantic_kernel.planners import SequentialPlanner
planner = SequentialPlanner(kernel, service_id)In [ ]:
sequential_plan = await planner.create_plan(goal=ask)In [ ]:
print("The plan's steps are:")
for step in sequential_plan._steps:
print(
f"- {step.description.replace('.', '') if step.description else 'No description'} using {step.metadata.fully_qualified_name} with parameters: {step.parameters}"
)In [ ]:
result = await sequential_plan.invoke(kernel)In [ ]:
print(result)In [ ]:
from semantic_kernel.kernel import Kernel
kernel = Kernel()
service_id = None
if selectedService == Service.OpenAI:
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
service_id = "default"
kernel.add_service(
OpenAIChatCompletion(
service_id=service_id,
),
)
elif selectedService == Service.AzureOpenAI:
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
service_id = "default"
kernel.add_service(
AzureChatCompletion(
service_id=service_id,
),
)In [ ]:
from typing import Annotated
from semantic_kernel.functions.kernel_function_decorator import kernel_function
class EmailPlugin:
"""
Description: EmailPlugin provides a set of functions to send emails.
Usage:
kernel.import_plugin_from_object(EmailPlugin(), plugin_name="email")
Examples:
{{email.SendEmail}} => Sends an email with the provided subject and body.
"""
@kernel_function(name="SendEmail", description="Given an e-mail and message body, send an e-email")
def send_email(
self,
subject: Annotated[str, "the subject of the email"],
body: Annotated[str, "the body of the email"],
) -> Annotated[str, "the output is a string"]:
"""Sends an email with the provided subject and body."""
return f"Email sent with subject: {subject} and body: {body}"
@kernel_function(name="GetEmailAddress", description="Given a name, find the email address")
def get_email_address(
self,
input: Annotated[str, "the name of the person"],
):
email = ""
if input == "Jane":
email = "[email protected]"
elif input == "Paul":
email = "[email protected]"
elif input == "Mary":
email = "[email protected]"
else:
email = "[email protected]"
return emailIn [ ]:
kernel.add_plugin(plugin_name="EmailPlugin", plugin=EmailPlugin())In [ ]:
from semantic_kernel.core_plugins.math_plugin import MathPlugin
from semantic_kernel.core_plugins.time_plugin import TimePlugin
kernel.add_plugin(plugin_name="MathPlugin", plugin=MathPlugin())
kernel.add_plugin(plugin_name="TimePlugin", plugin=TimePlugin())In [ ]:
from semantic_kernel.planners.function_calling_stepwise_planner import (
FunctionCallingStepwisePlanner,
FunctionCallingStepwisePlannerOptions,
)
questions = [
"What is the current hour number, plus 5?",
"What is 387 minus 22? Email the solution to John and Mary.",
"Write a limerick, translate it to Spanish, and send it to Jane",
]
options = FunctionCallingStepwisePlannerOptions(
max_iterations=10,
max_tokens=4000,
)
planner = FunctionCallingStepwisePlanner(service_id=service_id, options=options)In [ ]:
for question in questions:
result = await planner.invoke(kernel, question)
print(f"Q: {question}\nA: {result.final_answer}\n")
# Uncomment the following line to view the planner's process for completing the request
# print(f"Chat history: {result.chat_history}\n")