Skip to content

Commit

Permalink
Python: pysdk issue 3897 - update notebooks (microsoft#4177)
Browse files Browse the repository at this point in the history
### Motivation and Context

<!-- 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

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

microsoft#3897

In this PR, I updated the notebooks to use the latest version of the
python SDK.

To make the notebooks work, I need to:
1. Use keyword arguments
2. Change the default value in the `ContextVariables` from `int/float`
to `str`
3. Update `TextMemorySkill`
4. Update some inputs, such as `ChatMessage`

I have noted some inconsistency issues in the
microsoft#3897

### 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 😄

---------

Co-authored-by: Huijing Huang <[email protected]>
Co-authored-by: Evan Mattson <[email protected]>
  • Loading branch information
3 people authored Dec 14, 2023
1 parent b6f1938 commit 49a1cb8
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 140 deletions.
12 changes: 6 additions & 6 deletions python/notebooks/00-getting-started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install semantic-kernel==0.3.15.dev0"
"!python -m pip install semantic-kernel==0.4.2.dev0"
]
},
{
Expand Down Expand Up @@ -44,7 +44,7 @@
"OPENAI_ORG_ID=\"\"\n",
"```\n",
"\n",
"and add OpenAI Chat Completion to the kernel:"
"Use \"keyword arguments\" to instantiate an OpenAI Chat Completion service and add it to the kernel:"
]
},
{
Expand All @@ -57,7 +57,7 @@
"\n",
"api_key, org_id = sk.openai_settings_from_dot_env()\n",
"\n",
"kernel.add_chat_service(\"chat-gpt\", OpenAIChatCompletion(\"gpt-3.5-turbo\", api_key, org_id))"
"kernel.add_chat_service(\"chat-gpt\", OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo\", api_key=api_key, org_id=org_id))"
]
},
{
Expand All @@ -75,7 +75,7 @@
"AZURE_OPENAI_DEPLOYMENT_NAME=\"...\"\n",
"```\n",
"\n",
"and add Azure OpenAI Chat Completion to the kernel:"
"Use \"keyword arguments\" to instantiate an Azure OpenAI Chat Completion service and add it to the kernel:"
]
},
{
Expand All @@ -88,7 +88,7 @@
"\n",
"deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
"\n",
"kernel.add_chat_service(\"chat_completion\", AzureChatCompletion(deployment, endpoint, api_key))\n"
"kernel.add_chat_service(\"chat_completion\", AzureChatCompletion(deployment_name=deployment, endpoint=endpoint, api_key=api_key))\n"
]
},
{
Expand Down Expand Up @@ -130,7 +130,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down
24 changes: 12 additions & 12 deletions python/notebooks/01-basic-loading-the-kernel.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install semantic-kernel==0.3.15.dev0"
"!python -m pip install semantic-kernel==0.4.2.dev0"
]
},
{
Expand Down Expand Up @@ -87,21 +87,21 @@
"source": [
"kernel = sk.Kernel()\n",
"\n",
"kernel.add_chat_service( # We are adding a text service\n",
" \"Azure_curie\", # The alias we can use in prompt templates' config.json\n",
"kernel.add_chat_service( # We are adding a text service\n",
" \"Azure_curie\", # The alias we can use in prompt templates' config.json\n",
" AzureChatCompletion(\n",
" \"my-finetuned-Curie\", # Azure OpenAI *Deployment name*\n",
" \"https://contoso.openai.azure.com/\", # Azure OpenAI *Endpoint*\n",
" \"...your Azure OpenAI Key...\" # Azure OpenAI *Key*\n",
" deployment_name=\"my-finetuned-Curie\", # Azure OpenAI *Deployment name*\n",
" endpoint=\"https://contoso.openai.azure.com/\", # Azure OpenAI *Endpoint*\n",
" api_key=\"...your Azure OpenAI Key...\" # Azure OpenAI *Key*\n",
" )\n",
")\n",
"\n",
"kernel.add_chat_service( # We are adding a text service\n",
" \"OpenAI_chat_gpt\", # The alias we can use in prompt templates' config.json\n",
"kernel.add_chat_service( # We are adding a text service\n",
" \"OpenAI_chat_gpt\", # The alias we can use in prompt templates' config.json\n",
" OpenAIChatCompletion(\n",
" \"gpt-3.5-turbo\", # OpenAI Model Name\n",
" \"...your OpenAI API Key...\", # OpenAI API key\n",
" \"...your OpenAI Org ID...\" # *optional* OpenAI Organization ID\n",
" ai_model_id=\"gpt-3.5-turbo\", # OpenAI Model Name\n",
" api_key=\"...your OpenAI API Key...\", # OpenAI API key\n",
" org_id=\"...your OpenAI Org ID...\" # *optional* OpenAI Organization ID\n",
" )\n",
")"
]
Expand Down Expand Up @@ -154,7 +154,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
"version": "3.11.6"
},
"polyglot_notebook": {
"kernelInfo": {
Expand Down
10 changes: 6 additions & 4 deletions python/notebooks/02-running-prompts-from-file.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install semantic-kernel==0.3.15.dev0"
"!python -m pip install semantic-kernel==0.4.2.dev0"
]
},
{
Expand All @@ -109,10 +109,12 @@
"# Configure AI service used by the kernel\n",
"if useAzureOpenAI:\n",
" deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
" kernel.add_chat_service(\"chat_completion\", AzureChatCompletion(deployment, endpoint, api_key))\n",
" azure_chat_service = AzureChatCompletion(deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key) # set the deployment name to the value of your chat model\n",
" kernel.add_chat_service(\"chat_completion\", azure_chat_service)\n",
"else:\n",
" api_key, org_id = sk.openai_settings_from_dot_env()\n",
" kernel.add_chat_service(\"chat-gpt\", OpenAIChatCompletion(\"gpt-3.5-turbo\", api_key, org_id))"
" oai_chat_service = OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo\", api_key=api_key, org_id=org_id)\n",
" kernel.add_chat_service(\"chat-gpt\", oai_chat_service)"
]
},
{
Expand Down Expand Up @@ -190,7 +192,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down
25 changes: 12 additions & 13 deletions python/notebooks/03-semantic-function-inline.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install semantic-kernel==0.3.15.dev0"
"!python -m pip install semantic-kernel==0.4.2.dev0"
]
},
{
Expand All @@ -75,10 +75,12 @@
"# Configure AI service used by the kernel\n",
"if useAzureOpenAI:\n",
" deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
" kernel.add_text_completion_service(\"dv\", AzureTextCompletion(deployment, endpoint, api_key))\n",
" azure_text_service = AzureTextCompletion(deployment_name=\"text\", endpoint=endpoint, api_key=api_key) # set the deployment name to the value of your text model\n",
" kernel.add_text_completion_service(\"dv\", azure_text_service)\n",
"else:\n",
" api_key, org_id = sk.openai_settings_from_dot_env()\n",
" kernel.add_text_completion_service(\"dv\", OpenAITextCompletion(\"text-davinci-003\", api_key, org_id))"
" oai_text_service = OpenAITextCompletion(ai_model_id=\"text-davinci-003\", api_key=api_key, org_id=org_id)\n",
" kernel.add_text_completion_service(\"dv\", oai_text_service)"
]
},
{
Expand All @@ -103,7 +105,7 @@
"Summarize the content above.\n",
"\"\"\"\n",
"\n",
"summarize = kernel.create_semantic_function(prompt, max_tokens=2000, temperature=0.2, top_p=0.5)"
"summarize = kernel.create_semantic_function(prompt_template=prompt, max_tokens=2000, temperature=0.2, top_p=0.5)"
]
},
{
Expand Down Expand Up @@ -203,15 +205,12 @@
"# Configure AI service used by the kernel\n",
"if useAzureOpenAI:\n",
" deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
" kernel.add_chat_service(\n",
" \"chat_completion\",\n",
" AzureChatCompletion(deployment, endpoint, api_key),\n",
" )\n",
" azure_chat_service = AzureChatCompletion(deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key) # set the deployment name to the value of your chat model\n",
" kernel.add_chat_service(\"chat_completion\", azure_chat_service)\n",
"else:\n",
" api_key, org_id = sk.openai_settings_from_dot_env()\n",
" kernel.add_chat_service(\n",
" \"chat-gpt\", OpenAIChatCompletion(\"gpt-3.5-turbo\", api_key, org_id)\n",
" )"
" oai_chat_service = OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo\", api_key=api_key, org_id=org_id)\n",
" kernel.add_chat_service(\"chat-gpt\", oai_chat_service)"
]
},
{
Expand All @@ -238,7 +237,7 @@
" does not conflict with the First or Second Law.\n",
"\"\"\"\n",
"\n",
"tldr_function = kernel.create_semantic_function(sk_prompt, max_tokens=200, temperature=0, top_p=0.5)\n",
"tldr_function = kernel.create_semantic_function(prompt_template=sk_prompt, max_tokens=200, temperature=0, top_p=0.5)\n",
"\n",
"summary = tldr_function(text)\n",
"\n",
Expand All @@ -262,7 +261,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down
15 changes: 10 additions & 5 deletions python/notebooks/04-context-variables-chat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install semantic-kernel==0.3.15.dev0"
"!python -m pip install semantic-kernel==0.4.2.dev0"
]
},
{
Expand All @@ -46,10 +46,10 @@
"# Configure AI service used by the kernel\n",
"if useAzureOpenAI:\n",
" deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
" kernel.add_chat_service(\"chat_completion\", AzureChatCompletion(deployment, endpoint, api_key))\n",
" kernel.add_chat_service(\"chat_completion\", AzureChatCompletion(deployment_name=deployment, endpoint=endpoint, api_key=api_key))\n",
"else:\n",
" api_key, org_id = sk.openai_settings_from_dot_env()\n",
" kernel.add_chat_service(\"chat-gpt\", OpenAIChatCompletion(\"gpt-3.5-turbo\", api_key, org_id))\n"
" kernel.add_chat_service(\"gpt-3.5\", OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo\", api_key=api_key, org_id=org_id))\n"
]
},
{
Expand Down Expand Up @@ -93,7 +93,12 @@
"metadata": {},
"outputs": [],
"source": [
"chat_function = kernel.create_semantic_function(sk_prompt, \"ChatBot\", max_tokens=2000, temperature=0.7, top_p=0.5)"
"chat_function = kernel.create_semantic_function(\n",
" prompt_template=sk_prompt,\n",
" function_name=\"ChatBot\",\n",
" max_tokens=2000,\n",
" temperature=0.7,\n",
" top_p=0.5)"
]
},
{
Expand Down Expand Up @@ -264,7 +269,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down
17 changes: 10 additions & 7 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.12.dev0"
"!python -m pip install semantic-kernel==0.4.2.dev0"
]
},
{
Expand All @@ -42,12 +42,11 @@
"\n",
"# Configure AI backend used by the kernel\n",
"if useAzureOpenAI:\n",
" \n",
" deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
" kernel.add_chat_service(\"gpt-3.5\", AzureChatCompletion(deployment, endpoint, api_key))\n",
" kernel.add_chat_service(\"chat_completion\", AzureChatCompletion(deployment_name=deployment, endpoint=endpoint, api_key=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))"
" kernel.add_chat_service(\"gpt-3.5\", OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo\", api_key=api_key, org_id=org_id))"
]
},
{
Expand Down Expand Up @@ -181,8 +180,12 @@
"\n",
"Rewrite the above in the style of Shakespeare.\n",
"\"\"\"\n",
"shakespeareFunction = kernel.create_semantic_function(sk_prompt, \"shakespeare\", \"ShakespeareSkill\",\n",
" max_tokens=2000, temperature=0.8)"
"shakespeareFunction = kernel.create_semantic_function(\n",
" prompt_template=sk_prompt,\n",
" function_name=\"shakespeare\",\n",
" skill_name=\"ShakespeareSkill\",\n",
" max_tokens=2000,\n",
" temperature=0.8)"
]
},
{
Expand Down Expand Up @@ -646,7 +649,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down
33 changes: 19 additions & 14 deletions python/notebooks/06-memory-and-embeddings.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install semantic-kernel==0.3.15.dev0"
"!python -m pip install semantic-kernel==0.4.2.dev0"
]
},
{
Expand Down Expand Up @@ -65,18 +65,23 @@
"source": [
"kernel = sk.Kernel()\n",
"\n",
"useAzureOpenAI = True\n",
"useAzureOpenAI = False\n",
"\n",
"# Configure AI service used by the kernel\n",
"if useAzureOpenAI:\n",
" deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
" kernel.add_chat_service(\"chat_completion\", AzureChatCompletion(deployment, endpoint, api_key))\n",
" # next line assumes embeddings deployment name is \"text-embedding-ada-002\", adjust this if appropriate \n",
" kernel.add_text_embedding_generation_service(\"ada\", AzureTextEmbedding(\"text-embedding-ada-002\", endpoint, api_key))\n",
" # next line assumes chat deployment name is \"turbo\", adjust the deployment name to the value of your chat model if needed\n",
" azure_chat_service = AzureChatCompletion(deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key)\n",
" # next line assumes embeddings deployment name is \"text-embedding\", adjust the deployment name to the value of your chat model if needed \n",
" azure_text_embedding = AzureTextEmbedding(deployment_name=\"text-embedding\", endpoint=endpoint, api_key=api_key)\n",
" kernel.add_chat_service(\"chat_completion\", azure_chat_service)\n",
" kernel.add_text_embedding_generation_service(\"ada\", azure_text_embedding)\n",
"else:\n",
" api_key, org_id = sk.openai_settings_from_dot_env()\n",
" kernel.add_chat_service(\"chat-gpt\", OpenAIChatCompletion(\"gpt-3.5-turbo\", api_key, org_id))\n",
" kernel.add_text_embedding_generation_service(\"ada\", OpenAITextEmbedding(\"text-embedding-ada-002\", api_key, org_id))\n",
" oai_chat_service = OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo\", api_key=api_key, org_id=org_id)\n",
" oai_text_embedding = OpenAITextEmbedding(ai_model_id=\"text-embedding-ada-002\", api_key=api_key, org_id=org_id)\n",
" kernel.add_chat_service(\"chat-gpt\", oai_chat_service)\n",
" kernel.add_text_embedding_generation_service(\"ada\", oai_text_embedding)\n",
"\n",
"kernel.register_memory_store(memory_store=sk.memory.VolatileMemoryStore())\n",
"kernel.import_skill(sk.core_skills.TextMemorySkill())"
Expand Down Expand Up @@ -113,19 +118,19 @@
"async def populate_memory(kernel: sk.Kernel) -> None:\n",
" # Add some documents to the semantic memory\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info1\", text=\"My name is Andrea\"\n",
" collection=\"aboutMe\", id=\"info1\", text=\"My name is Andrea\"\n",
" )\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info2\", text=\"I currently work as a tour guide\"\n",
" collection=\"aboutMe\", id=\"info2\", text=\"I currently work as a tour guide\"\n",
" )\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info3\", text=\"I've been living in Seattle since 2005\"\n",
" collection= \"aboutMe\", id=\"info3\", text=\"I've been living in Seattle since 2005\"\n",
" )\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info4\", text=\"I visited France and Italy five times since 2015\"\n",
" collection=\"aboutMe\", id=\"info4\", text=\"I visited France and Italy five times since 2015\"\n",
" )\n",
" await kernel.memory.save_information_async(\n",
" \"aboutMe\", id=\"info5\", text=\"My family is from New York\"\n",
" collection=\"aboutMe\", id=\"info5\", text=\"My family is from New York\"\n",
" )"
]
},
Expand Down Expand Up @@ -237,7 +242,7 @@
" context[\"fact5\"] = \"what do I do for work?\"\n",
"\n",
" context[sk.core_skills.TextMemorySkill.COLLECTION_PARAM] = \"aboutMe\"\n",
" context[sk.core_skills.TextMemorySkill.RELEVANCE_PARAM] = 0.8\n",
" context[sk.core_skills.TextMemorySkill.RELEVANCE_PARAM] = \"0.8\"\n",
"\n",
" context[\"chat_history\"] = \"\"\n",
"\n",
Expand Down Expand Up @@ -525,7 +530,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 49a1cb8

Please sign in to comment.