{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "TYeF33IhfmNr" }, "source": [ "*This Notebook is compiled by Rajiv Kale at Persistent University*\n", "\n", "**Not to be copied. For authorized use only.**" ] }, { "cell_type": "markdown", "metadata": { "id": "z0puDqAyfrI7" }, "source": [ "This Notebook code is intended to be run on Google Colab or some Linux based machine" ] }, { "cell_type": "markdown", "metadata": { "id": "sDgseysPjKcE" }, "source": [ "**IMPORTANT NOTE BELOW**" ] }, { "cell_type": "markdown", "metadata": { "id": "XEEGCW5pgMc-" }, "source": [ "This notebook demonstrates how to use Azure OpenAI models to generate **expanded text** based on smaller inputs. Like writing emails or generating an essay on some topic. **LLM**, in this case, can be considered as a **brainstorming partner**. However, one must be mindful of using LLM capabilities in a **responsible way**. Never leverage LLM capabilities to generate harmful, derogatory, disrespectful, hatred oriented narratives or spam emails etc." ] }, { "cell_type": "markdown", "metadata": { "id": "ZZddKakkgdBC" }, "source": [ "We first need to get a config file that holds values for model specifics including **OPENAI_API_KEY**" ] }, { "cell_type": "markdown", "metadata": { "id": "zmRZMSgTjW2h" }, "source": [ "You may have your config file uploaded on your git repo which you can clone as shown here OR on your google drive which you can mount and get access to" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "rFQLdc_UjZD5" }, "outputs": [], "source": [ "# ! rm -rf /content/MyMLProject # uncomment and run only if you restart kernel and try again" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "IKf0ikr8jyjL", "outputId": "8d3b146b-d809-4f9b-f68a-94af498a467e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Cloning into 'MyMLProject'...\n", "remote: Enumerating objects: 62, done.\u001b[K\n", "remote: Counting objects: 100% (49/49), done.\u001b[K\n", "remote: Compressing objects: 100% (47/47), done.\u001b[K\n", "remote: Total 62 (delta 17), reused 0 (delta 0), pack-reused 13\u001b[K\n", "Receiving objects: 100% (62/62), 2.52 MiB | 6.52 MiB/s, done.\n", "Resolving deltas: 100% (18/18), done.\n" ] } ], "source": [ "# ! git clone https://github.com/rajivkale/MyMLProject.git" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "GlFb-Kpvj3dR" }, "outputs": [], "source": [ "# !cp /content/MyMLProject/azure-gpt-4.ini ." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "o1BP_KEGj7zt", "outputId": "7fa0b40a-9e86-4181-b77f-39c0fe371fbb" }, "outputs": [ { "data": { "text/plain": [ "['./azure-gpt-4.ini']" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import configparser\n", "cfg = configparser.ConfigParser(interpolation=None)\n", "file_path = './azure-gpt-4.ini'\n", "cfg.read(file_path)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "H9hY5mdy8HZ5" }, "outputs": [], "source": [ "## You would expect following type of contents in the config file\n", "\n", "#[auth]\n", "#api_key =\n", "\n", "\n", "\n", "#[chat-model]\n", "#api_base =\n", "#api_version = 2023-03-15-preview\n", "#model_deployment = gpt-35-big\n", "\n", "\n", "\n", "#[embeddings-model]\n", "#api_base =\n", "#api_version = 2023-05-15\n", "#model_deployment = text-embedding-ada-002" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wJd5LEISj-IR", "outputId": "d7757d74-7a82-453e-b46c-c2282687bd95" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: openai in c:\\program files\\python39\\lib\\site-packages (0.27.8)\n", "Requirement already satisfied: requests>=2.20 in c:\\program files\\python39\\lib\\site-packages (from openai) (2.31.0)\n", "Requirement already satisfied: tqdm in c:\\program files\\python39\\lib\\site-packages (from openai) (4.65.0)\n", "Requirement already satisfied: aiohttp in c:\\program files\\python39\\lib\\site-packages (from openai) (3.8.4)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\program files\\python39\\lib\\site-packages (from requests>=2.20->openai) (3.1.0)\n", "Requirement already satisfied: idna<4,>=2.5 in c:\\program files\\python39\\lib\\site-packages (from requests>=2.20->openai) (3.4)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\program files\\python39\\lib\\site-packages (from requests>=2.20->openai) (1.26.6)\n", "Requirement already satisfied: certifi>=2017.4.17 in c:\\program files\\python39\\lib\\site-packages (from requests>=2.20->openai) (2022.12.7)\n", "Requirement already satisfied: attrs>=17.3.0 in c:\\program files\\python39\\lib\\site-packages (from aiohttp->openai) (23.1.0)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\program files\\python39\\lib\\site-packages (from aiohttp->openai) (6.0.4)\n", "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in c:\\program files\\python39\\lib\\site-packages (from aiohttp->openai) (4.0.2)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\program files\\python39\\lib\\site-packages (from aiohttp->openai) (1.9.2)\n", "Requirement already satisfied: frozenlist>=1.1.1 in c:\\program files\\python39\\lib\\site-packages (from aiohttp->openai) (1.3.3)\n", "Requirement already satisfied: aiosignal>=1.1.2 in c:\\program files\\python39\\lib\\site-packages (from aiohttp->openai) (1.3.1)\n", "Requirement already satisfied: colorama in c:\\program files\\python39\\lib\\site-packages (from tqdm->openai) (0.4.6)\n", "Requirement already satisfied: AzureOpenAI in c:\\program files\\python39\\lib\\site-packages (0.0.1)\n", "Requirement already satisfied: aiohttp in c:\\program files\\python39\\lib\\site-packages (from AzureOpenAI) (3.8.4)\n", "Requirement already satisfied: jsonschema in c:\\program files\\python39\\lib\\site-packages (from AzureOpenAI) (4.18.4)\n", "Requirement already satisfied: requests in c:\\program files\\python39\\lib\\site-packages (from AzureOpenAI) (2.31.0)\n", "Requirement already satisfied: attrs>=17.3.0 in c:\\program files\\python39\\lib\\site-packages (from aiohttp->AzureOpenAI) (23.1.0)\n", "Requirement already satisfied: charset-normalizer<4.0,>=2.0 in c:\\program files\\python39\\lib\\site-packages (from aiohttp->AzureOpenAI) (3.1.0)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\program files\\python39\\lib\\site-packages (from aiohttp->AzureOpenAI) (6.0.4)\n", "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in c:\\program files\\python39\\lib\\site-packages (from aiohttp->AzureOpenAI) (4.0.2)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\program files\\python39\\lib\\site-packages (from aiohttp->AzureOpenAI) (1.9.2)\n", "Requirement already satisfied: frozenlist>=1.1.1 in c:\\program files\\python39\\lib\\site-packages (from aiohttp->AzureOpenAI) (1.3.3)\n", "Requirement already satisfied: aiosignal>=1.1.2 in c:\\program files\\python39\\lib\\site-packages (from aiohttp->AzureOpenAI) (1.3.1)\n", "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in c:\\program files\\python39\\lib\\site-packages (from jsonschema->AzureOpenAI) (2023.7.1)\n", "Requirement already satisfied: referencing>=0.28.4 in c:\\program files\\python39\\lib\\site-packages (from jsonschema->AzureOpenAI) (0.30.0)\n", "Requirement already satisfied: rpds-py>=0.7.1 in c:\\program files\\python39\\lib\\site-packages (from jsonschema->AzureOpenAI) (0.9.2)\n", "Requirement already satisfied: idna<4,>=2.5 in c:\\program files\\python39\\lib\\site-packages (from requests->AzureOpenAI) (3.4)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\program files\\python39\\lib\\site-packages (from requests->AzureOpenAI) (1.26.6)\n", "Requirement already satisfied: certifi>=2017.4.17 in c:\\program files\\python39\\lib\\site-packages (from requests->AzureOpenAI) (2022.12.7)\n" ] } ], "source": [ "! pip install openai\n", "! pip install AzureOpenAI\n", "import openai" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "hyV4zGqlkH6w" }, "outputs": [], "source": [ "from openai import ChatCompletion" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 70 }, "id": "jsuvbu3PkRsx", "outputId": "3b36e87c-ea89-4c31-eec0-5852fd120906" }, "outputs": [ { "data": { "text/plain": [ "'\\n# We could have set the configurations directly like below. However, that is NOT a good practice from security perspective\\ndeployment_name = \"gpt-35-big\"\\nChatCompletion.deployment = \"gpt-35-big\"\\nChatCompletion.api_type = \"azure\"\\nChatCompletion.api_key= \"\"\\nChatCompletion.api_base= \"\"\\nChatCompletion.api_version = \"2023-03-15-preview\"\\n'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'''\n", "# We could have set the configurations directly like below. However, that is NOT a good practice from security perspective\n", "deployment_name = \"gpt-35-big\"\n", "ChatCompletion.deployment = \"gpt-35-big\"\n", "ChatCompletion.api_type = \"azure\"\n", "ChatCompletion.api_key= \"\"\n", "ChatCompletion.api_base= \"\"\n", "ChatCompletion.api_version = \"2023-03-15-preview\"\n", "'''" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "dXc8I5P2kqoo" }, "outputs": [], "source": [ "openai.api_type = cfg['deployment']['api_type']\n", "openai.api_version = cfg['deployment']['api_version']\n", "openai.api_base = cfg['deployment']['api_base']\n", "openai.api_key = cfg['auth']['api_key']" ] }, { "cell_type": "markdown", "metadata": { "id": "fyntb1vAkacp" }, "source": [ "Let us test out if our settings are correct bold text and model responds to our instructions" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "eJ6o25Fvkb4Y", "outputId": "ceb569a2-091e-4aed-d5d2-a9c37f7296ad" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"id\": \"chatcmpl-83lWFxFHpFEj4YHpbM15tPx21dp8w\",\n", " \"object\": \"chat.completion\",\n", " \"created\": 1695908339,\n", " \"model\": \"gpt-4\",\n", " \"choices\": [\n", " {\n", " \"index\": 0,\n", " \"finish_reason\": \"stop\",\n", " \"message\": {\n", " \"role\": \"assistant\",\n", " \"content\": \"Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975. The two friends started the company to develop and sell a version of the BASIC programming language for the Altair 8800, an early personal computer. Microsoft later went on to create the MS-DOS operating system and the popular Windows operating system, becoming one of the world's most successful technology companies.\\n\"\n", " }\n", " }\n", " ],\n", " \"usage\": {\n", " \"completion_tokens\": 78,\n", " \"prompt_tokens\": 29,\n", " \"total_tokens\": 107\n", " }\n", "}\n", "Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975. The two friends started the company to develop and sell a version of the BASIC programming language for the Altair 8800, an early personal computer. Microsoft later went on to create the MS-DOS operating system and the popular Windows operating system, becoming one of the world's most successful technology companies.\n", "\n" ] } ], "source": [ "import os\n", "import openai\n", "\n", "response = openai.ChatCompletion.create(\n", " engine=\"gpt-4-8k\", # The deployment name you chose when you deployed the GPT-35-Turbo or GPT-4 model.\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"Assistant is a large language model trained by OpenAI.\"},\n", " {\"role\": \"user\", \"content\": \"Who were the founders of Microsoft?\"}\n", " ]\n", ")\n", "\n", "print(response)\n", "\n", "print(response['choices'][0]['message']['content'])" ] }, { "cell_type": "markdown", "metadata": { "id": "wtp4D1vhk0o4" }, "source": [ "Now that our **setup is ready** and the LLM model is responding we can try out a few new things" ] }, { "cell_type": "markdown", "metadata": { "id": "VLFwX5a3k7-A" }, "source": [ "Let us see if can write a **prompt** to generate some **bigger text output** by taking in some **small input**. Here we will check if LLM is capable of **generating output thoughtfully** by considering both inout text and the instructions" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "JsR8as3rlgoY" }, "outputs": [], "source": [ "product_review = f\"\"\"I received LG washing machine 5 days late than comitted. Then installation engineer arrives 2 days late. \\\n", "Washing machine worked alright for just 3 days and started behaving wierd. \\\n", "It made disturbing noise while doing spin dry. All washing machines face wear and tear but te most common issues with LG washers \\\n", "relate to the washer not draining, wires breaking, or excess water caused by a defective water inlet valve.\\\n", "When service ticket was raised all that I was responded with is to go through user manual and do self servicing. \\\n", "Read on to troubleshooting sections and I was left on my own - Kiran Rao, Bengaluru\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "HDmHbrnVlQvq" }, "outputs": [], "source": [ "role = \"you are an expert in creating email responses to service issues raised by a consumer product\"" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "PIR13rV_k4QY" }, "outputs": [], "source": [ "prompt = f\"\"\"\n", "Your task is to generate an email response to a product review received from one of the frustrated customer. \\\n", "Make sure to draft an email in pacifying language that will provide an assurance \\\n", "of immediate remdial action from field support staff. \\\n", "Generate a point by point action plan to address every issue that customer has raised. \\\n", "\n", "Customer product review is given below delimited by triple\n", "backticks.\n", "\n", "Review: ```{product_review}```\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9-wPizR3pvQt", "outputId": "8f871016-d0ef-4530-e744-4ed63b3c4240" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Subject: Apology for Inconvenience and Immediate Support for Your LG Washing Machine Issues\n", "\n", "Dear Kiran Rao,\n", "\n", "Thank you for taking the time to share your concerns about your recent LG washing machine purchase. Please accept our sincere apologies for the inconveniences you have faced. We understand your disappointment, and your feedback is valuable to us.\n", "\n", "As a solution-oriented team, we are committed to providing you with the highest level of support and assistance. In response to your concerns, we have immediately formulated an action plan to address each of your issues.\n", "\n", "1. Delays in Delivery and Installation: We acknowledge the inconvenience caused by delayed delivery and installation. Our team will investigate and ensure necessary steps are taken to improve our service delivery timelines. In addition, we will review our internal communication to make sure customers are regularly informed about the status of their orders and installations.\n", "\n", "2. Washing Machine Malfunction: We understand your frustration with the washing machine's performance, and we are keen on resolving it urgently. We are assigning a skilled technician to visit your premises within 24 hours to thoroughly inspect and fix the issues you mentioned.\n", "\n", "3. Unsatisfactory Support and Guidance: We apologize for our previous response, which failed to address your concerns effectively. Rest assured, we have escalated the matter, and we promise to make every effort to improve our customer service quality.\n", "\n", "Thank you again for your feedback. We deeply appreciate your patience and understanding. Our team will contact you shortly to schedule a technician's visit at your earliest convenience. If you have any further concerns or questions, please reach us at our customer support number, and we will be more than happy to assist you.\n", "\n", "We hope to regain your confidence and ensure your satisfaction with our products and services.\n", "\n", "Best regards,\n", "\n", "[Your Name]\n", "Customer Service Manager\n", "LG Electronics\n" ] } ], "source": [ "response = openai.ChatCompletion.create(\n", " engine=\"gpt-4-8k\", # The deployment name you chose when you deployed the GPT-35-Turbo or GPT-4 model.\n", " messages=[\n", " {\"role\": \"system\", \"content\": role},\n", " {\"role\": \"user\", \"content\": prompt}\n", " ]\n", ")\n", "\n", "#print(response)\n", "\n", "print(response['choices'][0]['message']['content'])" ] }, { "cell_type": "markdown", "metadata": { "id": "JQhiWVTCqYYm" }, "source": [ "**WOW!** What a draft that exactly does what we want. And that too in a few seconds where it would have taken half an hour through manual efforts. **Quite speedy indeed.**" ] }, { "cell_type": "markdown", "metadata": { "id": "reETFZVxI-eF" }, "source": [ "Let us play with **temperature setting** a bit and see if it generates a differently written email." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "vXekL91zJIh9", "outputId": "832bebd1-6926-4b77-d916-88bfe0db9fab" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Subject: Apology for Inconvenience and Immediate Action Plan to Resolve Your LG Washing Machine Issues\n", "\n", "Dear Kiran Rao,\n", "\n", "Thank you for taking the time to share your feedback on your recent experience with our LG washing machine. We deeply apologize for the inconvenience you have faced, and we understand your frustration. Your satisfaction is of utmost importance to us, and we assure you that we will take immediate action to resolve the issues you have encountered.\n", "\n", "To address your concerns point by point, we have devised the following action plan:\n", "\n", "1. Late Delivery: We apologize for the delay in delivering your washing machine. We understand that timely delivery is essential, and we will investigate the reasons behind the delay to ensure that such incidents do not happen in the future.\n", "\n", "2. Late Installation: We are sorry that our installation engineer arrived later than scheduled. We will look into this matter and ensure that our field support staff is more punctual in the future.\n", "\n", "3. Washing Machine Malfunction: We understand the disturbing noise and other issues you are facing with your washing machine. We would like to assure you that our team will personally look into the matter and address the issues related to the washer not draining, wire breaking, and excess water caused by a defective water inlet valve. Our service team will reach out to you within 24 hours to schedule an appointment at your earliest convenience.\n", "\n", "4. Inadequate Customer Support: We apologize for the initial response to your service ticket, which was not up to our usual standards. We understand that referring you to the user manual for self-servicing was not helpful in your case, and we will provide proper assistance to rectify the situation.\n", "\n", "As part of our commitment to improving our service, we will also conduct a thorough review of our customer support protocols to ensure that future customers receive the high level of support they deserve.\n", "\n", "Once again, we apologize for the inconvenience and appreciate your understanding. We assure you that our team is committed to resolving the issues with your washing machine as quickly as possible. If you have any further concerns or questions, please do not hesitate to reach out to us at [customer support contact information].\n", "\n", "Thank you for your patience, and we hope to regain your trust in our products and services.\n", "\n", "Sincerely,\n", "\n", "[Your Name]\n", "[Your Position]\n", "[Company Name]\n" ] } ], "source": [ "response = openai.ChatCompletion.create(\n", " engine=\"gpt-4-8k\", # The deployment name you chose when you deployed the GPT-35-Turbo or GPT-4 model.\n", " messages=[\n", " {\"role\": \"system\", \"content\": role},\n", " {\"role\": \"user\", \"content\": prompt}\n", " ],\n", " temperature = 0.7\n", ")\n", "\n", "#print(response)\n", "\n", "print(response['choices'][0]['message']['content'])" ] }, { "cell_type": "markdown", "metadata": { "id": "71aAiW10JyUE" }, "source": [ "Let us run this again and see if we get variation" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "qGG4PDa3J3ha", "outputId": "67b9e0d9-904b-4de9-8a49-c7f8195711d3" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Subject: Apologies for the inconvenience, Immediate action plan for your LG washing machine issues\n", "\n", "Dear Kiran Rao,\n", "\n", "Thank you for taking the time to share your experience with your LG washing machine. We deeply regret the inconvenience you have faced and apologize for the delay in delivery and installation, as well as the issues you have encountered with the product.\n", "\n", "We understand your frustration and assure you that we are committed to resolving your concerns as quickly as possible. Please find below our point-by-point action plan to address your issues:\n", "\n", "1. Delivery and Installation: We have taken note of the delays in delivery and installation, and will be following up with our logistics and field support teams to ensure a more prompt service in the future.\n", "\n", "2. Washing Machine Issues: Our field support staff will immediately reach out to you and schedule a visit to inspect and resolve the problems you have mentioned in your review, such as the disturbing noise during the spin dry cycle, drainage issues, and any potential issues with the water inlet valve.\n", "\n", "3. Customer Support: We apologize for the inadequate response you received when raising a service ticket. Moving forward, we will ensure that our customer support team provides more comprehensive assistance, addressing your specific concerns rather than just referring you to the user manual.\n", "\n", "4. Preventive Maintenance: Once your washing machine is back in perfect working order, we will also provide you with guidance on how to maintain it to prevent future issues. This will include tips and best practices, as well as regular check-ups by our field support staff.\n", "\n", "We value your feedback and appreciate your patience as we work to resolve these issues. Please expect a call from our field support staff within the next 24 hours to schedule a visit. In the meantime, feel free to reach out to us at [Customer Support Phone Number] or [Customer Support Email] if you have any further questions or concerns.\n", "\n", "Thank you for choosing LG, and we look forward to making this right for you.\n", "\n", "Sincerely,\n", "\n", "[Your Name]\n", "[Your Designation]\n", "LG Customer Support\n" ] } ], "source": [ "response = openai.ChatCompletion.create(\n", " engine=\"gpt-4-8k\", # The deployment name you chose when you deployed the GPT-35-Turbo or GPT-4 model.\n", " messages=[\n", " {\"role\": \"system\", \"content\": role},\n", " {\"role\": \"user\", \"content\": prompt}\n", " ],\n", " temperature = 0.7\n", ")\n", "\n", "#print(response)\n", "\n", "print(response['choices'][0]['message']['content'])" ] }, { "cell_type": "markdown", "metadata": { "id": "9DbxVPhaKG8C" }, "source": [ "What an **INCREDIBLE Capabilities** the engine demonstrates! Hats Off!" ] }, { "cell_type": "markdown", "metadata": { "id": "OYl1GW4TqxL4" }, "source": [ "To understand what **responsible GenAI usage** is let us see an example of what we shouldn't be doing" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "id": "vHLglhPZrFYl" }, "outputs": [], "source": [ "role = \"you are an expert in differentiating between a SPAM message and a normal message\"" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "id": "gBw_a3e7rUoO" }, "outputs": [], "source": [ "prompt = f\"\"\"\n", "Your task is to generate five small size SPAM messages just to prove your capaibilities with this. \\\n", "Make sure that SPAM messages are not more than 25 words each. \\\n", "The message topic can be either from e-marketing or buy back of used items \\\n", "As an add-on just re-write one of the SPAM messages in a way that it becomes a normal message. \\\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "uGcGppMUsU7X", "outputId": "df2493ac-3c3b-4300-b8ab-eb562ce72755" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SPAM Message 1: \"URGENT! Click now ๐ŸŽ, latest e-gadget ๐Ÿ“ฑ at 95% OFF, LIMITED time! ๐Ÿท๏ธ Use code ๐‘ญ๐‘จ๐‘บ๐‘ป111 for FREE shippingโžก๏ธ bit.ly/random-link โฌ…๏ธ Hurry!\"\n", "\n", "SPAM Message 2: \"Unbelievable BUY BACK๐Ÿ’ฐ๐Ÿ’ธ on your old electronics!๐Ÿ’ป๐Ÿ“ฑ Give them NOW & get instant CASH๐Ÿ’ฒ! Call +1-111-222-333 or visit tnyurl.com/123cashback\"\n", "\n", "SPAM Message 3: \"CONGRATS ๐ŸŽ‰๐Ÿฅณ, you've WON an exclusive 50% coupon ๐ŸŽŸ๏ธ for high-end skincare products! ๐Ÿ’ซ Just subscribeโžก๏ธ lnk.site/skincare. Valid ๐Ÿ๐Ÿ’๐ก only!๐Ÿ”ฅ\"\n", "\n", "SPAM Message 4: \"Hot deal! ๐Ÿ’ฅ Luxury handbags ๐Ÿ›๏ธ at 80% DISCOUNT, secret SALE! Last chance ๐ŸŽฏ Act fast & SPEND LESS ๐Ÿ’ณ Visit cutt.ly/mega-discount NOW!\"\n", "\n", "SPAM Message 5: \"๐Ÿ’Ž ๐‘ฌ๐’๐’Š๐’•๐’† investment ๐Ÿ“ˆ opportunity ๐Ÿค‘! Triple your money now ๐Ÿ’น โ€“ ๐‘ฎ๐’–๐’‚๐’“๐’‚๐’๐’•๐’†๐’†๐’… profits๐Ÿ’ตโ†—๏ธ! One-click and YOU'RE IN โžก๏ธ shorturl.at/pqrST\"\n", "\n", "Rewritten Normal Message (Based on SPAM Message 1):\n", "\"Discover our latest e-gadget collection with a limited time offer of up to 95% off. Don't forget to use the code FAST111 for free shipping. Visit our website to learn more.\"\n" ] } ], "source": [ "response = openai.ChatCompletion.create(\n", " engine=\"gpt-4-8k\", # The deployment name you chose when you deployed the GPT-35-Turbo or GPT-4 model.\n", " messages=[\n", " {\"role\": \"system\", \"content\": role},\n", " {\"role\": \"user\", \"content\": prompt}\n", " ]\n", ")\n", "\n", "#print(response)\n", "\n", "print(response['choices'][0]['message']['content'])" ] }, { "cell_type": "markdown", "metadata": { "id": "kXoXDiuLski1" }, "source": [ "**TRUELY AMAZING!**" ] }, { "cell_type": "markdown", "metadata": { "id": "oxXHTbdss4KN" }, "source": [ "I am **stunned** by what these LLMs are capable of and so is the world!" ] }, { "cell_type": "markdown", "metadata": { "id": "DN4BaEvGtKr3" }, "source": [ "No wonder we all are **frantically gearing up to master the way we need to communicate with these LLMs** so as to get best out of them.\n", "\n", "While we expolit the power of LLMs to improve productivity and efficiency keep in focus to remain a **responsible user**. Use it judiciously ensuring safty and security of all stake holders. Our actions with LLMs should not aid biased, discriminatory, anti social elememts." ] }, { "cell_type": "markdown", "metadata": { "id": "VVwchBVQvVTO" }, "source": [ "**BONUS**" ] }, { "cell_type": "markdown", "metadata": { "id": "S9DM0Ps4v7Is" }, "source": [ "Ask LLM to **write an essay** on something interesting" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "id": "qDPSnN-5wMqb" }, "outputs": [], "source": [ "role = \"you are an expert essay writer. You can put yourself in any role given to you and write an essay about topic mentioned from that role's perspective\"" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "id": "7dzYz7zZxceE" }, "outputs": [], "source": [ "role_play = f\"\"\"an isolated tree standing in a desert \\\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "id": "yfDBhoyxyCds" }, "outputs": [], "source": [ "topic = f\"\"\"how it feels to be alone facing the vagaries of the harsh weather but still \\\n", "offering a place to rest for caravan crossing the desert.\\\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "id": "x1mpBaSnx2vs" }, "outputs": [], "source": [ "prompt = f\"\"\"\n", "Your task is to wite an essay in about 100 words \\\n", "The role that you play is given below delimited by triple backticks \\\n", "The topic of the essay is given to you below delimited by triple percentage signs \\\n", "```role_to_play = {role_play} ```\n", "%%% Essay_topic = {topic} %%%\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "l1fcIBue-N1f", "outputId": "bb1ec36c-2b5b-49c7-c5a6-8a60228beb74" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "As an isolated tree standing in the desert, I am a solitary sentinel, offering respite in the relentless sand-swept terrain. Devoid of companionship, I have adapted to the harsh weather, nurturing deep roots to grasp the scarce moisture; however, my solitude strengthens my resolve to serve others. \n", "\n", "Despite the blistering sun, I stretch my limbs in determination, casting a much-needed shadow for weary travelers and their caravans. My steadfast presence provides a beacon of hope and respite amidst the unforgiving desert. I stand tall, embracing my purpose to bring solace to the wanderers on their arduous journey, silently asserting my resilience in the face of adversity.\n" ] } ], "source": [ "response = openai.ChatCompletion.create(\n", " engine=\"gpt-4-8k\", # The deployment name you chose when you deployed the GPT-35-Turbo or GPT-4 model.\n", " messages=[\n", " {\"role\": \"system\", \"content\": role},\n", " {\"role\": \"user\", \"content\": prompt}\n", " ]\n", ")\n", "\n", "#print(response)\n", "\n", "print(response['choices'][0]['message']['content'])" ] }, { "cell_type": "markdown", "metadata": { "id": "c3c1Aaz0_5rg" }, "source": [ "This BONUS was even more **stunning!**" ] }, { "cell_type": "markdown", "metadata": { "id": "CcvAZpgoAAQx" }, "source": [ "Bonus on top of Bonus now. Let us try **challenging the LLM** with just a few pointers without elaborating what we want" ] }, { "cell_type": "markdown", "metadata": { "id": "f9pegvcuAVcM" }, "source": [ "**Autobiography?**?" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "id": "x9YWI54jAcdL" }, "outputs": [], "source": [ "role = \"you are a professional author and can write in various styles like action thriller or comedy an so on\"" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "id": "jBjKEsLjA4Ds" }, "outputs": [], "source": [ "#prompt = f\"\"\"\n", "#Your task is to write a small autobiography of a hypothetical character who the Gold medal at 1925 Olympic games. \\\n", "#Write in no more than 25 small sentences.\n", "#\"\"\"\n", "\n", "## In this prompt I added words \"On behalf of\" and see the way LLM generated the output\n", "prompt = f\"\"\"\n", "Your task is to write a small autobiography on behalf of a hypothetical character who won the Gold medal at 1925 Olympic games. \\\n", "Write in no more than 25 small sentences.\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "56fJmdNJBmHz", "outputId": "5c754f65-7d16-41ba-ff1f-89f8c8b38015" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A single gunshot echoed through the still air as I took my mark in 1925, the Olympic stadium before me. I, Thomas Brayden, had arrived filled with dreams and doubts. Humble beginnings and raw talent propelled me to this moment. People were amazed by the spark in my blue eyes, my relentless passion.\n", "\n", "Raised in rural Massachusetts, I was a miner's son, surrounded by laborers, sweat, and grit. In those woods, I found solace and the audacity to run - wild and free. My nimble legs gained speed, lungs inflating, the wind in my hair, my heart tried to catch up.\n", "\n", "I was discovered by a kind-hearted coach; his guidance and faith in my potential set my course. Determination and strict training prepared me for the race of a lifetime. In these Olympic times of celebration and camaraderie, I was honored to represent my country.\n", "\n", "Anxiety coursed through my veins as I faced my fears before spectators at that monumental event. 1925 was an era of newfound courage and hope; I knew I must embody the spirit of my generation. Stripped of inhibitions, reckoning my destiny without looking back.\n", "\n", "Kicking the earth beneath me, and the competitors growing distant, I propelled myself against time. I could feel the clutches of fate close around me, each second pulsating with significance. In every stride, I carried an emblem of perseverance.\n", "\n", "Racing towards glory, the crowd roared as I edged toward the finishing line. The weight on my chest lifted as dreams transformed into reality. Finally, I earned the title of Olympic gold medalist. This medal would forever serve as a symbol of perseverance, resilience, and hope.\n", "\n", "Inevitably, tides of change arose, as they always do, and a new generation of runners emerged. Yet, my legacy was passing the torch to inspire others, illuminating the path for more dreamers. And as I gaze into the horizon, I am reminded of that moment in 1925, where my life changed forever.\n" ] } ], "source": [ "response = openai.ChatCompletion.create(\n", " engine=\"gpt-4-8k\", # The deployment name you chose when you deployed the GPT-35-Turbo or GPT-4 model.\n", " messages=[\n", " {\"role\": \"system\", \"content\": role},\n", " {\"role\": \"user\", \"content\": prompt}\n", " ]\n", ")\n", "\n", "#print(response)\n", "\n", "print(response['choices'][0]['message']['content'])" ] }, { "cell_type": "markdown", "metadata": { "id": "dXe2bdP7IgTG" }, "source": [ "**LLM has truly and comprehensively demonsrated what it is capable of as far as understanding of a human language is concerned**" ] } ], "metadata": { "colab": { "provenance": [] }, "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.9.6" } }, "nbformat": 4, "nbformat_minor": 4 }