Skip to content

Commit

Permalink
Python: Update notebooks to run with dotenv settings. (#6713)
Browse files Browse the repository at this point in the history
### Motivation and Context

We've received feedback that the Python notebooks may difficult to run
for folks as they get started.

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

To be explicit, we're now requiring that the Python notebooks reference
a valid `.env` file with the required settings.
All notebooks are running with the valid `.env` file for both AzureOpen
and OpenAI config settings.

<!-- 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 😄
  • Loading branch information
moonbox3 committed Jun 14, 2024
1 parent 1cc6464 commit 7d61c7c
Show file tree
Hide file tree
Showing 15 changed files with 1,298 additions and 346 deletions.
1 change: 1 addition & 0 deletions python/samples/getting_started/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
GLOBAL_LLM_SERVICE=""
OPENAI_API_KEY=""
OPEN_AI_CHAT_MODEL_ID=""
OPEN_AI_TEXT_MODEL_ID=""
Expand Down
136 changes: 80 additions & 56 deletions python/samples/getting_started/00-getting-started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"source": [
"# Setup\n",
"\n",
"**Step 1**: Import Semantic Kernel SDK from pypi.org\n"
"Import Semantic Kernel SDK from pypi.org"
]
},
{
Expand All @@ -20,22 +20,79 @@
"%pip install semantic-kernel==1.1.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initial configuration for the notebook to run properly."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantic_kernel import Kernel\n",
"# Make sure paths are correct for the imports\n",
"\n",
"kernel = Kernel()"
"import os\n",
"import sys\n",
"\n",
"notebook_dir = os.path.abspath(\"\")\n",
"parent_dir = os.path.dirname(notebook_dir)\n",
"grandparent_dir = os.path.dirname(parent_dir)\n",
"\n",
"\n",
"sys.path.append(grandparent_dir)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Configuring the Kernel\n",
"\n",
"Let's get started with the necessary configuration to run Semantic Kernel. For Notebooks, we require a `.env` file with the proper settings for the model you use. Create a new file named `.env` and place it in this directory. Copy the contents of the `.env.example` file from this directory and paste it into the `.env` file that you just created.\n",
"\n",
"**NOTE: Please make sure to include `GLOBAL_LLM_SERVICE` set to either OpenAI, AzureOpenAI, or HuggingFace in your .env file. If this setting is not included, the Service will default to AzureOpenAI.**\n",
"\n",
"#### Option 1: using OpenAI\n",
"\n",
"Add your [OpenAI Key](https://openai.com/product/) key to your `.env` file (org Id only if you have multiple orgs):\n",
"\n",
"```\n",
"GLOBAL_LLM_SERVICE=\"OpenAI\"\n",
"OPENAI_API_KEY=\"sk-...\"\n",
"OPENAI_ORG_ID=\"\"\n",
"OPENAI_CHAT_MODEL_ID=\"\"\n",
"OPENAI_TEXT_MODEL_ID=\"\"\n",
"OPENAI_EMBEDDING_MODEL_ID=\"\"\n",
"```\n",
"The names should match the names used in the `.env` file, as shown above.\n",
"\n",
"#### Option 2: using Azure OpenAI\n",
"\n",
"Add your [Azure Open AI Service key](https://learn.microsoft.com/azure/cognitive-services/openai/quickstart?pivots=programming-language-studio) settings to the `.env` file in the same folder:\n",
"\n",
"```\n",
"GLOBAL_LLM_SERVICE=\"AzureOpenAI\"\n",
"AZURE_OPENAI_API_KEY=\"...\"\n",
"AZURE_OPENAI_ENDPOINT=\"https://...\"\n",
"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME=\"...\"\n",
"AZURE_OPENAI_TEXT_DEPLOYMENT_NAME=\"...\"\n",
"AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME=\"...\"\n",
"AZURE_OPENAI_API_VERSION=\"...\"\n",
"```\n",
"The names should match the names used in the `.env` file, as shown above.\n",
"\n",
"For more advanced configuration, please follow the steps outlined in the [setup guide](./CONFIGURING_THE_KERNEL.md)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Configure the service you'd like to use via the `Service` Enum.\n"
"Let's define our kernel for this example."
]
},
{
Expand All @@ -44,17 +101,16 @@
"metadata": {},
"outputs": [],
"source": [
"# Make sure paths are correct for the imports\n",
"\n",
"import sys\n",
"import os\n",
"\n",
"notebook_dir = os.path.abspath('')\n",
"parent_dir = os.path.dirname(notebook_dir)\n",
"grandparent_dir = os.path.dirname(parent_dir)\n",
"\n",
"from semantic_kernel import Kernel\n",
"\n",
"sys.path.append(grandparent_dir)"
"kernel = Kernel()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will load our settings and get the LLM service to use for the notebook."
]
},
{
Expand All @@ -79,48 +135,10 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Option 1: using OpenAI\n",
"\n",
"**Step 2**: Add your [OpenAI Key](https://openai.com/product/) key to either your environment variables or to the `.env` file in the same folder (org Id only if you have multiple orgs):\n",
"\n",
"```\n",
"OPENAI_API_KEY=\"sk-...\"\n",
"OPENAI_ORG_ID=\"\"\n",
"```\n",
"The environment variables names should match the names used in the `.env` file, as shown above.\n",
"\n",
"If using the `.env` file, please configure the `env_file_path` parameter with a valid path when creating the ChatCompletion class:\n",
"\n",
"```\n",
"chat_completion = OpenAIChatCompletion(service_id=\"test\", env_file_path=<path_to_file>)\n",
"```\n",
"\n",
"Use \"keyword arguments\" to instantiate an OpenAI Chat Completion service and add it to the kernel:\n",
"\n",
"## Option 2: using Azure OpenAI\n",
"\n",
"**Step 2**: Add your [Azure Open AI Service key](https://learn.microsoft.com/azure/cognitive-services/openai/quickstart?pivots=programming-language-studio) settings to either your system's environment variables or to the `.env` file in the same folder:\n",
"\n",
"```\n",
"AZURE_OPENAI_API_KEY=\"...\"\n",
"AZURE_OPENAI_ENDPOINT=\"https://...\"\n",
"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME=\"...\"\n",
"AZURE_OPENAI_TEXT_DEPLOYMENT_NAME=\"...\"\n",
"AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME=\"...\"\n",
"```\n",
"The environment variables names should match the names used in the `.env` file, as shown above.\n",
"\n",
"If using the `.env` file, please configure the `env_file_path` parameter with a valid path when creating the ChatCompletion class:\n",
"\n",
"```\n",
"chat_completion = AzureChatCompletion(service_id=\"test\", env_file_path=<path_to_file>)\n",
"```\n",
"\n",
"Use \"keyword arguments\" to instantiate an Azure OpenAI Chat Completion service and add it to the kernel:\n"
"We now configure our Chat Completion service on the kernel."
]
},
{
Expand All @@ -138,14 +156,20 @@
"\n",
" service_id = \"default\"\n",
" kernel.add_service(\n",
" OpenAIChatCompletion(service_id=service_id, ai_model_id=\"gpt-3.5-turbo\"),\n",
" OpenAIChatCompletion(\n",
" service_id=service_id,\n",
" env_file_path=\".env\",\n",
" ),\n",
" )\n",
"elif selectedService == Service.AzureOpenAI:\n",
" from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion\n",
"\n",
" service_id = \"default\"\n",
" kernel.add_service(\n",
" AzureChatCompletion(service_id=service_id),\n",
" AzureChatCompletion(\n",
" service_id=service_id,\n",
" env_file_path=\".env\",\n",
" ),\n",
" )"
]
},
Expand Down Expand Up @@ -202,7 +226,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 7d61c7c

Please sign in to comment.