From 11239ce6b5e0172965d592cb92285a4217602f44 Mon Sep 17 00:00:00 2001 From: Carlotta Castelluccio <82521518+carlotta94c@users.noreply.github.com> Date: Mon, 12 Feb 2024 11:35:03 +0000 Subject: [PATCH] Adding support to non-Azure OpenAI endpoints, applying new folder structure --- .env.copy | 4 ++ 07-building-chat-applications/README.md | 22 +++---- .../aoai-assigment-simple.ipynb} | 2 +- .../aoai-assignment.ipynb} | 0 .../python/oai-assigment-simple.ipynb | 65 +++++++++++++++++++ .../oai-assignment.ipynb} | 2 +- 6 files changed, 82 insertions(+), 13 deletions(-) rename 07-building-chat-applications/{notebook-simple-azure-openai.ipynb => python/aoai-assigment-simple.ipynb} (98%) rename 07-building-chat-applications/{notebook-azure-openai.ipynb => python/aoai-assignment.ipynb} (100%) create mode 100644 07-building-chat-applications/python/oai-assigment-simple.ipynb rename 07-building-chat-applications/{notebook-openai.ipynb => python/oai-assignment.ipynb} (99%) diff --git a/.env.copy b/.env.copy index f473794d3..5b2ab55d0 100644 --- a/.env.copy +++ b/.env.copy @@ -1,4 +1,8 @@ +# Azure OpenAI configuration AZURE_OPENAI_ENDPOINT='' AZURE_OPENAI_DEPLOYMENT='' AZURE_OPENAI_KEY='' AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT='' + +# OpenAI Configuration +OPENAI_API_KEY='' \ No newline at end of file diff --git a/07-building-chat-applications/README.md b/07-building-chat-applications/README.md index 40c19463b..74aa725a5 100644 --- a/07-building-chat-applications/README.md +++ b/07-building-chat-applications/README.md @@ -60,23 +60,23 @@ When building a chat application, a great first step is to assess what is alread - **Easier maintenance**: Updates and improvements are easier to manage as most APIs and SDKs simply require an update to a library when a newer version is released. - **Access to cutting edge technology**: Leveraging models that have been fined tuned and trained on extensive datasets provides your application with natural language capabilities. -Accessing functionality of an SDK or API typically involves obtaining permission to use the provided services, which is often through the use of a unique key or authentication token. We'll use the OpenAI Python Library to explore what this looks like. You can also try it out on your own in the following [notebook for OpenAI](./notebook-openai.ipynb?WT.mc_id=academic-105485-koreyst) or [notebook for Azure OpenAI Services](./notebook-azure-openai.ipynb?WT.mc_id=academic-105485-koreys) for this lesson. +Accessing functionality of an SDK or API typically involves obtaining permission to use the provided services, which is often through the use of a unique key or authentication token. We'll use the OpenAI Python Library to explore what this looks like. You can also try it out on your own in the following [notebook for OpenAI](./python/oai-assignment.ipynb?WT.mc_id=academic-105485-koreyst) or [notebook for Azure OpenAI Services](./python/aoai-assignment.ipynb?WT.mc_id=academic-105485-koreys) for this lesson. ```python import os -import openai +from openai import OpenAI -openai.api_key = os.getenv("OPENAI_API_KEY") +API_KEY = os.getenv("OPENAI_API_KEY","") -chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Suggest two titles for an instructional lesson on chat applications for generative AI."}]) -``` - -The above example uses the GPT-3.5 Turbo model to complete the prompt, but notice that the API key is set prior to doing so. You'd receive the following error if you didn't set the key. +client = OpenAI( + api_key=API_KEY + ) -```output -AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = ', or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = '. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details. +chat_completion = client.chat.completions.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Suggest two titles for an instructional lesson on chat applications for generative AI."}]) ``` +The above example uses the GPT-3.5 Turbo model to complete the prompt, but notice that the API key is set prior to doing so. You'd receive an error if you didn't set the key. + ## User Experience (UX) General UX principles apply to chat applications, but here are some additional considerations that become particularly important due to the machine learning components involved. @@ -128,7 +128,7 @@ Fine-tuning is often considered when a pre-trained model falls short in a specia For instance, medical queries are complex and require a lot of context. When a medical professional diagnoses a patient it's based on a variety of factors such as lifestyle or pre-existing conditions, and may even rely on recent medical journals to validate their diagnosis. In such nuanced scenarios, a general-purpose AI chat application cannot be a reliable source. -### Scenario: a medical application** +### Scenario: a medical application Consider a chat application designed to assist medical practitioners by providing quick references to treatment guidelines, drug interactions, or recent research findings. @@ -175,7 +175,7 @@ Microsoft's approach to Responsible AI has identified six principles that should ## Assignment -See [assignment](./notebook-azure-openai.ipynb?WT.mc_id=academic-105485-koreyst) it will take you through a series of exercises from running your first chat prompts, to classifying and summarizing text and more. +See [assignment](./python?WT.mc_id=academic-105485-koreyst) it will take you through a series of exercises from running your first chat prompts, to classifying and summarizing text and more. Notice that the assignments are available in different programming languages! ## Great Work! Continue the Journey diff --git a/07-building-chat-applications/notebook-simple-azure-openai.ipynb b/07-building-chat-applications/python/aoai-assigment-simple.ipynb similarity index 98% rename from 07-building-chat-applications/notebook-simple-azure-openai.ipynb rename to 07-building-chat-applications/python/aoai-assigment-simple.ipynb index bacca917b..71efa8b84 100644 --- a/07-building-chat-applications/notebook-simple-azure-openai.ipynb +++ b/07-building-chat-applications/python/aoai-assigment-simple.ipynb @@ -59,7 +59,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.10.13" }, "orig_nbformat": 4 }, diff --git a/07-building-chat-applications/notebook-azure-openai.ipynb b/07-building-chat-applications/python/aoai-assignment.ipynb similarity index 100% rename from 07-building-chat-applications/notebook-azure-openai.ipynb rename to 07-building-chat-applications/python/aoai-assignment.ipynb diff --git a/07-building-chat-applications/python/oai-assigment-simple.ipynb b/07-building-chat-applications/python/oai-assigment-simple.ipynb new file mode 100644 index 000000000..c06f0fa2b --- /dev/null +++ b/07-building-chat-applications/python/oai-assigment-simple.ipynb @@ -0,0 +1,65 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "from openai import OpenAI\n", + "from dotenv import load_dotenv\n", + "\n", + "load_dotenv()\n", + "\n", + "API_KEY = os.getenv(\"OPENAI_API_KEY\",\"\").strip()\n", + "assert API_KEY, \"ERROR: OpenAI Key is missing\"\n", + "client = OpenAI(\n", + " api_key=API_KEY\n", + " )\n", + "\n", + "model = \"gpt-3.5-turbo\" " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Create your first prompt\n", + "text_prompt = \" My foot hurts, what can be wrong?\"\n", + "\n", + "response = client.chat.completions.create(\n", + " model=model,\n", + " messages = [\n", + " {\"role\":\"system\", \"content\":\"I'm a doctor, specialist on surgery\"},\n", + " {\"role\":\"user\",\"content\":text_prompt},])\n", + "\n", + "response.choices[0].message.content" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.13" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/07-building-chat-applications/notebook-openai.ipynb b/07-building-chat-applications/python/oai-assignment.ipynb similarity index 99% rename from 07-building-chat-applications/notebook-openai.ipynb rename to 07-building-chat-applications/python/oai-assignment.ipynb index 4eb0295d3..09e7ca40d 100644 --- a/07-building-chat-applications/notebook-openai.ipynb +++ b/07-building-chat-applications/python/oai-assignment.ipynb @@ -646,7 +646,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.10.13" }, "microsoft": { "host": {