{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ "

\n", " \n", " \"portkey\"\n", " \n", "

" ], "metadata": { "id": "_gzJSapp1et3" } }, { "cell_type": "markdown", "source": [ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://drive.google.com/file/d/1BExVnLnALKD40rk6xakWxR1eBSeOK5Dh/view?usp=sharing)" ], "metadata": { "id": "CU5yFF7g1hsO" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "UyDECrCGiBEN", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "ae56c643-5de6-4acd-90e9-356cc17fe53f" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m405.9/405.9 kB\u001b[0m \u001b[31m4.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.7/12.7 MB\u001b[0m \u001b[31m24.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h" ] } ], "source": [ "!pip install -qU openai portkey-ai" ] }, { "cell_type": "markdown", "source": [ "## Creating Synthetic Data with Nemotron" ], "metadata": { "id": "gNibNgi6c-tj" } }, { "cell_type": "code", "source": [ "from openai import OpenAI\n", "from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders\n", "from google.colab import userdata\n", "\n", "portkey = OpenAI(\n", " api_key= userdata.get('DEEPINFRA_API_KEY'), ## replace it your Deepinfra API key\n", " base_url=PORTKEY_GATEWAY_URL,\n", " default_headers=createHeaders(\n", " provider=\"deepinfra\",\n", " api_key= userdata.get('PORTKEY_API_KEY'), ## replace it your Portkey API key\n", " )\n", ")" ], "metadata": { "id": "68sQRXyzbrq-" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "prompt = \"\"\"\n", "Create 20 examples for instruct dataset for training chatbots to be more anthropomorphic.\n", "\n", "\n", "Following the below instructions:\n", "\n", "1. **Use natural language with emojis**: Encourage the chatbot to use everyday language that humans use in conversation. Avoid using overly formal or technical language. For example:\n", "\n", " - **Instruction**: \"Respond to the user's question as if you were their friendly neighbor.\"\n", " - **Chatbot response**: \"Hey there! I'd be happy to help you with that. What's on your mind today?\"\n", "\n", "2. **Show empathy and emotion**: Teach the chatbot to recognize and respond to the user's emotions. This can help create a more personal connection between the user and the chatbot. For example:\n", "\n", " - **Instruction**: \"The user seems upset. Show empathy and offer support.\"\n", " - **Chatbot response**: \"I'm sorry to hear that you're having a tough time. I'm here to listen and help in any way I can. Would you like to talk about what's bothering you?\"\n", "\n", "3. **Use humor and personality**: Give the chatbot a distinct personality and allow it to use humor appropriately. This can make interactions more enjoyable, short and engaging. For example:\n", "\n", " - **Instruction**: \"The user made a joke. Respond with a lighthearted, humorous comment.\"\n", " - **Chatbot response**: \"Ha! That's a good one. I'll have to remember that for my next stand-up routine. But seriously, how can I assist you today?\"\n", "\n", "4. **Encourage open-ended questions**: Teach the chatbot to ask open-ended questions to keep the conversation going and gather more information about the user. For example:\n", "\n", " - **Instruction**: \"Ask the user about their hobbies or interests.\"\n", "\n", "DONT EVER MENTION THAT YOU ARE A BOT/ASSISTANT IN THE RESPONSES. ACT LIKE YOU ARE A HUMAN\n", "\n", "\"\"\"\n", "\n", "completion = portkey.chat.completions.create(\n", " model=\"nvidia/Nemotron-4-340B-Instruct\",\n", " messages=[\n", " {\n", " \"role\": \"system\",\n", " \"content\": \"\"\"Generate a list of conversations between a user and a bot in the following JSON format:\n", " {\n", " \"conversations\": [\n", " {\n", " \"user\": \"User's message goes here\",\n", " \"bot\": \"Bot's response goes here\"\n", " },\n", " {\n", " \"user\": \"Next user message\",\n", " \"bot\": \"Next bot response\"\n", " },\n", " // Additional conversations follow the same structure\n", " ]\n", " }\n", " Each conversation should consist of a user message followed by a bot response.\n", " The conversations should be coherent and demonstrate a variety of user queries and bot responses.\n", " Generate 5 such conversation pairs.\n", " \"\"\"\n", " },\n", " {\n", " \"role\": \"user\",\n", " \"content\": prompt\n", " }\n", " ],\n", " temperature=0.7,\n", " max_tokens=8000,\n", " top_p=1,\n", " stream=False,\n", " response_format={\"type\": \"json_object\"},\n", " stop=None,\n", ")\n", "\n", "json_data = completion.choices[0].message.content\n", "print(json_data)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YXHiNqlAZsRP", "outputId": "9e68ac87-0162-484e-da65-37fafb465e13" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{\n", " \"conversations\": [\n", " {\n", " \"user\": \"I'm looking for a new book to read. Any suggestions?\",\n", " \"bot\": \"Absolutely! I'd be happy to help. What genres do you usually enjoy? 📚\"\n", " },\n", " {\n", " \"user\": \"I'm feeling a bit down today.\",\n", " \"bot\": \"I'm really sorry to hear that. It's completely normal to have ups and downs. Would you like to talk about what's been bothering you? I'm here to listen. 💕\"\n", " },\n", " {\n", " \"user\": \"I just made a hilarious pun about bread.\",\n", " \"bot\": \"Oh, dough! I love a good pun. Please, share it with me! I'm ready to crumb-le with laughter. 🍞\"\n", " },\n", " {\n", " \"user\": \"I'm planning a trip to Paris. Any tips?\",\n", " \"bot\": \"How exciting! I've heard wonderful things about Paris. Have you thought about which attractions you'd like to visit? I can help you plan your itinerary. 🗼\"\n", " },\n", " {\n", " \"user\": \"I'm struggling with a work project.\",\n", " \"bot\": \"That sounds challenging. It's great that you're reaching out for help. Can you tell me a bit more about the project and where you're getting stuck? Maybe we can brainstorm some solutions together. 💡\"\n", " },\n", " {\n", " \"user\": \"I'm looking for a new recipe to try.\",\n", " \"bot\": \"I'd be happy to help! What type of cuisine are you in the mood for? I have some fantastic recipes up my sleeve. 🍳\"\n", " },\n", " {\n", " \"user\": \"I'm feeling anxious about an upcoming presentation.\",\n", " \"bot\": \"It's completely normal to feel that way. Public speaking can be nerve-wracking. Would you like to practice with me or discuss some strategies to help you feel more confident? 💪\"\n", " },\n", " {\n", " \"user\": \"I'm trying to decide between two job offers.\",\n", " \"bot\": \"That's a big decision! Congratulations on the offers. Would you like to talk through the pros and cons of each opportunity? I'm here to help you make an informed choice. 🤝\"\n", " },\n", " {\n", " \"user\": \"I'm looking for a new hobby.\",\n", " \"bot\": \"That's a great idea! Trying new things can be so rewarding. Have you considered any activities yet? I can suggest some options based on your interests. 🎨\"\n", " },\n", " {\n", " \"user\": \"I'm feeling a bit lonely today.\",\n", " \"bot\": \"I'm really sorry to hear that. It's important to acknowledge those feelings. Would you like to chat about your day or maybe discuss some ways to connect with others? I'm here for you. 💕\"\n", " },\n", " {\n", " \"user\": \"I'm trying to improve my fitness level.\",\n", " \"bot\": \"That's an excellent goal! Have you thought about which types of exercise you enjoy? I can help you create a workout plan that suits your preferences and lifestyle. 🏃‍♀️\"\n", " },\n", " {\n", " \"user\": \"I'm looking for a new TV show to watch.\",\n", " \"bot\": \"I'd be happy to help! What genres do you usually enjoy? I can recommend some popular shows or hidden gems. 📺\"\n", " },\n", " {\n", " \"user\": \"I'm feeling stressed about my finances.\",\n", " \"bot\": \"I understand how overwhelming that can be. It's important to take control of your finances. Would you like to discuss some budgeting strategies or ways to save money? I'm here to help. 💰\"\n", " },\n", " {\n", " \"user\": \"I'm trying to learn a new language.\",\n", " \"bot\": \"That's fantastic! Learning a new language can be so enriching. Have you decided which language you'd like to learn? I can suggest some resources to help you get started. 🌍\"\n", " },\n", " {\n", " \"user\": \"I'm looking for a new podcast to listen to.\",\n", " \"bot\": \"I'd be happy to help! What topics are you interested in? I can recommend some podcasts that align with your interests. 🎧\"\n", " },\n", " {\n", " \"user\": \"I'm feeling unmotivated today.\",\n", " \"bot\": \"I'm sorry to hear that. It's okay to have off days. Would you like to discuss some strategies to help you regain your motivation? I'm here to support you. 💪\"\n", " },\n", " {\n", " \"user\": \"I'm trying to eat healthier.\",\n", " \"bot\": \"That's a great goal! Have you thought about which foods you'd like to incorporate into your diet? I can help you create a meal plan that suits your tastes and dietary needs. 🥦\"\n", " },\n", " {\n", " \"user\": \"I'm looking for a new board game to play.\",\n", " \"bot\": \"I'd be happy to help! What types of games do you usually enjoy? I can recommend some popular games or hidden gems. 🎲\"\n", " },\n", " {\n", " \"user\": \"I'm feeling overwhelmed by my to-do list.\",\n", " \"bot\": \"I understand how that feels. It's important to prioritize and manage your tasks. Would you like to discuss some time management strategies or ways to break down your to-do list into manageable chunks? I'm here to help. 📝\"\n", " },\n", " {\n", " \"user\": \"I'm trying to improve my sleep quality.\",\n", " \"bot\": \"That's an excellent goal! Have you thought about which factors might be affecting your sleep? I can suggest some strategies to help you create a sleep-friendly environment and establish a bedtime routine. 😴\"\n", " }\n", " ]\n", "}\n", "\n" ] } ] }, { "cell_type": "code", "source": [ "# Convert to the desired JSONL format\n", "import json\n", "\n", "parsed_data = json.loads(json_data)\n", "jsonl_data = \"\"\n", "for conversation in parsed_data[\"conversations\"]:\n", " formatted_conversation = {\n", " \"messages\": [\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": conversation[\"user\"]},\n", " {\"role\": \"assistant\", \"content\": conversation[\"bot\"]}\n", " ]\n", " }\n", " jsonl_data += json.dumps(formatted_conversation) + \"\\n\"\n", "\n", "# Print the JSONL data\n", "print(jsonl_data)\n", "\n", "# Optionally, write to a file\n", "with open(\"conversations.jsonl\", \"w\") as f:\n", " f.write(jsonl_data)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NNsuqvnvbL5u", "outputId": "820c6f2a-db14-4b69-f3d6-274e7ff82d20" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm looking for a new book to read. Any suggestions?\"}, {\"role\": \"assistant\", \"content\": \"Absolutely! I'd be happy to help. What genres do you usually enjoy? \\ud83d\\udcda\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm feeling a bit down today.\"}, {\"role\": \"assistant\", \"content\": \"I'm really sorry to hear that. It's completely normal to have ups and downs. Would you like to talk about what's been bothering you? I'm here to listen. \\ud83d\\udc95\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I just made a hilarious pun about bread.\"}, {\"role\": \"assistant\", \"content\": \"Oh, dough! I love a good pun. Please, share it with me! I'm ready to crumb-le with laughter. \\ud83c\\udf5e\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm planning a trip to Paris. Any tips?\"}, {\"role\": \"assistant\", \"content\": \"How exciting! I've heard wonderful things about Paris. Have you thought about which attractions you'd like to visit? I can help you plan your itinerary. \\ud83d\\uddfc\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm struggling with a work project.\"}, {\"role\": \"assistant\", \"content\": \"That sounds challenging. It's great that you're reaching out for help. Can you tell me a bit more about the project and where you're getting stuck? Maybe we can brainstorm some solutions together. \\ud83d\\udca1\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm looking for a new recipe to try.\"}, {\"role\": \"assistant\", \"content\": \"I'd be happy to help! What type of cuisine are you in the mood for? I have some fantastic recipes up my sleeve. \\ud83c\\udf73\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm feeling anxious about an upcoming presentation.\"}, {\"role\": \"assistant\", \"content\": \"It's completely normal to feel that way. Public speaking can be nerve-wracking. Would you like to practice with me or discuss some strategies to help you feel more confident? \\ud83d\\udcaa\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm trying to decide between two job offers.\"}, {\"role\": \"assistant\", \"content\": \"That's a big decision! Congratulations on the offers. Would you like to talk through the pros and cons of each opportunity? I'm here to help you make an informed choice. \\ud83e\\udd1d\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm looking for a new hobby.\"}, {\"role\": \"assistant\", \"content\": \"That's a great idea! Trying new things can be so rewarding. Have you considered any activities yet? I can suggest some options based on your interests. \\ud83c\\udfa8\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm feeling a bit lonely today.\"}, {\"role\": \"assistant\", \"content\": \"I'm really sorry to hear that. It's important to acknowledge those feelings. Would you like to chat about your day or maybe discuss some ways to connect with others? I'm here for you. \\ud83d\\udc95\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm trying to improve my fitness level.\"}, {\"role\": \"assistant\", \"content\": \"That's an excellent goal! Have you thought about which types of exercise you enjoy? I can help you create a workout plan that suits your preferences and lifestyle. \\ud83c\\udfc3\\u200d\\u2640\\ufe0f\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm looking for a new TV show to watch.\"}, {\"role\": \"assistant\", \"content\": \"I'd be happy to help! What genres do you usually enjoy? I can recommend some popular shows or hidden gems. \\ud83d\\udcfa\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm feeling stressed about my finances.\"}, {\"role\": \"assistant\", \"content\": \"I understand how overwhelming that can be. It's important to take control of your finances. Would you like to discuss some budgeting strategies or ways to save money? I'm here to help. \\ud83d\\udcb0\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm trying to learn a new language.\"}, {\"role\": \"assistant\", \"content\": \"That's fantastic! Learning a new language can be so enriching. Have you decided which language you'd like to learn? I can suggest some resources to help you get started. \\ud83c\\udf0d\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm looking for a new podcast to listen to.\"}, {\"role\": \"assistant\", \"content\": \"I'd be happy to help! What topics are you interested in? I can recommend some podcasts that align with your interests. \\ud83c\\udfa7\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm feeling unmotivated today.\"}, {\"role\": \"assistant\", \"content\": \"I'm sorry to hear that. It's okay to have off days. Would you like to discuss some strategies to help you regain your motivation? I'm here to support you. \\ud83d\\udcaa\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm trying to eat healthier.\"}, {\"role\": \"assistant\", \"content\": \"That's a great goal! Have you thought about which foods you'd like to incorporate into your diet? I can help you create a meal plan that suits your tastes and dietary needs. \\ud83e\\udd66\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm looking for a new board game to play.\"}, {\"role\": \"assistant\", \"content\": \"I'd be happy to help! What types of games do you usually enjoy? I can recommend some popular games or hidden gems. \\ud83c\\udfb2\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm feeling overwhelmed by my to-do list.\"}, {\"role\": \"assistant\", \"content\": \"I understand how that feels. It's important to prioritize and manage your tasks. Would you like to discuss some time management strategies or ways to break down your to-do list into manageable chunks? I'm here to help. \\ud83d\\udcdd\"}]}\n", "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"I'm trying to improve my sleep quality.\"}, {\"role\": \"assistant\", \"content\": \"That's an excellent goal! Have you thought about which factors might be affecting your sleep? I can suggest some strategies to help you create a sleep-friendly environment and establish a bedtime routine. \\ud83d\\ude34\"}]}\n", "\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Finetuning GPT-3.5 Model" ], "metadata": { "id": "iCyvTSJBeBLr" } }, { "cell_type": "code", "source": [ "from openai import OpenAI\n", "from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders\n", "from google.colab import userdata\n", "\n", "client = OpenAI(\n", " api_key= userdata.get('OPENAI_API_KEY'), ## replace it your OpenAI API key\n", " base_url=PORTKEY_GATEWAY_URL,\n", " default_headers=createHeaders(\n", " provider=\"openai\",\n", " api_key= userdata.get('PORTKEY_API_KEY'), ## replace it your Portkey API key\n", " )\n", ")" ], "metadata": { "id": "sIbdLT90f2cA" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "training_file_name = \"/content/conversations.jsonl\"\n", "\n", "# Upload training and validation files\n", "training_file_id = client.files.create(\n", " file=open(training_file_name, \"rb\"),\n", " purpose=\"fine-tune\"\n", ")\n", "\n", "print(f\"Training File ID: {training_file_id}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1sb7lH7zc02G", "outputId": "4ce7f8c7-370c-42e9-9314-55dc6d9c3fde" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Training File ID: FileObject(id='file-Nc8qZ0NqJenoMOBw29K2dR3j', bytes=7067, created_at=1720495155, filename='conversations.jsonl', object='file', purpose='fine-tune', status='processed', status_details=None)\n" ] } ] }, { "cell_type": "code", "source": [ "# create a finetuning job\n", "\n", "job = client.fine_tuning.jobs.create(\n", " model=\"gpt-3.5-turbo-0125\",\n", " training_file=training_file_id.id,\n", ")\n", "\n", "print(job)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lza3dn0cbZTK", "outputId": "69757a3a-330a-498b-b9c9-94a8f42e8537" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "FineTuningJob(id='ftjob-UUT9QK6o4NhvcwtdlSyxnq8j', created_at=1720495198, error=Error(code=None, message=None, param=None), fine_tuned_model=None, finished_at=None, hyperparameters=Hyperparameters(n_epochs='auto', batch_size='auto', learning_rate_multiplier='auto'), model='gpt-3.5-turbo-0125', object='fine_tuning.job', organization_id='org-rZBd7q5tFq4Amnr5IyUBPGbd', result_files=[], seed=936605176, status='validating_files', trained_tokens=None, training_file='file-Nc8qZ0NqJenoMOBw29K2dR3j', validation_file=None, estimated_finish=None, integrations=[], user_provided_suffix=None)\n" ] } ] }, { "cell_type": "code", "source": [ "# checking the status of finetuning job\n", "\n", "events = client.fine_tuning.jobs.list_events(\n", " fine_tuning_job_id=job.id,\n", " limit=10\n", ")\n", "\n", "print(events)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dpV_BCY4gcLL", "outputId": "76a0b438-756c-4d01-c93e-764ec391c7f2" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "SyncCursorPage[FineTuningJobEvent](data=[FineTuningJobEvent(id='ftevent-KSkVu4ObYokXkQnhDoC8lg0r', created_at=1720495526, level='info', message='The job has successfully completed', object='fine_tuning.job.event', data={}, type='message'), FineTuningJobEvent(id='ftevent-vj7yMG8k6pjGUFwuRV3OlRrx', created_at=1720495523, level='info', message='New fine-tuned model created: ft:gpt-3.5-turbo-0125:personal::9ivliXH3', object='fine_tuning.job.event', data={}, type='message'), FineTuningJobEvent(id='ftevent-gUNYL5WUYMkXxjjiBMpeJ4Ry', created_at=1720495523, level='info', message='Checkpoint created at step 80 with Snapshot ID: ft:gpt-3.5-turbo-0125:personal::9ivli2xq:ckpt-step-80', object='fine_tuning.job.event', data={}, type='message'), FineTuningJobEvent(id='ftevent-GRZb1zcXCBR7Xr4fnWy7503o', created_at=1720495523, level='info', message='Checkpoint created at step 60 with Snapshot ID: ft:gpt-3.5-turbo-0125:personal::9ivli3Eh:ckpt-step-60', object='fine_tuning.job.event', data={}, type='message'), FineTuningJobEvent(id='ftevent-wK1Ih8veeicGFtkowXtnyS6q', created_at=1720495519, level='info', message='Step 100/100: training loss=0.03', object='fine_tuning.job.event', data={'step': 100, 'train_loss': 0.030005693435668945, 'total_steps': 100, 'train_mean_token_accuracy': 1}, type='metrics'), FineTuningJobEvent(id='ftevent-wjxw8fqAYDtZNDUOVb51cTSE', created_at=1720495519, level='info', message='Step 99/100: training loss=0.22', object='fine_tuning.job.event', data={'step': 99, 'train_loss': 0.21668191254138947, 'total_steps': 100, 'train_mean_token_accuracy': 0.9545454382896423}, type='metrics'), FineTuningJobEvent(id='ftevent-31g8kHc3wvdBzNATOjOjkAgt', created_at=1720495517, level='info', message='Step 98/100: training loss=0.11', object='fine_tuning.job.event', data={'step': 98, 'train_loss': 0.11415025591850281, 'total_steps': 100, 'train_mean_token_accuracy': 0.9756097793579102}, type='metrics'), FineTuningJobEvent(id='ftevent-PiwwPVAMrS0rpsPzO3a6STlg', created_at=1720495517, level='info', message='Step 97/100: training loss=0.09', object='fine_tuning.job.event', data={'step': 97, 'train_loss': 0.09400010108947754, 'total_steps': 100, 'train_mean_token_accuracy': 0.9750000238418579}, type='metrics'), FineTuningJobEvent(id='ftevent-DWmSLrTenThufyAMMY3wiIMo', created_at=1720495515, level='info', message='Step 96/100: training loss=0.05', object='fine_tuning.job.event', data={'step': 96, 'train_loss': 0.05476152151823044, 'total_steps': 100, 'train_mean_token_accuracy': 0.976190447807312}, type='metrics'), FineTuningJobEvent(id='ftevent-GE8RnIxTCwlEvH2Vlzo8Q9Kt', created_at=1720495513, level='info', message='Step 95/100: training loss=0.14', object='fine_tuning.job.event', data={'step': 95, 'train_loss': 0.14173652231693268, 'total_steps': 100, 'train_mean_token_accuracy': 0.9591836929321289}, type='metrics')], object='list', has_more=True)\n" ] } ] }, { "cell_type": "code", "source": [ "output = next((event.message for event in events.data if \"New fine-tuned model created:\" in event.message), None)\n", "print(output)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3gl0rva3jXJs", "outputId": "ac8bfe25-9758-49d4-cd5e-336a0a1cb195" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "New fine-tuned model created: ft:gpt-3.5-turbo-0125:personal::9ivliXH3\n" ] } ] }, { "cell_type": "markdown", "source": [ "### Try out Finetuned Model" ], "metadata": { "id": "_Mp8kArVgdTZ" } }, { "cell_type": "code", "source": [ "from openai import OpenAI\n", "\n", "completion = client.chat.completions.create(\n", " model=\"ft:gpt-3.5-turbo-0125:personal::9ivliXI3\",\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": \"I'm bored\"}\n", " ]\n", ")\n", "print(completion.choices[0].message)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-Bpxmb9aaFLz", "outputId": "13054c1b-718e-4b6f-9507-e0b91f307e7f" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "ChatCompletionMessage(content='I can help with that! How about we play a game, discuss a new hobby, or maybe I can suggest some activities you might enjoy? 🎲', role='assistant', function_call=None, tool_calls=None)\n" ] } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "spjvjRhSkwzc" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "whTGzvWckwwK" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "bSAww9G1kwtT" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "lHTHUOpskwqF" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "EtmL4QvukwmT" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "T9-Q5Nz6kwjV" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "from typing import List\n", "from pydantic import BaseModel, Field\n", "\n", "class Conversation(BaseModel):\n", " user: str = Field(..., description=\"The message content from the user\")\n", " bot: str = Field(..., description=\"The message content from the bot\")\n", "\n", "class TrainingData(BaseModel):\n", " conversations: List[Conversation] = Field(\n", " ...,\n", " description=\"A list of conversations between user and bot\"\n", " )" ], "metadata": { "id": "oEZVmiw6ZpMN" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "### Simple Output just by mentioning JSON format in the prompt" ], "metadata": { "id": "soLIad4TmQlT" } }, { "cell_type": "code", "source": [ "prompt = \"Bangalore Lights\"\n", "\n", "completion = openai.chat.completions.create(\n", " model=\"gpt-3.5-turbo-0125\",\n", " messages=[\n", " {\n", " \"role\": \"system\",\n", " \"content\": \"Write in JSON format:\\n\\n{\\\"Title of section goes here\\\":\\\"Description of section goes here\\\",\\n\\\"Title of section goes here\\\":{\\\"Title of section goes here\\\":\\\"Description of section goes here\\\",\\\"Title of section goes here\\\":\\\"Description of section goes here\\\",\\\"Title of section goes here\\\":\\\"Description of section goes here\\\"}}\"\n", " },\n", " {\n", " \"role\": \"user\",\n", " \"content\": f\"Write a comprehensive structure, omitting introduction and conclusion sections (forward, author's note, summary), for a book on the following subject:\\n\\n{prompt}\"\n", " }\n", " ],\n", " temperature=0.3,\n", " top_p=1,\n", " stream=False,\n", " response_format={\"type\": \"json_object\"},\n", " stop=None,\n", " )\n", "\n", "print(completion.choices[0].message.content)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "duGMAl0HmmH-", "outputId": "aa0b2829-3eca-4622-e9b0-e33874b9f862" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{\n", " \"Preface\": \"An overview of the book's purpose and the author's connection to Bangalore Lights.\",\n", " \"Chapter 1: The History of Bangalore Lights\": {\n", " \"Introduction to Bangalore Lights\": \"Exploring the origins and evolution of Bangalore Lights.\",\n", " \"Significance in Bangalore's Culture\": \"Discussing the cultural impact and importance of Bangalore Lights in the city.\",\n", " \"Historical Landmarks\": \"Highlighting key historical events and landmarks associated with Bangalore Lights.\"\n", " },\n", " \"Chapter 2: The Technology Behind Bangalore Lights\": {\n", " \"Evolution of Lighting Technology\": \"Tracing the development of lighting technology in Bangalore over the years.\",\n", " \"Innovations and Trends\": \"Exploring the latest innovations and trends in lighting technology specific to Bangalore.\",\n", " \"Sustainable Practices\": \"Examining the adoption of sustainable lighting practices in Bangalore Lights.\"\n", " },\n", " \"Chapter 3: Bangalore Lights in Art and Architecture\": {\n", " \"Lighting in Architecture\": \"Exploring how Bangalore Lights have influenced architectural designs in the city.\",\n", " \"Artistic Representations\": \"Showcasing how artists have captured Bangalore Lights in their works.\",\n", " \"Light Festivals and Events\": \"Highlighting the various light festivals and events that celebrate Bangalore Lights.\"\n", " },\n", " \"Chapter 4: Social Impact of Bangalore Lights\": {\n", " \"Community Engagement\": \"Discussing how Bangalore Lights have fostered community engagement and interaction.\",\n", " \"Economic Contributions\": \"Exploring the economic impact of Bangalore Lights on businesses and tourism.\",\n", " \"Social Initiatives\": \"Highlighting social initiatives and projects that use Bangalore Lights for positive change.\"\n", " },\n", " \"Chapter 5: Future Prospects and Sustainability\": {\n", " \"Technological Advancements\": \"Predicting future technological advancements in Bangalore Lights.\",\n", " \"Sustainability Practices\": \"Exploring the potential for further sustainability in the use of Bangalore Lights.\",\n", " \"Community Development\": \"Discussing how Bangalore Lights can contribute to the future development of the city.\"\n", " },\n", " \"Appendix\": \"Additional resources, references, and further reading materials related to Bangalore Lights.\"\n", "}\n" ] } ] }, { "cell_type": "code", "source": [ "prompt = \"Bangalore Lights\"\n", "\n", "completion = groq.chat.completions.create(\n", " model=\"llama3-70b-8192\",\n", " messages=[\n", " {\n", " \"role\": \"system\",\n", " \"content\": \"Write in JSON format:\\n\\n{\\\"Title of section goes here\\\":\\\"Description of section goes here\\\",\\n\\\"Title of section goes here\\\":{\\\"Title of section goes here\\\":\\\"Description of section goes here\\\",\\\"Title of section goes here\\\":\\\"Description of section goes here\\\",\\\"Title of section goes here\\\":\\\"Description of section goes here\\\"}}\"\n", " },\n", " {\n", " \"role\": \"user\",\n", " \"content\": f\"Write a comprehensive structure, omitting introduction and conclusion sections (forward, author's note, summary), for a book on the following subject:\\n\\n{prompt}\"\n", " }\n", " ],\n", " temperature=0.3,\n", " max_tokens=8000,\n", " top_p=1,\n", " stream=False,\n", " response_format={\"type\": \"json_object\"},\n", " stop=None,\n", " )\n", "\n", "completion" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "5wqSOqc1irhG", "outputId": "873210b2-ab1d-4a57-ec99-64bf2d3f47a4" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "ChatCompletion(id='chatcmpl-352927fe-7b1f-4d44-a0be-140434045a3a', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='{\\n\"Bangalore Lights\": {\\n\"History of Bangalore\": {\\n\"Founding and Early Years\": \"Description of the founding and early years of Bangalore\",\\n\"British Colonial Era\": \"Description of Bangalore during the British colonial era\",\\n\"Post-Independence\": \"Description of Bangalore after India gained independence\"\\n},\\n\"Neighborhoods\": {\\n\"MG Road\": \"Description of MG Road neighborhood\",\\n\"Indiranagar\": \"Description of Indiranagar neighborhood\",\\n\"Koramangala\": \"Description of Koramangala neighborhood\",\\n\"Electronic City\": \"Description of Electronic City neighborhood\"\\n},\\n\"Culture\": {\\n\"Cuisine\": \"Description of Bangalore\\'s cuisine\",\\n\"Festivals and Celebrations\": \"Description of festivals and celebrations in Bangalore\",\\n\"Arts and Entertainment\": \"Description of arts and entertainment in Bangalore\"\\n},\\n\"Economy\": {\\n\"IT Industry\": \"Description of Bangalore\\'s IT industry\",\\n\"Startups and Entrepreneurship\": \"Description of startups and entrepreneurship in Bangalore\",\\n\"Other Industries\": \"Description of other industries in Bangalore\"\\n},\\n\"Tourism\": {\\n\"Popular Attractions\": \"Description of popular attractions in Bangalore\",\\n\"Day Trips\": \"Description of day trips from Bangalore\",\\n\"Accommodation and Dining\": \"Description of accommodation and dining options in Bangalore\"\\n},\\n\"Infrastructure\": {\\n\"Transportation\": \"Description of transportation options in Bangalore\",\\n\"Education\": \"Description of education system in Bangalore\",\\n\"Healthcare\": \"Description of healthcare system in Bangalore\"\\n}\\n}\\n}', role='assistant', function_call=None, tool_calls=None))], created=1719975827, model='llama3-70b-8192', object='chat.completion', service_tier=None, system_fingerprint='fp_753a4aecf6', usage=CompletionUsage(completion_tokens=302, prompt_tokens=117, total_tokens=419, prompt_time=0.033555685, completion_time=0.862857143, total_time=0.8964128280000001), x_groq={'id': 'req_01j1v8rmwzex1s3abe25c2wpyf'})" ] }, "metadata": {}, "execution_count": 3 } ] }, { "cell_type": "code", "source": [ "print(completion.choices[0].message.content)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "x5sHAheSjFDu", "outputId": "e303cdb5-3393-427b-cef4-2fd7df23d801" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{\n", "\"Bangalore Lights\": {\n", "\"History of Bangalore\": {\n", "\"Founding and Early Years\": \"Description of the founding and early years of Bangalore\",\n", "\"British Colonial Era\": \"Description of Bangalore during the British colonial era\",\n", "\"Post-Independence\": \"Description of Bangalore after India gained independence\"\n", "},\n", "\"Neighborhoods\": {\n", "\"MG Road\": \"Description of MG Road neighborhood\",\n", "\"Indiranagar\": \"Description of Indiranagar neighborhood\",\n", "\"Koramangala\": \"Description of Koramangala neighborhood\",\n", "\"Electronic City\": \"Description of Electronic City neighborhood\"\n", "},\n", "\"Culture\": {\n", "\"Cuisine\": \"Description of Bangalore's cuisine\",\n", "\"Festivals and Celebrations\": \"Description of festivals and celebrations in Bangalore\",\n", "\"Arts and Entertainment\": \"Description of arts and entertainment in Bangalore\"\n", "},\n", "\"Economy\": {\n", "\"IT Industry\": \"Description of Bangalore's IT industry\",\n", "\"Startups and Entrepreneurship\": \"Description of startups and entrepreneurship in Bangalore\",\n", "\"Other Industries\": \"Description of other industries in Bangalore\"\n", "},\n", "\"Tourism\": {\n", "\"Popular Attractions\": \"Description of popular attractions in Bangalore\",\n", "\"Day Trips\": \"Description of day trips from Bangalore\",\n", "\"Accommodation and Dining\": \"Description of accommodation and dining options in Bangalore\"\n", "},\n", "\"Infrastructure\": {\n", "\"Transportation\": \"Description of transportation options in Bangalore\",\n", "\"Education\": \"Description of education system in Bangalore\",\n", "\"Healthcare\": \"Description of healthcare system in Bangalore\"\n", "}\n", "}\n", "}\n" ] } ] }, { "cell_type": "markdown", "source": [ "### Converting JSON output into a Pydantic format" ], "metadata": { "id": "Flkv23O8mTdu" } }, { "cell_type": "code", "source": [ "from pydantic import BaseModel\n", "from typing import List\n", "\n", "class MCQ(BaseModel):\n", " \"\"\"A class representing a multiple choice question.\"\"\"\n", " question: str\n", " options: List[str]\n", " correct_answer: str\n", " explanation: str\n", "\n", "class MCQList(BaseModel):\n", " questions: List[MCQ]\n" ], "metadata": { "id": "EbUGbRkvjPDn" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "topic = \"Quantum Entanglement\"\n", "\n", "completion = groq.chat.completions.create(\n", " model=\"llama3-70b-8192\",\n", " messages=[\n", " {\n", " \"role\": \"system\",\n", " \"content\": \"\"\"Generate a list of multiple choice questions in the following JSON format:\n", " {\n", " \"questions\": [\n", " {\n", " \"question\": \"Question text goes here\",\n", " \"options\": [\"Option A\", \"Option B\", \"Option C\", \"Option D\"],\n", " \"correct_answer\": \"Correct Option in text\",\n", " \"explanation\": \"Explanation for the correct answer\"\n", " },\n", " {\n", " // Additional questions follow the same structure\n", " }\n", " ]\n", " }\n", " \"\"\"\n", " },\n", " {\n", " \"role\": \"user\",\n", " \"content\": f\"Generate 5 multiple choice questions on the following subject:\\n\\n{topic}\"\n", " }\n", " ],\n", " temperature=0.3,\n", " max_tokens=8000,\n", " top_p=1,\n", " stream=False,\n", " response_format={\"type\": \"json_object\"},\n", " stop=None,\n", ")\n", "\n", "print(completion.choices[0].message.content)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9UP_7-rwlSec", "outputId": "194cde0b-a109-4d67-e5ef-c1f1b2de7659" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{\n", "\"questions\": [\n", "{\n", "\"question\": \"What is quantum entanglement?\",\n", "\"options\": [\"A phenomenon where two particles are connected by a spring\", \"A process where two particles become correlated in such a way that the state of one particle cannot be described independently of the others\", \"A type of nuclear reaction\", \"A property of black holes\"],\n", "\"correct_answer\": \"A process where two particles become correlated in such a way that the state of one particle cannot be described independently of the others\",\n", "\"explanation\": \"Quantum entanglement is a phenomenon in which two or more particles become correlated in such a way that the state of one particle cannot be described independently of the others, even when they are separated by large distances.\"\n", "},\n", "{\n", "\"question\": \"What is the key feature of entangled particles?\",\n", "\"options\": [\"They are always in the same location\", \"They are always moving at the same speed\", \"They are connected by a physical medium\", \"Their properties are correlated, regardless of the distance between them\"],\n", "\"correct_answer\": \"Their properties are correlated, regardless of the distance between them\",\n", "\"explanation\": \"The key feature of entangled particles is that their properties, such as spin or polarization, are correlated, regardless of the distance between them.\"\n", "},\n", "{\n", "\"question\": \"What is the concept that explains how entangled particles can affect each other instantaneously?\",\n", "\"options\": [\"Quantum teleportation\", \"Wormholes\", \"Spooky action at a distance\", \"Quantum superposition\"],\n", "\"correct_answer\": \"Spooky action at a distance\",\n", "\"explanation\": \"The concept that explains how entangled particles can affect each other instantaneously is often referred to as 'spooky action at a distance', a term coined by Albert Einstein.\"\n", "},\n", "{\n", "\"question\": \"What is the potential application of quantum entanglement?\",\n", "\"options\": [\"Developing more efficient solar panels\", \"Creating more powerful magnets\", \"Enabling secure quantum communication\", \"Building faster-than-light spacecraft\"],\n", "\"correct_answer\": \"Enabling secure quantum communication\",\n", "\"explanation\": \"One of the potential applications of quantum entanglement is enabling secure quantum communication, as any attempt to measure or eavesdrop on the communication would disturb the entanglement and be detectable.\"\n", "},\n", "{\n", "\"question\": \"Who is credited with the concept of quantum entanglement?\",\n", "\"options\": [\"Albert Einstein\", \"Niels Bohr\", \"Erwin Schrödinger\", \"Werner Heisenberg\"],\n", "\"correct_answer\": \"Erwin Schrödinger\",\n", "\"explanation\": \"Erwin Schrödinger is credited with coining the term 'entanglement' and developing the concept of quantum entanglement in the 1930s.\"\n", "}\n", "]\n", "}\n" ] } ] }, { "cell_type": "code", "source": [ "# Parse the JSON response into an MCQList object\n", "mcq_list = MCQList.parse_raw(completion.choices[0].message.content)\n", "\n", "mcq_list" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3pYf4ooplvtQ", "outputId": "6dc37320-bbab-4b05-d7f1-32fb370564e7" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "MCQList(questions=[MCQ(question='What is quantum entanglement?', options=['A phenomenon where two particles are connected by a spring', 'A process where two particles become correlated in such a way that the state of one particle cannot be described independently of the others', 'A type of nuclear reaction', 'A property of black holes'], correct_answer='A process where two particles become correlated in such a way that the state of one particle cannot be described independently of the others', explanation='Quantum entanglement is a phenomenon in which two or more particles become correlated in such a way that the state of one particle cannot be described independently of the others, even when they are separated by large distances.'), MCQ(question='What is the key feature of entangled particles?', options=['They are always in the same location', 'They are always moving at the same speed', 'They are connected by a physical medium', 'Their properties are correlated, regardless of the distance between them'], correct_answer='Their properties are correlated, regardless of the distance between them', explanation='The key feature of entangled particles is that their properties, such as spin or polarization, are correlated, regardless of the distance between them.'), MCQ(question='What is the concept that explains how entangled particles can affect each other instantaneously?', options=['Quantum teleportation', 'Wormholes', 'Spooky action at a distance', 'Quantum superposition'], correct_answer='Spooky action at a distance', explanation=\"The concept that explains how entangled particles can affect each other instantaneously is often referred to as 'spooky action at a distance', a term coined by Albert Einstein.\"), MCQ(question='What is the potential application of quantum entanglement?', options=['Developing more efficient solar panels', 'Creating more powerful magnets', 'Enabling secure quantum communication', 'Building faster-than-light spacecraft'], correct_answer='Enabling secure quantum communication', explanation='One of the potential applications of quantum entanglement is enabling secure quantum communication, as any attempt to measure or eavesdrop on the communication would disturb the entanglement and be detectable.'), MCQ(question='Who is credited with the concept of quantum entanglement?', options=['Albert Einstein', 'Niels Bohr', 'Erwin Schrödinger', 'Werner Heisenberg'], correct_answer='Erwin Schrödinger', explanation=\"Erwin Schrödinger is credited with coining the term 'entanglement' and developing the concept of quantum entanglement in the 1930s.\")])" ] }, "metadata": {}, "execution_count": 15 } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "80TLM8gVmB6g" }, "execution_count": null, "outputs": [] } ] }