Spaces:
Runtime error
Runtime error
File size: 11,117 Bytes
73588d1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
{
"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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>brand</th>\n",
" <th>product_category</th>\n",
" <th>sub_product_category</th>\n",
" <th>strain_name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Cookies</td>\n",
" <td>Edibles</td>\n",
" <td>Gummy</td>\n",
" <td>LondonPoundCake75</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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
}
|