mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### Motivation and Context Bump python project version for release. Update notebooks. <!-- 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 Bump python project version for release. Update notebooks. <!-- 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 😄
17 KiB
17 KiB
In [ ]:
!python -m pip install semantic-kernel==0.5.1.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(
"chat_completion",
AzureChatCompletion(deployment_name=deployment, endpoint=endpoint, api_key=api_key),
)
else:
api_key, org_id = sk.openai_settings_from_dot_env()
kernel.add_chat_service(
"gpt-4",
OpenAIChatCompletion(ai_model_id="gpt-3.5-turbo-1106", api_key=api_key, org_id=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_plugins.text_plugin import TextPlugin
plugins_directory = "../../samples/plugins/"
summarize_plugin = kernel.import_semantic_plugin_from_directory(plugins_directory, "SummarizePlugin")
writer_plugin = kernel.import_semantic_plugin_from_directory(plugins_directory, "WriterPlugin")
text_plugin = kernel.import_plugin(TextPlugin(), "TextPlugin")In [ ]:
from semantic_kernel.planning.basic_planner import BasicPlanner
planner = BasicPlanner()In [ ]:
basic_plan = await planner.create_plan(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(
prompt_template=sk_prompt,
function_name="shakespeare",
plugin_name="ShakespearePlugin",
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(ask, kernel)In [ ]:
print(new_plan.generated_plan)In [ ]:
results = await planner.execute_plan(new_plan, kernel)In [ ]:
print(results)In [ ]:
from semantic_kernel.planning import SequentialPlanner
planner = SequentialPlanner(kernel)In [ ]:
sequential_plan = await planner.create_plan(goal=ask)In [ ]:
for step in sequential_plan._steps:
print(step.description, ":", step._state.__dict__)In [ ]:
result = await sequential_plan.invoke()In [ ]:
print(result)In [ ]:
from semantic_kernel.planning import ActionPlanner
planner = ActionPlanner(kernel)In [ ]:
from semantic_kernel.core_plugins import (
FileIOPlugin,
MathPlugin,
TextPlugin,
TimePlugin,
)
kernel.import_plugin(MathPlugin(), "math")
kernel.import_plugin(FileIOPlugin(), "fileIO")
kernel.import_plugin(TimePlugin(), "time")
kernel.import_plugin(TextPlugin(), "text")In [ ]:
ask = "What is the sum of 110 and 990?"In [ ]:
plan = await planner.create_plan(goal=ask)In [ ]:
result = await plan.invoke()In [ ]:
print(result)In [ ]:
from semantic_kernel.planning import StepwisePlanner
from semantic_kernel.planning.stepwise_planner.stepwise_planner_config import (
StepwisePlannerConfig,
)In [ ]:
class WebSearchEnginePlugin:
"""
A search engine plugin.
"""
from semantic_kernel.orchestration.kernel_context import KernelContext
from semantic_kernel.plugin_definition import (
kernel_function,
kernel_function_context_parameter,
)
def __init__(self, connector) -> None:
self._connector = connector
@kernel_function(description="Performs a web search for a given query", name="searchAsync")
@kernel_function_context_parameter(
name="query",
description="The search query",
)
async def search(self, query: str, context: KernelContext) -> str:
query = query or context.variables.get("query")[1]
result = await self._connector.search(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_plugin(WebSearchEnginePlugin(connector), plugin_name="WebSearch")In [ ]:
from semantic_kernel.core_plugins.math_plugin import MathPlugin
from semantic_kernel.core_plugins.time_plugin import TimePlugin
kernel.import_plugin(TimePlugin(), "time")
kernel.import_plugin(MathPlugin(), "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()In [ ]:
print(result)In [ ]:
for index, step in enumerate(plan._steps):
print("Step:", index)
print("Description:", step.description)
print("Function:", step.plugin_name + "." + step._function.name)
if len(step._outputs) > 0:
print(" Output:\n", str.replace(result[step._outputs[0]], "\n", "\n "))