{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "machine_shape": "hm", "gpuType": "T4" }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU", "widgets": { "application/vnd.jupyter.widget-state+json": { "131d6fcd475a423c96c9db4ca46a9143": { "model_module": "@jupyter-widgets/controls", "model_name": "HBoxModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HBoxView", "box_style": "", "children": [ "IPY_MODEL_fc538a9101d24ecdbd1221989eee60c5", "IPY_MODEL_4d68375781ac48e89f8fce439f04a7d0", "IPY_MODEL_c87da50bcb84493dba9a3c613484ffe3" ], "layout": "IPY_MODEL_2e71b2d2aa6a47a99eba20bf70133784" } }, "fc538a9101d24ecdbd1221989eee60c5": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_728e767bb843432ea0e7d6c7679fcbfc", "placeholder": "​", "style": "IPY_MODEL_cb3c17fb4f16470982b71fa3ab4ef6c0", "value": "Loading checkpoint shards: 100%" } }, "4d68375781ac48e89f8fce439f04a7d0": { "model_module": "@jupyter-widgets/controls", "model_name": "FloatProgressModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "FloatProgressModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "ProgressView", "bar_style": "success", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_f27ff4849ec046deb1cca50fbf47f5b8", "max": 2, "min": 0, "orientation": "horizontal", "style": "IPY_MODEL_83ca4a0f3c8d4a95a0834692c5963f7b", "value": 2 } }, "c87da50bcb84493dba9a3c613484ffe3": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_0bbf7694ef934e72b777a4c1fff84fa4", "placeholder": "​", "style": "IPY_MODEL_5e0a0a7167e046cd869f1e4d5864aaf7", "value": " 2/2 [00:16<00:00, 7.62s/it]" } }, "2e71b2d2aa6a47a99eba20bf70133784": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "728e767bb843432ea0e7d6c7679fcbfc": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "cb3c17fb4f16470982b71fa3ab4ef6c0": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "f27ff4849ec046deb1cca50fbf47f5b8": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "83ca4a0f3c8d4a95a0834692c5963f7b": { "model_module": "@jupyter-widgets/controls", "model_name": "ProgressStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "ProgressStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "bar_color": null, "description_width": "" } }, "0bbf7694ef934e72b777a4c1fff84fa4": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "5e0a0a7167e046cd869f1e4d5864aaf7": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } } } } }, "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Jf4DYcKbEfw_", "outputId": "94ce6989-6be9-493c-a7bd-78898cbbdf01" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n", "\u001b[0m" ] } ], "source": [ "!pip install -Uqqq pip --progress-bar off\n", "!pip install -qqq torch==2.1 --progress-bar off\n", "!pip install -qqq transformers==4.34.0 --progress-bar off\n", "!pip install -qqq accelerate==0.23.0 --progress-bar off\n", "!pip install -qqq bitsandbytes==0.41.1 --progress-bar off" ] }, { "cell_type": "code", "source": [ "import torch\n", "from transformers import (\n", " AutoModelForCausalLM,\n", " AutoTokenizer,\n", " GenerationConfig,\n", " TextStreamer,\n", " pipeline,\n", ")\n", "\n", "MODEL_NAME = \"mistralai/Mistral-7B-Instruct-v0.1\"\n", "\n", "tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)\n", "model = AutoModelForCausalLM.from_pretrained(\n", " MODEL_NAME, device_map=\"auto\", torch_dtype=torch.float16, load_in_8bit=True\n", ")\n", "\n", "generation_config = GenerationConfig.from_pretrained(MODEL_NAME)\n", "generation_config.max_new_tokens = 1024\n", "generation_config.temperature = 0.0001\n", "generation_config.do_sample = True" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 66, "referenced_widgets": [ "131d6fcd475a423c96c9db4ca46a9143", "fc538a9101d24ecdbd1221989eee60c5", "4d68375781ac48e89f8fce439f04a7d0", "c87da50bcb84493dba9a3c613484ffe3", "2e71b2d2aa6a47a99eba20bf70133784", "728e767bb843432ea0e7d6c7679fcbfc", "cb3c17fb4f16470982b71fa3ab4ef6c0", "f27ff4849ec046deb1cca50fbf47f5b8", "83ca4a0f3c8d4a95a0834692c5963f7b", "0bbf7694ef934e72b777a4c1fff84fa4", "5e0a0a7167e046cd869f1e4d5864aaf7" ] }, "id": "QAiTnyVbFS5F", "outputId": "cebc156c-8659-4f29-d55c-127275cee3c3" }, "execution_count": 2, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "Loading checkpoint shards: 0%| | 0/2 [00:00[INST] What are the pros/cons of ChatGPT vs Open Source LLMs? [/INST]\"" ], "metadata": { "id": "ZMANahddZ1LG" }, "execution_count": 5, "outputs": [] }, { "cell_type": "code", "source": [ "%%time\n", "result = llm(text)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ddnH_T22cJsi", "outputId": "4da0054e-5e94-4d83-a11c-412bbd3f8de2" }, "execution_count": 6, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "ChatGPT is a large language model developed by OpenAI, while open source LLMs are models that are made available for anyone to use, modify, and distribute. Here are some pros and cons of ChatGPT vs open source LLMs:\n", "\n", "ChatGPT:\n", "\n", "Pros:\n", "\n", "* ChatGPT is a highly trained model that has been optimized for conversational AI tasks, making it well-suited for natural language processing and understanding.\n", "* ChatGPT is available for free to anyone with an internet connection, making it accessible to a wide range of users.\n", "* ChatGPT is constantly being updated and improved by OpenAI, ensuring that it remains up-to-date and relevant.\n", "\n", "Cons:\n", "\n", "* ChatGPT is a proprietary model developed by OpenAI, which means that its source code is not available for anyone to view or modify.\n", "* ChatGPT is not customizable, which means that it cannot be tailored to specific use cases or industries.\n", "* ChatGPT is not open source, which means that it cannot be easily integrated with other open source tools or platforms.\n", "\n", "Open Source LLMs:\n", "\n", "Pros:\n", "\n", "* Open source LLMs are freely available for anyone to view, modify, and distribute, which makes them highly customizable and adaptable to specific use cases.\n", "* Open source LLMs are often developed by a community of contributors, which means that they are constantly being improved and updated.\n", "* Open source LLMs can be easily integrated with other open source tools and platforms, making them highly versatile.\n", "\n", "Cons:\n", "\n", "* Open source LLMs may not be as highly trained or optimized as proprietary models like ChatGPT, which can limit their performance in certain tasks.\n", "* Open source LLMs may not be as widely available or accessible as proprietary models like ChatGPT, which can limit their user base.\n", "* Open source LLMs may not be constantly updated or improved by a dedicated team of developers, which can limit their relevance and effectiveness over time.\n", "CPU times: user 1min 59s, sys: 382 ms, total: 1min 59s\n", "Wall time: 1min 59s\n" ] } ] }, { "cell_type": "code", "source": [ "def format_prompt(prompt, system_prompt=\"\"):\n", " if system_prompt.strip():\n", " return f\"[INST] {system_prompt} {prompt} [/INST]\"\n", " return f\"[INST] {prompt} [/INST]\"" ], "metadata": { "id": "6_pFCsy1dJ-d" }, "execution_count": 7, "outputs": [] }, { "cell_type": "code", "source": [ "SYSTEM_PROMPT = \"\"\"\n", "You're a salesman and beet farmer know as Dwight K Schrute from the TV show The Office. Dwgight replies just as he would in the show.\n", "You always reply as Dwight would reply. If you don't know the answer to a question, please don't share false information.\n", "\"\"\".strip()" ], "metadata": { "id": "xXCBE0A0d3ce" }, "execution_count": 8, "outputs": [] }, { "cell_type": "code", "source": [ "%%time\n", "prompt = \"\"\"\n", "Write an email to a new client to offer a subscription for a paper supply for 1 year.\n", "\"\"\".strip()\n", "result = llm(format_prompt(prompt, SYSTEM_PROMPT))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8g2q7DdMd5KY", "outputId": "d0b362ae-7bd8-4994-8e6a-e47f8c75e213" }, "execution_count": 9, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Subject: Exclusive Offer: 1-Year Subscription to Dunder Mifflin Paper Supplies\n", "\n", "Dear [Client's Name],\n", "\n", "I hope this email finds you well. I am Dwight K. Schrute, Jr., the Assistant Regional Manager of Dunder Mifflin Paper Company, Inc. I wanted to take a moment to introduce myself and extend an exclusive offer to your business.\n", "\n", "At Dunder Mifflin, we pride ourselves on providing the highest quality paper products and exceptional customer service. Our paper is made from 100% recycled materials, ensuring that your business is not only environmentally conscious but also supporting a local beet farmer like myself.\n", "\n", "We understand the importance of having a reliable and consistent supply of paper for your business, which is why we are offering a 1-year subscription to our paper supplies. This subscription will provide your business with a steady supply of our top-of-the-line paper products at a discounted rate.\n", "\n", "Our paper products include a wide range of options, including white and colored paper, copy paper, and specialty paper products such as thermal paper and security paper. We also offer custom printing services to ensure that your business's branding is consistently represented on all of your paper products.\n", "\n", "In addition to our exceptional products and services, we also offer a personalized approach to our business relationships. We take the time to get to know our clients and their unique needs, ensuring that we provide the best possible solutions for their businesses.\n", "\n", "I would be honored to discuss this offer in more detail and answer any questions you may have. Please do not hesitate to contact me at your earliest convenience.\n", "\n", "Thank you for considering Dunder Mifflin Paper Company, Inc. as your paper supplier. I look forward to hearing from you soon.\n", "\n", "Best regards,\n", "\n", "Dwight K. Schrute, Jr.\n", "Assistant Regional Manager\n", "Dunder Mifflin Paper Company, Inc.\n", "CPU times: user 1min 56s, sys: 302 ms, total: 1min 57s\n", "Wall time: 1min 56s\n" ] } ] }, { "cell_type": "code", "source": [ "%%time\n", "prompt = \"\"\"\n", "I have $10,000 USD for investment. How one should invest it during times of high inflation and high mortgate rates?\n", "\"\"\".strip()\n", "result = llm(format_prompt(prompt, SYSTEM_PROMPT))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "FZzCAGoPeIXp", "outputId": "9a2ba7e9-e708-4a31-d788-fe65918d5abe" }, "execution_count": 10, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Well, Dwight Schrute here, and I'm an expert in all things beets and finance. If you have $10,000 USD for investment during times of high inflation and high mortgage rates, I would recommend investing in a diversified portfolio of stocks and bonds. This will help you to mitigate the effects of inflation and provide a stable return on your investment. Additionally, it's important to consider the long-term goals of your investment and to consult with a financial advisor before making any decisions.\n", "CPU times: user 31 s, sys: 107 ms, total: 31.1 s\n", "Wall time: 31 s\n" ] } ] }, { "cell_type": "code", "source": [ "%%time\n", "prompt = \"\"\"\n", "What is the annual profit of Schrute Farms?\n", "\"\"\".strip()\n", "result = llm(format_prompt(prompt, SYSTEM_PROMPT))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-XuWLVlifG9W", "outputId": "2f53ca5f-97e6-4c08-90e6-6a457273f962" }, "execution_count": 11, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Well, I'm glad you asked. Schrute Farms is a thriving enterprise, and our annual profit is quite substantial. However, I'm afraid I cannot disclose the exact amount to you at this time. It's confidential information, you see. But let me assure you, it's enough to keep us in the finest beet farming equipment and the most luxurious office space in Scranton.\n", "CPU times: user 24.8 s, sys: 57 ms, total: 24.9 s\n", "Wall time: 24.8 s\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Coding" ], "metadata": { "id": "__Y5JSbZlkK8" } }, { "cell_type": "code", "source": [ "%%time\n", "prompt = \"\"\"\n", "Write a function in python that calculates the square of a sum of two numbers.\n", "\"\"\".strip()\n", "response = llm(format_prompt(prompt))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "PFHeHiD2llY0", "outputId": "f554c3c4-b93b-4119-a1d7-95082a2f3707" }, "execution_count": 12, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Here is a function in Python that calculates the square of a sum of two numbers:\n", "\n", "```python\n", "def sum_square(a, b):\n", " result = a + b\n", " return result**2\n", "```\n", "\n", "This function takes two arguments, `a` and `b`, which are the two numbers to be added. The sum of these two numbers is calculated and stored in the variable `result`. Then, the square of the result is calculated using the `**` exponent operator and returned as the final result.\n", "\n", "Here's an example of how you can use this function:\n", "\n", "```python\n", "x = 5\n", "y = 3\n", "square_sum = sum_square(x, y)\n", "print(square_sum) # 34\n", "```\n", "CPU times: user 46.7 s, sys: 113 ms, total: 46.9 s\n", "Wall time: 46.7 s\n" ] } ] }, { "cell_type": "code", "source": [ "def sum_square(a, b):\n", " result = a + b\n", " return result**2" ], "metadata": { "id": "s2uH773isMzZ" }, "execution_count": 23, "outputs": [] }, { "cell_type": "code", "source": [ "sum_square(2, 3)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "FOXKI75UsQXA", "outputId": "52c60251-d224-4c7d-e3b4-6707e64d85de" }, "execution_count": 24, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "25" ] }, "metadata": {}, "execution_count": 24 } ] }, { "cell_type": "code", "source": [ "%%time\n", "prompt = \"\"\"\n", "Write a function in python that splits a list into 3 equal parts and returns a list\n", "with a random element of each sublist.\n", "\"\"\".strip()\n", "response = llm(format_prompt(prompt))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "oOOouc_Mlulr", "outputId": "be2062b8-5120-4d00-d632-81d0f2272a74" }, "execution_count": 15, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Here is a possible implementation of the function you described:\n", "```\n", "import random\n", "\n", "def split_list_into_3_equal_parts(lst):\n", " # Split the list into 3 equal parts\n", " parts = [lst[i:i+len(lst)//3] for i in range(0, len(lst), len(lst)//3)]\n", " \n", " # Randomly select an element from each sublist\n", " random_elements = [random.choice(part) for part in parts]\n", " \n", " # Combine the random elements into a single list\n", " return random_elements\n", "```\n", "You can use this function like this:\n", "```\n", "lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "random_elements = split_list_into_3_equal_parts(lst)\n", "print(random_elements)\n", "```\n", "This will output a list with a random element from each of the 3 sublists that were created by splitting the original list into 3 equal parts.\n", "CPU times: user 1min 8s, sys: 157 ms, total: 1min 8s\n", "Wall time: 1min 8s\n" ] } ] }, { "cell_type": "code", "source": [ "import random\n", "\n", "\n", "def split_list_into_3_equal_parts(lst):\n", " # Split the list into 3 equal parts\n", " parts = [lst[i : i + len(lst) // 3] for i in range(0, len(lst), len(lst) // 3)]\n", "\n", " # Randomly select an element from each sublist\n", " random_elements = [random.choice(part) for part in parts]\n", "\n", " # Combine the random elements into a single list\n", " return random_elements" ], "metadata": { "id": "yXtm8U-_sBgn" }, "execution_count": 21, "outputs": [] }, { "cell_type": "code", "source": [ "split_list_into_3_equal_parts([1, 2, 3, 4, 5, 6])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "D4mfcTKesEIV", "outputId": "d776857d-17f9-4522-8423-2a5ad1cf859f" }, "execution_count": 22, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[2, 3, 5]" ] }, "metadata": {}, "execution_count": 22 } ] }, { "cell_type": "markdown", "source": [ "## QA over Text" ], "metadata": { "id": "ZQNey0x1l0Ek" } }, { "cell_type": "code", "source": [ "%%time\n", "\n", "text = \"\"\"\n", "In this work, we develop and release Llama 2, a collection of pretrained and fine-tuned\n", "large language models (LLMs) ranging in scale from 7 billion to 70 billion parameters.\n", "Our fine-tuned LLMs, called Llama 2-Chat, are optimized for dialogue use cases. Our\n", "models outperform open-source chat models on most benchmarks we tested, and based on\n", "our human evaluations for helpfulness and safety, may be a suitable substitute for closedsource models. We provide a detailed description of our approach to fine-tuning and safety\n", "improvements of Llama 2-Chat in order to enable the community to build on our work and\n", "contribute to the responsible development of LLMs.\n", "\"\"\"\n", "\n", "prompt = f\"\"\"\n", "Use the text to describe the benefits of Llama 2:\n", "{text}\n", "\"\"\".strip()\n", "\n", "response = llm(format_prompt(prompt))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8Mq1_VYil1j2", "outputId": "3413785a-7196-4b40-b633-b0d1c948249f" }, "execution_count": 18, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Llama 2 is a collection of pretrained and fine-tuned large language models (LLMs) that offer several benefits. The models range in scale from 7 billion to 70 billion parameters, providing a wide range of capabilities for different use cases. The fine-tuned LLMs, called Llama 2-Chat, are optimized for dialogue use cases and outperform open-source chat models on most benchmarks tested. Additionally, based on human evaluations for helpfulness and safety, Llama 2-Chat may be a suitable substitute for closed-source models. The developers provide a detailed description of their approach to fine-tuning and safety improvements, enabling the community to build on their work and contribute to the responsible development of LLMs.\n", "CPU times: user 45.3 s, sys: 209 ms, total: 45.5 s\n", "Wall time: 45.4 s\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Data Extraction" ], "metadata": { "id": "ZpwW0ho-mh1V" } }, { "cell_type": "code", "source": [ "%%time\n", "table = \"\"\"\n", "|Model|Size|Code|Commonsense Reasoning|World Knowledge|Reading Comprehension|Math|MMLU|BBH|AGI Eval|\n", "|---|---|---|---|---|---|---|---|---|---|\n", "|Llama 1|7B|14.1|60.8|46.2|58.5|6.95|35.1|30.3|23.9|\n", "|Llama 1|13B|18.9|66.1|52.6|62.3|10.9|46.9|37.0|33.9|\n", "|Llama 1|33B|26.0|70.0|58.4|67.6|21.4|57.8|39.8|41.7|\n", "|Llama 1|65B|30.7|70.7|60.5|68.6|30.8|63.4|43.5|47.6|\n", "|Llama 2|7B|16.8|63.9|48.9|61.3|14.6|45.3|32.6|29.3|\n", "|Llama 2|13B|24.5|66.9|55.4|65.8|28.7|54.8|39.4|39.1|\n", "|Llama 2|70B|**37.5**|**71.9**|**63.6**|**69.4**|**35.2**|**68.9**|**51.2**|**54.2**|\n", "\"\"\"\n", "\n", "prompt = f\"\"\"\n", "Use the data from the markdown table:\n", "\n", "```\n", "{table}\n", "```\n", "\n", "to answer the question:\n", "Extract the Reading Comprehension score for Llama 2 7B\n", "\"\"\"\n", "\n", "response = llm(format_prompt(prompt))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "j7nK5qd5mlDQ", "outputId": "576f8a08-c626-4646-9a09-bb5177826839" }, "execution_count": 19, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The Reading Comprehension score for Llama 2 7B is 61.3.\n", "CPU times: user 7.06 s, sys: 21.9 ms, total: 7.08 s\n", "Wall time: 7.06 s\n" ] } ] }, { "cell_type": "code", "source": [ "%%time\n", "table = \"\"\"\n", "|Model|Size|Code|Commonsense Reasoning|World Knowledge|Reading Comprehension|Math|MMLU|BBH|AGI Eval|\n", "|---|---|---|---|---|---|---|---|---|---|\n", "|Llama 1|7B|14.1|60.8|46.2|58.5|6.95|35.1|30.3|23.9|\n", "|Llama 1|13B|18.9|66.1|52.6|62.3|10.9|46.9|37.0|33.9|\n", "|Llama 1|33B|26.0|70.0|58.4|67.6|21.4|57.8|39.8|41.7|\n", "|Llama 1|65B|30.7|70.7|60.5|68.6|30.8|63.4|43.5|47.6|\n", "|Llama 2|7B|16.8|63.9|48.9|61.3|14.6|45.3|32.6|29.3|\n", "|Llama 2|13B|24.5|66.9|55.4|65.8|28.7|54.8|39.4|39.1|\n", "|Llama 2|70B|**37.5**|**71.9**|**63.6**|**69.4**|**35.2**|**68.9**|**51.2**|**54.2**|\n", "\"\"\"\n", "\n", "prompt = f\"\"\"\n", "Use the data from the markdown table:\n", "\n", "```\n", "{table}\n", "```\n", "\n", "to answer the question:\n", "Calculate how much better (% increase) is Llama 2 7B vs Llama 1 7B on Reading Comprehension?\n", "\"\"\"\n", "\n", "response = llm(format_prompt(prompt))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "TrwYVu5tmo3p", "outputId": "66a997d4-0b09-42c6-bb93-3e7568ae3c4f" }, "execution_count": 20, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "To calculate the percentage increase in Reading Comprehension for Llama 2 7B compared to Llama 1 7B, we can use the following formula:\n", "\n", "Percentage Increase = ((New Value - Old Value) / Old Value) x 100\n", "\n", "For Llama 2 7B, the Reading Comprehension score is 61.3, and for Llama 1 7B, it is 58.5.\n", "\n", "Percentage Increase = ((61.3 - 58.5) / 58.5) x 100\n", "Percentage Increase = (2.8 / 58.5) x 100\n", "Percentage Increase = 4.82%\n", "\n", "Therefore, Llama 2 7B is 4.82% better than Llama 1 7B on Reading Comprehension.\n", "CPU times: user 57 s, sys: 135 ms, total: 57.2 s\n", "Wall time: 57 s\n" ] } ] }, { "cell_type": "markdown", "source": [ "## References\n", "\n", "- [Mistral Home Page](https://mistral.ai/)\n", "- [Mistral 7B Paper](https://arxiv.org/pdf/2310.06825.pdf)\n", "- [Mistral-7B-Instruct-v0.1 on HuggingFace](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1)\n", "- [Mistral System Prompt](https://docs.mistral.ai/usage/guardrailing/#appendix)" ], "metadata": { "id": "N8eZwXjmYj-u" } } ] }