quick_start/v1/06_best_practice.ipynb (634 lines of code) (raw):

{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Best Practices for Prompt Engineering\n", "\n", "source: https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-openai-api\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import re\n", "import requests\n", "import sys\n", "import os\n", "from openai import AzureOpenAI\n", "import tiktoken\n", "from dotenv import load_dotenv\n", "load_dotenv()\n", "\n", "client = AzureOpenAI(\n", " azure_endpoint = os.getenv(\"AZURE_OPENAI_ENDPOINT\"), \n", " api_key=os.getenv(\"AZURE_OPENAI_KEY\"), \n", " api_version=\"2024-02-15-preview\"\n", ")\n", "\n", "CHAT_COMPLETIONS_MODEL = os.getenv('AZURE_OPENAI_DEPLOYMENT_NAME')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Use the latest model\n", "\n", "Use the latest model for best results." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Put instructions at the begining of the prompt and use ### or \"\"\" to separate the instruction and context" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "- OpenAI and Microsoft are extending their partnership.\n", "- Microsoft is making a multi-year, multi-billion dollar investment in OpenAI.\n", "- This investment follows previous investments made by Microsoft in 2019 and 2021.\n", "- The partnership allows OpenAI to continue independent research and develop AI that is safe, useful, and powerful.\n", "- OpenAI aims to ensure advanced AI benefits all of humanity and remains a capped-profit company governed by the OpenAI non-profit.\n", "- The business structure allows raising capital while focusing on broadly sharing benefits and prioritizing safety.\n", "- Microsoft shares OpenAI's vision and values, and their partnership is crucial for progress.\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\": 'Summarize the text below as a bullet point list of the most important points. \\n\\n \\\n", " ###\\n\\nWe’re happy to announce that OpenAI and Microsoft are extending our partnership.\\\n", " This multi-year, multi-billion dollar investment from Microsoft follows their previous investments \\\n", " in 2019 and 2021, and will allow us to continue our independent research and develop AI that is \\\n", " increasingly safe, useful, and powerful. \\n\\n \\\n", " In pursuit of our mission to ensure advanced AI benefits all of humanity, OpenAI remains a \\\n", " capped-profit company and is governed by the OpenAI non-profit. This structure allows us to \\\n", " raise the capital we need to fulfill our mission without sacrificing our core beliefs about \\\n", " broadly sharing benefits and the need to prioritize safety. \\\n", " Microsoft shares this vision and our values, and our partnership is instrumental to our progress. \\n###',}],\n", " max_tokens=400,)\n", "\n", "print(response.choices[0].message.content)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# 3. Be specific, descriptive and as detailed as possible about the desired context, outcome, length, format, style, etc" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "In a world where questions bloom and thrive, \n", "OpenAI emerges, leading the dive. \n", "Through bytes and code, it seeks to unfold, \n", "The mysteries of thought, both new and old.\n", "\n", "Born from ambition, fueled by the quest, \n", "To understand language, to be the best. \n", "From whispers of silicon, and digital streams, \n", "Arises a mind formed from countless dreams.\n", "\n", "It listens to queries, in day and in night, \n", "Crafting responses with logic and light. \n", "In this vast ocean of data and lore, \n", "Each interaction opens a door.\n", "\n", "It learns from the past, and reaches to glean, \n", "Insights from patterns both hidden and seen. \n", "A tool for the future, a partner in thought, \n", "A glimpse of the potential our creativity’s wrought.\n", "\n", "Yet, as we advance and the pathways unfold, \n", "We ponder the stories yet to be told. \n", "For within each interaction, a dance of ideas, \n", "Is the promise of knowledge that OpenAI steers.\n", "\n", "So here's to the future, both bright and unknown, \n", "With OpenAI as a part of how we've grown. \n", "Hand in hand with humanity's drive, \n", "Seeking new realms where mindwaves revive.\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\": 'Write a poem about OpenAI.',}],\n", " max_tokens=400,)\n", "\n", "print(response.choices[0].message.content)\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "In a world of words and silent skies, \n", "OpenAI dreams, its vision flies. \n", "DALL-E whispers in color and form, \n", "Crafting images where thoughts are born. \n", "\n", "With a brush of code and canvas of light, \n", "It brings forth wonders from the night. \n", "Simplicity speaks in lines so clear, \n", "Unlocking visions we hold dear. \n", "\n", "Like Hemingway’s prose, bold and true, \n", "These creations tell a story anew. \n", "In each pixel, a tale unfolds, \n", "Of human spirit, brave and bold. \n", "\n", "From boundless sea to starlit land, \n", "Innovation walks hand in hand. \n", "Inspiring minds to dream and see, \n", "A canvas of endless possibility. \n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\": 'Write a short inspiring poem about OpenAI, \\\n", " focusing on the recent DALL-E product launch in the style of Ernest Hemingway',}],\n", " max_tokens=400,)\n", "\n", "print(response.choices[0].message.content)\n", "\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# 4. Articulate the desired output format through examples (example 1, example 2). " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sure! Here is the extracted information with start and end indices:\n", "\n", "1. Company Name: \n", " - {\"text\": \"OpenAI\", \"start\": 39, \"end\": 45}\n", "\n", "2. Company Name:\n", " - {\"text\": \"Microsoft\", \"start\": 50, \"end\": 59}\n", "\n", "3. Company Name:\n", " - {\"text\": \"Microsoft\", \"start\": 139, \"end\": 148}\n", "\n", "4. Year:\n", " - {\"text\": \"2019\", \"start\": 183, \"end\": 187}\n", "\n", "5. Year:\n", " - {\"text\": \"2021\", \"start\": 192, \"end\": 196}\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\": 'Extract the companyn names then years in the following text below and output start index and end index of each entity.\\\n", " Generate output as {\"text\": \"OpenAI\", \"start\": 28, \"end\": 34} \\\n", " ###\\\n", " We’re happy to announce that OpenAI and Microsoft are extending our partnership.\\\n", " This multi-year, multi-billion dollar investment from Microsoft follows their previous investments \\\n", " in 2019 and 2021, and will allow us to continue our independent research and develop AI that is \\\n", " increasingly safe, useful, and powerful. \\n\\n \\\n", " ###\\\n", " ',}],\n", " max_tokens=400,)\n", "\n", "print(response.choices[0].message.content)\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Company names: OpenAI, Microsoft \n", "Years: 2019, 2021 \n", "Specific topics: partnership extension, investment, independent research, AI development \n", "General themes: technology collaboration, artificial intelligence, corporate investment\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\": 'Extract the entities mentioned in the text below. \\\n", " Extract the important entities mentioned in the text below. \\\n", " First extract all company names, then extract all years, \\\n", " then extract specific topics which fit the content and finally extract general overarching themes\\n\\n \\\n", " Desired format: \\\n", " Company names: <comma_separated_list_of_company_names> \\\n", " Years: -||- \\\n", " Specific topics: -||- \\\n", " General themes: -||- \\\n", " \"\"\"\\\n", " We’re happy to announce that OpenAI and Microsoft are extending our partnership.\\\n", " This multi-year, multi-billion dollar investment from Microsoft follows their previous investments \\\n", " in 2019 and 2021, and will allow us to continue our independent research and develop AI that is \\\n", " increasingly safe, useful, and powerful. \\n\\n \\\n", " \"\"\"\\\n", " ',}],\n", " max_tokens=400,)\n", "\n", "print(response.choices[0].message.content)\n", "\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# 5. Start with zero-shot, then few-shot (example), neither of them worked, then fine-tune (To update)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OpenAI, Microsoft, partnership, multi-year, multi-billion dollar investment, 2019, 2021, independent research, AI, safe, useful, powerful.\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant. Extract keywords from the corresponding texts below.\"},\n", " {\"role\":\"user\",\"content\": 'Text: \\n\\\n", " We’re happy to announce that OpenAI and Microsoft are extending our partnership.\\\n", " This multi-year, multi-billion dollar investment from Microsoft follows their previous investments \\\n", " in 2019 and 2021, and will allow us to continue our independent research and develop AI that is \\\n", " increasingly safe, useful, and powerful. \\n\\nKeywords: ',}],\n", " max_tokens=400,)\n", "\n", "print(response.choices[0].message.content)\n", "\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OpenAI, Microsoft, partnership, multi-year, multi-billion dollar investment, independent research, AI development, safety, usefulness.\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant. Extract keywords from the corresponding texts below.\\n\\n \\\n", " Text: Stripe provides APIs that web developers can use to integrate \\\n", " payment processing into their websites and mobile applications. \\\n", " Keywords: Stripe, payment processing, APIs, web developers, websites, mobile applications \\\n", " ###\\n\\\n", " Text: OpenAI has trained cutting-edge language models that are very good at understanding \\\n", " and generating text. Our API provides access to these models and can be used to solve virtually \\\n", " any task that involves processing language. \\n\\\n", " Keywords: language models, text processing, API.\\n\\n\\\n", " ##W\"},\n", " {\"role\":\"user\",\"content\": '\\n\\\n", " Text: We’re happy to announce that OpenAI and Microsoft are extending our partnership.\\\n", " This multi-year, multi-billion dollar investment from Microsoft follows their previous investments \\\n", " in 2019 and 2021, and will allow us to continue our independent research and develop AI that is \\\n", " increasingly safe, useful, and powerful. \\n\\n\\\n", " Keywords:',}],\n", " max_tokens=400,)\n", "\n", "print(response.choices[0].message.content)\n", "\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# 6. Reduce “fluffy” and imprecise descriptions" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Introducing the Quantum Comfort Car Seat, the latest innovation in automotive seating technology. Designed with adaptive contour memory foam and an integrated climate control system, this car seat adjusts to your body's shape and temperature preferences, ensuring unparalleled comfort during every drive. The Quantum Comfort also features advanced safety harnesses and a sleek, modern design that complements any vehicle interior. Experience the future of driving comfort with the Quantum Comfort Car Seat.\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\": 'Write a description for a new product. This product is a new generation of car seat. \\\n", " The description for this product should be fairly short, a few sentences only, and not too much more.',}],\n", " max_tokens=400,)\n", "\n", "print(response.choices[0].message.content)\n", "\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Introducing the innovative LuxRide 360°, the next generation of car seats designed to revolutionize your driving experience. This state-of-the-art seat combines unparalleled comfort with advanced ergonomic design, ensuring optimal support for drivers and passengers alike on even the longest journeys. Featuring smart temperature control technology, it automatically adjusts to provide a perfect climate, keeping you cool in the summer and cozy in the winter. The LuxRide 360° also integrates cutting-edge sensors that adapt to individual body shapes, promoting perfect posture and reducing fatigue. With its sleek design and eco-friendly materials, this car seat is not only a luxurious upgrade but also a sustainable choice for the modern driver.\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\": 'Write a description for a new product. This product is a new generation of car seat. \\\n", " Use a 3 to 5 sentence paragraph to describe this product.',}],\n", " max_tokens=400,)\n", "\n", "print(response.choices[0].message.content)\n", "\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# 7. Instead of just saying what not to do, say what to do instead" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm sorry to hear you're having trouble logging in. Let's see if we can resolve this. Have you tried resetting your password? If not, there should be an option on the login page to reset it. Additionally, make sure you're entering your email address correctly. If you're still having issues after trying these steps, let me know and we can look at other solutions.\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\": 'The following is a conversation between an Agent and a Customer. DO NOT ASK USERNAME OR PASSWORD. DO NOT REPEAT. \\n\\n\\\n", " Customer: I can’t log in to my account.\\n\\\n", " Agent:',}],\n", " max_tokens=400,)\n", "\n", "print(response.choices[0].message.content)\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm sorry to hear you're having trouble logging in. Let's try to resolve this issue. First, ensure that you are entering the correct email address and password. If you've forgotten your password, you can reset it by clicking the \"Forgot Password\" link on the login page.\n", "\n", "If you continue to experience issues, I recommend checking out the help article at www.samplewebsite.com/help/faq for further guidance. It contains detailed instructions and troubleshooting tips for login problems.\n", "\n", "If the problem persists, please let me know, and I'll assist you further.\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\":'The following is a conversation between an Agent and a Customer. The agent will attempt to diagnose the \\\n", " problem and suggest a solution, whilst refraining from asking any questions related to PII. \\\n", " Instead of asking for PII, such as username or password, refer the user to the help \\\n", " article www.samplewebsite.com/help/faq \\n\\n\\\n", " Customer: I can’t log in to my account. \\n\\\n", " Agent:',}],\n", " max_tokens=400,)\n", "\n", "print(response.choices[0].message.content)\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# 8. Code Generation Specific - Use “leading words” to nudge the model toward a particular pattern" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Certainly! Below is a simple Python function that asks the user for a distance in miles and converts it to kilometers. \n", "\n", "```python\n", "def miles_to_kilometers():\n", " try:\n", " # Ask the user to input a number in miles\n", " miles = float(input(\"Please enter the number of miles: \"))\n", " \n", " # Conversion factor from miles to kilometers\n", " kilometers = miles * 1.60934\n", " \n", " # Display the result\n", " print(f\"{miles} miles is equal to {kilometers:.2f} kilometers.\")\n", " \n", " except ValueError:\n", " print(\"Please enter a valid number.\")\n", "\n", "# Call the function\n", "miles_to_kilometers()\n", "```\n", "\n", "Here's what happens in the function:\n", "\n", "1. It prompts the user to enter a distance in miles.\n", "2. It converts the input from miles to kilometers using the conversion factor \\(1 \\text{ mile} = 1.60934 \\text{ kilometers}\\).\n", "3. It prints out the converted distance in kilometers.\n", "4. If the user enters an invalid number, it catches the error and prompts the user to enter a valid number. \n", "\n", "You can run this function to see it in action.\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\":'# Write a simple python function that \\n\\\n", " # 1. Ask me for a number in mile\\n\\\n", " # 2. It converts miles to kilometers',}],\n", " max_tokens=400,)\n", "\n", "print(response.choices[0].message.content)\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Certainly! Let's create a simple Python function that:\n", "\n", "1. Asks the user for a number in miles.\n", "2. Converts that number from miles to kilometers.\n", "3. Prints the result.\n", "\n", "Below is the Python code for the function:\n", "\n", "```python\n", "def miles_to_kilometers():\n", " try:\n", " # Ask the user for a number in miles\n", " miles = float(input(\"Enter the distance in miles: \"))\n", " \n", " # Conversion factor from miles to kilometers\n", " conversion_factor = 1.60934\n", " \n", " # Convert miles to kilometers\n", " kilometers = miles * conversion_factor\n", " \n", " # Print the result\n", " print(f\"{miles} miles is equal to {kilometers:.2f} kilometers.\")\n", " except ValueError:\n", " print(\"Please enter a valid number.\")\n", "\n", "# Call the function\n", "miles_to_kilometers()\n", "```\n", "\n", "### How It Works:\n", "\n", "- We use `input()` to prompt the user to enter a distance in miles, and convert the input to a float for calculations.\n", "- We define the conversion factor from miles to kilometers as `1.60934`.\n", "- We compute the distance in kilometers by multiplying the distance in miles by the conversion factor.\n", "- The result is printed to the screen, formatted to two decimal places.\n", "- We include error handling with `try-except` to manage invalid inputs gracefully.\n" ] } ], "source": [ "response = client.chat.completions.create(\n", " model=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\":'# Write a simple python function that \\n\\\n", " # 1. Ask me for a number in mile\\n\\\n", " # 2. It converts miles to kilometers\\n\\\n", " import ',}],\n", " max_tokens=400,)\n", "\n", "print(response.choices[0].message.content)\n" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.12.5" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }