Skip to content

Commit

Permalink
Python: Add Stepwise planner example to planner notebook (#2675)
Browse files Browse the repository at this point in the history
### 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 <54643756+awharrison-28@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 15, 2023
1 parent 3028744 commit c74ba11
Showing 1 changed file with 195 additions and 3 deletions.
198 changes: 195 additions & 3 deletions python/notebooks/05-using-the-planner.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install semantic-kernel==0.3.10.dev0"
"!python -m pip install semantic-kernel==0.3.12.dev0"
]
},
{
Expand All @@ -44,7 +44,7 @@
"if useAzureOpenAI:\n",
" \n",
" deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
" kernel.add_chat_service(\"gpt-3.5\", AzureChatCompletion(\"gpt-35-turbo\", endpoint, api_key))\n",
" kernel.add_chat_service(\"gpt-3.5\", AzureChatCompletion(deployment, endpoint, api_key))\n",
"else:\n",
" api_key, org_id = sk.openai_settings_from_dot_env()\n",
" kernel.add_chat_service(\"gpt-3.5\", OpenAIChatCompletion(\"gpt-3.5-turbo\", api_key, org_id))"
Expand Down Expand Up @@ -444,6 +444,198 @@
"source": [
"print(result)"
]
},
{
"cell_type": "markdown",
"id": "789b651a",
"metadata": {},
"source": [
"# Stepwise Planner"
]
},
{
"cell_type": "markdown",
"id": "8a4bbcc3",
"metadata": {},
"source": [
"Stepwise Planner is based off the paper from MRKL (Modular Reasoning, Knowledge and Language) and is similar to other papers like ReACT (Reasoning and Acting in Language Models). At the core, the stepwise planner allows for the AI to form \"thoughts\" and \"observations\" and execute actions based off those to achieve a user's goal. This continues until all required functions are complete and a final output is generated.\n",
"\n",
"See a video walkthrough of Stepwise Planner [here.](https://youtu.be/DG_Ge1v0c4Q?si=T1CHaAm1vV0mWRHu)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "32839327",
"metadata": {},
"outputs": [],
"source": [
"from semantic_kernel.planning import StepwisePlanner\n",
"from semantic_kernel.planning.stepwise_planner.stepwise_planner_config import (\n",
" StepwisePlannerConfig,\n",
")"
]
},
{
"cell_type": "markdown",
"id": "e0a00bde",
"metadata": {},
"source": [
"Let's create a Bing Search native skill that we can pass in to the Kernel.\n",
"\n",
"Make sure you have a Bing Search API key in your `.env` file\n",
"\n",
"(https://www.microsoft.com/en-us/bing/apis/bing-web-search-api)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "92fc5de8",
"metadata": {},
"outputs": [],
"source": [
"class WebSearchEngineSkill:\n",
" \"\"\"\n",
" A search engine skill.\n",
" \"\"\"\n",
" from semantic_kernel.orchestration.sk_context import SKContext\n",
" from semantic_kernel.skill_definition import sk_function, sk_function_context_parameter\n",
"\n",
" def __init__(self, connector) -> None:\n",
" self._connector = connector\n",
"\n",
" @sk_function(\n",
" description=\"Performs a web search for a given query\", name=\"searchAsync\"\n",
" )\n",
" @sk_function_context_parameter(\n",
" name=\"query\",\n",
" description=\"The search query\",\n",
" )\n",
" async def search_async(self, query: str, context: SKContext) -> str:\n",
" query = query or context.variables.get(\"query\")[1]\n",
" result = await self._connector.search_async(query, num_results=5, offset=0)\n",
" return str(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "415f7876",
"metadata": {},
"outputs": [],
"source": [
"from semantic_kernel.connectors.search_engine import BingConnector\n",
"\n",
"BING_API_KEY = sk.bing_search_settings_from_dot_env()\n",
"connector = BingConnector(BING_API_KEY)\n",
"kernel.import_skill(WebSearchEngineSkill(connector), skill_name=\"WebSearch\")"
]
},
{
"cell_type": "markdown",
"id": "effdf3ab",
"metadata": {},
"source": [
"Let's also add a couple more skills"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abe150e0",
"metadata": {},
"outputs": [],
"source": [
"from semantic_kernel.core_skills.math_skill import MathSkill\n",
"from semantic_kernel.core_skills.time_skill import TimeSkill\n",
"\n",
"kernel.import_skill(TimeSkill(), \"time\")\n",
"kernel.import_skill(MathSkill(), \"math\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "06d08549",
"metadata": {},
"outputs": [],
"source": [
"planner = StepwisePlanner(\n",
" kernel, StepwisePlannerConfig(max_iterations=10, min_iteration_time_ms=1000)\n",
")"
]
},
{
"cell_type": "markdown",
"id": "50699ec3",
"metadata": {},
"source": [
"Now let's do a more complicated ask that will require planner to make a call to Bing to get the latest information."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "596ade21",
"metadata": {},
"outputs": [],
"source": [
"ask = \"\"\"How many total championships combined do the top 5 teams in the NBA have?\"\"\"\n",
"\n",
"plan = planner.create_plan(goal=ask)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "176988ac",
"metadata": {},
"outputs": [],
"source": [
"result = await plan.invoke_async()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d00c6f71",
"metadata": {},
"outputs": [],
"source": [
"print(result)"
]
},
{
"cell_type": "markdown",
"id": "cb40370d",
"metadata": {},
"source": [
"Let's see the steps that the AI took to get to the answer."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7159ca1b",
"metadata": {},
"outputs": [],
"source": [
"for index, step in enumerate(plan._steps):\n",
" print(\"Step:\", index)\n",
" print(\"Description:\",step.description)\n",
" print(\"Function:\", step.skill_name + \".\" + step._function.name)\n",
" if len(step._outputs) > 0:\n",
" print( \" Output:\\n\", str.replace(result[step._outputs[0]],\"\\n\", \"\\n \"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4652ac81",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -462,7 +654,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.10.11"
}
},
"nbformat": 4,
Expand Down

0 comments on commit c74ba11

Please sign in to comment.