mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### Motivation and Context Add an example of using the Stepwise Planner in notebook 5 --------- Co-authored-by: Alex Chao <achao@achao> Co-authored-by: Abby Harrison <[email protected]>
17 KiB
17 KiB
In [ ]:
!python -m pip install semantic-kernel==0.3.12.dev0In [ ]:
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion, AzureChatCompletion
kernel = sk.Kernel()
useAzureOpenAI = True
# Configure AI backend used by the kernel
if useAzureOpenAI:
deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()
kernel.add_chat_service("gpt-3.5", AzureChatCompletion(deployment, endpoint, api_key))
else:
api_key, org_id = sk.openai_settings_from_dot_env()
kernel.add_chat_service("gpt-3.5", OpenAIChatCompletion("gpt-3.5-turbo", api_key, org_id))In [ ]:
ask = """
Tomorrow is Valentine's day. I need to come up with a few date ideas. She speaks French so write it in French.
Convert the text to uppercase"""In [ ]:
from semantic_kernel.core_skills.text_skill import TextSkill
skills_directory = "../../samples/skills/"
summarize_skill = kernel.import_semantic_skill_from_directory(skills_directory, "SummarizeSkill")
writer_skill = kernel.import_semantic_skill_from_directory(skills_directory, "WriterSkill")
text_skill = kernel.import_skill(TextSkill(), "TextSkill")In [ ]:
from semantic_kernel.planning.basic_planner import BasicPlanner
planner = BasicPlanner()In [ ]:
basic_plan = await planner.create_plan_async(ask, kernel)In [ ]:
print(basic_plan.generated_plan)In [ ]:
sk_prompt = """
{{$input}}
Rewrite the above in the style of Shakespeare.
"""
shakespeareFunction = kernel.create_semantic_function(sk_prompt, "shakespeare", "ShakespeareSkill",
max_tokens=2000, temperature=0.8)In [ ]:
ask = """
Tomorrow is Valentine's day. I need to come up with a few date ideas.
She likes Shakespeare so write using his style. She speaks French so write it in French.
Convert the text to uppercase."""
new_plan = await planner.create_plan_async(ask, kernel)In [ ]:
print(new_plan.generated_plan)In [ ]:
results = await planner.execute_plan_async(new_plan, kernel)In [ ]:
print(results)In [ ]:
from semantic_kernel.planning import SequentialPlanner
planner = SequentialPlanner(kernel)In [ ]:
sequential_plan = await planner.create_plan_async(goal=ask)In [ ]:
for step in sequential_plan._steps:
print(step.description, ":", step._state.__dict__)In [ ]:
result = await sequential_plan.invoke_async()In [ ]:
print(result)In [ ]:
from semantic_kernel.planning import ActionPlanner
planner = ActionPlanner(kernel)In [ ]:
from semantic_kernel.core_skills import FileIOSkill, MathSkill, TextSkill, TimeSkill
kernel.import_skill(MathSkill(), "math")
kernel.import_skill(FileIOSkill(), "fileIO")
kernel.import_skill(TimeSkill(), "time")
kernel.import_skill(TextSkill(), "text")In [ ]:
ask = "What is the sum of 110 and 990?"In [ ]:
plan = await planner.create_plan_async(goal=ask)In [ ]:
result = await plan.invoke_async()In [ ]:
print(result)In [ ]:
from semantic_kernel.planning import StepwisePlanner
from semantic_kernel.planning.stepwise_planner.stepwise_planner_config import (
StepwisePlannerConfig,
)In [ ]:
class WebSearchEngineSkill:
"""
A search engine skill.
"""
from semantic_kernel.orchestration.sk_context import SKContext
from semantic_kernel.skill_definition import sk_function, sk_function_context_parameter
def __init__(self, connector) -> None:
self._connector = connector
@sk_function(
description="Performs a web search for a given query", name="searchAsync"
)
@sk_function_context_parameter(
name="query",
description="The search query",
)
async def search_async(self, query: str, context: SKContext) -> str:
query = query or context.variables.get("query")[1]
result = await self._connector.search_async(query, num_results=5, offset=0)
return str(result)In [ ]:
from semantic_kernel.connectors.search_engine import BingConnector
BING_API_KEY = sk.bing_search_settings_from_dot_env()
connector = BingConnector(BING_API_KEY)
kernel.import_skill(WebSearchEngineSkill(connector), skill_name="WebSearch")In [ ]:
from semantic_kernel.core_skills.math_skill import MathSkill
from semantic_kernel.core_skills.time_skill import TimeSkill
kernel.import_skill(TimeSkill(), "time")
kernel.import_skill(MathSkill(), "math")In [ ]:
planner = StepwisePlanner(
kernel, StepwisePlannerConfig(max_iterations=10, min_iteration_time_ms=1000)
)In [ ]:
ask = """How many total championships combined do the top 5 teams in the NBA have?"""
plan = planner.create_plan(goal=ask)In [ ]:
result = await plan.invoke_async()In [ ]:
print(result)In [ ]:
for index, step in enumerate(plan._steps):
print("Step:", index)
print("Description:",step.description)
print("Function:", step.skill_name + "." + step._function.name)
if len(step._outputs) > 0:
print( " Output:\n", str.replace(result[step._outputs[0]],"\n", "\n "))In [ ]: