use_cases/archive/email_response_generation/notebooks/email_response_generation.ipynb (334 lines of code) (raw):

{ "cells": [ { "cell_type": "markdown", "source": [ "# Azure OpenAI - Content generation\n", " \n", "Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.\n", "\n", "source: https://learnprompting.org/docs/basic_applications/writing_emails" ], "metadata": {}, "id": "011db2ec" }, { "cell_type": "code", "source": [ "import json\n", "import openai\n", "import os" ], "outputs": [], "execution_count": 1, "metadata": { "gather": { "logged": 1681842003771 }, "jupyter": { "source_hidden": false } }, "id": "a92744f4" }, { "cell_type": "markdown", "source": [ "### Setup Parameters\n", "\n", "\n", "Here we will load the configurations from _config.json_ file to setup deployment name, openai api base, openai api key and openai api version." ], "metadata": {}, "id": "e1966c51" }, { "cell_type": "code", "source": [ "# Setting up the deployment name\n", "deployment_name = \"text-davinci-003\"\n", "\n", "# This is set to `azure`\n", "openai.api_type = \"azure\"\n", "openai.api_version = \"2022-12-01\"\n", "\n", "# The API key for your Azure OpenAI resource.\n", "API_KEY = \"*********************\"\n", "assert API_KEY, \"ERROR: Azure OpenAI Key is missing\"\n", "openai.api_key = API_KEY\n", "\n", "RESOURCE_ENDPOINT = \"https://<ENDPOINT>.openai.azure.com/\"\n", "assert RESOURCE_ENDPOINT, \"ERROR: Azure OpenAI Endpoint is missing\"\n", "assert \"openai.azure.com\" in RESOURCE_ENDPOINT.lower(), \"ERROR: Azure OpenAI Endpoint should be in the form: \\n\\n\\t<your unique endpoint identifier>.openai.azure.com\"\n", "openai.api_base = RESOURCE_ENDPOINT" ], "outputs": [], "execution_count": 3, "metadata": { "gather": { "logged": 1681842042034 }, "jupyter": { "source_hidden": false } }, "id": "19ae1e36" }, { "cell_type": "code", "source": [ "def run_completion(prompt: str, deployment_name: str, temperaure=0.7, max_tokens=100, verbose=False):\n", " try:\n", " # Create a completion for the provided prompt and parameters\n", " # To know more about the parameters, checkout this documentation: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference\n", " completion = openai.Completion.create(\n", " prompt=prompt,\n", " temperature=temperaure,\n", " max_tokens=max_tokens,\n", " top_p=1,\n", " frequency_penalty=0,\n", " presence_penalty=0,\n", " engine=deployment_name)\n", "\n", " # print the completion\n", " if (verbose):\n", " print(completion.choices[0].text.strip(\" \\n\"))\n", " \n", " return completion.choices[0].text\n", " \n", " # Here indicating if the response is filtered\n", " if completion.choices[0].finish_reason == \"content_filter\":\n", " print(\"The generated content is filtered.\")\n", " \n", " except openai.error.APIError as e:\n", " # Handle API error here, e.g. retry or log\n", " print(f\"OpenAI API returned an API Error: {e}\")\n", "\n", " except openai.error.AuthenticationError as e:\n", " # Handle Authentication error here, e.g. invalid API key\n", " print(f\"OpenAI API returned an Authentication Error: {e}\")\n", "\n", " except openai.error.APIConnectionError as e:\n", " # Handle connection error here\n", " print(f\"Failed to connect to OpenAI API: {e}\")\n", "\n", " except openai.error.InvalidRequestError as e:\n", " # Handle connection error here\n", " print(f\"Invalid Request Error: {e}\")\n", "\n", " except openai.error.RateLimitError as e:\n", " # Handle rate limit error\n", " print(f\"OpenAI API request exceeded rate limit: {e}\")\n", "\n", " except openai.error.ServiceUnavailableError as e:\n", " # Handle Service Unavailable error\n", " print(f\"Service Unavailable: {e}\")\n", "\n", " except openai.error.Timeout as e:\n", " # Handle request timeout\n", " print(f\"Request timed out: {e}\")" ], "outputs": [], "execution_count": 4, "metadata": { "gather": { "logged": 1681842044295 }, "jupyter": { "source_hidden": false } }, "id": "b15862a1" }, { "cell_type": "code", "source": [ "def print_response(r):\r\n", " for x in r.split(\"\\n\"):\r\n", " print(x)\r\n" ], "outputs": [], "execution_count": 5, "metadata": { "jupyter": { "source_hidden": false, "outputs_hidden": false }, "nteract": { "transient": { "deleting": false } }, "gather": { "logged": 1681842045413 } }, "id": "3243c47f-4f4b-4840-938b-99f193088ca4" }, { "cell_type": "markdown", "source": [ "## Responding to an Email\r\n", "\r\n", "Two-way / two-hop approach:\r\n", "\r\n", "1. Generate summary and action points\r\n", "1. Generate reply based on the summary and action points" ], "metadata": { "nteract": { "transient": { "deleting": false } } }, "id": "72effb63-d4ae-4bda-aef6-1b4813f37058" }, { "cell_type": "code", "source": [ "prompt = \"\"\"\r\n", "Dear Mario,\r\n", "\r\n", "I hope you're doing well. I am writing to provide you with some updates on our company and to request your assistance in addressing an issue with the user interface (UI) of the software you have been working on.\r\n", "\r\n", "As you know, our company has been focusing on providing a user-friendly experience to our customers, and we have identified some issues with the UI of the software. Specifically, we have received feedback from our users that they are having difficulty accessing certain features, and that the UI is not intuitive enough.\r\n", "\r\n", "We need your help to address these issues and make improvements to the UI of the software. This may involve making changes to the layout, design, or functionality of the UI. We believe that your expertise in software development and your familiarity with the software will make you the ideal person to help us with this task.\r\n", "\r\n", "If you need any additional resources or support to complete this task, please do not hesitate to let me know. Additionally, I would appreciate it if you could provide me with an estimated timeline for when you expect these modifications to be completed.\r\n", "\r\n", "If you have any questions or need further clarification, please do not hesitate to contact me.\r\n", "\r\n", "Thank you for your hard work and dedication to our company. I look forward to hearing back from you soon.\r\n", "\r\n", "Best regards,\r\n", "John\r\n", "\r\n", "Generate a summary of this and a list of action items.\r\n", "\"\"\"\r\n", "r = run_completion(prompt=prompt, deployment_name=deployment_name, temperaure=1.0, max_tokens=500, verbose=False)\r\n", "print_response(r)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": "\nSummary: John has requested assistance from Mario with the user-interface (UI) of the software he has been working on. The company has received feedback from customers that they are having difficulty accessing certain features of the UI and that it is not intuitive enough. John needs assistance in making changes to the layout, design, or functionality of the UI in order to improve the customer experience.\n\nAction Items:\n- Make modifications to the UI of the software \n- Provide John with an estimated timeline for when these modifications will be complete\n- Let John know if any additional resources or support are necessary to complete the task\n- Contact John if any questions or clarification are needed\n" } ], "execution_count": 6, "metadata": { "jupyter": { "source_hidden": false, "outputs_hidden": false }, "nteract": { "transient": { "deleting": false } }, "gather": { "logged": 1681842052625 } }, "id": "ec717dfb-cb62-4596-87ac-62517057406a" }, { "cell_type": "code", "source": [ "prompt = \"\"\"\r\n", "Summary: John is writing to inform Mario of recent feedback the company has received regarding their user interface (UI) of the software. John is requesting Mario's help in addressing these issues and making improvements to the software's UI. \r\n", "\r\n", "Action Items:\r\n", "1. Assess the layout, design, and functionality of the UI and make necessary changes.\r\n", "2. Provide John with an estimated timeline for when these modifications are expected to be complete.\r\n", "3. Let John know if any additional resources or support are needed to complete the task.\r\n", "\r\n", "Write a response email from Mario using the above email summary:\r\n", "\"\"\"\r\n", "\r\n", "r = run_completion(prompt=prompt, deployment_name=deployment_name, temperaure=1.0, max_tokens=500, verbose=False)\r\n", "print_response(r)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": "\nDear John,\n\nThank you for bringing this feedback to my attention. I understand the importance of improving our software's user interface and am committed to resolving the issues as quickly as possible. \n\nI have already begun assessing the layout, design and functionality of the UI, and will make necessary changes as soon as I can. I will provide you with an estimated timeline for when I expect these modifications to be complete, and will keep you updated on my progress. \n\nIf I need any additional resources or support during this process, I will let you know. \n\nThank you for your prompt response to this issue. I look forward to continuing our work on the software's user interface.\n\nSincerely,\nMario\n" } ], "execution_count": 7, "metadata": { "jupyter": { "source_hidden": false, "outputs_hidden": false }, "nteract": { "transient": { "deleting": false } }, "gather": { "logged": 1681842062305 } }, "id": "75aa30a2-131d-4b92-80fe-40e09fe5ca54" }, { "cell_type": "code", "source": [], "outputs": [], "execution_count": null, "metadata": { "jupyter": { "source_hidden": false, "outputs_hidden": false }, "nteract": { "transient": { "deleting": false } } }, "id": "1e6a8e2a-ee24-4b13-9eb7-58a0f9bc3640" } ], "metadata": { "kernelspec": { "name": "python3", "language": "python", "display_name": "Python 3 (ipykernel)" }, "language_info": { "name": "python", "version": "3.8.5", "mimetype": "text/x-python", "codemirror_mode": { "name": "ipython", "version": 3 }, "pygments_lexer": "ipython3", "nbconvert_exporter": "python", "file_extension": ".py" }, "microsoft": { "ms_spell_check": { "ms_spell_check_language": "en" }, "host": { "AzureML": { "notebookHasBeenCompleted": true } } }, "kernel_info": { "name": "python3" }, "nteract": { "version": "nteract-front-end@1.0.0" } }, "nbformat": 4, "nbformat_minor": 5 }