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