quick_start/v1/07_prompt_engineering.ipynb (203 lines of code) (raw):
{
"cells": [
{
"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')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# A Few Shot Learning"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"😍,👏,✨\n"
]
}
],
"source": [
"# Zero-shot classification\n",
"system_prompt =\"\"\"Predict up to 5 emojis as a response to a text chat message. The output\n",
"should only include emojis.\n",
"\n",
"input: The new visual design is blowing my mind 🤯\n",
"output: ➕,💘, ❤🔥\n",
"\n",
"input: Well that looks great regardless\n",
"output: ❤️,🪄\n",
"\n",
"input: Unfortunately this won't work\n",
"output: 💔,😔\n",
"\n",
"input: sounds good, I'll look into that\n",
"output: 🙏,👍\n",
"\n",
"input: 10hr cut of jeff goldblum laughing URL\n",
"output: 😂,💀,⚰️\n",
"\"\"\"\n",
"user_prompt = \"The new user interface is amazing!\"\n",
"response = client.chat.completions.create(\n",
" model=CHAT_COMPLETIONS_MODEL,\n",
" messages = [{\"role\":\"system\", \"content\":system_prompt},\n",
" {\"role\":\"user\",\"content\": user_prompt,}])\n",
"print(response.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Prompt Engineering Best Practices\n",
"\n",
"## Write clear instructions\n",
"\n",
"Examples:\n",
"\n",
"-----------------------\n",
"Prompt:\n",
"\n",
"Write code to calculate the Fibonacci sequence.\n",
"\n",
"Better:\n",
"\n",
"Write a TypeScript function to efficiently calculate the Fibonacci sequence. Comment the code liberally to explain what each piece does and why it's written that way.\n",
"\n",
"----------------------\n",
"\n",
"Prompt:\n",
"\n",
"Summarize the meeting notes.\n",
"\n",
"Better:\n",
"\n",
"Summarize the meeting notes in a single paragraph. Then write a markdown list of the speakers and each of their key points. Finally, list the next steps or action items suggested by the speakers, if any.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Role Playing\n",
"\n",
"Examples:\n",
"\n",
"-----------------------\n",
"\n",
"System Message: When I ask for help to write something, you will reply with a document that contains at least one joke or playful comment in every paragraph.\n",
"\n",
"----------------------\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Segment input text\n",
"\n",
"Examples:\n",
"\n",
"------------------------\n",
"\n",
"user message: Summarize the text delimited by triple quotes with a haiku.\n",
"\n",
"\"\"\"insert text here\"\"\"\n",
"\n",
"------------------------\n",
"\n",
"system message: You will be provided with a pair of articles (delimited with XML tags) about the same topic. First summarize the arguments of each article. Then indicate which of them makes a better argument and explain why.\n",
"\n",
"user message: \n",
"\n",
"\\<article> insert first article here \\</article>\n",
"\n",
"\\<article> insert second article here \\</article>\n",
"\n",
"------------------------"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Explain steps and processes to complete a task\n",
"\n",
"Examples:\n",
"\n",
"--------------------------------\n",
"\n",
"System Message:\n",
"Use the following step-by-step instructions to respond to user inputs.\n",
"\n",
"Step 1 - The user will provide you with text in triple quotes. Summarize this text in one sentence with a prefix that says \"Summary: \".\n",
"\n",
"Step 2 - Translate the summary from Step 1 into Spanish, with a prefix that says \"Translation: \".\n",
"\n",
"---------------------------------"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use few-shot learning\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"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
}