{ "cells": [ { "cell_type": "markdown", "id": "1972a352-a0e3-41b7-81dc-dd4ae2b890c3", "metadata": {}, "source": [ "## Online Meeting" ] }, { "cell_type": "markdown", "id": "05d999bc-83a3-454f-a8a4-44cbff1fcedc", "metadata": {}, "source": [ "\r\n", " \"Open\r\n", "" ] }, { "cell_type": "markdown", "id": "fe3ed1ce-d38d-4048-9db6-9707b55dc642", "metadata": {}, "source": [ "Using generative AI like ChatGPT in online meetings can greatly improve work efficiency (e.g., **Teams**). However, the context in such applications tends to be more conversational, with a high degree of redundancy and a large number of tokens(more than **40k**). By utilizing LLMLingua to compress prompts, we can significantly reduce the length of prompts, which in turn helps to reduce latency. This makes the AI more efficient and responsive in real-time communication scenarios like online meetings, enabling smoother interactions and better overall performance. We use meeting transcripts from the [**MeetingBank** dataset](https://huggingface.co/datasets/lytang/MeetingBank-transcript) as an example to demonstrate the capabilities of LLMLingua." ] }, { "cell_type": "markdown", "id": "18422597-687a-43aa-a6ed-ce6244d0eb55", "metadata": {}, "source": [ "### MeetingBank Dataset" ] }, { "cell_type": "markdown", "id": "51a7accd-5ec2-4ed2-9582-1afdb441a998", "metadata": {}, "source": [ "Next, we will demonstrate the use of LongLLMLingua on the **MeetingBank** dataset, which can achieve similar or even better performance with significantly fewer tokens. The online meeting scenario is quite similar to RAG, as it also suffers from the \"lost in the middle\" issue, where noise data at the beginning or end of the prompt interferes with LLMs extracting key information. This dataset closely resembles real-world online meeting scenarios, with prompt lengths exceeding **60k tokens at their longest. \n", " \n", "The original dataset can be found at https://huggingface.co/datasets/lytang/MeetingBank-transcript" ] }, { "cell_type": "code", "execution_count": 1, "id": "a970a901-11bd-43af-a8bc-7fb2fc6a1a07", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Defaulting to user installation because normal site-packages is not writeable\n", "Requirement already satisfied: llmlingua in /home/hjiang/Code/github/LLMLingua (0.1.2)\n", "Requirement already satisfied: datasets in /home/hjiang/.local/lib/python3.9/site-packages (2.14.4)\n", "Requirement already satisfied: nltk in /home/hjiang/.local/lib/python3.9/site-packages (from llmlingua) (3.8.1)\n", "Requirement already satisfied: numpy in /home/hjiang/.local/lib/python3.9/site-packages (from llmlingua) (1.23.5)\n", "Requirement already satisfied: tiktoken in /home/hjiang/.local/lib/python3.9/site-packages (from llmlingua) (0.4.0)\n", "Requirement already satisfied: torch in /home/hjiang/.local/lib/python3.9/site-packages (from llmlingua) (1.13.1+cu116)\n", "Requirement already satisfied: transformers>=4.26.0 in /home/hjiang/.local/lib/python3.9/site-packages (from llmlingua) (4.34.1)\n", "Requirement already satisfied: pyarrow>=8.0.0 in /home/hjiang/.local/lib/python3.9/site-packages (from datasets) (11.0.0)\n", "Requirement already satisfied: dill<0.3.8,>=0.3.0 in /home/hjiang/.local/lib/python3.9/site-packages (from datasets) (0.3.7)\n", "Requirement already satisfied: pandas in /home/hjiang/.local/lib/python3.9/site-packages (from datasets) (2.0.3)\n", "Requirement already satisfied: requests>=2.19.0 in /home/hjiang/.local/lib/python3.9/site-packages (from datasets) (2.29.0)\n", "Requirement already satisfied: tqdm>=4.62.1 in /home/hjiang/.local/lib/python3.9/site-packages (from datasets) (4.65.0)\n", "Requirement already satisfied: xxhash in /home/hjiang/.local/lib/python3.9/site-packages (from datasets) (3.3.0)\n", "Requirement already satisfied: multiprocess in /home/hjiang/.local/lib/python3.9/site-packages (from datasets) (0.70.15)\n", "Requirement already satisfied: fsspec[http]>=2021.11.1 in /home/hjiang/.local/lib/python3.9/site-packages (from datasets) (2023.6.0)\n", "Requirement already satisfied: aiohttp in /home/hjiang/.local/lib/python3.9/site-packages (from datasets) (3.8.5)\n", "Requirement already satisfied: huggingface-hub<1.0.0,>=0.14.0 in /home/hjiang/.local/lib/python3.9/site-packages (from datasets) (0.16.4)\n", "Requirement already satisfied: packaging in /home/hjiang/.local/lib/python3.9/site-packages (from datasets) (23.0)\n", "Requirement already satisfied: pyyaml>=5.1 in /usr/lib/python3/dist-packages (from datasets) (5.3.1)\n", "Requirement already satisfied: attrs>=17.3.0 in /home/hjiang/.local/lib/python3.9/site-packages (from aiohttp->datasets) (23.1.0)\n", "Requirement already satisfied: charset-normalizer<4.0,>=2.0 in /home/hjiang/.local/lib/python3.9/site-packages (from aiohttp->datasets) (3.2.0)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in /home/hjiang/.local/lib/python3.9/site-packages (from aiohttp->datasets) (6.0.4)\n", "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /home/hjiang/.local/lib/python3.9/site-packages (from aiohttp->datasets) (4.0.2)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in /home/hjiang/.local/lib/python3.9/site-packages (from aiohttp->datasets) (1.9.2)\n", "Requirement already satisfied: frozenlist>=1.1.1 in /home/hjiang/.local/lib/python3.9/site-packages (from aiohttp->datasets) (1.4.0)\n", "Requirement already satisfied: aiosignal>=1.1.2 in /home/hjiang/.local/lib/python3.9/site-packages (from aiohttp->datasets) (1.3.1)\n", "Requirement already satisfied: filelock in /home/hjiang/.local/lib/python3.9/site-packages (from huggingface-hub<1.0.0,>=0.14.0->datasets) (3.12.2)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /home/hjiang/.local/lib/python3.9/site-packages (from huggingface-hub<1.0.0,>=0.14.0->datasets) (4.7.1)\n", "Requirement already satisfied: idna<4,>=2.5 in /usr/lib/python3/dist-packages (from requests>=2.19.0->datasets) (2.8)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/hjiang/.local/lib/python3.9/site-packages (from requests>=2.19.0->datasets) (1.26.16)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests>=2.19.0->datasets) (2019.11.28)\n", "Requirement already satisfied: regex!=2019.12.17 in /home/hjiang/.local/lib/python3.9/site-packages (from transformers>=4.26.0->llmlingua) (2023.6.3)\n", "Requirement already satisfied: tokenizers<0.15,>=0.14 in /home/hjiang/.local/lib/python3.9/site-packages (from transformers>=4.26.0->llmlingua) (0.14.1)\n", "Requirement already satisfied: safetensors>=0.3.1 in /home/hjiang/.local/lib/python3.9/site-packages (from transformers>=4.26.0->llmlingua) (0.3.1)\n", "Requirement already satisfied: click in /home/hjiang/.local/lib/python3.9/site-packages (from nltk->llmlingua) (8.1.6)\n", "Requirement already satisfied: joblib in /home/hjiang/.local/lib/python3.9/site-packages (from nltk->llmlingua) (1.3.1)\n", "Requirement already satisfied: python-dateutil>=2.8.2 in /home/hjiang/.local/lib/python3.9/site-packages (from pandas->datasets) (2.8.2)\n", "Requirement already satisfied: pytz>=2020.1 in /home/hjiang/.local/lib/python3.9/site-packages (from pandas->datasets) (2023.3)\n", "Requirement already satisfied: tzdata>=2022.1 in /home/hjiang/.local/lib/python3.9/site-packages (from pandas->datasets) (2023.3)\n", "Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.8.2->pandas->datasets) (1.14.0)\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.1\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3.9 -m pip install --upgrade pip\u001b[0m\n" ] } ], "source": [ "# Install dependency.\n", "!pip install llmlingua datasets" ] }, { "cell_type": "code", "execution_count": 6, "id": "7bbb89f7-9f0e-4998-97a6-da033351ef1a", "metadata": {}, "outputs": [], "source": [ "# Download the original prompt and dataset\n", "from datasets import load_dataset\n", "dataset = load_dataset(\"lytang/MeetingBank-transcript\")[\"train\"]" ] }, { "cell_type": "code", "execution_count": 8, "id": "cbbbf3de-a9d6-46cf-afab-dcb72a6154ec", "metadata": {}, "outputs": [], "source": [ "# Using the OAI\n", "import openai\n", "openai.api_key = \"\"" ] }, { "cell_type": "code", "execution_count": 10, "id": "46506810-8565-43da-984b-d862c56b49c2", "metadata": {}, "outputs": [], "source": [ "# or Using the AOAI\n", "import openai\n", "openai.api_key = \"\"\n", "openai.api_base = \"https://xxxx.openai.azure.com/\"\n", "openai.api_type = 'azure'\n", "openai.api_version = '2023-05-15'" ] }, { "cell_type": "markdown", "id": "f8676ffa-5117-44dc-9742-bb9ab1d56e0c", "metadata": {}, "source": [ "### Setup Data" ] }, { "cell_type": "code", "execution_count": 12, "id": "cc17bbc5-86cb-4d15-a730-955af85a10b2", "metadata": {}, "outputs": [], "source": [ "# select an example from MeetingBank\n", "contexts = dataset[1][\"source\"]" ] }, { "cell_type": "markdown", "id": "ba1c6d52-dc87-434c-a41c-0bbc8a286504", "metadata": {}, "source": [ "### Q1" ] }, { "cell_type": "code", "execution_count": 13, "id": "f8a54b7f-5bd4-4d4f-9249-b900bd703884", "metadata": {}, "outputs": [], "source": [ "question = \"Question: How much did the crime rate increase last year?\\nAnswer:\"\n", "reference = \"5.4%\"" ] }, { "cell_type": "code", "execution_count": 15, "id": "3d441f10-c5c7-4d45-b09a-717e536b36bf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"id\": \"chatcmpl-8FNC3cZSVtzUCxOVhB04RxnEUVrf8\",\n", " \"object\": \"chat.completion\",\n", " \"created\": 1698674767,\n", " \"model\": \"gpt-4-32k\",\n", " \"choices\": [\n", " {\n", " \"index\": 0,\n", " \"finish_reason\": \"stop\",\n", " \"message\": {\n", " \"role\": \"assistant\",\n", " \"content\": \"The crime rate increased by 5.4% year to date.\"\n", " }\n", " }\n", " ],\n", " \"usage\": {\n", " \"prompt_tokens\": 30096,\n", " \"completion_tokens\": 14,\n", " \"total_tokens\": 30110\n", " }\n", "}\n" ] } ], "source": [ "# The response from original prompt, using GPT-4-32k\n", "import json\n", "prompt = \"\\n\\n\".join([contexts, question])\n", "\n", "message = [\n", " {\"role\": \"user\", \"content\": prompt},\n", "]\n", "\n", "request_data = {\n", " \"messages\": message,\n", " \"max_tokens\": 100,\n", " \"temperature\": 0,\n", " \"top_p\": 1,\n", " \"n\": 1,\n", " \"stream\": False,\n", "}\n", "response = openai.ChatCompletion.create(\n", " \"gpt-4-32k\",\n", " **request_data,\n", ")\n", "print(json.dumps(response, indent=4))" ] }, { "cell_type": "code", "execution_count": 1, "id": "7859d7d7-a6cd-499a-a780-643ba8e0b832", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ba191aa3d6554337a49e9b0896fc73e6", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Loading checkpoint shards: 0%| | 0/2 [00:00