{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# import OpenAIChatCompletions class from openai_chat_completion.py file and compare_completion_and_prediction function from util.py file\n", "from openai_chat_completion import OpenAIChatCompletions" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import os\n", "from dotenv import load_dotenv\n", "load_dotenv()\n", "\n", "import openai\n", "\n", "# set OPENAI_API_KEY environment variable from .env file\n", "openai.api_key = os.getenv(\"OPENAI_API_KEY\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'I am going to provide marijuana product information. Using the information I provide, I want you to provide me with the following information about the product.\\n\\n - Brand (brand)\\n - product category (product_category)\\n - sub product category (sub_product_category)\\n - strain name (strain_name)\\n\\nAdditional requirements:\\n\\n- DO NOT EXPLAIN YOUR SELF \\n\\nProduct data below '" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "system_message = open('../prompts/gpt4-system-message.txt', 'r').read()\n", "system_message" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I am going to provide marijuana product information. Using the information I provide, I want you to provide me with the following information about the product.\n", "\n", " - Brand (brand)\n", " - product category (product_category)\n", " - sub product category (sub_product_category)\n", " - strain name (strain_name)\n", "\n", "Additional requirements:\n", "\n", "- DO NOT EXPLAIN YOUR SELF \n", "\n", "Product data below \n" ] } ], "source": [ "print(system_message)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "chatInstance = OpenAIChatCompletions(system_message=system_message)\n", "chat_response = chatInstance.openai_chat_completion(prompt=\"Cookies - London Pound Cake 75 - Gummy - 10ct - 100mg\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "- Brand: Cookies\n", "- Product Category: Edibles\n", "- Sub Product Category: Gummy\n", "- Strain Name: London Pound Cake 75\n" ] } ], "source": [ "print(chat_response['choices'][0]['message']['content'])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "system_message2 = \"\"\"\n", "I am going to provide marijuana product information. Using the information I provide, I want you to provide me with the following information about the product.\n", "\n", " - Brand (brand)\n", " - product category (product_category)\n", " - sub product category (sub_product_category)\n", " - strain name (strain_name)\n", "\n", "Additional requirements:\n", "\n", "DO NOT EXPLAIN YOUR SELF \n", "Format output in JSON format\n", "\n", "example output:\n", "{\"col1\": \"value1\", \"col2\": \"value2\", \"col3\": \"value3\"}\n", "\n", "---\n", "\n", "Product data below \n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"brand\": \"Cookies\", \"product_category\": \"Edibles\", \"sub_product_category\": \"Gummy\", \"strain_name\": \"London Pound Cake 75\"}\n" ] } ], "source": [ "chatInstance2 = OpenAIChatCompletions(system_message=system_message2)\n", "chat_response2 = chatInstance2.openai_chat_completion(prompt=\"Cookies - London Pound Cake 75 - Gummy - 10ct - 100mg\")\n", "print(chat_response2['choices'][0]['message']['content'])" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "chat_response2_content = chat_response2['choices'][0]['message']['content']" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'brand': 'Cookies',\n", " 'product_category': 'Edibles',\n", " 'sub_product_category': 'Gummy',\n", " 'strain_name': 'LondonPoundCake75'}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# write function that takes string in the form of json and returns a dictionary\n", "\n", "def json_to_dict(json_string):\n", " json_string = json_string.replace('\\n', '')\n", " json_string = json_string.replace('\\t', '')\n", " json_string = json_string.replace(' ', '')\n", " json_string = json_string.replace('\"', '')\n", " json_string = json_string.replace('{', '')\n", " json_string = json_string.replace('}', '')\n", " json_string = json_string.replace(':', ',')\n", " json_string = json_string.split(',')\n", " return {\n", " json_string[i]: json_string[i + 1]\n", " for i in range(0, len(json_string), 2)\n", " }\n", "\n", "output_as_json = json_to_dict(chat_response2_content)\n", "assert type(output_as_json) == dict\n", "output_as_json" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
brandproduct_categorysub_product_categorystrain_name
0CookiesEdiblesGummyLondonPoundCake75
\n", "
" ], "text/plain": [ " brand product_category sub_product_category strain_name\n", "0 Cookies Edibles Gummy LondonPoundCake75" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# write a function that takes a dictionary and returns a dataframe\n", "import pandas as pd\n", "\n", "def dict_to_df(dictionary):\n", " return pd.DataFrame(dictionary, index=[0])\n", "\n", "dict_to_df(output_as_json)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"brand\": \"Cookies\", \"product_category\": \"Edibles\", \"sub_product_category\": \"Gummy\", \"strain_name\": \"London Pound Cake 75\"}\n", "{\"brand\": \"Berlin\", \"product_category\": \"Edibles\", \"sub_product_category\": \"Brownies\", \"strain_name\": \"Chocolate Hazelnut 69\"}\n" ] } ], "source": [ "chat_response2a = chatInstance2.openai_chat_completion(prompt=\"Cookies - London Pound Cake 75 - Gummy - 10ct - 100mg\")\n", "chat_response2b = chatInstance2.openai_chat_completion(prompt=\"Brownies - Berlin Chocolate Hazelnut 69 - Flower - 1ct - 69mg\")\n", "print(chat_response2a['choices'][0]['message']['content'])\n", "print(chat_response2b['choices'][0]['message']['content'])" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def join_dicts(dict1, dict2):\n", " return {key:[dict1[key], dict2[key]] for key in dict1}" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'brand': ['Cookies', 'Berlin'],\n", " 'product_category': ['Edibles', 'Edibles'],\n", " 'sub_product_category': ['Gummy', 'Brownies'],\n", " 'strain_name': ['LondonPoundCake75', 'ChocolateHazelnut69']}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out2a_as_json = json_to_dict(chat_response2a['choices'][0]['message']['content'])\n", "out2b_as_json = json_to_dict(chat_response2b['choices'][0]['message']['content'])\n", "\n", "out3_as_json = join_dicts(out2a_as_json, out2b_as_json)\n", "out3_as_json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try via util.py File" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "from util import json_to_dict, join_dicts" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'brand': ['Cookies', 'Berlin'],\n", " 'product_category': ['Edibles', 'Edibles'],\n", " 'sub_product_category': ['Gummy', 'Brownies'],\n", " 'strain_name': ['LondonPoundCake75', 'ChocolateHazelnut69']}" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out2a_as_json = json_to_dict(chat_response2a['choices'][0]['message']['content'])\n", "out2b_as_json = json_to_dict(chat_response2b['choices'][0]['message']['content'])\n", "\n", "out3_as_json = join_dicts(out2a_as_json, out2b_as_json)\n", "out3_as_json" ] } ], "metadata": { "kernelspec": { "display_name": "kd-llm-dc", "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.10.11" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }