From 18aa61dd210d31d24bd684b83b1f777914180707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCrtz?= Date: Tue, 14 Mar 2023 16:24:03 +0100 Subject: [PATCH 1/4] Add dalle notebook --- README.md | 1 + examples/azure/dalle.ipynb | 137 +++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 examples/azure/dalle.ipynb diff --git a/README.md b/README.md index c9a0aa0bbd..f7286ced9d 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ Most code examples are written in Python, though the concepts can be applied in - [How to get completions from Azure OpenAI](examples/azure/completions.ipynb) - [How to get embeddings from Azure OpenAI](examples/azure/embeddings.ipynb) - [How to fine-tune GPT-3 with Azure OpenAI](examples/azure/finetuning.ipynb) + - [How to Dall-E with Azure OpenAI](examples/azure/dalle.ipynb) - Apps - [File Q and A](apps/file-q-and-a/) - [Web Crawl Q and A](apps/web-crawl-q-and-a) diff --git a/examples/azure/dalle.ipynb b/examples/azure/dalle.ipynb new file mode 100644 index 0000000000..82b4bffd90 --- /dev/null +++ b/examples/azure/dalle.ipynb @@ -0,0 +1,137 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Azure Dall-E image generation example\n", + "In this example we'll try to go over all operations needed to get image generation working using the Azure endpoints." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import openai" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Setup\n", + "For the following sections to work properly we first have to setup some things. Let's start with the `api_base`. To find your `api_base` go to https://portal.azure.com, find your resource and then under \"Resource Management\" -> \"Keys and Endpoints\" look for the \"Endpoint\" value." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "openai.api_base = '' # Please add your endpoint here\n", + "# Since using image generation on Azure is currently under preview, the api version cannot be set and is fixed to '2022-11-23-preview'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We next have to setup the `api_type` and `api_key`. We can either get the key from the portal or we can get it through Microsoft Active Directory Authentication. Depending on this the `api_type` is either `azure` or `azure_ad`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Setup: Portal\n", + "Let's first look at getting the key from the portal. Go to https://portal.azure.com, find your resource and then under \"Resource Management\" -> \"Keys and Endpoints\" look for one of the \"Keys\" values." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "openai.api_type = 'azure'\n", + "openai.api_key = '' # Please add your api key here" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### (Optional) Setup: Microsoft Active Directory Authentication\n", + "Let's now see how we can get a key via Microsoft Active Directory Authentication. Uncomment the following code if you want to use Active Directory Authentication instead of keys from the portal." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# from azure.identity import DefaultAzureCredential\n", + "\n", + "# default_credential = DefaultAzureCredential()\n", + "# token = default_credential.get_token(\"https://cognitiveservices.azure.com/.default\")\n", + "\n", + "# openai.api_type = 'azure_ad'\n", + "# openai.api_key = token.token" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Image generation\n", + "Now let's generate two images." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "openai.Image.create(\n", + " prompt=\"A cyberpunk monkey hacker dreaming of a beautiful bunch of bananas, digital art\",\n", + " n=2,\n", + " size=\"1024x1024\"\n", + ")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.8.10" + }, + "vscode": { + "interpreter": { + "hash": "3a5103089ab7e7c666b279eeded403fcec76de49a40685dbdfe9f9c78ad97c17" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 25508adb951325bc287422fc4961d5ec0f2f3278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCrtz?= Date: Tue, 14 Mar 2023 16:25:29 +0100 Subject: [PATCH 2/4] Fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f7286ced9d..bc19b35ff1 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Most code examples are written in Python, though the concepts can be applied in - [How to get completions from Azure OpenAI](examples/azure/completions.ipynb) - [How to get embeddings from Azure OpenAI](examples/azure/embeddings.ipynb) - [How to fine-tune GPT-3 with Azure OpenAI](examples/azure/finetuning.ipynb) - - [How to Dall-E with Azure OpenAI](examples/azure/dalle.ipynb) + - [How to use Dall-E image generation with Azure OpenAI](examples/azure/dalle.ipynb) - Apps - [File Q and A](apps/file-q-and-a/) - [Web Crawl Q and A](apps/web-crawl-q-and-a) From eb718007b116e94a3ca3098a1fd8a99fdeb2964f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCrtz?= Date: Fri, 17 Mar 2023 17:19:51 +0100 Subject: [PATCH 3/4] Some small adjustments --- examples/azure/dalle.ipynb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/examples/azure/dalle.ipynb b/examples/azure/dalle.ipynb index 82b4bffd90..b31bab44a8 100644 --- a/examples/azure/dalle.ipynb +++ b/examples/azure/dalle.ipynb @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -29,12 +29,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "openai.api_base = '' # Please add your endpoint here\n", - "# Since using image generation on Azure is currently under preview, the api version cannot be set and is fixed to '2022-11-23-preview'" + "\n", + "# IMPORTANT: Dall-E is currently only available through api version '2023-04-01-preview'. This version only supports Dall-E, no completions, etc..\n", + "# This means that if you want to use DallE and completions together, you have to override the api_version in the constructor as also shown throughout this guide\n", + "# and set the global api_version to a version that supports completions, etc..\n", + "openai.api_version = '2023-04-01-preview'" ] }, { @@ -101,9 +105,12 @@ "outputs": [], "source": [ "openai.Image.create(\n", - " prompt=\"A cyberpunk monkey hacker dreaming of a beautiful bunch of bananas, digital art\",\n", - " n=2,\n", - " size=\"1024x1024\"\n", + " # IMPORTANT: Dall-E is currently only available through api version '2023-04-01-preview'. This version only supports Dall-E, no completions, etc..\n", + " # This means that if you want to use Dall-E and completions together, you have to override the api_version here.\n", + " api_version='2023-04-01-preview',\n", + " prompt='A cyberpunk monkey hacker dreaming of a beautiful bunch of bananas, digital art',\n", + " size='1024x1024',\n", + " n=2\n", ")" ] } From 0ee3d26a505af949effb3a5a88efe3461fa302ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCrtz?= Date: Fri, 17 Mar 2023 20:52:50 +0100 Subject: [PATCH 4/4] Smaller adjustments --- examples/azure/dalle.ipynb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/azure/dalle.ipynb b/examples/azure/dalle.ipynb index b31bab44a8..5ad85b6fc3 100644 --- a/examples/azure/dalle.ipynb +++ b/examples/azure/dalle.ipynb @@ -35,10 +35,9 @@ "source": [ "openai.api_base = '' # Please add your endpoint here\n", "\n", - "# IMPORTANT: Dall-E is currently only available through api version '2023-04-01-preview'. This version only supports Dall-E, no completions, etc..\n", - "# This means that if you want to use DallE and completions together, you have to override the api_version in the constructor as also shown throughout this guide\n", - "# and set the global api_version to a version that supports completions, etc..\n", - "openai.api_version = '2023-04-01-preview'" + "# IMPORTANT: Dall-E is currently only available through api_version '2023-04-01-preview'. This version only supports Dall-E.\n", + "# If you want to use Dall-E and completions together, you have to override the api_version in the constructor as shown throughout the example.\n", + "openai.api_version = '2023-04-01-preview' # If this version is set globally completions and embeddings will not work without an override. " ] }, {