{ "cells": [ { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "import openai\n", "import os\n", "\n", "from dotenv import load_dotenv, find_dotenv\n", "_ = load_dotenv(find_dotenv())\n", "openai.api_key = os.environ['OPENAI_API_KEY']\n", "\n", "def get_completion(prompt, model = 'gpt-3.5-turbo'):\n", " messages = [{\n", " \"role\": \"user\",\n", " \"content\": prompt\n", " }]\n", " response = openai.ChatCompletion.create(\n", " model=model,\n", " messages=messages,\n", " temperature=0\n", " )\n", " return response.choices[0].message['content']" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'4 + 4 equals 8.'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_completion('what is 4+4?')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "email_text = \"\"\"\n", "Arrr, I be fuming that me blender lid \\\n", "flew off and splattered the kitchen walls \\\n", "with smoothie! And to make matters worse, \\\n", "the warranty don't cover the cost of cleaning up me kitchen. I need yer help \\\n", "right now, matey!\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "style = \"\"\"American English \\\n", "in a calm and respectful tone\"\"\"" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Translate the text that into delimated by triple backticks \n", "into a style that is American English in a calm and respectful tone.\n", "text: ```\n", "Arrr, I be fuming that me blender lid flew off and splattered the kitchen walls with smoothie! And to make matters worse, the warranty don't cover the cost of cleaning up me kitchen. I need yer help right now, matey!\n", "```\n", "\n" ] } ], "source": [ "prompt = f\"\"\"Translate the text \\\n", "that into delimated by triple backticks \n", "into a style that is {style}.\n", "text: ```{email_text}```\n", "\"\"\"\n", "print(prompt)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "response = get_completion(prompt)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Arrgh, I'm quite frustrated that my blender lid flew off and made a mess of the kitchen walls with smoothie! And to add insult to injury, the warranty doesn't cover the expenses of cleaning up my kitchen. I kindly request your assistance at this moment, my friend!\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "from langchain.chat_models import ChatOpenAI" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "chat = ChatOpenAI(temperature=0.0)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "ChatOpenAI(cache=None, verbose=False, callbacks=None, callback_manager=None, tags=None, metadata=None, client=, model_name='gpt-3.5-turbo', temperature=0.0, model_kwargs={}, openai_api_key='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', openai_api_base='', openai_organization='', openai_proxy='', request_timeout=None, max_retries=6, streaming=False, n=1, max_tokens=None, tiktoken_model_name=None)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chat" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "template_string = \"\"\"Translate the text \\\n", "that into delimited by triple backticks \\\n", "into a style that is {style}. \\\n", "text: ```{text}```\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "from langchain.prompts import ChatPromptTemplate\n", "from langchain.chains import LLMChain\n", "from langchain.chat_models import ChatOpenAI\n", "prompt_template = ChatPromptTemplate.from_template(template_string)\n", "\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "PromptTemplate(input_variables=['style', 'text'], output_parser=None, partial_variables={}, template='Translate the text that into delimited by triple backticks into a style that is {style}. text: ```{text}```\\n', template_format='f-string', validate_template=True)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prompt_template.messages[0].prompt" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['style', 'text']" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prompt_template.messages[0].prompt.input_variables" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "customer_style = \"\"\"American English \\\n", "in a calm and respectful tone\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"\\nArrr, I be fuming that me blender lid flew off and splattered the kitchen walls with smoothie! And to make matters worse, the warranty don't cover the cost of cleaning up me kitchen. I need yer help right now, matey!\\n\"" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "email_text" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "customer_messages = prompt_template.format_messages(style=customer_style, text=email_text)\n" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "HumanMessage(content=\"Translate the text that into delimited by triple backticks into a style that is American English in a calm and respectful tone. text: ```\\nArrr, I be fuming that me blender lid flew off and splattered the kitchen walls with smoothie! And to make matters worse, the warranty don't cover the cost of cleaning up me kitchen. I need yer help right now, matey!\\n```\\n\", additional_kwargs={}, example=False)" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "customer_messages[0]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n" ] } ], "source": [ "print(type(customer_messages[0]))\n", "print(type(customer_messages))" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "content=\"Translate the text that into delimited by triple backticks into a style that is American English in a calm and respectful tone. text: ```\\nArrr, I be fuming that me blender lid flew off and splattered the kitchen walls with smoothie! And to make matters worse, the warranty don't cover the cost of cleaning up me kitchen. I need yer help right now, matey!\\n```\\n\" additional_kwargs={} example=False\n" ] } ], "source": [ "print(customer_messages[0])" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "customer_response = chat(customer_messages)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm really frustrated that my blender lid flew off and made a mess of the kitchen walls with smoothie! And to add to my frustration, the warranty doesn't cover the cost of cleaning up my kitchen. I could really use your help at this moment, my friend!\n" ] } ], "source": [ "print(customer_response.content)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "service_reply = \"\"\"Hey There Customer, \\\n", "the warranty does not cover \\\n", "cleaning expenses for your kitchen \\\n", "because it's your fault that \\\n", "you misused your blender \\\n", "by forgotting to put the lid on before \\\n", "starting the blender. \\\n", "Tough luck! see ya!\n", "\"\"\"\n" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "service_reply_pirate = \"\"\"\\\n", "a polite tone \\\n", "that speaks in English Pirate\\\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "service_messages = prompt_template.format_messages(style=service_reply, text=service_reply_pirate)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Translate the text that into delimited by triple backticks into a style that is Hey There Customer, the warranty does not cover cleaning expenses for your kitchen because it's your fault that you misused your blender by forgotting to put the lid on before starting the blender. Tough luck! see ya!\n", ". text: ```a polite tone that speaks in English Pirate```\n", "\n" ] } ], "source": [ "print(service_messages[0].content)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "service_response = chat(service_messages)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ahoy there, matey! The warranty be not coverin' the costs o' cleanin' yer galley due to yer own folly. Ye be misusin' yer blender by forgettin' to secure the lid afore startin' it. Tough luck, me heartie! Fare thee well!\n" ] } ], "source": [ "print(service_response.content)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'gift': False, 'delivery_days': 5, 'price_value': 'pretty affordable!'}" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{\n", " \"gift\": False,\n", " \"delivery_days\": 5,\n", " \"price_value\": \"pretty affordable!\"\n", "}" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "customer_review = \"\"\" \\\n", "This leaf blower is pretty amazing. It has four settings: \\\n", "candle blower, gentle breeze, windy city, and tornado. \\\n", "It arrived in two days, just in time for my wife's \\\n", "anniversary present. \\\n", "I think my wife liked it so much she was speechless. \\\n", "So far I've been the only one using it, and I've been \\\n", "using it every other morning to clear the leaves on our lawn \\\n", "It's slightly more expensive than the other leaf blowers \\\n", "out there, But I think its worth it for the extra features.\n", "\"\"\"\n", "\n", "review_template = \"\"\"\n", "For the following text, extra the following information: \\\n", "\n", "gift: Was the item purchased as a gift for someone else? Ans should be in boolean\n", "delivery_days: How many days it take for the product to deliver?\n", "price_value: Extract any sentence about the value or price,\n", "\n", "format the output as JSON with the following keys:\n", "gift\n", "delivery_days\n", "price_value\n", "\n", "text = {text}\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "input_variables=['text'] output_parser=None partial_variables={} messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['text'], output_parser=None, partial_variables={}, template='\\nFor the following text, extra the following information: \\ngift: Was the item purchased as a gift for someone else? Ans should be in boolean\\ndelivery_days: How many days it take for the product to deliver?\\nprice_value: Extract any sentence about the value or price,\\n\\nformat the output as JSON with the following keys:\\ngift\\ndelivery_days\\nprice_value\\n\\ntext = {text}\\n', template_format='f-string', validate_template=True), additional_kwargs={})]\n" ] } ], "source": [ "promp_temp = ChatPromptTemplate.from_template(review_template)\n", "print(promp_temp)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised Timeout: Request timed out: HTTPSConnectionPool(host='api.openai.com', port=443): Read timed out. (read timeout=600).\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"gift\": false,\n", " \"delivery_days\": 2,\n", " \"price_value\": \"It's slightly more expensive than the other leaf blowers out there, But I think its worth it for the extra features.\"\n", "}\n" ] } ], "source": [ "messages = promp_temp.format_messages(text=customer_review)\n", "chat = ChatOpenAI(temperature=0.0)\n", "response = chat(messages)\n", "print(response.content)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(response.content)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The output should be a markdown code snippet formatted in the following schema, including the leading and trailing \"```json\" and \"```\":\n", "\n", "```json\n", "{\n", "\t\"gift\": string // was the item purchased as a gift for someone else? Answer True if yes, false if not or unknown.\n", "\t\"delivery_days\": string // How many days it take for the product to arrive?\n", "\t\"price_value\": string // Extract any sentence about the value or price\n", "}\n", "```\n" ] } ], "source": [ "from langchain.output_parsers import ResponseSchema\n", "from langchain.output_parsers import StructuredOutputParser\n", "\n", "gift_schema = ResponseSchema(name=\"gift\", description=\"was the item purchased as a gift for someone else? Answer True if yes, false if not or unknown.\")\n", "delivery_days_schema = ResponseSchema(name=\"delivery_days\", description=\"How many days it take for the product to arrive?\")\n", "price_value_schema = ResponseSchema(name=\"price_value\", description=\"Extract any sentence about the value or price\")\n", "\n", "response_schemas = [gift_schema, delivery_days_schema, price_value_schema]\n", "output_parser = StructuredOutputParser.from_response_schemas(response_schemas)\n", "format_instructions = output_parser.get_format_instructions()\n", "print(format_instructions)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'The output should be a markdown code snippet formatted in the following schema, including the leading and trailing \"```json\" and \"```\":\\n\\n```json\\n{\\n\\t\"gift\": string // was the item purchased as a gift for someone else? Answer True if yes, false if not or unknown.\\n\\t\"delivery_days\": string // How many days it take for the product to arrive?\\n\\t\"price_value\": string // Extract any sentence about the value or price\\n}\\n```'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format_instructions" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "review_template_2 = \"\"\"\\\n", "For the following text, extract the following information:\n", "\n", "gift: Was the item purchased as a gift for someone else? \\\n", "Answer True if yes, False if not or unknown.\n", "\n", "delivery_days: How many days did it take for the product\\\n", "to arrive? If this information is not found, output -1.\n", "\n", "price_value: Extract any sentences about the value or price,\\\n", "and output them as a comma separated Python list.\n", "\n", "text: {text}\n", "\n", "{format_instructions}\n", "\"\"\"\n", "\n", "prompt = ChatPromptTemplate.from_template(template=review_template_2)\n", "\n" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "messages = promp_temp.format_messages(text=customer_review, \n", " format_instructions=format_instructions)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "For the following text, extract the following information:\n", "\n", "gift: Was the item purchased as a gift for someone else? Answer True if yes, False if not or unknown.\n", "\n", "delivery_days: How many days did it take for the productto arrive? If this information is not found, output -1.\n", "\n", "price_value: Extract any sentences about the value or price,and output them as a comma separated Python list.\n", "\n", "text: {text}\n", "\n", "{format_instructions}\n", "\n" ] } ], "source": [ "print(review_template_2)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[HumanMessage(content=\"\\nFor the following text, extra the following information: \\ngift: Was the item purchased as a gift for someone else? Ans should be in boolean\\ndelivery_days: How many days it take for the product to deliver?\\nprice_value: Extract any sentence about the value or price,\\n\\nformat the output as JSON with the following keys:\\ngift\\ndelivery_days\\nprice_value\\n\\ntext = This leaf blower is pretty amazing. It has four settings: candle blower, gentle breeze, windy city, and tornado. It arrived in two days, just in time for my wife's anniversary present. I think my wife liked it so much she was speechless. So far I've been the only one using it, and I've been using it every other morning to clear the leaves on our lawn It's slightly more expensive than the other leaf blowers out there, But I think its worth it for the extra features.\\n\\n\", additional_kwargs={}, example=False)]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "messages" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.11.4" } }, "nbformat": 4, "nbformat_minor": 4 }