Fix bug #884 - Port How_to_finetune_chat_models to API V1 (#885)

pull/908/head
Gabor Cselle 6 months ago committed by GitHub
parent e766dfae18
commit 9ab34baa84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -42,7 +42,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 12,
"id": "6e1f4403-37e1-4115-a215-12fd7daa1eb6", "id": "6e1f4403-37e1-4115-a215-12fd7daa1eb6",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -53,7 +53,8 @@
"import pandas as pd\n", "import pandas as pd\n",
"from pprint import pprint\n", "from pprint import pprint\n",
"\n", "\n",
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\", \"\")" "OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\", \"\")\n",
"openai.api_key = OPENAI_API_KEY"
] ]
}, },
{ {
@ -67,7 +68,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 3,
"id": "f57ebc23-14b7-47f9-90b8-1d791ccfc9bc", "id": "f57ebc23-14b7-47f9-90b8-1d791ccfc9bc",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -151,22 +152,22 @@
"</div>" "</div>"
], ],
"text/plain": [ "text/plain": [
" title ingredients \\\n", " title ingredients \n",
"0 No-Bake Nut Cookies [\"1 c. firmly packed brown sugar\", \"1/2 c. eva... \n", "0 No-Bake Nut Cookies [\"1 c. firmly packed brown sugar\", \"1/2 c. eva... \\\n",
"1 Jewell Ball'S Chicken [\"1 small jar chipped beef, cut up\", \"4 boned ... \n", "1 Jewell Ball'S Chicken [\"1 small jar chipped beef, cut up\", \"4 boned ... \n",
"2 Creamy Corn [\"2 (16 oz.) pkg. frozen corn\", \"1 (8 oz.) pkg... \n", "2 Creamy Corn [\"2 (16 oz.) pkg. frozen corn\", \"1 (8 oz.) pkg... \n",
"3 Chicken Funny [\"1 large whole chicken\", \"2 (10 1/2 oz.) cans... \n", "3 Chicken Funny [\"1 large whole chicken\", \"2 (10 1/2 oz.) cans... \n",
"4 Reeses Cups(Candy) [\"1 c. peanut butter\", \"3/4 c. graham cracker ... \n", "4 Reeses Cups(Candy) [\"1 c. peanut butter\", \"3/4 c. graham cracker ... \n",
"\n", "\n",
" directions \\\n", " directions \n",
"0 [\"In a heavy 2-quart saucepan, mix brown sugar... \n", "0 [\"In a heavy 2-quart saucepan, mix brown sugar... \\\n",
"1 [\"Place chipped beef on bottom of baking dish.... \n", "1 [\"Place chipped beef on bottom of baking dish.... \n",
"2 [\"In a slow cooker, combine all ingredients. C... \n", "2 [\"In a slow cooker, combine all ingredients. C... \n",
"3 [\"Boil and debone chicken.\", \"Put bite size pi... \n", "3 [\"Boil and debone chicken.\", \"Put bite size pi... \n",
"4 [\"Combine first four ingredients and press in ... \n", "4 [\"Combine first four ingredients and press in ... \n",
"\n", "\n",
" link source \\\n", " link source \n",
"0 www.cookbooks.com/Recipe-Details.aspx?id=44874 www.cookbooks.com \n", "0 www.cookbooks.com/Recipe-Details.aspx?id=44874 www.cookbooks.com \\\n",
"1 www.cookbooks.com/Recipe-Details.aspx?id=699419 www.cookbooks.com \n", "1 www.cookbooks.com/Recipe-Details.aspx?id=699419 www.cookbooks.com \n",
"2 www.cookbooks.com/Recipe-Details.aspx?id=10570 www.cookbooks.com \n", "2 www.cookbooks.com/Recipe-Details.aspx?id=10570 www.cookbooks.com \n",
"3 www.cookbooks.com/Recipe-Details.aspx?id=897570 www.cookbooks.com \n", "3 www.cookbooks.com/Recipe-Details.aspx?id=897570 www.cookbooks.com \n",
@ -405,7 +406,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 13,
"id": "69462d9e-e6bd-49b9-a064-9eae4ea5b7a8", "id": "69462d9e-e6bd-49b9-a064-9eae4ea5b7a8",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -413,21 +414,24 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Training file ID: file-jcdvNl27iuBMZfwi4q30IIka\n", "Training file ID: file-PVkEstNM2WWd1OQe3Hp3tC5E\n",
"Validation file ID: file-O144OIHkZ1xjB32ednBmbOXP\n" "Validation file ID: file-WSdTwLYrKxNhKi1WWGjxXi87\n"
] ]
} }
], ],
"source": [ "source": [
"training_response = openai.File.create(\n", "with open(training_file_name, \"rb\") as training_fd:\n",
" file=open(training_file_name, \"rb\"), purpose=\"fine-tune\"\n", " training_response = openai.files.create(\n",
")\n", " file=training_fd, purpose=\"fine-tune\"\n",
"training_file_id = training_response[\"id\"]\n", " )\n",
"\n", "\n",
"validation_response = openai.File.create(\n", "training_file_id = training_response.id\n",
" file=open(validation_file_name, \"rb\"), purpose=\"fine-tune\"\n", "\n",
")\n", "with open(validation_file_name, \"rb\") as validation_fd:\n",
"validation_file_id = validation_response[\"id\"]\n", " validation_response = openai.files.create(\n",
" file=validation_fd, purpose=\"fine-tune\"\n",
" )\n",
"validation_file_id = validation_response.id\n",
"\n", "\n",
"print(\"Training file ID:\", training_file_id)\n", "print(\"Training file ID:\", training_file_id)\n",
"print(\"Validation file ID:\", validation_file_id)" "print(\"Validation file ID:\", validation_file_id)"
@ -447,7 +451,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 14,
"id": "05541ceb-5628-447e-962d-7e57c112439c", "id": "05541ceb-5628-447e-962d-7e57c112439c",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -455,23 +459,23 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Job ID: ftjob-9xVzrp0Oem9rWj2UFWDcFLqT\n", "Job ID: ftjob-bIVrnhnZEEizSP7rqWsRwv2R\n",
"Status: created\n" "Status: validating_files\n"
] ]
} }
], ],
"source": [ "source": [
"response = openai.FineTuningJob.create(\n", "response = openai.fine_tuning.jobs.create(\n",
" training_file=training_file_id,\n", " training_file=training_file_id,\n",
" validation_file=validation_file_id,\n", " validation_file=validation_file_id,\n",
" model=\"gpt-3.5-turbo\",\n", " model=\"gpt-3.5-turbo\",\n",
" suffix=\"recipe-ner\",\n", " suffix=\"recipe-ner\",\n",
")\n", ")\n",
"\n", "\n",
"job_id = response[\"id\"]\n", "job_id = response.id\n",
"\n", "\n",
"print(\"Job ID:\", response[\"id\"])\n", "print(\"Job ID:\", response.id)\n",
"print(\"Status:\", response[\"status\"])" "print(\"Status:\", response.status)"
] ]
}, },
{ {
@ -488,7 +492,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 21,
"id": "d7392f48", "id": "d7392f48",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -496,18 +500,18 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Job ID: ftjob-9xVzrp0Oem9rWj2UFWDcFLqT\n", "Job ID: ftjob-bIVrnhnZEEizSP7rqWsRwv2R\n",
"Status: running\n", "Status: running\n",
"Trained Tokens: None\n" "Trained Tokens: None\n"
] ]
} }
], ],
"source": [ "source": [
"response = openai.FineTuningJob.retrieve(job_id)\n", "response = openai.fine_tuning.jobs.retrieve(job_id)\n",
"\n", "\n",
"print(\"Job ID:\", response[\"id\"])\n", "print(\"Job ID:\", response.id)\n",
"print(\"Status:\", response[\"status\"])\n", "print(\"Status:\", response.status)\n",
"print(\"Trained Tokens:\", response[\"trained_tokens\"])\n" "print(\"Trained Tokens:\", response.trained_tokens)\n"
] ]
}, },
{ {
@ -520,7 +524,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 47,
"id": "08cace28", "id": "08cace28",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -528,51 +532,37 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Created fine-tune: ftjob-9xVzrp0Oem9rWj2UFWDcFLqT\n", "Step 131/303: training loss=0.25, validation loss=0.37\n",
"Fine tuning job started\n", "Step 141/303: training loss=0.00, validation loss=0.19\n",
"Step 10: training loss=2.41\n", "Step 151/303: training loss=0.00, validation loss=0.11\n",
"Step 20: training loss=0.06\n", "Step 161/303: training loss=0.00, validation loss=0.06\n",
"Step 30: training loss=0.38\n", "Step 171/303: training loss=0.10, validation loss=0.00\n",
"Step 40: training loss=0.09\n", "Step 181/303: training loss=0.00, validation loss=0.38\n",
"Step 50: training loss=0.19\n", "Step 191/303: training loss=0.00, validation loss=0.15\n",
"Step 60: training loss=0.10\n", "Step 201/303: training loss=0.06, validation loss=0.64\n",
"Step 70: training loss=0.00\n", "Step 211/303: training loss=0.00, validation loss=0.04\n",
"Step 80: training loss=0.01\n", "Step 221/303: training loss=0.59, validation loss=0.85\n",
"Step 90: training loss=0.72\n", "Step 231/303: training loss=0.00, validation loss=0.00\n",
"Step 100: training loss=0.13\n", "Step 241/303: training loss=0.04, validation loss=0.42\n",
"Step 110: training loss=0.15\n", "Step 251/303: training loss=0.00, validation loss=0.14\n",
"Step 120: training loss=0.00\n", "Step 261/303: training loss=0.00, validation loss=0.00\n",
"Step 130: training loss=0.47\n", "Step 271/303: training loss=0.15, validation loss=0.50\n",
"Step 140: training loss=0.00\n", "Step 281/303: training loss=0.00, validation loss=0.72\n",
"Step 150: training loss=0.10\n", "Step 291/303: training loss=0.08, validation loss=0.16\n",
"Step 160: training loss=0.06\n", "Step 301/303: training loss=0.00, validation loss=1.76\n",
"Step 170: training loss=0.03\n", "New fine-tuned model created: ft:gpt-3.5-turbo-0613:personal:recipe-ner:8PjmcwDH\n",
"Step 180: training loss=0.08\n", "The job has successfully completed\n"
"Step 190: training loss=0.04\n",
"Step 200: training loss=0.01\n",
"Step 210: training loss=0.03\n",
"Step 220: training loss=0.00\n",
"Step 230: training loss=0.08\n",
"Step 240: training loss=0.00\n",
"Step 250: training loss=0.00\n",
"Step 260: training loss=0.00\n",
"Step 270: training loss=0.00\n",
"Step 280: training loss=0.00\n",
"Step 290: training loss=0.00\n",
"Step 300: training loss=0.60\n",
"New fine-tuned model created: ft:gpt-3.5-turbo-0613:openai:recipe-ner:7qTvyJ81\n",
"Fine-tuning job successfully completed\n"
] ]
} }
], ],
"source": [ "source": [
"response = openai.FineTuningJob.list_events(id=job_id, limit=50)\n", "response = openai.fine_tuning.jobs.list_events(job_id)\n",
"\n", "\n",
"events = response[\"data\"]\n", "events = response.data\n",
"events.reverse()\n", "events.reverse()\n",
"\n", "\n",
"for event in events:\n", "for event in events:\n",
" print(event[\"message\"])" " print(event.message)"
] ]
}, },
{ {
@ -585,7 +575,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 48,
"id": "40b28c26", "id": "40b28c26",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -593,13 +583,13 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Fine-tuned model ID: ft:gpt-3.5-turbo-0613:openai:recipe-ner:7qTvyJ81\n" "Fine-tuned model ID: ft:gpt-3.5-turbo-0613:personal:recipe-ner:8PjmcwDH\n"
] ]
} }
], ],
"source": [ "source": [
"response = openai.FineTuningJob.retrieve(job_id)\n", "response = openai.fine_tuning.jobs.retrieve(job_id)\n",
"fine_tuned_model_id = response[\"fine_tuned_model\"]\n", "fine_tuned_model_id = response.fine_tuned_model\n",
"\n", "\n",
"if fine_tuned_model_id is None: \n", "if fine_tuned_model_id is None: \n",
" raise RuntimeError(\"Fine-tuned model ID not found. Your job has likely not been completed yet.\")\n", " raise RuntimeError(\"Fine-tuned model ID not found. Your job has likely not been completed yet.\")\n",
@ -625,7 +615,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 49,
"id": "1c7de631-b68f-4eff-9ae7-051641579c2b", "id": "1c7de631-b68f-4eff-9ae7-051641579c2b",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -636,11 +626,12 @@
"[{'content': 'You are a helpful recipe assistant. You are to extract the '\n", "[{'content': 'You are a helpful recipe assistant. You are to extract the '\n",
" 'generic ingredients from each of the recipes provided.',\n", " 'generic ingredients from each of the recipes provided.',\n",
" 'role': 'system'},\n", " 'role': 'system'},\n",
" {'content': 'Title: Pancakes\\n'\n", " {'content': 'Title: Beef Brisket\\n'\n",
" '\\n'\n", " '\\n'\n",
" 'Ingredients: [\"1 c. flour\", \"1 tsp. soda\", \"1 tsp. salt\", \"1 '\n", " 'Ingredients: [\"4 lb. beef brisket\", \"1 c. catsup\", \"1 c. water\", '\n",
" 'Tbsp. sugar\", \"1 egg\", \"3 Tbsp. margarine, melted\", \"1 c. '\n", " '\"1/2 onion, minced\", \"2 Tbsp. cider vinegar\", \"1 Tbsp. prepared '\n",
" 'buttermilk\"]\\n'\n", " 'horseradish\", \"1 Tbsp. prepared mustard\", \"1 tsp. salt\", \"1/2 '\n",
" 'tsp. pepper\"]\\n'\n",
" '\\n'\n", " '\\n'\n",
" 'Generic ingredients: ',\n", " 'Generic ingredients: ',\n",
" 'role': 'user'}]\n" " 'role': 'user'}]\n"
@ -660,7 +651,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 50,
"id": "1a1d2589", "id": "1a1d2589",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -668,15 +659,15 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"[\"flour\", \"soda\", \"salt\", \"sugar\", \"egg\", \"margarine\", \"buttermilk\"]\n" "[\"beef brisket\", \"catsup\", \"water\", \"onion\", \"cider vinegar\", \"horseradish\", \"mustard\", \"salt\", \"pepper\"]\n"
] ]
} }
], ],
"source": [ "source": [
"response = openai.ChatCompletion.create(\n", "response = openai.chat.completions.create(\n",
" model=fine_tuned_model_id, messages=test_messages, temperature=0, max_tokens=500\n", " model=fine_tuned_model_id, messages=test_messages, temperature=0, max_tokens=500\n",
")\n", ")\n",
"print(response[\"choices\"][0][\"message\"][\"content\"])" "print(response.choices[0].message.content)"
] ]
}, },
{ {
@ -706,7 +697,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.9.13" "version": "3.11.3"
} }
}, },
"nbformat": 4, "nbformat": 4,

Loading…
Cancel
Save