Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Added examples of using ChatCompletion models for skill building in Jupyter Notebooks #1242

Merged
merged 5 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions samples/notebooks/python/03-semantic-function-inline.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,60 @@
"print(summary)"
]
},
{
"cell_type": "markdown",
"id": "1c2c1262",
"metadata": {},
"source": [
"# Using ChatCompletion for Semantic Skills"
]
},
{
"cell_type": "markdown",
"id": "29b59b28",
"metadata": {},
"source": [
"You can also use chat completion models (like `gpt-35-turbo` and `gpt4`) for creating skills. Normally you would have to tweak the API to accommodate for a system and user role, but SK abstracts that away for you by using `kernel.add_chat_service` and `AzureChatCompletion` or `OpenAIChatCompletion`"
]
},
{
"cell_type": "markdown",
"id": "4777f447",
"metadata": {},
"source": [
"Here's one more example of how to write an inline Semantic Function that gives a TLDR for a piece of text.\n",
"Here's one more example of how to write an inline Semantic Function that gives a TLDR for a piece of text using a ChatCompletion model\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c5886aeb",
"metadata": {},
"outputs": [],
"source": [
"import semantic_kernel as sk\n",
"from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, OpenAIChatCompletion\n",
"\n",
"kernel = sk.Kernel()\n",
"\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(\n",
" \"chat_completion\",\n",
" AzureChatCompletion(\"gpt-35-turbo\", endpoint, api_key),\n",
" )\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",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -170,7 +214,7 @@
"sk_prompt = \"\"\"\n",
"{{$input}}\n",
"\n",
"Give me the TLDR in 5 words.\n",
"Give me the TLDR in 5 words or less.\n",
"\"\"\"\n",
"\n",
"text = \"\"\"\n",
Expand Down Expand Up @@ -208,7 +252,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
"version": "3.10.11"
}
},
"nbformat": 4,
Expand Down
104 changes: 103 additions & 1 deletion samples/notebooks/python/05-using-the-planner.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,108 @@
"source": [
"print(results)"
]
},
{
"cell_type": "markdown",
"id": "1d2f8d04",
"metadata": {},
"source": [
"# Let's see how the planner would work with a ChatCompletion model"
]
},
{
"cell_type": "markdown",
"id": "25aa0e28",
"metadata": {},
"source": [
"Planner works best with more powerful models like `gpt4`, but because that's a ChatCompletion model, we need to modify the kernel to use that instead. Let's see how it works with the cheaper `gpt-35-turbo` model."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "06907360",
"metadata": {},
"outputs": [],
"source": [
"import semantic_kernel as sk\n",
"from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, OpenAIChatCompletion\n",
"\n",
"kernel = sk.Kernel()\n",
"\n",
"useAzureOpenAI = True\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(\n",
" \"chat_completion\",\n",
" AzureChatCompletion(\"gpt-35-turbo\", endpoint, api_key),\n",
" )\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",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "551cabd9",
"metadata": {},
"outputs": [],
"source": [
"from semantic_kernel.core_skills.text_skill import TextSkill\n",
"\n",
"skills_directory = \"../../skills/\"\n",
"summarize_skill = kernel.import_semantic_skill_from_directory(skills_directory, \"SummarizeSkill\")\n",
"writer_skill = kernel.import_semantic_skill_from_directory(skills_directory, \"WriterSkill\")\n",
"text_skill = kernel.import_skill(TextSkill(), \"TextSkill\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "183de28f",
"metadata": {},
"outputs": [],
"source": [
"ask = \"\"\"\n",
"I need to plan a birthday party for my best friend. He loves racecars and only speaks Spanish.\n",
"Translate the ideas and make the text in all capital letters.\"\"\"\n",
"chatgpt_plan = await planner.create_plan_async(ask, kernel)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9546ed8",
"metadata": {},
"outputs": [],
"source": [
"print(chatgpt_plan.generated_plan)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "21782c1d",
"metadata": {},
"outputs": [],
"source": [
"results = await planner.execute_plan_async(chatgpt_plan, kernel)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afe8170c",
"metadata": {},
"outputs": [],
"source": [
"print(results)"
]
}
],
"metadata": {
Expand All @@ -249,7 +351,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
"version": "3.10.11"
}
},
"nbformat": 4,
Expand Down