Skip to content

Commit

Permalink
Fixing issues in the Jupyter notebook for the planner (#873)
Browse files Browse the repository at this point in the history
### Motivation and Context

This change fixes the issue in dotnet Jupyternotebooks, for the planner examples
It useses different classes for the planer, and in addition it fixes few smaller issues regarding configuration and presenting of the resultes

### Description

1)  Changed the  used nuget version
2)  Changed  the old PlannerSkill class to SequentialPlanner
3)  local config files, for azure and open AI, both must have all the properties as otherwise Json deserializer throws an exception, as I used manually changed config files, had issue in configuring both of them, so I added missing json properties
4) in the Planner originalPlan.Variables.ToPlan().PlanString has been removed so I created a new method in Utils and used that one
  • Loading branch information
kaza committed May 10, 2023
1 parent 08f318a commit 7406233
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 164 deletions.
192 changes: 30 additions & 162 deletions samples/notebooks/dotnet/05-using-the-planner.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,26 @@
},
"outputs": [],
"source": [
"#r \"nuget: Microsoft.SemanticKernel, 0.12.207.1-preview\"\n",
"#r \"nuget: Microsoft.SemanticKernel, 0.13.442.1-preview\"\n",
"\n",
"#!import config/Settings.cs\n",
"#!import config/Utils.cs\n",
"\n",
"using Microsoft.SemanticKernel;\n",
"using Microsoft.SemanticKernel.CoreSkills;\n",
"using Microsoft.SemanticKernel.KernelExtensions;\n",
"using Microsoft.SemanticKernel.Orchestration;\n",
"using Microsoft.SemanticKernel.Planning;\n",
"using Microsoft.SemanticKernel.Planning.Sequential;\n",
"\n",
"IKernel kernel = KernelBuilder.Create();\n",
"\n",
"// Configure AI backend used by the kernel\n",
"var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = Settings.LoadFromFile();\n",
"\n",
"if (useAzureOpenAI)\n",
" kernel.Config.AddAzureTextCompletionService(\"davinci\", model, azureEndpoint, apiKey);\n",
" kernel.Config.AddAzureTextCompletionService( model, azureEndpoint, apiKey);\n",
"else\n",
" kernel.Config.AddOpenAITextCompletionService(\"davinci\", model, apiKey, orgId);"
" kernel.Config.AddOpenAITextCompletionService( model, apiKey, orgId);"
]
},
{
Expand All @@ -67,7 +70,7 @@
"outputs": [],
"source": [
"// Load native skill into the kernel registry, sharing its functions with prompt templates\n",
"var planner = kernel.ImportSkill(new PlannerSkill(kernel));"
"var planner = new SequentialPlanner(kernel);"
]
},
{
Expand Down Expand Up @@ -116,30 +119,13 @@
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Original plan:\n",
"\n",
"<goal>\n",
"Tomorrow is Valentine's day. I need to come up with a few date ideas and e-mail them to my significant other.\n",
"</goal>\n",
"<plan>\n",
" <function.WriterSkill.Brainstorm input=\"Valentine's Day Date Ideas\" setContextVariable=\"DATE_IDEAS\"/>\n",
" <function._GLOBAL_FUNCTIONS_.BucketOutputs input=\"$DATE_IDEAS\" bucketCount=\"3\" bucketLabelPrefix=\"DATE_IDEA\"/>\n",
" <function.WriterSkill.EmailTo to=\"My Significant Other\" input=\"$DATE_IDEA_1;$DATE_IDEA_2;$DATE_IDEA_3\" sender=\"Me\" />\n",
"</plan>\n"
]
}
],
"outputs": [],
"source": [
"var ask = \"Tomorrow is Valentine's day. I need to come up with a few date ideas and e-mail them to my significant other.\";\n",
"var originalPlan = await kernel.RunAsync(ask, planner[\"CreatePlan\"]);\n",
"var originalPlan = await planner.CreatePlanAsync(ask);\n",
"\n",
"Console.WriteLine(\"Original plan:\\n\");\n",
"Console.WriteLine(originalPlan.Variables.ToPlan().PlanString);"
"Console.WriteLine(JsonSerializer.Serialize(originalPlan, new JsonSerializerOptions { WriteIndented = true }));"
]
},
{
Expand Down Expand Up @@ -206,23 +192,20 @@
"outputs": [],
"source": [
"var ask = @\"Tomorrow is Valentine's day. I need to come up with a few date ideas.\n",
" She likes Shakespeare so write using his style. E-mail these ideas to my significant other\";"
"She likes Shakespeare so write using his style. E-mail these ideas to my significant other\";\n",
"var newPlan = await planner.CreatePlanAsync(ask);\n",
"Console.WriteLine(\"Updated plan:\\n\");\n",
"Console.WriteLine(JsonSerializer.Serialize(newPlan, new JsonSerializerOptions { WriteIndented = true }));"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"var newPlan = await kernel.RunAsync(ask, planner[\"CreatePlan\"]);"
"### Executing the plans\n",
"\n",
"Now that we have different plans, let's try to execute them! The Kernel can execute the plan using RunAsync."
]
},
{
Expand All @@ -236,38 +219,20 @@
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated plan:\n",
"\n",
"<goal>\n",
"Tomorrow is Valentine's day. I need to come up with a few date ideas.\n",
" She likes Shakespeare so write using his style. E-mail these ideas to my significant other\n",
"</goal>\n",
"<plan>\n",
" <function.WriterSkill.Brainstorm input=\"Valentine's Day Date Ideas\" setContextVariable=\"IDEAS\"/>\n",
" <function.ShakespeareSkill.shakespeare input=\"$IDEAS\" setContextVariable=\"SHAKESPEARE_IDEAS\"/>\n",
" <function.WriterSkill.EmailTo to=\"My Significant Other\" input=\"$SHAKESPEARE_IDEAS\" sender=\"Me\" appendToResult=\"RESULT__EMAIL\"/>\n",
"</plan>\n"
]
}
],
"outputs": [],
"source": [
"Console.WriteLine(\"Updated plan:\\n\");\n",
"Console.WriteLine(newPlan.Variables.ToPlan().PlanString);"
"var originalPlanResult = await kernel.RunAsync(originalPlan);\n",
"\n",
"Console.WriteLine(\"Original Plan results:\\n\");\n",
"Console.WriteLine(Utils.WordWrap(originalPlanResult.Result, 80));"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Executing the plan\n",
"\n",
"Now that we have a plan, let's try to execute it! The Planner has a skill called `ExecutePlan`."
"Now lets execute and print the new plan:"
]
},
{
Expand All @@ -283,106 +248,9 @@
},
"outputs": [],
"source": [
"var executionResults = newPlan;"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Step 1 - Execution results:\n",
"\n",
"<goal>\n",
"Tomorrow is Valentine's day. I need to come up with a few date ideas.\n",
" She likes Shakespeare so write using his style. E-mail these ideas to my significant other\n",
"</goal><plan>\n",
" <function.ShakespeareSkill.shakespeare input=\"$IDEAS\" setContextVariable=\"SHAKESPEARE_IDEAS\" />\n",
" <function.WriterSkill.EmailTo to=\"My Significant Other\" input=\"$SHAKESPEARE_IDEAS\" sender=\"Me\" appendToResult=\"RESULT__EMAIL\" />\n",
"</plan>\n",
"\n",
"Step 2 - Execution results:\n",
"\n",
"<goal>\n",
"Tomorrow is Valentine's day. I need to come up with a few date ideas.\n",
" She likes Shakespeare so write using his style. E-mail these ideas to my significant other\n",
"</goal><plan>\n",
" <function.WriterSkill.EmailTo to=\"My Significant Other\" input=\"$SHAKESPEARE_IDEAS\" sender=\"Me\" appendToResult=\"RESULT__EMAIL\" />\n",
"</plan>\n",
"\n",
"Step 3 - Execution results:\n",
"\n",
"<goal>\n",
"Tomorrow is Valentine's day. I need to come up with a few date ideas.\n",
" She likes Shakespeare so write using his style. E-mail these ideas to my significant other\n",
"</goal><plan>\n",
"</plan>\n",
"Step 3 - COMPLETE!\n",
"RESULT__EMAIL\n",
"\n",
"\n",
"Dear My Significant Other,\n",
"\n",
"I hope you're doing well. I wanted to suggest some fun activities that we can do together. \n",
"\n",
"1. Let's cook a dinner of love's delight.\n",
"2. How about a picnic in the park?\n",
"3. We can take a romantic walk in the moonlight.\n",
"4. Let's go to a drive-in movie.\n",
"5. Visiting a museum or art gallery would be great.\n",
"6. We can have a game night.\n",
"7. Going to a concert would be fun.\n",
"8. Let's take a hot air balloon ride.\n",
"9. We can visit a local winery.\n",
"10. Let's have a spa day.\n",
"\n",
"I'm looking forward to spending time with you.\n",
"\n",
"Thanks,\n",
"Me\n"
]
}
],
"source": [
"int step = 1;\n",
"int maxSteps = 10;\n",
"while (!executionResults.Variables.ToPlan().IsComplete && step < maxSteps)\n",
"{\n",
" var results = await kernel.RunAsync(executionResults.Variables, planner[\"ExecutePlan\"]);\n",
" if (results.Variables.ToPlan().IsSuccessful)\n",
" {\n",
" Console.WriteLine($\"Step {step} - Execution results:\\n\");\n",
" Console.WriteLine(results.Variables.ToPlan().PlanString);\n",
"\n",
" if (results.Variables.ToPlan().IsComplete)\n",
" {\n",
" Console.WriteLine($\"Step {step} - COMPLETE!\");\n",
" Console.WriteLine(results.Variables.ToPlan().Result);\n",
" break;\n",
" }\n",
" }\n",
" else\n",
" {\n",
" Console.WriteLine($\"Step {step} - Execution failed:\");\n",
" Console.WriteLine(results.Variables.ToPlan().Result);\n",
" break;\n",
" }\n",
" \n",
" executionResults = results;\n",
" step++;\n",
" Console.WriteLine(\"\");\n",
"}"
"var newPlanResult = await kernel.RunAsync(newPlan);\n",
"Console.WriteLine(\"New Plan results:\\n\");\n",
"Console.WriteLine(newPlanResult.Result);"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion samples/notebooks/dotnet/config/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ public static string WordWrap(string text, int maxLineLength)

return result.ToString();
}
}
}
3 changes: 2 additions & 1 deletion samples/notebooks/dotnet/config/settings.json.azure-example
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"type": "azure",
"model": "... your Azure OpenAI deployment name ...",
"endpoint": "https:// ... your endpoint ... .openai.azure.com/",
"apikey": "... your Azure OpenAI key ..."
"apikey": "... your Azure OpenAI key ...",
"org": "" // it's not used for azure, but the parser requires it so do not delete
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"type": "openai",
"endpoint": "NOT-USED-BUT-REQUIRED-FOR-PARSER",
"model": "text-davinci-003",
"apikey": "... your OpenAI key ...",
"org": ""
Expand Down

0 comments on commit 7406233

Please sign in to comment.