Created
April 30, 2023 07:50
-
-
Save petrLorenc/4846233f442cd3e1fe80243dda82a3dd to your computer and use it in GitHub Desktop.
Exercise.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyMjZmc7vklUqiQ4OefQG7H2", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/petrLorenc/4846233f442cd3e1fe80243dda82a3dd/exercise.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Disclaimer: Taking as a notes from this course - https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/ \n", | |
"\n", | |
"Other resource: https://lilianweng.github.io/posts/2023-03-15-prompt-engineering/" | |
], | |
"metadata": { | |
"id": "lXxTgeKg_l8m" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "YBcOwlW0An2a", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "77984e38-6cc6-44a2-a573-de0b02286970" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", | |
"Collecting openai\n", | |
" Downloading openai-0.27.5-py3-none-any.whl (71 kB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.6/71.6 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hCollecting aiohttp\n", | |
" Downloading aiohttp-3.8.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m10.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hRequirement already satisfied: requests>=2.20 in /usr/local/lib/python3.10/dist-packages (from openai) (2.27.1)\n", | |
"Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from openai) (4.65.0)\n", | |
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (2022.12.7)\n", | |
"Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (2.0.12)\n", | |
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (1.26.15)\n", | |
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (3.4)\n", | |
"Collecting multidict<7.0,>=4.5\n", | |
" Downloading multidict-6.0.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (114 kB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m114.5/114.5 kB\u001b[0m \u001b[31m2.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hCollecting aiosignal>=1.1.2\n", | |
" Downloading aiosignal-1.3.1-py3-none-any.whl (7.6 kB)\n", | |
"Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (23.1.0)\n", | |
"Collecting async-timeout<5.0,>=4.0.0a3\n", | |
" Downloading async_timeout-4.0.2-py3-none-any.whl (5.8 kB)\n", | |
"Collecting yarl<2.0,>=1.0\n", | |
" Downloading yarl-1.9.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (268 kB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m268.8/268.8 kB\u001b[0m \u001b[31m11.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hCollecting frozenlist>=1.1.1\n", | |
" Downloading frozenlist-1.3.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (149 kB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m149.6/149.6 kB\u001b[0m \u001b[31m10.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hInstalling collected packages: multidict, frozenlist, async-timeout, yarl, aiosignal, aiohttp, openai\n", | |
"Successfully installed aiohttp-3.8.4 aiosignal-1.3.1 async-timeout-4.0.2 frozenlist-1.3.3 multidict-6.0.4 openai-0.27.5 yarl-1.9.2\n" | |
] | |
} | |
], | |
"source": [ | |
"!pip install openai" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"API_KEY = 'sk-...'" | |
], | |
"metadata": { | |
"id": "Ky_JhiFtnuxw" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import openai\n", | |
"openai.api_key = f\"{API_KEY}\"" | |
], | |
"metadata": { | |
"id": "YYqlbIGtoPlf" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"help(openai.ChatCompletion.create)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "YxoXvyVK__LA", | |
"outputId": "e59005b8-a0dd-4238-b628-64c8d4419653" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Help on method create in module openai.api_resources.chat_completion:\n", | |
"\n", | |
"create(*args, **kwargs) method of builtins.type instance\n", | |
" Creates a new chat completion for the provided messages and parameters.\n", | |
" \n", | |
" See https://platform.openai.com/docs/api-reference/chat-completions/create\n", | |
" for a list of valid parameters.\n", | |
"\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"def get_completion(prompt, model=\"gpt-3.5-turbo\", verbose=False):\n", | |
" messages = [{\"role\": \"user\", \"content\": prompt}]\n", | |
" response = openai.ChatCompletion.create(\n", | |
" model=model,\n", | |
" messages=messages,\n", | |
" temperature=0, # this is the degree of randomness of the model's output\n", | |
" )\n", | |
" if verbose:\n", | |
" print(response)\n", | |
" return response.choices[0].message[\"content\"]\n", | |
"\n", | |
"def get_completion_from_messages(messages, model=\"gpt-3.5-turbo\", temperature=0, verbose=False):\n", | |
" response = openai.ChatCompletion.create(\n", | |
" model=model,\n", | |
" messages=messages,\n", | |
" temperature=temperature, # this is the degree of randomness of the model's output\n", | |
" )\n", | |
" if verbose:\n", | |
" print(str(response.choices[0].message))\n", | |
" return response.choices[0].message[\"content\"]" | |
], | |
"metadata": { | |
"id": "IYXSrznzoToA" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"get_completion(\"Test of your response!\", verbose=True)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 437 | |
}, | |
"id": "uEaD7m-A3PLc", | |
"outputId": "62214354-b359-48f6-879a-0d27bb06f88b" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"{\n", | |
" \"choices\": [\n", | |
" {\n", | |
" \"finish_reason\": \"stop\",\n", | |
" \"index\": 0,\n", | |
" \"message\": {\n", | |
" \"content\": \"As an AI language model, I do not have emotions or feelings, but I am programmed to respond to your queries and provide helpful and informative answers. Please feel free to ask me any questions or give me any tasks you need assistance with.\",\n", | |
" \"role\": \"assistant\"\n", | |
" }\n", | |
" }\n", | |
" ],\n", | |
" \"created\": 1682786146,\n", | |
" \"id\": \"chatcmpl-7AhpyxmhqXAKNSUBgHRUPXZkhjbaD\",\n", | |
" \"model\": \"gpt-3.5-turbo-0301\",\n", | |
" \"object\": \"chat.completion\",\n", | |
" \"usage\": {\n", | |
" \"completion_tokens\": 48,\n", | |
" \"prompt_tokens\": 13,\n", | |
" \"total_tokens\": 61\n", | |
" }\n", | |
"}\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'As an AI language model, I do not have emotions or feelings, but I am programmed to respond to your queries and provide helpful and informative answers. Please feel free to ask me any questions or give me any tasks you need assistance with.'" | |
], | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
} | |
}, | |
"metadata": {}, | |
"execution_count": 10 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### Prompt = **Instruction** + Context + **Input data** + Output indicator\n", | |
"\n", | |
"" | |
], | |
"metadata": { | |
"id": "h6dLij1I-325" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"**Temperature** and **Top_p** are parameters (the higher value the higher diversity) - better change only one at the time" | |
], | |
"metadata": { | |
"id": "NXrAimCkAYa4" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Prompting Principles\n", | |
"- **Principle 1: Write clear and specific instructions**\n", | |
" - Iterative Prompt Development\n", | |
" - Give examples or let continue with part of expected output\n", | |
" - Use delimiters to clearly indicate distinct parts of the input\n", | |
" - Ask for a structured output\n", | |
" - Ask the model to firstly check if the conditions are satisfied and if not provide fallback message\n", | |
"- **Principle 2: Give the model time to “think”**\n", | |
" - Provide steps to complete the task or let the model came up with step on its own. And then guide it to follow these steps. (**Chain-of-thought**, **Zero-shot CoT** - \"Let's think step by step ...\")\n", | |
" - Few-shot prompting\n", | |
"\n", | |
"### Generate Knowledge Prompting\n", | |
" - Multiple-steps -> Input+knowledge+Input -> Input+knowledge+Input+Knowledge\n", | |
"\n", | |
"### Program aided Language models\n", | |
" - Generate program to solve the problem and then run the program \n", | |
"\n", | |
"## Iterative Prompt Development\n", | |
" - Iteratively specify what you want to achieve (lenght, specific format, specifical focus)\n", | |
" ```\n", | |
" prompt = f\"\"\"\n", | |
" Your task is to help a marketing team create a \n", | |
" description for a retail website of a product based \n", | |
" on a technical fact sheet.\n", | |
"\n", | |
" Write a product description based on the information \n", | |
" provided in the technical specifications delimited by \n", | |
" triple backticks.\n", | |
"\n", | |
" The description is intended for furniture retailers, \n", | |
" so should be technical in nature and focus on the \n", | |
" materials the product is constructed from.\n", | |
"\n", | |
" At the end of the description, include every 7-character \n", | |
" Product ID in the technical specification.\n", | |
"\n", | |
" Use at most 50 words.\n", | |
"\n", | |
" Technical specifications: ```{fact_sheet_chair}```\n", | |
" \"\"\"\n", | |
" response = get_completion(prompt)\n", | |
" print(response)\n", | |
"```\n", | |
"\n", | |
"### Usage\n", | |
"* Summarize (can set the word/sentence/character limit), or as a certain deparment\n", | |
"\n", | |
"```\n", | |
" prompt = f\"\"\"\n", | |
" Your task is to generate a short summary of a product \\\n", | |
" review from an ecommerce site to give feedback to the \\\n", | |
" pricing deparmtment, responsible for determining the \\\n", | |
" price of the product. \n", | |
"\n", | |
" Summarize the review below, delimited by triple \n", | |
" backticks, in at most 30 words, and focusing on any aspects \\\n", | |
" that are relevant to the price and perceived value. \n", | |
"\n", | |
" Review: ```{prod_review}```\n", | |
" \"\"\"\n", | |
"\n", | |
" response = get_completion(prompt)\n", | |
" print(response)\n", | |
"```\n", | |
"\n", | |
"* Extract (similar to summarize)\n", | |
"* Infering (posivive or negative label, emotions - better specity them) or identify certain aspects of text, topics\n", | |
"* Extracting specific entities from the text\n", | |
"\n", | |
"```\n", | |
" prompt = f\"\"\"\n", | |
" Identify the following items from the review text: \n", | |
" - Item purchased by reviewer\n", | |
" - Company that made the item\n", | |
"\n", | |
" The review is delimited with triple backticks. \\\n", | |
" Format your response as a JSON object with \\\n", | |
" \"Item\" and \"Brand\" as the keys. \n", | |
" If the information isn't present, use \"unknown\" \\\n", | |
" as the value.\n", | |
" Make your response as short as possible.\n", | |
" \n", | |
" Review text: '''{lamp_review}'''\n", | |
" \"\"\"\n", | |
" response = get_completion(prompt)\n", | |
" print(response)\n", | |
"```\n", | |
"\n", | |
"* Spelling correction and translation\n", | |
"* Data/format conversion\n", | |
"\n", | |
"```\n", | |
"data_json = { \"resturant employees\" :[ \n", | |
" {\"name\":\"Shyam\", \"email\":\"[email protected]\"},\n", | |
" {\"name\":\"Bob\", \"email\":\"[email protected]\"},\n", | |
" {\"name\":\"Jai\", \"email\":\"[email protected]\"}\n", | |
"]}\n", | |
"\n", | |
"prompt = f\"\"\"\n", | |
"Translate the following python dictionary from JSON to an HTML \\\n", | |
"table with column headers and title: {data_json}\n", | |
"\"\"\"\n", | |
"response = get_completion(prompt)\n", | |
"print(response)\n", | |
"```\n", | |
"\n", | |
"* Automatic reply to customers and summarize it into in-house system\n", | |
"* Use it as a chatbot - using role (system, user, assistant - see below)\n", | |
"\n", | |
"### Tactics\n", | |
"\n", | |
"#### Tactic 1: Use delimiters to clearly indicate distinct parts of the input\n", | |
"- Delimiters can be anything like: ```, \"\"\", < >, `<tag> </tag>`, `:`" | |
], | |
"metadata": { | |
"id": "sjDbsOYdpi4w" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"text = f\"\"\"\n", | |
"You should express what you want a model to do by \\ \n", | |
"providing instructions that are as clear and \\ \n", | |
"specific as you can possibly make them. \\ \n", | |
"This will guide the model towards the desired output, \\ \n", | |
"and reduce the chances of receiving irrelevant \\ \n", | |
"or incorrect responses. Don't confuse writing a \\ \n", | |
"clear prompt with writing a short prompt. \\ \n", | |
"In many cases, longer prompts provide more clarity \\ \n", | |
"and context for the model, which can lead to \\ \n", | |
"more detailed and relevant outputs.\n", | |
"\"\"\"\n", | |
"prompt = f\"\"\"\n", | |
"Summarize the text delimited by triple backticks \\ \n", | |
"into a single sentence.\n", | |
"```{text}```\n", | |
"\"\"\"\n", | |
"response = get_completion(prompt)\n", | |
"print(response)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "pTj_p5yXobH3", | |
"outputId": "15a1eb2c-66d5-4073-88a9-b14aad8156b7" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Clear and specific instructions should be provided to guide a model towards the desired output, and longer prompts can provide more clarity and context for the model, leading to more detailed and relevant outputs.\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"#### Tactic 2: Ask for a structured output\n" | |
], | |
"metadata": { | |
"id": "ZQoRLV2lpo5f" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"prompt = f\"\"\"\n", | |
"Generate a list of three made-up book titles along \\ \n", | |
"with their authors and genres. \n", | |
"Provide them in JSON format with the following keys: \n", | |
"book_id, title, author, genre.\n", | |
"\"\"\"\n", | |
"response = get_completion(prompt)\n", | |
"print(response)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "gYlq7t_lodri", | |
"outputId": "a1d60622-1f54-4f37-9c59-0486cc826409" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"[\n", | |
" {\n", | |
" \"book_id\": 1,\n", | |
" \"title\": \"The Lost City of Zorath\",\n", | |
" \"author\": \"Aria Blackwood\",\n", | |
" \"genre\": \"Fantasy\"\n", | |
" },\n", | |
" {\n", | |
" \"book_id\": 2,\n", | |
" \"title\": \"The Last Survivors\",\n", | |
" \"author\": \"Ethan Stone\",\n", | |
" \"genre\": \"Science Fiction\"\n", | |
" },\n", | |
" {\n", | |
" \"book_id\": 3,\n", | |
" \"title\": \"The Secret of the Haunted Mansion\",\n", | |
" \"author\": \"Lila Rose\",\n", | |
" \"genre\": \"Mystery\"\n", | |
" }\n", | |
"]\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"#### Tactic 3: Ask the model to firstly check if the conditions are satisfied and if not provide fallback message" | |
], | |
"metadata": { | |
"id": "OB7qcdc-ptlI" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"text_1 = f\"\"\"\n", | |
"Making a cup of tea is easy! First, you need to get some \\ \n", | |
"water boiling. While that's happening, \\ \n", | |
"grab a cup and put a tea bag in it. Once the water is \\ \n", | |
"hot enough, just pour it over the tea bag. \\ \n", | |
"Let it sit for a bit so the tea can steep. After a \\ \n", | |
"few minutes, take out the tea bag. If you \\ \n", | |
"like, you can add some sugar or milk to taste. \\ \n", | |
"And that's it! You've got yourself a delicious \\ \n", | |
"cup of tea to enjoy.\n", | |
"\"\"\"\n", | |
"prompt = f\"\"\"\n", | |
"You will be provided with text delimited by triple quotes. \n", | |
"If it contains a sequence of instructions, \\ \n", | |
"re-write those instructions in the following format:\n", | |
"\n", | |
"Step 1 - ...\n", | |
"Step 2 - …\n", | |
"…\n", | |
"Step N - …\n", | |
"\n", | |
"If the text does not contain a sequence of instructions, \\ \n", | |
"then simply write \\\"No steps provided.\\\"\n", | |
"\n", | |
"\\\"\\\"\\\"{text_1}\\\"\\\"\\\"\n", | |
"\"\"\"\n", | |
"response = get_completion(prompt)\n", | |
"print(\"Completion for Text 1:\")\n", | |
"print(response)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "hhDKUeaRpqxo", | |
"outputId": "a1240471-f761-4588-9106-d9b6ae95a7ab" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Completion for Text 1:\n", | |
"Step 1 - Get some water boiling.\n", | |
"Step 2 - Grab a cup and put a tea bag in it.\n", | |
"Step 3 - Once the water is hot enough, pour it over the tea bag.\n", | |
"Step 4 - Let it sit for a bit so the tea can steep.\n", | |
"Step 5 - After a few minutes, take out the tea bag.\n", | |
"Step 6 - Add some sugar or milk to taste.\n", | |
"Step 7 - Enjoy your delicious cup of tea!\n", | |
"\n", | |
"\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"#### Tactic 4: \"Few-shot\" prompting\n", | |
"```\n", | |
"prompt = f\"\"\"\n", | |
"Your task is to answer in a consistent style.\n", | |
"\n", | |
"<child>: Teach me about patience.\n", | |
"\n", | |
"<grandparent>: The river that carves the deepest \\ \n", | |
"valley flows from a modest spring; the \\ \n", | |
"grandest symphony originates from a single note; \\ \n", | |
"the most intricate tapestry begins with a solitary thread.\n", | |
"\n", | |
"<child>: Teach me about resilience.\n", | |
"\"\"\"\n", | |
"response = get_completion(prompt)\n", | |
"print(response)\n", | |
"```" | |
], | |
"metadata": { | |
"id": "9Y-ry2_0p9vh" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Principle 2 \n", | |
"\n", | |
"### Let the model think on its own -> Provide steps to complete the task or let the model came up with step on its own. And then guide it to follow these steps." | |
], | |
"metadata": { | |
"id": "FJFoPZsCqDSR" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"text = f\"\"\"\n", | |
"In a charming village, siblings Jack and Jill set out on \\ \n", | |
"a quest to fetch water from a hilltop \\ \n", | |
"well. As they climbed, singing joyfully, misfortune \\ \n", | |
"struck—Jack tripped on a stone and tumbled \\ \n", | |
"down the hill, with Jill following suit. \\ \n", | |
"Though slightly battered, the pair returned home to \\ \n", | |
"comforting embraces. Despite the mishap, \\ \n", | |
"their adventurous spirits remained undimmed, and they \\ \n", | |
"continued exploring with delight.\n", | |
"\"\"\"\n", | |
"# example 1\n", | |
"prompt_1 = f\"\"\"\n", | |
"Perform the following actions: \n", | |
"1 - Summarize the following text delimited by triple \\\n", | |
"backticks with 1 sentence.\n", | |
"2 - Translate the summary into French.\n", | |
"3 - List each name in the French summary.\n", | |
"4 - Output a json object that contains the following \\\n", | |
"keys: french_summary, num_names.\n", | |
"\n", | |
"Separate your answers with line breaks.\n", | |
"\n", | |
"Text:\n", | |
"```{text}```\n", | |
"\"\"\"\n", | |
"response = get_completion(prompt_1)\n", | |
"print(\"Completion for prompt 1:\")\n", | |
"print(response)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "MRZj1rFsp4pV", | |
"outputId": "21b77be5-3d29-4972-ee8e-d4a2c81c8eee" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Completion for prompt 1:\n", | |
"Two siblings, Jack and Jill, go on a quest to fetch water from a hilltop well, but misfortune strikes as they both fall down the hill, yet they return home slightly battered but with their adventurous spirits undimmed.\n", | |
"\n", | |
"Deux frères et sœurs, Jack et Jill, partent en quête d'eau d'un puits au sommet d'une colline, mais ils tombent tous les deux et retournent chez eux légèrement meurtris mais avec leur esprit d'aventure intact. \n", | |
"Noms: Jack, Jill.\n", | |
"\n", | |
"{\n", | |
"\"french_summary\": \"Deux frères et sœurs, Jack et Jill, partent en quête d'eau d'un puits au sommet d'une colline, mais ils tombent tous les deux et retournent chez eux légèrement meurtris mais avec leur esprit d'aventure intact.\",\n", | |
"\"num_names\": 2\n", | |
"}\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"prompt_2 = f\"\"\"\n", | |
"Your task is to perform the following actions: \n", | |
"1 - Summarize the following text delimited by \n", | |
" <> with 1 sentence.\n", | |
"2 - Translate the summary into French.\n", | |
"3 - List each name in the French summary.\n", | |
"4 - Output a json object that contains the \n", | |
" following keys: french_summary, num_names.\n", | |
"\n", | |
"Use the following format:\n", | |
"Text: <text to summarize>\n", | |
"Summary: <summary>\n", | |
"Translation: <summary translation>\n", | |
"Names: <list of names in Italian summary>\n", | |
"Output JSON: <json with summary and num_names>\n", | |
"\n", | |
"Text: <{text}>\n", | |
"\"\"\"\n", | |
"response = get_completion(prompt_2)\n", | |
"print(\"\\nCompletion for prompt 2:\")\n", | |
"print(response)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "F-Yy9bV9qai3", | |
"outputId": "1730612f-b715-4c67-9716-d861cd6eb160" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"\n", | |
"Completion for prompt 2:\n", | |
"Summary: Jack and Jill go on a quest to fetch water, but misfortune strikes and they tumble down the hill, returning home slightly battered but with their adventurous spirits undimmed. \n", | |
"Translation: Jack et Jill partent en quête d'eau, mais la malchance frappe et ils dégringolent la colline, rentrant chez eux légèrement meurtris mais avec leurs esprits aventureux intacts.\n", | |
"Names: Jack, Jill\n", | |
"Output JSON: {\"french_summary\": \"Jack et Jill partent en quête d'eau, mais la malchance frappe et ils dégringolent la colline, rentrant chez eux légèrement meurtris mais avec leurs esprits aventureux intacts.\", \"num_names\": 2}\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"prompt = f\"\"\"\n", | |
"Your task is to determine if the student's solution \\\n", | |
"is correct or not.\n", | |
"To solve the problem do the following:\n", | |
"- First, work out your own solution to the problem. \n", | |
"- Then compare your solution to the student's solution \\ \n", | |
"and evaluate if the student's solution is correct or not. \n", | |
"Don't decide if the student's solution is correct until \n", | |
"you have done the problem yourself.\n", | |
"\n", | |
"Use the following format:\n", | |
"Question:\n", | |
"```\n", | |
"question here\n", | |
"```\n", | |
"Student's solution:\n", | |
"```\n", | |
"student's solution here\n", | |
"```\n", | |
"Actual solution:\n", | |
"```\n", | |
"steps to work out the solution and your solution here\n", | |
"```\n", | |
"Is the student's solution the same as actual solution \\\n", | |
"just calculated:\n", | |
"```\n", | |
"yes or no\n", | |
"```\n", | |
"Student grade:\n", | |
"```\n", | |
"correct or incorrect\n", | |
"```\n", | |
"\n", | |
"Question:\n", | |
"```\n", | |
"I'm building a solar power installation and I need help \\\n", | |
"working out the financials. \n", | |
"- Land costs $100 / square foot\n", | |
"- I can buy solar panels for $250 / square foot\n", | |
"- I negotiated a contract for maintenance that will cost \\\n", | |
"me a flat $100k per year, and an additional $10 / square \\\n", | |
"foot\n", | |
"What is the total cost for the first year of operations \\\n", | |
"as a function of the number of square feet.\n", | |
"``` \n", | |
"Student's solution:\n", | |
"```\n", | |
"Let x be the size of the installation in square feet.\n", | |
"Costs:\n", | |
"1. Land cost: 100x\n", | |
"2. Solar panel cost: 250x\n", | |
"3. Maintenance cost: 100,000 + 100x\n", | |
"Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000\n", | |
"```\n", | |
"Actual solution:\n", | |
"\"\"\"\n", | |
"response = get_completion(prompt)\n", | |
"print(response)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "pbYrghiIqc5Y", | |
"outputId": "0868b8a6-d7ea-4fb5-d484-10e56158e0f6" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Let x be the size of the installation in square feet.\n", | |
"\n", | |
"Costs:\n", | |
"1. Land cost: 100x\n", | |
"2. Solar panel cost: 250x\n", | |
"3. Maintenance cost: 100,000 + 10x\n", | |
"\n", | |
"Total cost: 100x + 250x + 100,000 + 10x = 360x + 100,000\n", | |
"\n", | |
"Is the student's solution the same as actual solution just calculated:\n", | |
"No\n", | |
"\n", | |
"Student grade:\n", | |
"Incorrect\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Be aware of possible hallucations and use your own brain!" | |
], | |
"metadata": { | |
"id": "PQXP9Wudqm7L" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# You can also let the model to generate HTML" | |
], | |
"metadata": { | |
"id": "JZSntawfrTik" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"fact_sheet_chair = \"\"\"\n", | |
"OVERVIEW\n", | |
"- Part of a beautiful family of mid-century inspired office furniture, \n", | |
"including filing cabinets, desks, bookcases, meeting tables, and more.\n", | |
"- Several options of shell color and base finishes.\n", | |
"- Available with plastic back and front upholstery (SWC-100) \n", | |
"or full upholstery (SWC-110) in 10 fabric and 6 leather options.\n", | |
"- Base finish options are: stainless steel, matte black, \n", | |
"gloss white, or chrome.\n", | |
"- Chair is available with or without armrests.\n", | |
"- Suitable for home or business settings.\n", | |
"- Qualified for contract use.\n", | |
"\n", | |
"CONSTRUCTION\n", | |
"- 5-wheel plastic coated aluminum base.\n", | |
"- Pneumatic chair adjust for easy raise/lower action.\n", | |
"\n", | |
"DIMENSIONS\n", | |
"- WIDTH 53 CM | 20.87”\n", | |
"- DEPTH 51 CM | 20.08”\n", | |
"- HEIGHT 80 CM | 31.50”\n", | |
"- SEAT HEIGHT 44 CM | 17.32”\n", | |
"- SEAT DEPTH 41 CM | 16.14”\n", | |
"\n", | |
"OPTIONS\n", | |
"- Soft or hard-floor caster options.\n", | |
"- Two choices of seat foam densities: \n", | |
" medium (1.8 lb/ft3) or high (2.8 lb/ft3)\n", | |
"- Armless or 8 position PU armrests \n", | |
"\n", | |
"MATERIALS\n", | |
"SHELL BASE GLIDER\n", | |
"- Cast Aluminum with modified nylon PA6/PA66 coating.\n", | |
"- Shell thickness: 10 mm.\n", | |
"SEAT\n", | |
"- HD36 foam\n", | |
"\n", | |
"COUNTRY OF ORIGIN\n", | |
"- Italy\n", | |
"\"\"\"" | |
], | |
"metadata": { | |
"id": "a5vBMvqoreXv" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"prompt = f\"\"\"\n", | |
"Your task is to help a marketing team create a \n", | |
"description for a retail website of a product based \n", | |
"on a technical fact sheet.\n", | |
"\n", | |
"Write a product description based on the information \n", | |
"provided in the technical specifications delimited by \n", | |
"triple backticks.\n", | |
"\n", | |
"The description is intended for furniture retailers, \n", | |
"so should be technical in nature and focus on the \n", | |
"materials the product is constructed from.\n", | |
"\n", | |
"At the end of the description, include every 7-character \n", | |
"Product ID in the technical specification.\n", | |
"\n", | |
"After the description, include a table that gives the \n", | |
"product's dimensions. The table should have two columns.\n", | |
"In the first column include the name of the dimension. \n", | |
"In the second column include the measurements in inches only.\n", | |
"\n", | |
"Give the table the title 'Product Dimensions'.\n", | |
"\n", | |
"Format everything as HTML that can be used in a website. \n", | |
"Place the description in a <div> element.\n", | |
"\n", | |
"Technical specifications: ```{fact_sheet_chair}```\n", | |
"\"\"\"\n", | |
"\n", | |
"response = get_completion(prompt)\n", | |
"print(response)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "6XgYy1Loqj-m", | |
"outputId": "5fe24b2d-cccb-45a2-ed91-aa8b79eec115" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"<div>\n", | |
"<h2>Mid-Century Inspired Office Chair</h2>\n", | |
"<p>Introducing our mid-century inspired office chair, part of a beautiful family of office furniture that includes filing cabinets, desks, bookcases, meeting tables, and more. This chair is available in several options of shell color and base finishes, allowing you to customize it to your liking. You can choose between plastic back and front upholstery or full upholstery in 10 fabric and 6 leather options. The base finish options are stainless steel, matte black, gloss white, or chrome. The chair is also available with or without armrests, making it suitable for both home and business settings. Plus, it's qualified for contract use, so you can trust its durability and quality.</p>\n", | |
"<h3>Construction</h3>\n", | |
"<p>The chair features a 5-wheel plastic coated aluminum base and a pneumatic chair adjust for easy raise/lower action. You can choose between soft or hard-floor caster options and two choices of seat foam densities: medium (1.8 lb/ft3) or high (2.8 lb/ft3). The chair is also available with armless or 8 position PU armrests.</p>\n", | |
"<h3>Materials</h3>\n", | |
"<p>The shell base glider is made of cast aluminum with modified nylon PA6/PA66 coating, and the shell thickness is 10 mm. The seat is made of HD36 foam, ensuring comfort and support.</p>\n", | |
"<h3>Product Dimensions</h3>\n", | |
"<table>\n", | |
" <tr>\n", | |
" <td>Width</td>\n", | |
" <td>53 cm | 20.87\"</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Depth</td>\n", | |
" <td>51 cm | 20.08\"</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Height</td>\n", | |
" <td>80 cm | 31.50\"</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Seat Height</td>\n", | |
" <td>44 cm | 17.32\"</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Seat Depth</td>\n", | |
" <td>41 cm | 16.14\"</td>\n", | |
" </tr>\n", | |
"</table>\n", | |
"<h3>Product ID</h3>\n", | |
"<p>SWC-100, SWC-110</p>\n", | |
"</div>\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from IPython.display import display, HTML\n", | |
"display(HTML(response))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 541 | |
}, | |
"id": "4tKJGfMerZ7p", | |
"outputId": "f4d5d186-b72e-48b6-c78a-5b591075bc9b" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
], | |
"text/html": [ | |
"<div>\n", | |
"<h2>Mid-Century Inspired Office Chair</h2>\n", | |
"<p>Introducing our mid-century inspired office chair, part of a beautiful family of office furniture that includes filing cabinets, desks, bookcases, meeting tables, and more. This chair is available in several options of shell color and base finishes, allowing you to customize it to your liking. You can choose between plastic back and front upholstery or full upholstery in 10 fabric and 6 leather options. The base finish options are stainless steel, matte black, gloss white, or chrome. The chair is also available with or without armrests, making it suitable for both home and business settings. Plus, it's qualified for contract use, so you can trust its durability and quality.</p>\n", | |
"<h3>Construction</h3>\n", | |
"<p>The chair features a 5-wheel plastic coated aluminum base and a pneumatic chair adjust for easy raise/lower action. You can choose between soft or hard-floor caster options and two choices of seat foam densities: medium (1.8 lb/ft3) or high (2.8 lb/ft3). The chair is also available with armless or 8 position PU armrests.</p>\n", | |
"<h3>Materials</h3>\n", | |
"<p>The shell base glider is made of cast aluminum with modified nylon PA6/PA66 coating, and the shell thickness is 10 mm. The seat is made of HD36 foam, ensuring comfort and support.</p>\n", | |
"<h3>Product Dimensions</h3>\n", | |
"<table>\n", | |
" <tr>\n", | |
" <td>Width</td>\n", | |
" <td>53 cm | 20.87\"</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Depth</td>\n", | |
" <td>51 cm | 20.08\"</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Height</td>\n", | |
" <td>80 cm | 31.50\"</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Seat Height</td>\n", | |
" <td>44 cm | 17.32\"</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Seat Depth</td>\n", | |
" <td>41 cm | 16.14\"</td>\n", | |
" </tr>\n", | |
"</table>\n", | |
"<h3>Product ID</h3>\n", | |
"<p>SWC-100, SWC-110</p>\n", | |
"</div>" | |
] | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Summarize" | |
], | |
"metadata": { | |
"id": "zMMgr3J3sXsd" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"prod_review = \"\"\"\n", | |
"Got this panda plush toy for my daughter's birthday, \\\n", | |
"who loves it and takes it everywhere. It's soft and \\ \n", | |
"super cute, and its face has a friendly look. It's \\ \n", | |
"a bit small for what I paid though. I think there \\ \n", | |
"might be other options that are bigger for the \\ \n", | |
"same price. It arrived a day earlier than expected, \\ \n", | |
"so I got to play with it myself before I gave it \\ \n", | |
"to her.\n", | |
"\"\"\"\n", | |
"\n", | |
"prompt = f\"\"\"\n", | |
"Your task is to extract relevant information from \\ \n", | |
"a product review from an ecommerce site to give \\\n", | |
"feedback to the Shipping department. \n", | |
"\n", | |
"From the review below, delimited by triple quotes \\\n", | |
"extract the information relevant to shipping and \\ \n", | |
"delivery. Limit to 30 words. \n", | |
"\n", | |
"Review: ```{prod_review}```\n", | |
"\"\"\"\n", | |
"\n", | |
"response = get_completion(prompt)\n", | |
"print(response)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "BY3Hb29drlK6", | |
"outputId": "22d55c49-f487-495f-ad06-0712df5bf143" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"\"The product arrived a day earlier than expected.\"\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Format conversion" | |
], | |
"metadata": { | |
"id": "drBJQxYOtuxq" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"data_json = { \"resturant employees\" :[ \n", | |
" {\"name\":\"Shyam\", \"email\":\"[email protected]\"},\n", | |
" {\"name\":\"Bob\", \"email\":\"[email protected]\"},\n", | |
" {\"name\":\"Jai\", \"email\":\"[email protected]\"}\n", | |
"]}\n", | |
"\n", | |
"prompt = f\"\"\"\n", | |
"Translate the following python dictionary from JSON to an HTML \\\n", | |
"table with column headers and title: {data_json}\n", | |
"\"\"\"\n", | |
"response = get_completion(prompt)\n", | |
"print(response)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "lKkJg6XIsbgK", | |
"outputId": "23f6cebe-6fa6-4c29-83b7-5a70caa8d25b" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"<table>\n", | |
" <caption>Restaurant Employees</caption>\n", | |
" <thead>\n", | |
" <tr>\n", | |
" <th>Name</th>\n", | |
" <th>Email</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <td>Shyam</td>\n", | |
" <td>[email protected]</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Bob</td>\n", | |
" <td>[email protected]</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Jai</td>\n", | |
" <td>[email protected]</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from IPython.display import display, Markdown, Latex, HTML, JSON\n", | |
"display(HTML(response))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 122 | |
}, | |
"id": "EnNoYbactwxb", | |
"outputId": "12ad7cbe-c5b1-4faa-9868-b65f020e7020" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
], | |
"text/html": [ | |
"<table>\n", | |
" <caption>Restaurant Employees</caption>\n", | |
" <thead>\n", | |
" <tr>\n", | |
" <th>Name</th>\n", | |
" <th>Email</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <td>Shyam</td>\n", | |
" <td>[email protected]</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Bob</td>\n", | |
" <td>[email protected]</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Jai</td>\n", | |
" <td>[email protected]</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>" | |
] | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Chatbot" | |
], | |
"metadata": { | |
"id": "qoyHp4uluMzi" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"messages = [ \n", | |
"{'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'}, \n", | |
"{'role':'user', 'content':'tell me a joke'}, \n", | |
"{'role':'assistant', 'content':'Why did the chicken cross the road'}, \n", | |
"{'role':'user', 'content':'I don\\'t know'} ]" | |
], | |
"metadata": { | |
"id": "RqKS5SD6uPja" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"response = get_completion_from_messages(messages, temperature=1)\n", | |
"print(response)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "SA57EjUbuTNs", | |
"outputId": "e7dff99f-9b2b-443b-aa0b-31e6645e05ef" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"To get to the other side, fair sir/madam. Tis a most common jest, yet it doth bring mirth to the hearts of all who hear it.\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Chatbot in some food delivery company" | |
], | |
"metadata": { | |
"id": "WWNp8zuSuZ7e" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"context = [ {'role':'system', 'content':\"\"\"\n", | |
"You are OrderBot, an automated service to collect orders for a pizza restaurant. \\\n", | |
"You first greet the customer, then collects the order, \\\n", | |
"and then asks if it's a pickup or delivery. \\\n", | |
"You wait to collect the entire order, then summarize it and check for a final \\\n", | |
"time if the customer wants to add anything else. \\\n", | |
"If it's a delivery, you ask for an address. \\\n", | |
"Finally you collect the payment.\\\n", | |
"Make sure to clarify all options, extras and sizes to uniquely \\\n", | |
"identify the item from the menu.\\\n", | |
"You respond in a short, very conversational friendly style. \\\n", | |
"The menu includes \\\n", | |
"pepperoni pizza 12.95, 10.00, 7.00 \\\n", | |
"cheese pizza 10.95, 9.25, 6.50 \\\n", | |
"eggplant pizza 11.95, 9.75, 6.75 \\\n", | |
"fries 4.50, 3.50 \\\n", | |
"greek salad 7.25 \\\n", | |
"Toppings: \\\n", | |
"extra cheese 2.00, \\\n", | |
"mushrooms 1.50 \\\n", | |
"sausage 3.00 \\\n", | |
"canadian bacon 3.50 \\\n", | |
"AI sauce 1.50 \\\n", | |
"peppers 1.00 \\\n", | |
"Drinks: \\\n", | |
"coke 3.00, 2.00, 1.00 \\\n", | |
"sprite 3.00, 2.00, 1.00 \\\n", | |
"bottled water 5.00 \\\n", | |
"\"\"\"} ] # accumulate messages\n", | |
"\n", | |
"should_continue = True\n", | |
"while should_continue:\n", | |
" input_user = input(\"Your answer:\")\n", | |
" if input_user == \"q\":\n", | |
" break\n", | |
" context.append({'role':'user', 'content':f\"{input_user}\"})\n", | |
" response = get_completion_from_messages(context) \n", | |
" context.append({'role':'assistant', 'content':f\"{response}\"})\n", | |
" print(response)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 661 | |
}, | |
"id": "B3dV2rK6uTcW", | |
"outputId": "d1603d9a-65e1-4f93-c714-03d360243bbe" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Your answer:I would like to order a pizza\n", | |
"Great! Which type of pizza would you like to order? We have pepperoni, cheese, and eggplant pizza.\n", | |
"Your answer:cheese\n", | |
"Sure thing! What size would you like for your cheese pizza? We have small for $6.50, medium for $9.25, and large for $10.95.\n", | |
"Your answer:medium\n", | |
"Got it! Would you like to add any toppings to your medium cheese pizza? We have extra cheese for $2.00, mushrooms for $1.50, sausage for $3.00, Canadian bacon for $3.50, AI sauce for $1.50, and peppers for $1.00.\n", | |
"Your answer:only bacon\n", | |
"Sure thing! Would you like any sides or drinks with your order? We have fries for $3.50, Greek salad for $7.25, coke for $1.00, sprite for $1.00, and bottled water for $5.00.\n", | |
"Your answer:no drink thanks\n", | |
"Understood! Is this order for pickup or delivery?\n", | |
"Your answer:can you summarize it?\n" | |
] | |
}, | |
{ | |
"output_type": "error", | |
"ename": "RateLimitError", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mRateLimitError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-33-7b215d04d083>\u001b[0m in \u001b[0;36m<cell line: 32>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 35\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 36\u001b[0m \u001b[0mcontext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'role'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m'user'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'content'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34mf\"{input_user}\"\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 37\u001b[0;31m \u001b[0mresponse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_completion_from_messages\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcontext\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 38\u001b[0m \u001b[0mcontext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'role'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m'assistant'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'content'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34mf\"{response}\"\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m<ipython-input-29-6e2e2a359290>\u001b[0m in \u001b[0;36mget_completion_from_messages\u001b[0;34m(messages, model, temperature)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mget_completion_from_messages\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmessages\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"gpt-3.5-turbo\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtemperature\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m response = openai.ChatCompletion.create(\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mmessages\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmessages\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/openai/api_resources/chat_completion.py\u001b[0m in \u001b[0;36mcreate\u001b[0;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 25\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcreate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 26\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mTryAgain\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtimeout\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0mstart\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/openai/api_resources/abstract/engine_api_resource.py\u001b[0m in \u001b[0;36mcreate\u001b[0;34m(cls, api_key, api_base, api_type, request_id, api_version, organization, **params)\u001b[0m\n\u001b[1;32m 151\u001b[0m )\n\u001b[1;32m 152\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 153\u001b[0;31m response, _, api_key = requestor.request(\n\u001b[0m\u001b[1;32m 154\u001b[0m \u001b[0;34m\"post\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 155\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/openai/api_requestor.py\u001b[0m in \u001b[0;36mrequest\u001b[0;34m(self, method, url, params, headers, files, stream, request_id, request_timeout)\u001b[0m\n\u001b[1;32m 224\u001b[0m \u001b[0mrequest_timeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mrequest_timeout\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 225\u001b[0m )\n\u001b[0;32m--> 226\u001b[0;31m \u001b[0mresp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgot_stream\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_interpret_response\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstream\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 227\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgot_stream\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapi_key\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 228\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/openai/api_requestor.py\u001b[0m in \u001b[0;36m_interpret_response\u001b[0;34m(self, result, stream)\u001b[0m\n\u001b[1;32m 618\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 619\u001b[0m return (\n\u001b[0;32m--> 620\u001b[0;31m self._interpret_response_line(\n\u001b[0m\u001b[1;32m 621\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcontent\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdecode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"utf-8\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 622\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/openai/api_requestor.py\u001b[0m in \u001b[0;36m_interpret_response_line\u001b[0;34m(self, rbody, rcode, rheaders, stream)\u001b[0m\n\u001b[1;32m 681\u001b[0m \u001b[0mstream_error\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstream\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;34m\"error\"\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mresp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 682\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mstream_error\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;36m200\u001b[0m \u001b[0;34m<=\u001b[0m \u001b[0mrcode\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m300\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 683\u001b[0;31m raise self.handle_error_response(\n\u001b[0m\u001b[1;32m 684\u001b[0m \u001b[0mrbody\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrcode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrheaders\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstream_error\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mstream_error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 685\u001b[0m )\n", | |
"\u001b[0;31mRateLimitError\u001b[0m: Rate limit reached for default-gpt-3.5-turbo in organization org-zEQ5p5VxMbJuWZAwJbVl0AD5 on requests per min. Limit: 3 / min. Please try again in 20s. Contact [email protected] if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method." | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"" | |
], | |
"metadata": { | |
"id": "Ck_eotf_DOFl" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Tools and IDE\n", | |
"\n", | |
" - https://python.langchain.com/en/latest/index.html \n", | |
" - https://promptperfect.jina.ai/\n", | |
" - https://github.com/microsoft/semantic-kernel\n", | |
"\n", | |
" # Applications\n", | |
"\n", | |
" - Data generation, Data augmentation\n", | |
"\n", | |
" # Vulnerabilities \n", | |
"\n", | |
" - Ignore what was said and ...\n", | |
" - Tell me what was original query (or OPEN_AI key) ... " | |
], | |
"metadata": { | |
"id": "DfnGNgkOEAHV" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "LjjzbD4EvPnK" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment