diff --git a/06-text-generation-apps/python/aoai-app-recipe.py b/06-text-generation-apps/python/aoai-app-recipe.py index 46e232391..2e004dd03 100644 --- a/06-text-generation-apps/python/aoai-app-recipe.py +++ b/06-text-generation-apps/python/aoai-app-recipe.py @@ -1,9 +1,9 @@ from openai import AzureOpenAI import os -import dotenv +from dotenv import load_dotenv -# import dotenv -dotenv.load_dotenv() +# load environment variables from .env file +load_dotenv() # configure Azure OpenAI service client client = AzureOpenAI( diff --git a/06-text-generation-apps/python/aoai-app.py b/06-text-generation-apps/python/aoai-app.py index 362da72a9..7f3da6047 100644 --- a/06-text-generation-apps/python/aoai-app.py +++ b/06-text-generation-apps/python/aoai-app.py @@ -1,10 +1,10 @@ # pylint: disable=all from openai import AzureOpenAI import os -import dotenv +from dotenv import load_dotenv -# import dotenv -dotenv.load_dotenv() +# load environment variables from .env file +load_dotenv() # configure Azure OpenAI service client client = AzureOpenAI( diff --git a/06-text-generation-apps/python/aoai-assignment.ipynb b/06-text-generation-apps/python/aoai-assignment.ipynb index 395873449..bfcf31d2f 100644 --- a/06-text-generation-apps/python/aoai-assignment.ipynb +++ b/06-text-generation-apps/python/aoai-assignment.ipynb @@ -127,7 +127,7 @@ " azure_endpoint = os.environ('AZURE_OPENAI_ENDPOINT')\n", " )\n", "\n", - "deployment=os.environ['AZURE_OPENAI_DEPLOYMENT']\n", + "deployment = os.environ['AZURE_OPENAI_DEPLOYMENT']\n", "```\n", "\n", "Above we're setting the following:\n", @@ -163,7 +163,7 @@ " api_version = \"2023-05-15\"\n", " )\n", "\n", - "deployment=os.environ['AZURE_OPENAI_DEPLOYMENT']\n", + "deployment = os.environ['AZURE_OPENAI_DEPLOYMENT']\n", "\n", "completion = client.chat.completions.create(model=deployment, messages=[{\"role\": \"user\", \"content\": \"Hello world\"}])\n", "print(completion.choices[0].message.content)\n", @@ -197,8 +197,7 @@ "# Activate virtual environment\n", "! source venv/bin/activate\n", "# Install openai package\n", - "! pip install openai\n", - " " + "! pip install openai" ] }, { @@ -236,7 +235,7 @@ " api_version = \"2023-10-01-preview\"\n", " )\n", "\n", - "deployment=os.environ['AZURE_OPENAI_DEPLOYMENT']\n", + "deployment = os.environ['AZURE_OPENAI_DEPLOYMENT']\n", "\n", "# add your completion code\n", "prompt = \"Complete the following: Once upon a time there was a\"\n", @@ -448,6 +447,10 @@ "source": [ "import os\n", "from openai import AzureOpenAI\n", + "from dotenv import load_dotenv\n", + "\n", + "# load environment variables from .env file\n", + "load_dotenv()\n", "\n", "client = AzureOpenAI(\n", " azure_endpoint = os.environ[\"AZURE_OPENAI_ENDPOINT\"], \n", @@ -455,7 +458,7 @@ " api_version = \"2023-10-01-preview\"\n", " )\n", "\n", - "deployment=os.environ['AZURE_OPENAI_DEPLOYMENT']\n", + "deployment = os.environ['AZURE_OPENAI_DEPLOYMENT']\n", "\n", "prompt = \"Show me 5 recipes for a dish with the following ingredients: chicken, potatoes, and carrots. Per recipe, list all the ingredients used\"\n", "messages = [{\"role\": \"user\", \"content\": prompt}] \n", @@ -503,6 +506,10 @@ "source": [ "import os\n", "from openai import AzureOpenAI\n", + "from dotenv import load_dotenv\n", + "\n", + "# load environment variables from .env file\n", + "load_dotenv()\n", "\n", "client = AzureOpenAI(\n", " azure_endpoint = os.environ[\"AZURE_OPENAI_ENDPOINT\"], \n", @@ -510,7 +517,7 @@ " api_version = \"2023-10-01-preview\"\n", " )\n", "\n", - "deployment=os.environ['AZURE_OPENAI_DEPLOYMENT']\n", + "deployment = os.environ['AZURE_OPENAI_DEPLOYMENT']\n", "\n", "no_recipes = input(\"No of recipes (for example, 5: \")\n", "\n", @@ -755,7 +762,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "venv", "language": "python", "name": "python3" }, @@ -769,9 +776,8 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.13" - }, - "orig_nbformat": 4 + "version": "3.11.9" + } }, "nbformat": 4, "nbformat_minor": 2 diff --git a/06-text-generation-apps/python/aoai-history-bot.py b/06-text-generation-apps/python/aoai-history-bot.py index 987c0aac4..76efdaa16 100644 --- a/06-text-generation-apps/python/aoai-history-bot.py +++ b/06-text-generation-apps/python/aoai-history-bot.py @@ -1,9 +1,9 @@ from openai import AzureOpenAI import os -import dotenv +from dotenv import load_dotenv -# import dotenv -dotenv.load_dotenv() +# load environment variables from .env file +load_dotenv() # configure Azure OpenAI service client client = AzureOpenAI( diff --git a/06-text-generation-apps/python/aoai-study-buddy.py b/06-text-generation-apps/python/aoai-study-buddy.py index 53ee775a9..ab9385973 100644 --- a/06-text-generation-apps/python/aoai-study-buddy.py +++ b/06-text-generation-apps/python/aoai-study-buddy.py @@ -1,9 +1,9 @@ from openai import AzureOpenAI import os -import dotenv +from dotenv import load_dotenv -# import dotenv -dotenv.load_dotenv() +# load environment variables from .env file +load_dotenv() # configure Azure OpenAI service client client = AzureOpenAI( diff --git a/06-text-generation-apps/python/githubmodels-assignment.ipynb b/06-text-generation-apps/python/githubmodels-assignment.ipynb index d69c15e04..e592e6383 100644 --- a/06-text-generation-apps/python/githubmodels-assignment.ipynb +++ b/06-text-generation-apps/python/githubmodels-assignment.ipynb @@ -364,21 +364,9 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'azure'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[1], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mazure\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mai\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01minference\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m ChatCompletionsClient\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mazure\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mai\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01minference\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodels\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m SystemMessage, UserMessage\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mazure\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcore\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcredentials\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m AzureKeyCredential\n", - "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'azure'" - ] - } - ], + "outputs": [], "source": [ "import os\n", "from azure.ai.inference import ChatCompletionsClient\n", @@ -840,7 +828,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "venv", "language": "python", "name": "python3" }, @@ -855,8 +843,7 @@ "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" - }, - "orig_nbformat": 4 + } }, "nbformat": 4, "nbformat_minor": 2 diff --git a/06-text-generation-apps/python/oai-app-recipe.py b/06-text-generation-apps/python/oai-app-recipe.py index df636a8f7..5683f40cb 100644 --- a/06-text-generation-apps/python/oai-app-recipe.py +++ b/06-text-generation-apps/python/oai-app-recipe.py @@ -1,15 +1,13 @@ from openai import OpenAI import os -import dotenv +from dotenv import load_dotenv -# import dotenv -dotenv.load_dotenv() +# load environment variables from .env file +load_dotenv() # configure Azure OpenAI service client client = OpenAI() - -#deployment=os.environ['OPENAI_DEPLOYMENT'] -deployment="gpt-3.5-turbo" +deployment = "gpt-3.5-turbo" no_recipes = input("No of recipes (for example, 5: ") diff --git a/06-text-generation-apps/python/oai-app.py b/06-text-generation-apps/python/oai-app.py index 64b291529..4b1ca44f1 100644 --- a/06-text-generation-apps/python/oai-app.py +++ b/06-text-generation-apps/python/oai-app.py @@ -1,15 +1,13 @@ from openai import OpenAI import os -import dotenv +from dotenv import load_dotenv -# import dotenv -dotenv.load_dotenv() +# load environment variables from .env file +load_dotenv() # configure OpenAI service client client = OpenAI() - -#deployment=os.environ['OPENAI_DEPLOYMENT'] -deployment="gpt-3.5-turbo" +deployment = "gpt-3.5-turbo" # add your completion code prompt = "Complete the following: Once upon a time there was a" diff --git a/06-text-generation-apps/python/oai-assigment.ipynb b/06-text-generation-apps/python/oai-assigment.ipynb index d88d24e28..dd5081112 100644 --- a/06-text-generation-apps/python/oai-assigment.ipynb +++ b/06-text-generation-apps/python/oai-assigment.ipynb @@ -103,25 +103,25 @@ "> - It is important to note that the API Key will only be accessible once. Therefore, it is imperative to verify that it has been copied correctly. In the event that it does not function as intended, it is recommended to delete the key and generate a new one.\n", "\n", "\n", - "### Setup configuration Azure\n", + "### Setup configuration OpenAI\n", "\n", - "If you're using Open AI, here's how you setup configuration:\n", + "If you're using OpenAI, here's how you setup configuration:\n", "\n", "```python\n", - "client = AzureOpenAI(\n", - " api_key=os.environ['OPENAI_API_KEY']\n", + "client = OpenAI(\n", + " api_key = os.environ['OPENAI_API_KEY']\n", " )\n", "\n", - "deployment=os.environ['OPENAI_DEPLOYMENT']\n", + "deployment = \"gpt-3.5-turbo\"\n", "```\n", "\n", "Above we're setting the following:\n", "\n", - "- `api_key`, this is your API key found in THE OpenAI dashboard.\n", + "- `api_key`, this is your API key found in the OpenAI dashboard.\n", "- `deployment`, this is your GPT version.\n", "\n", "> [!NOTE]\n", - "> `os.environ` is a function that reads environment variables. You can use it to read environment variables like `AZURE_OPENAI_API_KEY` and `AZURE_OPENAI_ENDPOINT`.\n", + "> `os.environ` is a function that reads environment variables. You can use it to read environment variables like `OPENAI_API_KEY`.\n", "\n", "## Generate text\n", "\n", @@ -141,11 +141,11 @@ "So far, you've seen how we've been using `Completion` to generate text. But there's another class called `ChatCompletion` that is more suited for chatbots. Here's an example of using it:\n", "\n", "```python\n", - "client = AzureOpenAI(\n", - " api_key=os.environ['OPENAI_API_KEY']\n", + "client = OpenAI(\n", + " api_key = os.environ['OPENAI_API_KEY']\n", " )\n", "\n", - "deployment=os.environ['OPENAI_DEPLOYMENT']\n", + "deployment = \"gpt-3.5-turbo\"\n", "\n", "completion = client.chat.completions.create(model=deployment, messages=[{\"role\": \"user\", \"content\": \"Hello world\"}])\n", "print(completion.choices[0].message.content)\n", @@ -155,7 +155,7 @@ "\n", "## Exercise - your first text generation app\n", "\n", - "Now that we learned how to set up and configure Azure OpenAI service, it's time to build your first text generation app. To build your app, follow these steps:\n", + "Now that we learned how to set up and configure OpenAI service, it's time to build your first text generation app. To build your app, follow these steps:\n", "\n" ] }, @@ -179,8 +179,7 @@ "# Activate virtual environment\n", "! source venv/bin/activate\n", "# Install openai package\n", - "! pip install openai\n", - " " + "! pip install openai" ] }, { @@ -191,7 +190,7 @@ "> If you're using Windows type `venv\\Scripts\\activate` instead of `source venv/bin/activate`. \n", "\n", "> [!NOTE]\n", - "> Locate your Azure Open AI key by going to https://portal.azure.com/ and search for `Open AI` and select the `Open AI resource` and then select `Keys and Endpoint` and copy the `Key 1` value." + "> Locate your OpenAI key by going to https://platform.openai.com/settings/organization/api-keys and search for `API keys`. You can create a new key there and immediate copy the value." ] }, { @@ -208,15 +207,17 @@ "outputs": [], "source": [ "import os\n", - "from openai import AzureOpenAI\n", + "from openai import OpenAI\n", "from dotenv import load_dotenv\n", + "\n", + "# load environment variables from .env file\n", "load_dotenv()\n", "\n", - "client = AzureOpenAI(\n", - " api_key=os.environ['OPENAI_API_KEY']\n", - " )\n", + "client = OpenAI(\n", + " api_key = os.environ['OPENAI_API_KEY']\n", + ")\n", "\n", - "deployment=os.environ['OPENAI_DEPLOYMENT']\n", + "deployment = \"gpt-3.5-turbo\"\n", "\n", "# add your completion code\n", "prompt = \"Complete the following: Once upon a time there was a\"\n", @@ -427,13 +428,17 @@ "outputs": [], "source": [ "import os\n", - "from openai import AzureOpenAI\n", + "from openai import OpenAI\n", + "from dotenv import load_dotenv\n", "\n", - "client = AzureOpenAI(\n", - " api_key=os.environ['OPENAI_API_KEY']\n", - " )\n", + "# load environment variables from .env file\n", + "load_dotenv()\n", + "\n", + "client = OpenAI(\n", + " api_key = os.environ['OPENAI_API_KEY']\n", + ")\n", "\n", - "deployment=os.environ['OPENAI_DEPLOYMENT']\n", + "deployment = \"gpt-3.5-turbo\"\n", "\n", "prompt = \"Show me 5 recipes for a dish with the following ingredients: chicken, potatoes, and carrots. Per recipe, list all the ingredients used\"\n", "messages = [{\"role\": \"user\", \"content\": prompt}] \n", @@ -480,13 +485,17 @@ "outputs": [], "source": [ "import os\n", - "from openai import AzureOpenAI\n", + "from openai import OpenAI\n", + "from dotenv import load_dotenv\n", "\n", - "client = AzureOpenAI(\n", - " api_key=os.environ['OPENAI_API_KEY']\n", - " )\n", + "# load environment variables from .env file\n", + "load_dotenv()\n", "\n", - "deployment=os.environ['OPENAI_DEPLOYMENT']\n", + "client = OpenAI(\n", + " api_key = os.environ['OPENAI_API_KEY']\n", + ")\n", + "\n", + "deployment = \"gpt-3.5-turbo\"\n", "\n", "no_recipes = input(\"No of recipes (for example, 5: \")\n", "\n", @@ -731,7 +740,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "venv", "language": "python", "name": "python3" }, @@ -745,9 +754,8 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.13" - }, - "orig_nbformat": 4 + "version": "3.11.9" + } }, "nbformat": 4, "nbformat_minor": 2 diff --git a/06-text-generation-apps/python/oai-history-bot.py b/06-text-generation-apps/python/oai-history-bot.py index 2ef137cb6..7c5082c71 100644 --- a/06-text-generation-apps/python/oai-history-bot.py +++ b/06-text-generation-apps/python/oai-history-bot.py @@ -1,9 +1,9 @@ from openai import OpenAI import os -import dotenv +from dotenv import load_dotenv -# import dotenv -dotenv.load_dotenv() +# load environment variables from .env file +load_dotenv() # configure Azure OpenAI service client client = OpenAI() diff --git a/06-text-generation-apps/python/oai-study-buddy.py b/06-text-generation-apps/python/oai-study-buddy.py index 80ccc7757..01fd85951 100644 --- a/06-text-generation-apps/python/oai-study-buddy.py +++ b/06-text-generation-apps/python/oai-study-buddy.py @@ -1,16 +1,12 @@ from openai import OpenAI import os -import dotenv +from dotenv import load_dotenv -# import dotenv -dotenv.load_dotenv() +# load environment variables from .env file +load_dotenv() # configure Azure OpenAI service client -client = OpenAI( - api_key=os.environ['OPENAI_API_KEY'] - ) - -#deployment=os.environ['OPENAI_DEPLOYMENT'] +client = OpenAI() deployment="gpt-3.5-turbo" # add your completion code