{ "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", " | brand | \n", "product_category | \n", "sub_product_category | \n", "strain_name | \n", "
---|---|---|---|---|
0 | \n", "Cookies | \n", "Edibles | \n", "Gummy | \n", "LondonPoundCake75 | \n", "