From 6d236595fd36b3244a4f1b90f9760a3b2320cf24 Mon Sep 17 00:00:00 2001 From: Pablo Nunes Date: Fri, 9 Feb 2024 16:27:25 +0000 Subject: [PATCH] Updating classes 00, adding OpenAI samples to 06 and 09 to the course. --- .env.copy | 4 + 00-course-setup/README.md | 4 + 06-text-generation-apps/openai/app-recipe.py | 41 + 06-text-generation-apps/openai/app.py | 25 + 06-text-generation-apps/openai/history-bot.py | 31 + .../openai/notebook-openai.ipynb | 754 ++++++++++++++++++ 06-text-generation-apps/openai/study-buddy.py | 38 + 09-building-image-applications/app.py | 20 +- .../notebook-azureopenai.ipynb | 15 +- .../openai/app-variation.py | 39 + 09-building-image-applications/openai/app.py | 55 ++ .../openai/notebook-openai.ipynb | 318 ++++++++ 12 files changed, 1313 insertions(+), 31 deletions(-) create mode 100644 06-text-generation-apps/openai/app-recipe.py create mode 100644 06-text-generation-apps/openai/app.py create mode 100644 06-text-generation-apps/openai/history-bot.py create mode 100644 06-text-generation-apps/openai/notebook-openai.ipynb create mode 100644 06-text-generation-apps/openai/study-buddy.py create mode 100644 09-building-image-applications/openai/app-variation.py create mode 100644 09-building-image-applications/openai/app.py create mode 100644 09-building-image-applications/openai/notebook-openai.ipynb diff --git a/.env.copy b/.env.copy index f473794d3..03cdea7e0 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='' diff --git a/00-course-setup/README.md b/00-course-setup/README.md index 9414421fc..cdce38b08 100644 --- a/00-course-setup/README.md +++ b/00-course-setup/README.md @@ -111,6 +111,10 @@ While you wait for your application to be processed, each coding lesson also inc If this is your first time working with the Azure OpenAI service, please follow this guide on how to [create and deploy an Azure OpenAI Service resource.](https://learn.microsoft.com/azure/ai-services/openai/how-to/create-resource?pivots=web-portal&WT.mc_id=academic-105485-koreyst) +## Using the OpenAI API for the First Time + +If this is your first working with the OpenAI API, please follow the guide on how to [create and use the Interface.](https://platform.openai.com/docs/quickstart?context=pythont&WT.mc_id=academic-105485-koreyst) + ## Meet Other Learners We have created channels in our official [AI Community Discord server](https://aka.ms/genai-discord?WT.mc_id=academic-105485-koreyst) for meeting other learners. This is a great way to network with other like-minded entrepreneurs, builders, students, and anyone looking to level up in Generative AI. diff --git a/06-text-generation-apps/openai/app-recipe.py b/06-text-generation-apps/openai/app-recipe.py new file mode 100644 index 000000000..df636a8f7 --- /dev/null +++ b/06-text-generation-apps/openai/app-recipe.py @@ -0,0 +1,41 @@ +from openai import OpenAI +import os +import dotenv + +# import dotenv +dotenv.load_dotenv() + +# configure Azure OpenAI service client +client = OpenAI() + +#deployment=os.environ['OPENAI_DEPLOYMENT'] +deployment="gpt-3.5-turbo" + +no_recipes = input("No of recipes (for example, 5: ") + +ingredients = input("List of ingredients (for example, chicken, potatoes, and carrots: ") + +filter = input("Filter (for example, vegetarian, vegan, or gluten-free: ") + +# interpolate the number of recipes into the prompt an ingredients +prompt = f"Show me {no_recipes} recipes for a dish with the following ingredients: {ingredients}. Per recipe, list all the ingredients used, no {filter}: " +messages = [{"role": "user", "content": prompt}] + +completion = client.chat.completions.create(model=deployment, messages=messages, max_tokens=600, temperature = 0.1) + + +# print response +print("Recipes:") +print(completion.choices[0].message.content) + +old_prompt_result = completion.choices[0].message.content +prompt_shopping = "Produce a shopping list, and please don't include ingredients that I already have at home: " + +new_prompt = f"Given ingredients at home {ingredients} and these generated recipes: {old_prompt_result}, {prompt_shopping}" +messages = [{"role": "user", "content": new_prompt}] +completion = client.chat.completions.create(model=deployment, messages=messages, max_tokens=600, temperature=0) + +# print response +print("\n=====Shopping list ======= \n") +print(completion.choices[0].message.content) + diff --git a/06-text-generation-apps/openai/app.py b/06-text-generation-apps/openai/app.py new file mode 100644 index 000000000..64b291529 --- /dev/null +++ b/06-text-generation-apps/openai/app.py @@ -0,0 +1,25 @@ +from openai import OpenAI +import os +import dotenv + +# import dotenv +dotenv.load_dotenv() + +# configure OpenAI service client +client = OpenAI() + +#deployment=os.environ['OPENAI_DEPLOYMENT'] +deployment="gpt-3.5-turbo" + +# add your completion code +prompt = "Complete the following: Once upon a time there was a" +messages = [{"role": "user", "content": prompt}] +# make completion +completion = client.chat.completions.create(model=deployment, messages=messages) + +# print response +print(completion.choices[0].message.content) + +# very unhappy _____. + +# Once upon a time there was a very unhappy mermaid. diff --git a/06-text-generation-apps/openai/history-bot.py b/06-text-generation-apps/openai/history-bot.py new file mode 100644 index 000000000..2ef137cb6 --- /dev/null +++ b/06-text-generation-apps/openai/history-bot.py @@ -0,0 +1,31 @@ +from openai import OpenAI +import os +import dotenv + +# import dotenv +dotenv.load_dotenv() + +# configure Azure OpenAI service client +client = OpenAI() +deployment="gpt-3.5-turbo" + +# add your completion code +persona = input("Tell me the historical character I want to be: ") +question = input("Ask your question about the historical character: ") +prompt = f""" +You are going to play as a historical character {persona}. + +Whenever certain questions are asked, you need to remember facts about the timelines and incidents and respond the accurate answer only. Don't create content yourself. If you don't know something, tell that you don't remember. + +Provide answer for the question: {question} +""" +messages = [{"role": "user", "content": prompt}] +# make completion +completion = client.chat.completions.create(model=deployment, messages=messages, temperature=0) + +# print response +print(completion.choices[0].message.content) + +# very unhappy _____. + +# Once upon a time there was a very unhappy mermaid. \ No newline at end of file diff --git a/06-text-generation-apps/openai/notebook-openai.ipynb b/06-text-generation-apps/openai/notebook-openai.ipynb new file mode 100644 index 000000000..cb9c5027e --- /dev/null +++ b/06-text-generation-apps/openai/notebook-openai.ipynb @@ -0,0 +1,754 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Build text generation apps\n", + "\n", + "You've seen so far through this curriculum that there are core concepts like prompts and even a whole discipline called \"prompt engineering\". Many tools you can interact with like ChatGPT, Office 365, Microsoft Power Platform and more, support you using prompts to accomplish something.\n", + "\n", + "For you to add such an experience to an app, you need to understand concepts like prompts, completions and choose a library to work with. That's exactly what you'll learn in this chapter.\n", + "\n", + "## Introduction\n", + "\n", + "In this chapter, you will:\n", + "\n", + "- Learn about the openai library and its core concepts.\n", + "- Build a text generation app using openai.\n", + "- Understand how to use concepts like prompt, temperature, and tokens to build a text generation app.\n", + "\n", + "## Learning goals\n", + "\n", + "At the end of this lesson, you'll be able to:\n", + "\n", + "- Explain what a text generation app is.\n", + "- Build a text generation app using openai.\n", + "- Configure your app to use more or less tokens and also change the temperature, for a varied output.\n", + "\n", + "## What is a text generation app?\n", + "\n", + "Normally when you build an app it has some kind of interface like the following:\n", + "\n", + "- Command-based. Console apps are typical apps where you type a command and it carries out a task. For example, `git` is a command-based app.\n", + "- User interface (UI). Some apps have graphical user interfaces (GUIs) where you click buttons, input text, select options and more.\n", + "\n", + "### Console and UI apps are limited\n", + "\n", + "Compare it to a command-based app where you type a command: \n", + "\n", + "- **It's limited**. You can't just type any command, only the ones that the app supports.\n", + "- **Language specific**. Some apps support many languages, but by default the app is built for a specific language, even if you can add more language support. \n", + "\n", + "### Benefits of text generation apps\n", + "\n", + "So how is a text generation app different?\n", + "\n", + "In a text generation app, you have more flexibility, you're not limited to a set of commands or a specific input language. Instead, you can use natural language to interact with the app. Another benefit is that because you're already interacting with a data source that has been trained on a vast corpus of information, whereas a traditional app might be limited on what's in a database. \n", + "\n", + "### What can I build with a text generation app?\n", + "\n", + "There are many things you can build. For example:\n", + "\n", + "- **A chatbot**. A chatbot answering questions about topics, like your company and its products could be a good match.\n", + "- **Helper**. LLMs are great at things like summarizing text, getting insights from text, producing text like resumes and more.\n", + "- **Code assistant**. Depending on the language model you use, you can build a code assistant that helps you write code. For example, you can use a product like GitHub Copilot as well as ChatGPT to help you write code.\n", + "\n", + "## How can I get started?\n", + "\n", + "Well, you need to find a way to integrate with an LLM which usually entails the following two approaches:\n", + "\n", + "- Use an API. Here you're constructing web requests with your prompt and get generated text back.\n", + "- Use a library. Libraries help encapsulate the API calls and make them easier to use.\n", + "\n", + "## Libraries/SDKs\n", + "\n", + "There are a few well known libraries for working with LLMs like:\n", + "\n", + "- **openai**, this library makes it easy to connect to your model and send in prompts.\n", + "\n", + "Then there are libraries that operate on a higher level like:\n", + "\n", + "- **Langchain**. Langchain is well known and supports Python.\n", + "- **Semantic Kernel**. Semantic Kernel is a library by Microsoft supporting the languages C#, Python, and Java.\n", + "\n", + "## First app using openai\n", + "\n", + "Let's see how we can build our first app, what libraries we need, how much is required and so on.\n", + "\n", + "### Install openai\n", + "\n", + " > [!NOTE] This step is not necessary if run this notebook on Codespaces or within a Devcontainer\n", + "\n", + "\n", + "There are many libraries out there for interacting with OpenAI or Azure OpenAI. It's possible to use numerous programming languages as well like C#, Python, JavaScript, Java and more. \n", + "We've chosen to use the `openai` Python library, so we'll use `pip` to install it.\n", + "\n", + "```bash\n", + "pip install openai\n", + "```\n", + "\n", + "If you aren't running this notebook in a Codespaces or a Dev Container, you also need to install [Python](https://www.python.org/) on your machine.\n", + "\n", + "### Create a resource and locate your API key\n", + "\n", + "In case you didn't already, you need to carry out the following steps:\n", + "\n", + "- Create an account on OpenAI .\n", + "- Now, get your API keys . \n", + "\n", + ">[!NOTE]\n", + "> It's worth separating your API key from your code. You can do so by using environment variables.\n", + "> - Set the environment variable `OPENAI_KEY` to your API key in your .env file. If you already completed the previous exercises of this course, you are all set up.\n", + "> - 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", + "\n", + "If you're using Open AI, here's how you setup configuration:\n", + "\n", + "```python\n", + "client = AzureOpenAI(\n", + " api_key=os.environ['OPENAI_API_KEY']\n", + " )\n", + "\n", + "deployment=os.environ['OPENAI_DEPLOYMENT']\n", + "```\n", + "\n", + "Above we're setting the following:\n", + "\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_KEY` and `AZURE_OPENAI_ENDPOINT`.\n", + "\n", + "## Generate text\n", + "\n", + "The way to generate text is to use the `chat.completion` class. Here's an example:\n", + "\n", + "```python\n", + "prompt = \"Complete the following: Once upon a time there was a\"\n", + "\n", + "completion = client.chat.completions.create(model=deployment, messages=[{\"role\": \"user\", \"content\": prompt}])\n", + "print(completion.choices[0].message.content)\n", + "```\n", + "\n", + "In the above code, we create a completion object and pass in the model we want to use and the prompt. Then we print the generated text.\n", + "\n", + "### Chat completions\n", + "\n", + "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", + " )\n", + "\n", + "deployment=os.environ['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", + "```\n", + "\n", + "More on this functionality in a coming chapter.\n", + "\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", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. Create a virtual environment and install openai:\n", + "\n", + " > [!NOTE] This step is not necessary if you run this notebook on Codespaces or within a Devcontainer" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Create virtual environment\n", + "! python -m venv venv\n", + "# Activate virtual environment\n", + "! source venv/bin/activate\n", + "# Install openai package\n", + "! pip install openai\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> [!NOTE]\n", + "> 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." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. Create a *app.py* file and give it the following code:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "from openai import AzureOpenAI\n", + "from dotenv import load_dotenv\n", + "load_dotenv()\n", + "\n", + "client = AzureOpenAI(\n", + " api_key=os.environ['OPENAI_API_KEY']\n", + " )\n", + "\n", + "deployment=os.environ['OPENAI_DEPLOYMENT']\n", + "\n", + "# add your completion code\n", + "prompt = \"Complete the following: Once upon a time there was a\"\n", + "messages = [{\"role\": \"user\", \"content\": prompt}] \n", + "\n", + "# make completion\n", + "completion = client.chat.completions.create(model=deployment, messages=messages)\n", + "\n", + "# print response\n", + "print(completion.choices[0].message.content)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + " You should see an output like the following:\n", + "\n", + " ```output\n", + " very unhappy _____.\n", + "\n", + " Once upon a time there was a very unhappy mermaid.\n", + " ```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Different types of prompts, for different things\n", + "\n", + "Now you've seen how to generate text using a prompt. You even have a program up and running that you can modify and change to generate different types of text. \n", + "\n", + "Prompts can be used for all sorts of tasks. For example:\n", + "\n", + "- **Generate a type of text**. For example, you can generate a poem, questions for a quiz etc.\n", + "- **Lookup information**. You can use prompts to look for information like the following example 'What does CORS mean in web development?'.\n", + "- **Generate code**. You can use prompts to generate code, for example developing a regular expression used to validate emails or why not generate an entire program, like a web app? \n", + "\n", + "## A more practical use case: a recipe generator\n", + "\n", + "Imagine you have ingredients at home and you want to cook something. For that, you need a recipe. A way to find recipes is to use a search engine or you could use an LLM to do so.\n", + "\n", + "You could write a prompt like so:\n", + "\n", + "> \"Show me 5 recipes for a dish with the following ingredients: chicken, potatoes, and carrots. Per recipe, list all the ingredients used\"\n", + "\n", + "Given the above prompt, you might get a response similar to:\n", + "\n", + "```output\n", + "1. Roasted Chicken and Vegetables: \n", + "Ingredients: \n", + "- 4 chicken thighs\n", + "- 2 potatoes, cut into cubes\n", + "- 2 carrots, cut into cubes\n", + "- 2 tablespoons olive oil\n", + "- 2 cloves garlic, minced\n", + "- 1 teaspoon dried thyme\n", + "- 1 teaspoon dried oregano\n", + "- Salt and pepper, to taste\n", + "\n", + "2. Chicken and Potato Stew: \n", + "Ingredients: \n", + "- 2 tablespoons olive oil\n", + "- 1 onion, diced\n", + "- 2 cloves garlic, minced\n", + "- 2 chicken breasts, cut into cubes\n", + "- 2 potatoes, cut into cubes\n", + "- 2 carrots, cut into cubes\n", + "- 1 teaspoon dried oregano\n", + "- 1 teaspoon dried thyme\n", + "- 1 cup chicken broth\n", + "- Salt and pepper, to taste\n", + "\n", + "3. Chicken and Potato Bake: \n", + "Ingredients: \n", + "- 2 tablespoons olive oil\n", + "- 2 chicken breasts, cut into cubes\n", + "- 2 potatoes, cut into cubes\n", + "- 2 carrots, cut into cubes\n", + "- 1 onion, diced\n", + "- 2 cloves garlic, minced\n", + "- 1 teaspoon dried oregano\n", + "- 1 teaspoon dried thyme\n", + "- 1 cup chicken broth\n", + "- Salt and pepper, to taste\n", + "\n", + "4. Chicken and Potato Soup: \n", + "Ingredients: \n", + "- 2 tablespoons olive oil\n", + "- 1 onion, diced\n", + "- 2 cloves garlic, minced\n", + "- 2 chicken breasts, cut into cubes\n", + "- 2 potatoes, cut into cubes\n", + "- 2 carrots, cut into cubes\n", + "- 1 teaspoon dried oregano\n", + "- 1 teaspoon dried thyme\n", + "- 4 cups chicken broth\n", + "- Salt and pepper, to taste\n", + "\n", + "5. Chicken and Potato Hash: \n", + "Ingredients: \n", + "- 2 tablespoons olive oil\n", + "- 2 chicken breasts, cut into cubes\n", + "- 2 potatoes, cut into cubes\n", + "- 2 carrots, cut into cubes\n", + "- 1 onion, diced\n", + "- 2 cloves garlic, minced\n", + "- 1 teaspoon dried oregano\n", + "```\n", + "\n", + "This outcome is great, I know what to cook. At this point, what could be useful improvements are:\n", + "\n", + "- Filtering out ingredients I don't like or am allergic to.\n", + "- Produce a shopping list, in case I don't have all the ingredients at home.\n", + "\n", + "For the above cases, let's add an additional prompt:\n", + "\n", + "> \"Please remove recipes with garlic as I'm allergic and replace it with something else. Also, please produce a shopping list for the recipes, considering I already have chicken, potatoes and carrots at home.\"\n", + "\n", + "Now you have a new result, namely:\n", + "\n", + "```output\n", + "1. Roasted Chicken and Vegetables: \n", + "Ingredients: \n", + "- 4 chicken thighs\n", + "- 2 potatoes, cut into cubes\n", + "- 2 carrots, cut into cubes\n", + "- 2 tablespoons olive oil\n", + "- 1 teaspoon dried thyme\n", + "- 1 teaspoon dried oregano\n", + "- Salt and pepper, to taste\n", + "\n", + "2. Chicken and Potato Stew: \n", + "Ingredients: \n", + "- 2 tablespoons olive oil\n", + "- 1 onion, diced\n", + "- 2 chicken breasts, cut into cubes\n", + "- 2 potatoes, cut into cubes\n", + "- 2 carrots, cut into cubes\n", + "- 1 teaspoon dried oregano\n", + "- 1 teaspoon dried thyme\n", + "- 1 cup chicken broth\n", + "- Salt and pepper, to taste\n", + "\n", + "3. Chicken and Potato Bake: \n", + "Ingredients: \n", + "- 2 tablespoons olive oil\n", + "- 2 chicken breasts, cut into cubes\n", + "- 2 potatoes, cut into cubes\n", + "- 2 carrots, cut into cubes\n", + "- 1 onion, diced\n", + "- 1 teaspoon dried oregano\n", + "- 1 teaspoon dried thyme\n", + "- 1 cup chicken broth\n", + "- Salt and pepper, to taste\n", + "\n", + "4. Chicken and Potato Soup: \n", + "Ingredients: \n", + "- 2 tablespoons olive oil\n", + "- 1 onion, diced\n", + "- 2 chicken breasts, cut into cubes\n", + "- 2 potatoes, cut into cubes\n", + "- 2 carrots, cut into cubes\n", + "- 1 teaspoon dried oregano\n", + "- 1 teaspoon dried thyme\n", + "- 4 cups chicken broth\n", + "- Salt and pepper, to taste\n", + "\n", + "5. Chicken and Potato Hash: \n", + "Ingredients: \n", + "- 2 tablespoons olive oil\n", + "- 2 chicken breasts, cut into cubes\n", + "- 2 potatoes, cut into cubes\n", + "- 2 carrots, cut into cubes\n", + "- 1 onion, diced\n", + "- 1 teaspoon dried oregano\n", + "\n", + "Shopping List: \n", + "- Olive oil\n", + "- Onion\n", + "- Thyme\n", + "- Oregano\n", + "- Salt\n", + "- Pepper\n", + "```\n", + "\n", + "That's your five recipes, with no garlic mentioned and you also have a shopping list considering what you already have at home. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise - build a recipe generator\n", + "\n", + "Now that we have played out a scenario, let's write code to match the demonstrated scenario. To do so, follow these steps:\n", + "\n", + "1. Use the existing *app.py* file as a starting point\n", + "1. Locate the `prompt` variable and change its code to the following:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "from openai import AzureOpenAI\n", + "\n", + "client = AzureOpenAI(\n", + " api_key=os.environ['OPENAI_API_KEY']\n", + " )\n", + "\n", + "deployment=os.environ['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", + "\n", + "# make completion\n", + "completion = client.chat.completions.create(model=deployment, messages=messages, max_tokens=600)\n", + "\n", + "# print response\n", + "print(completion.choices[0].message.content)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you now run the code, you should see an output similar to:\n", + "\n", + "```output\n", + "-Chicken Stew with Potatoes and Carrots: 3 tablespoons oil, 1 onion, chopped, 2 cloves garlic, minced, 1 carrot, peeled and chopped, 1 potato, peeled and chopped, 1 bay leaf, 1 thyme sprig, 1/2 teaspoon salt, 1/4 teaspoon black pepper, 1 1/2 cups chicken broth, 1/2 cup dry white wine, 2 tablespoons chopped fresh parsley, 2 tablespoons unsalted butter, 1 1/2 pounds boneless, skinless chicken thighs, cut into 1-inch pieces\n", + "-Oven-Roasted Chicken with Potatoes and Carrots: 3 tablespoons extra-virgin olive oil, 1 tablespoon Dijon mustard, 1 tablespoon chopped fresh rosemary, 1 tablespoon chopped fresh thyme, 4 cloves garlic, minced, 1 1/2 pounds small red potatoes, quartered, 1 1/2 pounds carrots, quartered lengthwise, 1/2 teaspoon salt, 1/4 teaspoon black pepper, 1 (4-pound) whole chicken\n", + "-Chicken, Potato, and Carrot Casserole: cooking spray, 1 large onion, chopped, 2 cloves garlic, minced, 1 carrot, peeled and shredded, 1 potato, peeled and shredded, 1/2 teaspoon dried thyme leaves, 1/4 teaspoon salt, 1/4 teaspoon black pepper, 2 cups fat-free, low-sodium chicken broth, 1 cup frozen peas, 1/4 cup all-purpose flour, 1 cup 2% reduced-fat milk, 1/4 cup grated Parmesan cheese\n", + "\n", + "-One Pot Chicken and Potato Dinner: 2 tablespoons olive oil, 1 pound boneless, skinless chicken thighs, cut into 1-inch pieces, 1 large onion, chopped, 3 cloves garlic, minced, 1 carrot, peeled and chopped, 1 potato, peeled and chopped, 1 bay leaf, 1 thyme sprig, 1/2 teaspoon salt, 1/4 teaspoon black pepper, 2 cups chicken broth, 1/2 cup dry white wine\n", + "\n", + "-Chicken, Potato, and Carrot Curry: 1 tablespoon vegetable oil, 1 large onion, chopped, 2 cloves garlic, minced, 1 carrot, peeled and chopped, 1 potato, peeled and chopped, 1 teaspoon ground coriander, 1 teaspoon ground cumin, 1/2 teaspoon ground turmeric, 1/2 teaspoon ground ginger, 1/4 teaspoon cayenne pepper, 2 cups chicken broth, 1/2 cup dry white wine, 1 (15-ounce) can chickpeas, drained and rinsed, 1/2 cup raisins, 1/2 cup chopped fresh cilantro\n", + "```\n", + "\n", + "> NOTE, your LLM is nondeterministic, so you might get different results every time you run the program.\n", + "\n", + "Great, let's see how we can improve things. To improve things, we want to make sure the code is flexible, so ingredients and number of recipes can be improved and changed. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. Let's change the code in the following way:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "from openai import AzureOpenAI\n", + "\n", + "client = AzureOpenAI(\n", + " api_key=os.environ['OPENAI_API_KEY']\n", + " )\n", + "\n", + "deployment=os.environ['OPENAI_DEPLOYMENT']\n", + "\n", + "no_recipes = input(\"No of recipes (for example, 5: \")\n", + "\n", + "ingredients = input(\"List of ingredients (for example, chicken, potatoes, and carrots: \")\n", + "\n", + "# interpolate the number of recipes into the prompt an ingredients\n", + "prompt = f\"Show me {no_recipes} recipes for a dish with the following ingredients: {ingredients}. Per recipe, list all the ingredients used\"\n", + "messages = [{\"role\": \"user\", \"content\": prompt}] \n", + "\n", + "# make completion\n", + "completion = client.chat.completions.create(model=deployment, messages=messages, max_tokens=600)\n", + "\n", + "# print response\n", + "print(completion.choices[0].message.content)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "Taking the code for a test run, could look like this:\n", + " \n", + "```output\n", + "No of recipes (for example, 5: 3\n", + "List of ingredients (for example, chicken, potatoes, and carrots: milk,strawberries\n", + "\n", + "-Strawberry milk shake: milk, strawberries, sugar, vanilla extract, ice cubes\n", + "-Strawberry shortcake: milk, flour, baking powder, sugar, salt, unsalted butter, strawberries, whipped cream \n", + "-Strawberry milk: milk, strawberries, sugar, vanilla extract\n", + "```\n", + "\n", + "### Improve by adding filter and shopping list\n", + "\n", + "We now have a working app capable of producing recipes and it's flexible as it relies on inputs from the user, both on the number of recipes but also the ingredients used.\n", + "\n", + "To further improve it, we want to add the following:\n", + "\n", + "- **Filter out ingredients**. We want to be able to filter out ingredients we don't like or are allergic to. To accomplish this change, we can edit our existing prompt and add a filter condition to the end of it like so:\n", + "\n", + " ```python\n", + " filter = input(\"Filter (for example, vegetarian, vegan, or gluten-free: \")\n", + "\n", + " prompt = f\"Show me {no_recipes} recipes for a dish with the following ingredients: {ingredients}. Per recipe, list all the ingredients used, no {filter}\"\n", + " ```\n", + "\n", + " Above, we add `{filter}` to the end of the prompt and we also capture the filter value from the user.\n", + "\n", + " An example input of running the program can now look like so:\n", + " \n", + " ```output \n", + " No of recipes (for example, 5: 3\n", + " List of ingredients (for example, chicken, potatoes, and carrots: onion,milk\n", + " Filter (for example, vegetarian, vegan, or gluten-free: no milk\n", + "\n", + " 1. French Onion Soup\n", + "\n", + " Ingredients:\n", + " \n", + " -1 large onion, sliced\n", + " -3 cups beef broth\n", + " -1 cup milk\n", + " -6 slices french bread\n", + " -1/4 cup shredded Parmesan cheese\n", + " -1 tablespoon butter\n", + " -1 teaspoon dried thyme\n", + " -1/4 teaspoon salt\n", + " -1/4 teaspoon black pepper\n", + " \n", + " Instructions:\n", + " \n", + " 1. In a large pot, sauté onions in butter until golden brown.\n", + " 2. Add beef broth, milk, thyme, salt, and pepper. Bring to a boil.\n", + " 3. Reduce heat and simmer for 10 minutes.\n", + " 4. Place french bread slices on soup bowls.\n", + " 5. Ladle soup over bread.\n", + " 6. Sprinkle with Parmesan cheese.\n", + " \n", + " 2. Onion and Potato Soup\n", + " \n", + " Ingredients:\n", + " \n", + " -1 large onion, chopped\n", + " -2 cups potatoes, diced\n", + " -3 cups vegetable broth\n", + " -1 cup milk\n", + " -1/4 teaspoon black pepper\n", + " \n", + " Instructions:\n", + " \n", + " 1. In a large pot, sauté onions in butter until golden brown.\n", + " 2. Add potatoes, vegetable broth, milk, and pepper. Bring to a boil.\n", + " 3. Reduce heat and simmer for 10 minutes.\n", + " 4. Serve hot.\n", + " \n", + " 3. Creamy Onion Soup\n", + " \n", + " Ingredients:\n", + " \n", + " -1 large onion, chopped\n", + " -3 cups vegetable broth\n", + " -1 cup milk\n", + " -1/4 teaspoon black pepper\n", + " -1/4 cup all-purpose flour\n", + " -1/2 cup shredded Parmesan cheese\n", + " \n", + " Instructions:\n", + " \n", + " 1. In a large pot, sauté onions in butter until golden brown.\n", + " 2. Add vegetable broth, milk, and pepper. Bring to a boil.\n", + " 3. Reduce heat and simmer for 10 minutes.\n", + " 4. In a small bowl, whisk together flour and Parmesan cheese until smooth.\n", + " 5. Add to soup and simmer for an additional 5 minutes, or until soup has thickened.\n", + " ```\n", + "\n", + " As you can see, any recipes with milk in it has been filtered out. But, if you're lactose intolerant, you might want to filter out recipes with cheese in them as well, so there's a need to be clear.\n", + "\n", + " ```python\n", + " \n", + "- **Produce a shopping list**. We want to produce a shopping list, considering what we already have at home.\n", + "\n", + " For this functionality, we could either try to solve everything in one prompt or we could split it up into two prompts. Let's try the latter approach. Here we're suggesting adding an additional prompt, but for that to work, we need to add the result of the former prompt as context to the latter prompt. \n", + "\n", + " Locate the part in the code that prints out the result from the first prompt and add the following code below:\n", + " \n", + " ```python\n", + " old_prompt_result = completion.choices[0].text\n", + " prompt = \"Produce a shopping list for the generated recipes and please don't include ingredients that I already have.\"\n", + " \n", + " new_prompt = f\"{old_prompt_result} {prompt}\"\n", + " messages = [{\"role\": \"user\", \"content\": new_prompt}]\n", + " completion = client.chat.completion.create(model=deployment, messages=messages, max_tokens=1200)\n", + " \n", + " # print response\n", + " print(\"Shopping list:\")\n", + " print(completion.choices[0].message.content)\n", + " ```\n", + "\n", + " Note the following:\n", + "\n", + " - We're constructing a new prompt by adding the result from the first prompt to the new prompt: \n", + " \n", + " ```python\n", + " new_prompt = f\"{old_prompt_result} {prompt}\"\n", + " messages = [{\"role\": \"user\", \"content\": new_prompt}]\n", + " ```\n", + "\n", + " - We make a new request, but also considering the number of tokens we asked for in the first prompt, so this time we say `max_tokens` is 1200. \n", + "\n", + " ```python\n", + " completion = client.chat.completion.create(model=deployment, messages=messages, max_tokens=1200)\n", + " ``` \n", + "\n", + " Taking this code for a spin, we now arrive at the following output:\n", + "\n", + " ```output\n", + " No of recipes (for example, 5: 2\n", + " List of ingredients (for example, chicken, potatoes, and carrots: apple,flour\n", + " Filter (for example, vegetarian, vegan, or gluten-free: sugar\n", + " Recipes:\n", + " or milk.\n", + " \n", + " -Apple and flour pancakes: 1 cup flour, 1/2 tsp baking powder, 1/2 tsp baking soda, 1/4 tsp salt, 1 tbsp sugar, 1 egg, 1 cup buttermilk or sour milk, 1/4 cup melted butter, 1 Granny Smith apple, peeled and grated\n", + " -Apple fritters: 1-1/2 cups flour, 1 tsp baking powder, 1/4 tsp salt, 1/4 tsp baking soda, 1/4 tsp nutmeg, 1/4 tsp cinnamon, 1/4 tsp allspice, 1/4 cup sugar, 1/4 cup vegetable shortening, 1/4 cup milk, 1 egg, 2 cups shredded, peeled apples\n", + " Shopping list:\n", + " -Flour, baking powder, baking soda, salt, sugar, egg, buttermilk, butter, apple, nutmeg, cinnamon, allspice \n", + " ```\n", + " \n", + "- **A word on token length**. We should consider how many tokens we need to generate the text we want. Tokens cost money, so where possible, we should try to be economical with the number of tokens we use. For example, can we phrase the prompt so that we can use less tokens?\n", + "\n", + " To change tokens used, you can use the `max_tokens` parameter. For example, if you want to use 100 tokens, you would do:\n", + "\n", + " ```python\n", + " completion = client.chat.completion.create(model=deployment, messages=messages, max_tokens=100)\n", + " ```\n", + "\n", + "- **Experimenting with temperature**. Temperature is something we haven't mentioned so far but is an important context for how our program performs. The higher the temperature value the more random the output will be. Conversely the lower the temperature value the more predictable the output will be. Consider whether you want variation in your output or not.\n", + "\n", + " To alter the temperature, you can use the `temperature` parameter. For example, if you want to use a temperature of 0.5, you would do:\n", + "\n", + " ```python\n", + " completion = client.chat.completion.create(model=deployment, messages=messages, temperature=0.5)\n", + " ```\n", + "\n", + " > Note, the closer to 1.0, the more varied the output.\n", + "\n", + "\n", + "\n", + "## Assignment\n", + "\n", + "For this assignment, you can choose what to build.\n", + "\n", + "Here are some suggestions:\n", + "\n", + "- Tweak the recipe generator app to improve it further. Play around with temperature values, and the prompts to see what you can come up with.\n", + "- Build a \"study buddy\". This app should be able to answer questions about a topic for example Python, you could have prompts like \"What is a certain topic in Python?\", or you could have a prompt that says, show me code for a certain topic etc.\n", + "- History bot, make history come alive, instruct the bot to play a certain historical character and ask it questions about its life and times. \n", + "\n", + "## Solution\n", + "\n", + "### Study buddy\n", + "\n", + "- \"You're an expert on the Python language\n", + "\n", + " Suggest a beginner lesson for Python in the following format:\n", + " \n", + " Format:\n", + " - concepts:\n", + " - brief explanation of the lesson:\n", + " - exercise in code with solutions\"\n", + "\n", + "Above is a starter prompt, see how you can use it and tweak it to your liking.\n", + "\n", + "### History bot\n", + "\n", + "Here's some prompts you could be using:\n", + "\n", + "- \"You are Abe Lincoln, tell me about yourself in 3 sentences, and respond using grammar and words like Abe would have used\"\n", + "- \"You are Abe Lincoln, respond using grammar and words like Abe would have used:\n", + "\n", + " Tell me about your greatest accomplishments, in 300 words:\"\n", + "\n", + "## Knowledge check\n", + "\n", + "What does the concept temperature do?\n", + "\n", + "1. It controls how random the output is.\n", + "1. It controls how big the response is.\n", + "1. It controls how many tokens are used.\n", + "\n", + "A: 1\n", + "\n", + "What's a good way to store secrets like API keys?\n", + "\n", + "1. In code.\n", + "1. In a file.\n", + "1. In environment variables.\n", + "\n", + "A: 3, because environment variables are not stored in code and can be loaded from the code. " + ] + } + ], + "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/06-text-generation-apps/openai/study-buddy.py b/06-text-generation-apps/openai/study-buddy.py new file mode 100644 index 000000000..80ccc7757 --- /dev/null +++ b/06-text-generation-apps/openai/study-buddy.py @@ -0,0 +1,38 @@ +from openai import OpenAI +import os +import dotenv + +# import dotenv +dotenv.load_dotenv() + +# configure Azure OpenAI service client +client = OpenAI( + api_key=os.environ['OPENAI_API_KEY'] + ) + +#deployment=os.environ['OPENAI_DEPLOYMENT'] +deployment="gpt-3.5-turbo" + +# add your completion code +question = input("Ask your questions on python language to your study buddy: ") +prompt = f""" +You are an expert on the python language. + +Whenever certain questions are asked, you need to provide response in below format. + +- Concept +- Example code showing the concept implementation +- explanation of the example and how the concept is done for the user to understand better. + +Provide answer for the question: {question} +""" +messages = [{"role": "user", "content": prompt}] +# make completion +completion = client.chat.completions.create(model=deployment, messages=messages) + +# print response +print(completion.choices[0].message.content) + +# very unhappy _____. + +# Once upon a time there was a very unhappy mermaid. diff --git a/09-building-image-applications/app.py b/09-building-image-applications/app.py index 6c045f222..bba04461f 100644 --- a/09-building-image-applications/app.py +++ b/09-building-image-applications/app.py @@ -59,22 +59,4 @@ image=open(image_path, "rb"), n=1, size="1024x1024" -) -======= -# response = openai.Image.create_variation( -# image=open(image_path, "rb"), -# n=1, -# size="1024x1024" -# ) - -# image_path = os.path.join(image_dir, 'generated_variation.png') - -# image_url = response['data'][0]['url'] - -# generated_image = requests.get(image_url).content # download the image -# with open(image_path, "wb") as image_file: -# image_file.write(generated_image) - -# # Display the image in the default image viewer -# image = Image.open(image_path) -# image.show() \ No newline at end of file +) \ No newline at end of file diff --git a/09-building-image-applications/notebook-azureopenai.ipynb b/09-building-image-applications/notebook-azureopenai.ipynb index 448d42c14..7ad4c53e7 100644 --- a/09-building-image-applications/notebook-azureopenai.ipynb +++ b/09-building-image-applications/notebook-azureopenai.ipynb @@ -65,6 +65,7 @@ " \n", "\n", "![Image generated by Midjourney, mechanical pigeon](https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Rupert_Breheny_mechanical_dove_eca144e7-476d-4976-821d-a49c408e4f36.png/440px-Rupert_Breheny_mechanical_dove_eca144e7-476d-4976-821d-a49c408e4f36.png?WT.mc_id=academic-105485-koreyst)\n", + "\n", "*Image cred Wikipedia, image generated by Midjourney*\n", "\n", "## How does DALL-E and Midjourney Work \n", @@ -297,18 +298,8 @@ "\n", " The base image would only contain the rabbit but the final image would have the hat on the rabbit.\n", " \n", - "- **Create variations**. The idea is that you take an existing image and ask that variations are created. To create a variation, you provide an image and a text prompt and code like so:\n", - "\n", - " ```python\n", - " response = openai.Image.create_variation(\n", - " image=open(\"bunny-lollipop.png\", \"rb\"),\n", - " n=1,\n", - " size=\"1024x1024\"\n", - " )\n", - " image_url = response['data'][0]['url']\n", - " ```\n", - " \n", - " > Note, this is only supported on OpenAI" + "- **Create variations**. \n", + " Look at our [OpenAI notebook for more information](/09-building-image-applications/openai/notebook-openai.ipynb)." ] } ], diff --git a/09-building-image-applications/openai/app-variation.py b/09-building-image-applications/openai/app-variation.py new file mode 100644 index 000000000..fb07f6d46 --- /dev/null +++ b/09-building-image-applications/openai/app-variation.py @@ -0,0 +1,39 @@ +from openai import OpenAI +import os +import requests +from PIL import Image +import dotenv + +# import dotenv +dotenv.load_dotenv() + +openai = OpenAI() + +image_dir = os.path.join(os.curdir, 'images') + +# Initialize the image path (note the filetype should be png) +image_path = os.path.join(image_dir, 'generated-image.png') + +# ---creating variation below--- +try: + print("LOG creating variation") + response = openai.images.create_variation( + image=open("generated-image.png", "rb"), + n=1, + size="1024x1024" + ) + + image_path = os.path.join(image_dir, 'generated_variation.png') + + image_url = response.data[0].url + + print("LOG downloading image") + generated_image = requests.get(image_url).content # download the image + with open(image_path, "wb") as image_file: + image_file.write(generated_image) + + # Display the image in the default image viewer + image = Image.open(image_path) + image.show() +except openai.error.InvalidRequestError as err: + print(err) \ No newline at end of file diff --git a/09-building-image-applications/openai/app.py b/09-building-image-applications/openai/app.py new file mode 100644 index 000000000..d3206cb3f --- /dev/null +++ b/09-building-image-applications/openai/app.py @@ -0,0 +1,55 @@ +from openai import OpenAI +import os +import requests +from PIL import Image +import dotenv + +# import dotenv +dotenv.load_dotenv() + + +client = OpenAI() + + +try: + # Create an image by using the image generation API + generation_response = client.images.generate( + model="dall-e-3", + prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here + size='1024x1024', + n=1 + ) + # Set the directory for the stored image + image_dir = os.path.join(os.curdir, 'images') + + # If the directory doesn't exist, create it + if not os.path.isdir(image_dir): + os.mkdir(image_dir) + + # Initialize the image path (note the filetype should be png) + image_path = os.path.join(image_dir, 'generated-image.png') + + # Retrieve the generated image + print(generation_response) + + image_url = generation_response.data[0].url # extract image URL from response + generated_image = requests.get(image_url).content # download the image + with open(image_path, "wb") as image_file: + image_file.write(generated_image) + + # Display the image in the default image viewer + image = Image.open(image_path) + image.show() + +# catch exceptions +except client.error.InvalidRequestError as err: + print(err) + +# ---creating variation below--- + + +response = client.images.create_variation( + image=open(image_path, "rb"), + n=1, + size="1024x1024" +) \ No newline at end of file diff --git a/09-building-image-applications/openai/notebook-openai.ipynb b/09-building-image-applications/openai/notebook-openai.ipynb new file mode 100644 index 000000000..4027c5d26 --- /dev/null +++ b/09-building-image-applications/openai/notebook-openai.ipynb @@ -0,0 +1,318 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Building an Image Generation Application \n", + "\n", + "There's more to LLMs than text generation. It's also possible to generate images from text descriptions. Having images as a modality can be highly useful in a number of areas from MedTech, architecture, tourism, game development and more. In this chapter, we will look into the two most popular image generation models, DALL-E and Midjourney.\n", + "\n", + "## Introduction \n", + "\n", + "In this lesson, we will cover:\n", + "\n", + "- Image generation and why it's useful.\n", + "- DALL-E and Midjourney, what they are, and how they work.\n", + "- How you would build an image generation app.\n", + "\n", + "## Learning Goals \n", + "\n", + "After completing this lesson, you will be able to:\n", + "\n", + "- Build an image generation application.\n", + "- Define boundaries for your application with meta prompts. \n", + "- Work with DALL-E and Midjourney.\n", + "\n", + "## Why build an image generation application?\n", + "\n", + "Image generation applications are a great way to explore the capabilities of Generative AI. They can be used for, for example: \n", + "\n", + "- **Image editing and synthesis**. You can generate images for a variety of use cases, such as image editing and image synthesis. \n", + "\n", + "- **Applied to a variety of industries**. They can also be used to generate images for a variety of industries like Medtech, Tourism, Game development and more. \n", + "\n", + "## Scenario: Edu4All \n", + "\n", + "As part of this lesson, we will continue to work with our startup, Edu4All, in this lesson. The students will create images for their assessments, exactly what images is up to the students, but they could be illustrations for their own fairytale or create a new character for their story or help them visualize their ideas and concepts. \n", + "\n", + "Here's what Edu4All's students could generate for example if they're working in class on monuments:\n", + "\n", + "![Edu4All startup, class on monuments, Eifel Tower](./images/startup.png?WT.mc_id=academic-105485-koreyst)\n", + "\n", + "using a prompt like \n", + "\n", + "> \"Dog next to Eiffel Tower in early morning sunlight\"\n", + "\n", + "## What is DALL-E and Midjourney? \n", + "\n", + "[DALL-E](https://openai.com/dall-e-2?WT.mc_id=academic-105485-koreyst) and [Midjourney](https://www.midjourney.com/?WT.mc_id=academic-105485-koreyst) are two of the most popular image generation models, they allow you to use prompts to generate images.\n", + "\n", + "### DALL-E\n", + "\n", + "Let's start with DALL-E, which is a Generative AI model that generates images from text descriptions. \n", + "\n", + "> [DALL-E is a combination of two models, CLIP and diffused attention](https://towardsdatascience.com/openais-dall-e-and-clip-101-a-brief-introduction-3a4367280d4e?WT.mc_id=academic-105485-koreyst). \n", + "\n", + "- **CLIP**, is a model that generates embeddings, which are numerical representations of data, from images and text. \n", + "\n", + "- **Diffused attention**, is a model that generates images from embeddings. DALL-E is trained on a dataset of images and text and can be used to generate images from text descriptions. For example, DALL-E can be used to generate images of a cat in a hat, or a dog with a mohawk. \n", + "\n", + "### Midjourney\n", + " \n", + "Midjourney works in a similar way to DALL-E, it generates images from text prompts. Midjourney, can also be used to generate images using prompts like “a cat in a hat”, or a “dog with a mohawk”. \n", + "\n", + " \n", + "\n", + "![Image generated by Midjourney, mechanical pigeon](https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Rupert_Breheny_mechanical_dove_eca144e7-476d-4976-821d-a49c408e4f36.png/440px-Rupert_Breheny_mechanical_dove_eca144e7-476d-4976-821d-a49c408e4f36.png?WT.mc_id=academic-105485-koreyst)\n", + "\n", + "\n", + "*Image cred Wikipedia, image generated by Midjourney*\n", + "\n", + "## How does DALL-E and Midjourney Work \n", + "\n", + "First, [DALL-E](https://arxiv.org/pdf/2102.12092.pdf?WT.mc_id=academic-105485-koreyst). DALL-E is a Generative AI model based on the transformer architecture with an *autoregressive transformer*. \n", + "\n", + "An *autoregressive transformer* defines how a model generates images from text descriptions, it generates one pixel at a time, and then uses the generated pixels to generate the next pixel. Passing through multiple layers in a neural network, until the image is complete. \n", + "\n", + "With this process, DALL-E, controls attributes, objects, characteristics, and more in the image it generates. However, DALL-E 2 and 3 have more control over the generated image, " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Building your first image generation application\n", + "\n", + "So what does it take to build an image generation application? You need the following libraries:\n", + "\n", + "- **python-dotenv**, you're highly recommended to use this library to keep your secrets in a *.env* file away from the code.\n", + "- **openai**, this library is what you will use to interact with the OpenAI API.\n", + "- **pillow**, to work with images in Python.\n", + "- **requests**, to help you make HTTP requests.\n", + "\n", + "\n", + "1. Create a file *.env* with the following content:\n", + "\n", + " ```text\n", + " OPENAI_API_KEY=''\n", + " ```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "1. Collect the above libraries in a file called *requirements.txt* like so:\n", + "\n", + " ```text\n", + " python-dotenv\n", + " openai\n", + " pillow\n", + " requests\n", + " ```\n", + "\n", + "\n", + "1. Next, create virtual environment and install the libraries:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [], + "source": [ + "# create virtual env\n", + "! python3 -m venv venv\n", + "# activate environment\n", + "! source venv/bin/activate\n", + "# install libraries\n", + "# pip install -r requirements.txt, if using a requirements.txt file \n", + "! pip install python-dotenv openai pillow requests" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> [!NOTE]\n", + "> For Windows, use the following commands to create and activate your virtual environment:\n", + "\n", + " ```bash\n", + " python3 -m venv venv\n", + " venv\\Scripts\\activate.bat\n", + " ````\n", + "\n", + "1. Add the following code in file called *app.py*:\n", + "\n", + " ```python\n", + " import openai\n", + " import os\n", + " import requests\n", + " from PIL import Image\n", + " import dotenv\n", + "\n", + " # import dotenv\n", + " dotenv.load_dotenv()\n", + "\n", + " # Create OpenAI object\n", + " client = OpenAI()\n", + "\n", + "\n", + " try:\n", + " # Create an image by using the image generation API\n", + " generation_response = client.images.generate(\n", + " model=\"dall-e-3\",\n", + " prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here\n", + " size='1024x1024',\n", + " n=1\n", + " )\n", + " # Set the directory for the stored image\n", + " image_dir = os.path.join(os.curdir, 'images')\n", + "\n", + " # If the directory doesn't exist, create it\n", + " if not os.path.isdir(image_dir):\n", + " os.mkdir(image_dir)\n", + "\n", + " # Initialize the image path (note the filetype should be png)\n", + " image_path = os.path.join(image_dir, 'generated-image.png')\n", + "\n", + " # Retrieve the generated image\n", + " print(generation_response)\n", + "\n", + " image_url = generation_response.data[0].url # extract image URL from response\n", + " generated_image = requests.get(image_url).content # download the image\n", + " with open(image_path, \"wb\") as image_file:\n", + " image_file.write(generated_image)\n", + "\n", + " # Display the image in the default image viewer\n", + " image = Image.open(image_path)\n", + " image.show()\n", + "\n", + " # catch exceptions\n", + " except client.error.InvalidRequestError as err:\n", + " print(err)\n", + "\n", + " ```\n", + "\n", + "Let's explain this code:\n", + "\n", + "- First, we import the libraries we need, including the OpenAI library, the dotenv library, the requests library, and the Pillow library.\n", + "\n", + " ```python\n", + " import openai\n", + " import os\n", + " import requests\n", + " from PIL import Image\n", + " import dotenv \n", + " ```\n", + "\n", + "- After that, we create the object, which will get the API key from your ``.env``.\n", + "\n", + " ```python\n", + " # Create OpenAI object\n", + " client = OpenAI()\n", + " ```\n", + "\n", + "- Next, we generate the image:\n", + "\n", + " ```python\n", + " # Create an image by using the image generation API\n", + " generation_response = client.images.generate(\n", + " model=\"dall-e-3\",\n", + " prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here\n", + " size='1024x1024',\n", + " n=1\n", + " )\n", + " ```\n", + "\n", + " The above code responds with a JSON object that contains the URL of the generated image. We can use the URL to download the image and save it to a file.\n", + "\n", + "- Lastly, we open the image and use the standard image viewer to display it:\n", + "\n", + " ```python\n", + " image = Image.open(image_path)\n", + " image.show()\n", + " ```\n", + " \n", + "### More details on generating the image\n", + "\n", + "Let's look at the code that generates the image in more detail:\n", + "\n", + "```python\n", + "generation_response = client.images.generate(\n", + " model=\"dall-e-3\",\n", + " prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils', # Enter your prompt text here\n", + " size='1024x1024',\n", + " n=1\n", + " )\n", + "```\n", + "\n", + "- **prompt**, is the text prompt that is used to generate the image. In this case, we're using the prompt \"Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils\".\n", + "- **size**, is the size of the image that is generated. In this case, we're generating an image that is 1024x1024 pixels.\n", + "- **n**, is the number of images that are generated. In this case, we're generating two images.\n", + "\n", + "There are more things you can do with images that we will cover in the next section.\n", + "\n", + "## Additional capabilities of image generation\n", + "\n", + "You've seen so far how we were able to generate an image using a few lines in Python. However, there are more things you can do with images.\n", + "\n", + "You can also do the following:\n", + "\n", + "- **Perform edits**. By providing an existing image a mask and a prompt, you can alter an image. For example, you can add something to a portion of an image. Imagine our bunny image, you can add a hat to the bunny. How you would do that is by providing the image, a mask (identifying the part of the area for the change) and a text prompt to say what should be done.\n", + "\n", + " ```python\n", + " response = openai.images.edit(\n", + " image=open(\"base_image.png\", \"rb\"),\n", + " mask=open(\"mask.png\", \"rb\"),\n", + " prompt=\"An image of a rabbit with a hat on its head.\",\n", + " n=1,\n", + " size=\"1024x1024\"\n", + " )\n", + " image_url = response.data[0].url\n", + " ```\n", + "\n", + " The base image would only contain the rabbit but the final image would have the hat on the rabbit.\n", + " \n", + "- **Create variations**. The idea is that you take an existing image and ask that variations are created. To create a variation, you provide an image and a text prompt and code like so:\n", + "\n", + " ```python\n", + " response = openai.images.create_variation(\n", + " image=open(\"bunny-lollipop.png\", \"rb\"),\n", + " n=1,\n", + " size=\"1024x1024\"\n", + " )\n", + " image_url = response.data[0].url\n", + " ```" + ] + } + ], + "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.9.6" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +}