chore: import upstream snapshot with attribution
Documentation / build (push) Has been cancelled
Documentation / deploy (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:34:55 +08:00
commit 86db9aae8e
563 changed files with 137401 additions and 0 deletions
@@ -0,0 +1,198 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "6sr3Gk8hNDaF"
},
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "eQk5Dv3jyheg"
},
"source": [
"# If you are using Colab for free, we highly recommend you activate the T4 GPU\n",
"# hardware accelerator. Our models are designed to run with at least 16GB\n",
"# of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as opposed\n",
"# to the ~13GB Colab gives automatically.\n",
"# To activate T4:\n",
"# 1. click on the \"Runtime\" tab\n",
"# 2. click on \"Change runtime type\"\n",
"# 3. select T4 GPU under Hardware Accelerator\n",
"# NOTE: there is a weekly usage limit on using T4 for free"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "td7dVt4fD_j6"
},
"outputs": [],
"source": [
"!pip install llmware"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "IY8ysw9FElZR"
},
"outputs": [],
"source": [
"!pip install --upgrade huggingface_hub\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "AeR-LvVXD27i"
},
"outputs": [],
"source": [
"\n",
"\"\"\" This example shows a complex multi-part research analysis. In this example, we will:\n",
"\n",
" 1. Build a \"research\" library.\n",
" 2. Query the research library to identify topics of interest.\n",
" 3. Create an agent with several analytical tools: sentiment, emotions, topic, entities analysis\n",
" 4. Pass the results of our query to the agent to conduct multifaceted analysis.\n",
" 5. Apply a top-level filter ('sentiment') on the results from the query\n",
" 6. For any of the passages with negative sentiment, we will run a follow-up set of analyses.\n",
" 7. Finally, we will assemble the follow-up analysis into a list of detailed reports.\n",
"\"\"\"\n",
"\n",
"import os\n",
"import shutil\n",
"\n",
"from llmware.agents import LLMfx\n",
"from llmware.library import Library\n",
"from llmware.retrieval import Query\n",
"from llmware.configs import LLMWareConfig\n",
"from llmware.setup import Setup\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "qG3lAj2nM372"
},
"outputs": [],
"source": [
"def multistep_analysis():\n",
"\n",
" \"\"\" In this example, our objective is to research Microsoft history and rivalry in the 1980s with IBM. \"\"\"\n",
"\n",
" # step 1 - assemble source documents and create library\n",
"\n",
" print(\"update: Starting example - agent-multistep-analysis\")\n",
"\n",
" # note: lines 38-49 attempt to automatically pull sample document into local path\n",
" # depending upon permissions in your environment, you may need to set up directly\n",
" # if you pull down the samples files with Setup().load_sample_files(), in the Books folder,\n",
" # you will find the source: \"Bill-Gates-Biography.pdf\"\n",
" # if you have pulled sample documents in the past, then to update to latest: set over_write=True\n",
"\n",
" print(\"update: Loading sample files\")\n",
"\n",
" sample_files_path = Setup().load_sample_files(over_write=False)\n",
" bill_gates_bio = \"Bill-Gates-Biography.pdf\"\n",
" path_to_bill_gates_bio = os.path.join(sample_files_path, \"Books\", bill_gates_bio)\n",
"\n",
" microsoft_folder = os.path.join(LLMWareConfig().get_tmp_path(), \"example_microsoft\")\n",
"\n",
" print(\"update: attempting to create source input folder at path: \", microsoft_folder)\n",
"\n",
" if not os.path.exists(microsoft_folder):\n",
" os.mkdir(microsoft_folder)\n",
" os.chmod(microsoft_folder, 0o777)\n",
" shutil.copy(path_to_bill_gates_bio,os.path.join(microsoft_folder, bill_gates_bio))\n",
"\n",
" # create library\n",
" print(\"update: creating library and parsing source document\")\n",
"\n",
" LLMWareConfig().set_active_db(\"sqlite\")\n",
" my_lib = Library().create_new_library(\"microsoft_history_0210_1\")\n",
" my_lib.add_files(microsoft_folder)\n",
"\n",
" # run our first query - \"ibm\"\n",
" query = \"ibm\"\n",
" search_results = Query(my_lib).text_query(query)\n",
" print(f\"update: executing query to filter to key passages - {query} - results found - {len(search_results)}\")\n",
"\n",
" # create an agent and load several tools that we will be using\n",
" agent = LLMfx()\n",
" agent.load_tool_list([\"sentiment\", \"emotions\", \"topic\", \"tags\", \"ner\", \"answer\"])\n",
"\n",
" # load the search results into the agent's work queue\n",
" agent.load_work(search_results)\n",
"\n",
" while True:\n",
"\n",
" agent.sentiment()\n",
"\n",
" if not agent.increment_work_iteration():\n",
" break\n",
"\n",
" # analyze sections where the sentiment on ibm was negative\n",
" follow_up_list = agent.follow_up_list(key=\"sentiment\", value=\"negative\")\n",
"\n",
" for job_index in follow_up_list:\n",
"\n",
" # follow-up 'deep dive' on selected text that references ibm negatively\n",
" agent.set_work_iteration(job_index)\n",
" agent.exec_multitool_function_call([\"tags\", \"emotions\", \"topics\", \"ner\"])\n",
" agent.answer(\"What is a brief summary?\", key=\"summary\")\n",
"\n",
" my_report = agent.show_report(follow_up_list)\n",
"\n",
" activity_summary = agent.activity_summary()\n",
"\n",
" for entries in my_report:\n",
" print(\"my report entries: \", entries)\n",
"\n",
" return my_report"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "NDKDVuEoM9rL"
},
"outputs": [],
"source": [
"\n",
"if __name__ == \"__main__\":\n",
"\n",
" multistep_analysis()\n",
"\n"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"gpuType": "T4",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@@ -0,0 +1,468 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "eQk5Dv3jyheg"
},
"source": [
"# If you are using Colab for free, we highly recommend you activate the T4 GPU\n",
"# hardware accelerator. Our models are designed to run with at least 16GB\n",
"# of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as opposed\n",
"# to the ~13GB Colab gives automatically.\n",
"# To activate T4:\n",
"# 1. click on the \"Runtime\" tab\n",
"# 2. click on \"Change runtime type\"\n",
"# 3. select T4 GPU under Hardware Accelerator\n",
"# NOTE: there is a weekly usage limit on using T4 for free"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "POFehtFWxwlz"
},
"source": [
"\n",
" This example shows an end-to-end recipe for creating a CustomTable, and then creating an Agent process that\n",
" will query the table using natural language.\n",
"\n",
" Please note that this example is a 'generalized' and updated version of an earlier example -\n",
" \"text2sql-end-to-end-2.py\" - now using the more powerful CustomTables class integrated into the LLMfx process\n",
"\n",
" The example shows the following steps:\n",
"\n",
" 1. Creating a custom table resource from a sample CSV file, included in the slim-sql-tool kit, and also\n",
" available in the Examples section with Structured_Tables (customer_table.csv)\n",
"\n",
" 2 Asking basic natural language questions:\n",
" A. Looks up the table schema\n",
" B. Packages the table schema with query\n",
" C. Runs inference to convert text into SQL\n",
" D. Queries the database with the generated SQL\n",
" E. Returns result\n",
"\n",
" 3. Using CustomtTable class, this can be run on either Postgres or SQLite DB.\n",
"\n",
" Note: as you substitute for your own CSV and JSON, check out the other examples in this section for loading\n",
" configuration ideas and options.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gWyhnhW4v9zm"
},
"source": [
"This notebook is designed for the google collab environment\n",
"\n",
"\n",
"1) The first cell will allow this notebook to access files in your google drive. The example csv for this notebook is located at https://github.com/llmware-ai/llmware/blob/main/examples/Structured_Tables/customer_table.csv\n",
"\n",
"Upload it to your drive, and ensure that when you input parameters you point to the right path where the csv is located.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "AFH-qyZ_tVpn",
"outputId": "1a51d6ea-af94-441c-c99b-e2457108e8dc"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
]
}
],
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')\n"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "sDjd_1IPoGx9",
"outputId": "575d6053-98b6-4098-defb-e1c3cce6777b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: llmware in /usr/local/lib/python3.10/dist-packages (0.3.0)\n",
"Requirement already satisfied: boto3>=1.24.53 in /usr/local/lib/python3.10/dist-packages (from llmware) (1.34.124)\n",
"Requirement already satisfied: huggingface-hub>=0.19.4 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.23.2)\n",
"Requirement already satisfied: numpy>=1.23.2 in /usr/local/lib/python3.10/dist-packages (from llmware) (1.25.2)\n",
"Requirement already satisfied: pymongo>=4.7.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (4.7.3)\n",
"Requirement already satisfied: tokenizers>=0.15.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.19.1)\n",
"Requirement already satisfied: psycopg-binary==3.1.17 in /usr/local/lib/python3.10/dist-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: psycopg==3.1.17 in /usr/local/lib/python3.10/dist-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: pgvector==0.2.4 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.2.4)\n",
"Requirement already satisfied: colorama==0.4.6 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.4.6)\n",
"Requirement already satisfied: librosa>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.10.2.post1)\n",
"Requirement already satisfied: typing-extensions>=4.1 in /usr/local/lib/python3.10/dist-packages (from psycopg==3.1.17->llmware) (4.12.1)\n",
"Requirement already satisfied: botocore<1.35.0,>=1.34.124 in /usr/local/lib/python3.10/dist-packages (from boto3>=1.24.53->llmware) (1.34.124)\n",
"Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from boto3>=1.24.53->llmware) (1.0.1)\n",
"Requirement already satisfied: s3transfer<0.11.0,>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from boto3>=1.24.53->llmware) (0.10.1)\n",
"Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (3.14.0)\n",
"Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (2023.6.0)\n",
"Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (24.0)\n",
"Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (6.0.1)\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (2.31.0)\n",
"Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (4.66.4)\n",
"Requirement already satisfied: audioread>=2.1.9 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (3.0.1)\n",
"Requirement already satisfied: scipy>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.11.4)\n",
"Requirement already satisfied: scikit-learn>=0.20.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.2.2)\n",
"Requirement already satisfied: joblib>=0.14 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.4.2)\n",
"Requirement already satisfied: decorator>=4.3.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (4.4.2)\n",
"Requirement already satisfied: numba>=0.51.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.58.1)\n",
"Requirement already satisfied: soundfile>=0.12.1 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.12.1)\n",
"Requirement already satisfied: pooch>=1.1 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.8.1)\n",
"Requirement already satisfied: soxr>=0.3.2 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.3.7)\n",
"Requirement already satisfied: lazy-loader>=0.1 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.4)\n",
"Requirement already satisfied: msgpack>=1.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.0.8)\n",
"Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in /usr/local/lib/python3.10/dist-packages (from pymongo>=4.7.0->llmware) (2.6.1)\n",
"Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /usr/local/lib/python3.10/dist-packages (from botocore<1.35.0,>=1.34.124->boto3>=1.24.53->llmware) (2.8.2)\n",
"Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in /usr/local/lib/python3.10/dist-packages (from botocore<1.35.0,>=1.34.124->boto3>=1.24.53->llmware) (2.0.7)\n",
"Requirement already satisfied: llvmlite<0.42,>=0.41.0dev0 in /usr/local/lib/python3.10/dist-packages (from numba>=0.51.0->librosa>=0.10.0->llmware) (0.41.1)\n",
"Requirement already satisfied: platformdirs>=2.5.0 in /usr/local/lib/python3.10/dist-packages (from pooch>=1.1->librosa>=0.10.0->llmware) (4.2.2)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.4->llmware) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.4->llmware) (3.7)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.4->llmware) (2024.6.2)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=0.20.0->librosa>=0.10.0->llmware) (3.5.0)\n",
"Requirement already satisfied: cffi>=1.0 in /usr/local/lib/python3.10/dist-packages (from soundfile>=0.12.1->librosa>=0.10.0->llmware) (1.16.0)\n",
"Requirement already satisfied: pycparser in /usr/local/lib/python3.10/dist-packages (from cffi>=1.0->soundfile>=0.12.1->librosa>=0.10.0->llmware) (2.22)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.35.0,>=1.34.124->boto3>=1.24.53->llmware) (1.16.0)\n"
]
}
],
"source": [
"!pip install llmware"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"id": "zAUBmgSptRgQ"
},
"outputs": [],
"source": [
"import os\n",
"from llmware.agents import LLMfx\n",
"from llmware.resources import CustomTable\n",
"from llmware.configs import LLMWareConfig\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"id": "P0okOaH0taa0"
},
"outputs": [],
"source": [
"def build_table(db=None, table_name=None, load_fp=None, load_file=None):\n",
" \"\"\"Simple example script to take a CSV or JSON/JSONL and create a DB Table.\"\"\"\n",
" custom_table = CustomTable(db=db, table_name=table_name)\n",
" analysis = custom_table.validate_csv(load_fp, load_file)\n",
" print(\"update: analysis from validate_csv: \", analysis)\n",
"\n",
" if load_file.endswith(\".csv\"):\n",
" output = custom_table.load_csv(load_fp, load_file)\n",
" elif load_file.endswith(\".jsonl\") or load_file.endswith(\".json\"):\n",
" output = custom_table.load_json(load_fp, load_file)\n",
" else:\n",
" print(\"file type not supported for db load\")\n",
" return -1\n",
"\n",
" print(\"update: output from loading file: \", output)\n",
"\n",
" sample_range = min(10, len(custom_table.rows))\n",
" for x in range(0, sample_range):\n",
" print(\"update: sample rows: \", x, custom_table.rows[x])\n",
"\n",
" updated_schema = custom_table.test_and_remediate_schema(samples=20, auto_remediate=True)\n",
" print(\"update: updated schema: \", updated_schema)\n",
"\n",
" custom_table.insert_rows()\n",
" return 1\n"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"id": "Q6uD3D-wtfOG"
},
"outputs": [],
"source": [
"def agent_natural_language_sql_query(query_list, db=None, table_name=None):\n",
" \"\"\"Query a CustomTable in natural language.\"\"\"\n",
" agent = LLMfx()\n",
" agent.load_tool(\"sql\", sample=False, get_logits=True, temperature=0.0)\n",
"\n",
" for i, query in enumerate(query_list):\n",
" response = agent.query_custom_table(query, db=db, table=table_name)\n",
"\n",
" for x in range(len(agent.research_list)):\n",
" print(\"research: \", x, agent.research_list[x])\n",
"\n",
" return agent.research_list\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bJaDKQyAthUO",
"outputId": "1931b424-b49f-4176-8c0f-aa2c68d5304a"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"update: analysis from validate_csv: {'rows': 20, 'columns': 6, 'conforming_rows_percent': 1.0, 'column_frequency_analysis': {6: 20}, 'nonconforming_rows': []}\n",
"update: output from loading file: {'rows': 19, 'columns': 6, 'schema': {'customer_name': 'text', 'account_number': 'integer', 'customer_level': 'text', 'vip_customer': 'text', 'annual_spend': 'integer', 'user_name': 'text'}, 'skipped_rows': []}\n",
"update: sample rows: 0 {'customer_name': 'Martha Williams', 'account_number': '98320893', 'customer_level': 'gold', 'vip_customer': 'yes', 'annual_spend': '63250', 'user_name': 'mwilliams'}\n",
"update: sample rows: 1 {'customer_name': 'Susan Soinsin', 'account_number': '53439382', 'customer_level': 'silver', 'vip_customer': 'no', 'annual_spend': '112 ', 'user_name': 'ssinsin'}\n",
"update: sample rows: 2 {'customer_name': 'Michael Rogers', 'account_number': '88888444', 'customer_level': 'bronze', 'vip_customer': 'no', 'annual_spend': '3 ', 'user_name': 'rogersm'}\n",
"update: sample rows: 3 {'customer_name': 'John Jones', 'account_number': '9898482', 'customer_level': 'gold', 'vip_customer': 'yes', 'annual_spend': '12430', 'user_name': 'jjns'}\n",
"update: sample rows: 4 {'customer_name': 'Melinda Lyons', 'account_number': '1234953', 'customer_level': 'silver', 'vip_customer': 'no', 'annual_spend': '234', 'user_name': 'mlyons'}\n",
"update: sample rows: 5 {'customer_name': 'Arvind Arora', 'account_number': '8998899', 'customer_level': 'gold', 'vip_customer': 'yes', 'annual_spend': '42650', 'user_name': 'aarora'}\n",
"update: sample rows: 6 {'customer_name': 'Paul Rinno', 'account_number': '3829235', 'customer_level': 'silver', 'vip_customer': 'no', 'annual_spend': '1293', 'user_name': 'prinno'}\n",
"update: sample rows: 7 {'customer_name': 'Vinod Aggarwal', 'account_number': '9389532', 'customer_level': 'platinum', 'vip_customer': 'yes', 'annual_spend': '93540', 'user_name': 'aggarwal'}\n",
"update: sample rows: 8 {'customer_name': 'Bryan Lewis', 'account_number': '12399853', 'customer_level': 'gold', 'vip_customer': 'no', 'annual_spend': '9732', 'user_name': 'brlewis'}\n",
"update: sample rows: 9 {'customer_name': 'Allison Winters', 'account_number': '83902867', 'customer_level': 'gold', 'vip_customer': 'yes', 'annual_spend': '15000', 'user_name': 'wintersall'}\n",
"update: updated schema: {'customer_name': 'text', 'account_number': 'integer', 'customer_level': 'text', 'vip_customer': 'text', 'annual_spend': 'integer', 'user_name': 'text'}\n"
]
},
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Input parameters\n",
"db = \"sqlite\" # SQLite database path\n",
"table_name = \"customer_table\"\n",
"\n",
"# Path to the CSV file in Google Drive\n",
"input_fp = \"/content/drive/My Drive\"\n",
"input_fn = \"customer_table.csv\"\n",
"\n",
"# Build the table\n",
"build_table(db=db, table_name=table_name, load_fp=input_fp, load_file=input_fn)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FJQlgHC9uq8Q",
"outputId": "2a9f6715-2cd7-41b5-aca6-ed3c9f05fb0e"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"update: Launching LLMfx process\n",
"step - \t1 - \tcreating object - ready to start processing.\n",
"step - \t2 - \tloading tool - sql\n",
"step - \t3 - \tloading new processing text - 1 new entries\n",
"step - \t4 - \texecuting function call - deploying - text-to-sql\n",
"\t\t\t\t -- query - Which customers have vip customer status of yes?\n",
"\t\t\t\t -- table_schema - CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )\n",
"step - \t5 - \texecuting function call - getting response - sql\n",
"\t\t\t\t -- llm_response - SELECT customer_name FROM customer_table WHERE vip_customer = 'yes'\n",
"\t\t\t\t -- output type - text\n",
"\t\t\t\t -- usage - {'input': 58, 'output': 17, 'total': 75, 'metric': 'tokens', 'processing_time': 1.0267748832702637}\n",
"step - \t6 - \texecuting research call - executing query on db\n",
"\t\t\t\t -- db - sqlite\n",
"\t\t\t\t -- sql_query - SELECT customer_name FROM customer_table WHERE vip_customer = 'yes'\n",
"step - \t7 - \texecuting research - getting response - sql\n",
"\t\t\t\t -- result - [('Martha Williams',), ('John Jones',), ('Arvind Arora',), ('Vinod Aggarwal',), ('Allison Winters',), ('Olivia Smith',), ('Alan Wang',), ('Wilmer Rodriguez',), ('Eliza Listron',), ('Douglas Hudson',), ('Martha Williams',), ('John Jones',), ('Arvind Arora',), ('Vinod Aggarwal',), ('Allison Winters',), ('Olivia Smith',), ('Alan Wang',), ('Wilmer Rodriguez',), ('Eliza Listron',), ('Douglas Hudson',)]\n",
"step - \t8 - \tloading new processing text - 1 new entries\n",
"step - \t9 - \texecuting function call - deploying - text-to-sql\n",
"\t\t\t\t -- query - What is the highest annual spend of any customer?\n",
"\t\t\t\t -- table_schema - CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )\n",
"step - \t10 - \texecuting function call - getting response - sql\n",
"\t\t\t\t -- llm_response - SELECT MAX(annual_spend) FROM customer_table\n",
"\t\t\t\t -- output type - text\n",
"\t\t\t\t -- usage - {'input': 57, 'output': 13, 'total': 70, 'metric': 'tokens', 'processing_time': 1.495262861251831}\n",
"step - \t11 - \texecuting research call - executing query on db\n",
"\t\t\t\t -- db - sqlite\n",
"\t\t\t\t -- sql_query - SELECT MAX(annual_spend) FROM customer_table\n",
"step - \t12 - \texecuting research - getting response - sql\n",
"\t\t\t\t -- result - [(93540,)]\n",
"step - \t13 - \tloading new processing text - 1 new entries\n",
"step - \t14 - \texecuting function call - deploying - text-to-sql\n",
"\t\t\t\t -- query - Which customer has account number 1234953\n",
"\t\t\t\t -- table_schema - CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )\n",
"step - \t15 - \texecuting function call - getting response - sql\n",
"\t\t\t\t -- llm_response - SELECT customer_name FROM customer_table WHERE account_number = 1234953\n",
"\t\t\t\t -- output type - text\n",
"\t\t\t\t -- usage - {'input': 61, 'output': 21, 'total': 82, 'metric': 'tokens', 'processing_time': 2.2324819564819336}\n",
"step - \t16 - \texecuting research call - executing query on db\n",
"\t\t\t\t -- db - sqlite\n",
"\t\t\t\t -- sql_query - SELECT customer_name FROM customer_table WHERE account_number = 1234953\n",
"step - \t17 - \texecuting research - getting response - sql\n",
"\t\t\t\t -- result - [('Melinda Lyons',), ('Melinda Lyons',)]\n",
"step - \t18 - \tloading new processing text - 1 new entries\n",
"step - \t19 - \texecuting function call - deploying - text-to-sql\n",
"\t\t\t\t -- query - Which customer has the lowest annual spend?\n",
"\t\t\t\t -- table_schema - CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )\n",
"step - \t20 - \texecuting function call - getting response - sql\n",
"\t\t\t\t -- llm_response - SELECT customer_name FROM customer_table ORDER BY annual_spend LIMIT 1\n",
"\t\t\t\t -- output type - text\n",
"\t\t\t\t -- usage - {'input': 56, 'output': 17, 'total': 73, 'metric': 'tokens', 'processing_time': 1.4454705715179443}\n",
"step - \t21 - \texecuting research call - executing query on db\n",
"\t\t\t\t -- db - sqlite\n",
"\t\t\t\t -- sql_query - SELECT customer_name FROM customer_table ORDER BY annual_spend LIMIT 1\n",
"step - \t22 - \texecuting research - getting response - sql\n",
"\t\t\t\t -- result - [('Michael Rogers',)]\n",
"step - \t23 - \tloading new processing text - 1 new entries\n",
"step - \t24 - \texecuting function call - deploying - text-to-sql\n",
"\t\t\t\t -- query - Is Susan Soinsin a vip customer?\n",
"\t\t\t\t -- table_schema - CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )\n",
"step - \t25 - \texecuting function call - getting response - sql\n",
"\t\t\t\t -- llm_response - SELECT vip_customer FROM customer_table WHERE customer_name = 'Susan Soinsin'\n",
"\t\t\t\t -- output type - text\n",
"\t\t\t\t -- usage - {'input': 57, 'output': 22, 'total': 79, 'metric': 'tokens', 'processing_time': 1.4810450077056885}\n",
"step - \t26 - \texecuting research call - executing query on db\n",
"\t\t\t\t -- db - sqlite\n",
"\t\t\t\t -- sql_query - SELECT vip_customer FROM customer_table WHERE customer_name = 'Susan Soinsin'\n",
"step - \t27 - \texecuting research - getting response - sql\n",
"\t\t\t\t -- result - [('no',), ('no',)]\n",
"research: 0 {'step': 6, 'tool': 'sql', 'db_response': [('Martha Williams',), ('John Jones',), ('Arvind Arora',), ('Vinod Aggarwal',), ('Allison Winters',), ('Olivia Smith',), ('Alan Wang',), ('Wilmer Rodriguez',), ('Eliza Listron',), ('Douglas Hudson',), ('Martha Williams',), ('John Jones',), ('Arvind Arora',), ('Vinod Aggarwal',), ('Allison Winters',), ('Olivia Smith',), ('Alan Wang',), ('Wilmer Rodriguez',), ('Eliza Listron',), ('Douglas Hudson',)], 'sql_query': \"SELECT customer_name FROM customer_table WHERE vip_customer = 'yes'\", 'query': 'Which customers have vip customer status of yes?', 'db': 'sqlite', 'work_item': 'CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'}\n",
"research: 1 {'step': 11, 'tool': 'sql', 'db_response': [(93540,)], 'sql_query': 'SELECT MAX(annual_spend) FROM customer_table', 'query': 'What is the highest annual spend of any customer?', 'db': 'sqlite', 'work_item': 'CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'}\n",
"research: 2 {'step': 16, 'tool': 'sql', 'db_response': [('Melinda Lyons',), ('Melinda Lyons',)], 'sql_query': 'SELECT customer_name FROM customer_table WHERE account_number = 1234953', 'query': 'Which customer has account number 1234953', 'db': 'sqlite', 'work_item': 'CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'}\n",
"research: 3 {'step': 21, 'tool': 'sql', 'db_response': [('Michael Rogers',)], 'sql_query': 'SELECT customer_name FROM customer_table ORDER BY annual_spend LIMIT 1', 'query': 'Which customer has the lowest annual spend?', 'db': 'sqlite', 'work_item': 'CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'}\n",
"research: 4 {'step': 26, 'tool': 'sql', 'db_response': [('no',), ('no',)], 'sql_query': \"SELECT vip_customer FROM customer_table WHERE customer_name = 'Susan Soinsin'\", 'query': 'Is Susan Soinsin a vip customer?', 'db': 'sqlite', 'work_item': 'CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'}\n"
]
},
{
"data": {
"text/plain": [
"[{'step': 6,\n",
" 'tool': 'sql',\n",
" 'db_response': [('Martha Williams',),\n",
" ('John Jones',),\n",
" ('Arvind Arora',),\n",
" ('Vinod Aggarwal',),\n",
" ('Allison Winters',),\n",
" ('Olivia Smith',),\n",
" ('Alan Wang',),\n",
" ('Wilmer Rodriguez',),\n",
" ('Eliza Listron',),\n",
" ('Douglas Hudson',),\n",
" ('Martha Williams',),\n",
" ('John Jones',),\n",
" ('Arvind Arora',),\n",
" ('Vinod Aggarwal',),\n",
" ('Allison Winters',),\n",
" ('Olivia Smith',),\n",
" ('Alan Wang',),\n",
" ('Wilmer Rodriguez',),\n",
" ('Eliza Listron',),\n",
" ('Douglas Hudson',)],\n",
" 'sql_query': \"SELECT customer_name FROM customer_table WHERE vip_customer = 'yes'\",\n",
" 'query': 'Which customers have vip customer status of yes?',\n",
" 'db': 'sqlite',\n",
" 'work_item': 'CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'},\n",
" {'step': 11,\n",
" 'tool': 'sql',\n",
" 'db_response': [(93540,)],\n",
" 'sql_query': 'SELECT MAX(annual_spend) FROM customer_table',\n",
" 'query': 'What is the highest annual spend of any customer?',\n",
" 'db': 'sqlite',\n",
" 'work_item': 'CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'},\n",
" {'step': 16,\n",
" 'tool': 'sql',\n",
" 'db_response': [('Melinda Lyons',), ('Melinda Lyons',)],\n",
" 'sql_query': 'SELECT customer_name FROM customer_table WHERE account_number = 1234953',\n",
" 'query': 'Which customer has account number 1234953',\n",
" 'db': 'sqlite',\n",
" 'work_item': 'CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'},\n",
" {'step': 21,\n",
" 'tool': 'sql',\n",
" 'db_response': [('Michael Rogers',)],\n",
" 'sql_query': 'SELECT customer_name FROM customer_table ORDER BY annual_spend LIMIT 1',\n",
" 'query': 'Which customer has the lowest annual spend?',\n",
" 'db': 'sqlite',\n",
" 'work_item': 'CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'},\n",
" {'step': 26,\n",
" 'tool': 'sql',\n",
" 'db_response': [('no',), ('no',)],\n",
" 'sql_query': \"SELECT vip_customer FROM customer_table WHERE customer_name = 'Susan Soinsin'\",\n",
" 'query': 'Is Susan Soinsin a vip customer?',\n",
" 'db': 'sqlite',\n",
" 'work_item': 'CREATE TABLE customer_table (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'}]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"query_list = [\n",
" \"Which customers have vip customer status of yes?\",\n",
" \"What is the highest annual spend of any customer?\",\n",
" \"Which customer has account number 1234953\",\n",
" \"Which customer has the lowest annual spend?\",\n",
" \"Is Susan Soinsin a vip customer?\"\n",
"]\n",
"\n",
"# Execute the queries\n",
"agent_natural_language_sql_query(query_list, db=db, table_name=table_name)\n"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"gpuType": "T4",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@@ -0,0 +1,339 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Document Summaries with Small Language Models (SLIMs Cookbook)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The need for document summaries\n",
"\n",
"Often, we face an issue with smaller models, where the context window of the model is relatively small. If we want to pass in entire documents to the model that exceed the context window, we need to find a way to summarize the contents of the documents. Let's look at an example of how to do this with LLMWare."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### For Google Colab users"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you are using Colab for free, we highly recommend you activate the T4 GPU hardware accelerator. Our models are designed to run with at least 16GB of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as apposed to the ~13GB Colab gives automatically.\n",
"\n",
"To activate T4:\n",
"1. click on the \"Runtime\" tab\n",
"2. click on \"Change runtime type\"\n",
"3. select T4 GPU under Hardware Accelerator\n",
"\n",
"NOTE: there is a weekly usage limit on using T4 for free"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Installing and importing dependencies"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Defaulting to user installation because normal site-packages is not writeable\n",
"Requirement already satisfied: llmware in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (0.2.14)\n",
"Requirement already satisfied: boto3==1.24.53 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (1.24.53)\n",
"Requirement already satisfied: huggingface-hub>=0.19.4 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.19.4)\n",
"Requirement already satisfied: numpy>=1.23.2 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (1.26.0)\n",
"Requirement already satisfied: openai>=1.0.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (1.13.3)\n",
"Requirement already satisfied: pymongo>=4.7.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (4.7.2)\n",
"Requirement already satisfied: tokenizers>=0.15.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.15.2)\n",
"Requirement already satisfied: torch>=1.13.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (2.1.0+cu121)\n",
"Requirement already satisfied: transformers>=4.36.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (4.38.2)\n",
"Requirement already satisfied: Wikipedia-API==0.6.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.6.0)\n",
"Requirement already satisfied: psycopg-binary==3.1.17 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: psycopg==3.1.17 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: pgvector==0.2.4 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.2.4)\n",
"Requirement already satisfied: colorama==0.4.6 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.4.6)\n",
"Requirement already satisfied: einops==0.7.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.7.0)\n",
"Requirement already satisfied: librosa>=0.10.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.10.2.post1)\n",
"Requirement already satisfied: botocore<1.28.0,>=1.27.53 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from boto3==1.24.53->llmware) (1.27.96)\n",
"Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from boto3==1.24.53->llmware) (1.0.1)\n",
"Requirement already satisfied: s3transfer<0.7.0,>=0.6.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from boto3==1.24.53->llmware) (0.6.2)\n",
"Requirement already satisfied: typing-extensions>=4.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from psycopg==3.1.17->llmware) (4.8.0)\n",
"Requirement already satisfied: tzdata in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from psycopg==3.1.17->llmware) (2023.3)\n",
"Requirement already satisfied: requests in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from Wikipedia-API==0.6.0->llmware) (2.31.0)\n",
"Requirement already satisfied: filelock in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (3.13.1)\n",
"Requirement already satisfied: fsspec>=2023.5.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (2023.10.0)\n",
"Requirement already satisfied: tqdm>=4.42.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (4.66.1)\n",
"Requirement already satisfied: pyyaml>=5.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (6.0.1)\n",
"Requirement already satisfied: packaging>=20.9 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (23.1)\n",
"Requirement already satisfied: audioread>=2.1.9 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (3.0.1)\n",
"Requirement already satisfied: scipy>=1.2.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.11.3)\n",
"Requirement already satisfied: scikit-learn>=0.20.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.3.1)\n",
"Requirement already satisfied: joblib>=0.14 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.3.2)\n",
"Requirement already satisfied: decorator>=4.3.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (5.1.1)\n",
"Requirement already satisfied: numba>=0.51.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (0.59.1)\n",
"Requirement already satisfied: soundfile>=0.12.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (0.12.1)\n",
"Requirement already satisfied: pooch>=1.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.8.1)\n",
"Requirement already satisfied: soxr>=0.3.2 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (0.3.7)\n",
"Requirement already satisfied: lazy-loader>=0.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (0.4)\n",
"Requirement already satisfied: msgpack>=1.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.0.8)\n",
"Requirement already satisfied: anyio<5,>=3.5.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from openai>=1.0.0->llmware) (4.0.0)\n",
"Requirement already satisfied: distro<2,>=1.7.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from openai>=1.0.0->llmware) (1.9.0)\n",
"Requirement already satisfied: httpx<1,>=0.23.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from openai>=1.0.0->llmware) (0.23.3)\n",
"Requirement already satisfied: pydantic<3,>=1.9.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from openai>=1.0.0->llmware) (2.6.4)\n",
"Requirement already satisfied: sniffio in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from openai>=1.0.0->llmware) (1.3.0)\n",
"Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from pymongo>=4.7.0->llmware) (2.6.1)\n",
"Requirement already satisfied: sympy in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from torch>=1.13.1->llmware) (1.12)\n",
"Requirement already satisfied: networkx in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from torch>=1.13.1->llmware) (3.2.1)\n",
"Requirement already satisfied: jinja2 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from torch>=1.13.1->llmware) (3.1.2)\n",
"Requirement already satisfied: regex!=2019.12.17 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from transformers>=4.36.0->llmware) (2023.12.25)\n",
"Requirement already satisfied: safetensors>=0.4.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from transformers>=4.36.0->llmware) (0.4.2)\n",
"Requirement already satisfied: idna>=2.8 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from anyio<5,>=3.5.0->openai>=1.0.0->llmware) (2.10)\n",
"Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from botocore<1.28.0,>=1.27.53->boto3==1.24.53->llmware) (2.8.2)\n",
"Requirement already satisfied: urllib3<1.27,>=1.25.4 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from botocore<1.28.0,>=1.27.53->boto3==1.24.53->llmware) (1.26.18)\n",
"Requirement already satisfied: certifi in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from httpx<1,>=0.23.0->openai>=1.0.0->llmware) (2023.7.22)\n",
"Requirement already satisfied: httpcore<0.17.0,>=0.15.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from httpx<1,>=0.23.0->openai>=1.0.0->llmware) (0.16.3)\n",
"Requirement already satisfied: rfc3986<2,>=1.3 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from rfc3986[idna2008]<2,>=1.3->httpx<1,>=0.23.0->openai>=1.0.0->llmware) (1.5.0)\n",
"Requirement already satisfied: llvmlite<0.43,>=0.42.0dev0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from numba>=0.51.0->librosa>=0.10.0->llmware) (0.42.0)\n",
"Requirement already satisfied: platformdirs>=2.5.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from pooch>=1.1->librosa>=0.10.0->llmware) (3.10.0)\n",
"Requirement already satisfied: annotated-types>=0.4.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from pydantic<3,>=1.9.0->openai>=1.0.0->llmware) (0.6.0)\n",
"Requirement already satisfied: pydantic-core==2.16.3 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from pydantic<3,>=1.9.0->openai>=1.0.0->llmware) (2.16.3)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from requests->Wikipedia-API==0.6.0->llmware) (3.2.0)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from scikit-learn>=0.20.0->librosa>=0.10.0->llmware) (3.2.0)\n",
"Requirement already satisfied: cffi>=1.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from soundfile>=0.12.1->librosa>=0.10.0->llmware) (1.15.1)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from jinja2->torch>=1.13.1->llmware) (2.1.3)\n",
"Requirement already satisfied: mpmath>=0.19 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from sympy->torch>=1.13.1->llmware) (1.3.0)\n",
"Requirement already satisfied: pycparser in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from cffi>=1.0->soundfile>=0.12.1->librosa>=0.10.0->llmware) (2.21)\n",
"Requirement already satisfied: h11<0.15,>=0.13 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from httpcore<0.17.0,>=0.15.0->httpx<1,>=0.23.0->openai>=1.0.0->llmware) (0.14.0)\n",
"Requirement already satisfied: six>=1.5 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.28.0,>=1.27.53->boto3==1.24.53->llmware) (1.16.0)\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install llmware"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"from llmware.prompts import Prompt\n",
"from llmware.setup import Setup"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Worker function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The process for summarizing the document is as follows:\n",
"- The document gets split up into batches (each batch can be a few pages long)\n",
"- The SLIM Summary tool summarizes each batch into a list of points\n",
"- The points from each batch are aggregated at the end and printed, and the output is also available as a Python list\n",
"\n",
"The LLMWare library provides several sample files for you to run this example with, but if you wanted to use your own documents, then you can pass in the following optional parameters for the summarization:\n",
"- Topic: all the points in the summary will be about this topic\n",
"- Query: it is used to filter which parts of the document are considered for the summarization (only text chunks matching the query will be considered)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def test_summarize_document(example=\"jd salinger\"):\n",
"\n",
" # pull a sample document (or substitute a file_path and file_name of your own)\n",
" sample_files_path = Setup().load_sample_files(over_write=False)\n",
"\n",
" topic = None\n",
" query = None\n",
" fp = None\n",
" fn = None\n",
"\n",
" if example not in [\"jd salinger\", \"employment terms\", \"just the comp\", \"un resolutions\"]:\n",
" print (\"not found example\")\n",
" return []\n",
"\n",
" if example == \"jd salinger\":\n",
" fp = os.path.join(sample_files_path, \"SmallLibrary\")\n",
" fn = \"Jd-Salinger-Biography.docx\"\n",
" topic = \"jd salinger\"\n",
" query = None\n",
"\n",
" if example == \"employment terms\":\n",
" fp = os.path.join(sample_files_path, \"Agreements\")\n",
" fn = \"Athena EXECUTIVE EMPLOYMENT AGREEMENT.pdf\"\n",
" topic = \"executive compensation terms\"\n",
" query = None\n",
"\n",
" if example == \"just the comp\":\n",
" fp = os.path.join(sample_files_path, \"Agreements\")\n",
" fn = \"Athena EXECUTIVE EMPLOYMENT AGREEMENT.pdf\"\n",
" topic = \"executive compensation terms\"\n",
" query = \"base salary\"\n",
"\n",
" if example == \"un resolutions\":\n",
" fp = os.path.join(sample_files_path, \"SmallLibrary\")\n",
" fn = \"N2126108.pdf\"\n",
" # fn = \"N2137825.pdf\"\n",
" topic = \"key points\"\n",
" query = None\n",
"\n",
" # optional parameters: 'query' - will select among blocks with the query term\n",
" # 'topic' - will pass a topic/issue as the parameter to the model to 'focus' the summary\n",
" # 'max_batch_cap' - caps the number of batches sent to the model\n",
" # 'text_only' - returns just the summary text aggregated\n",
"\n",
" kp = Prompt().summarize_document_fc(fp, fn, topic=topic, query=query, text_only=True, max_batch_cap=15)\n",
"\n",
" print(f\"\\nDocument summary completed - {len(kp)} Points\")\n",
" for i, points in enumerate(kp):\n",
" print(i, points)\n",
"\n",
" return 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Main function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, we have our function call to the worker function above."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Example: Summarize Documents\n",
"\n",
"update: Prompt - summarize_document_fc - document - Athena EXECUTIVE EMPLOYMENT AGREEMENT.pdf\n",
"update: Prompt - summarize_document_fc - number of source batches - 14\n",
"update: iterating through source batches - 0 - ['Accrued Compensation - all compensation reimbursements and other amounts earned by payable to or accrued and vested for Executive through and including Executives Date of Termination including but not limited to (i) Base Salary; (ii) Executives Incentive Bonus for the fiscal year that ended immediately prior to Executives Date of Termination to the extent such Incentive Bonus was accrued and earned by but not yet paid to Executive as of Executives Date of Termination; (iii) pay for accrued but unused vacation; and (iv) reimbursable business expenses incurred by Executive on behalf of Employer']\n",
"update: iterating through source batches - 1 - ['Not Found']\n",
"update: iterating through source batches - 2 - ['Not to exceed 5% of base salary', 'Material Competitor', 'Sale of the Company', 'Material breach of the terms of the agreement']\n",
"update: iterating through source batches - 3 - ['Base Salary: $500,000 annual rate', 'Target Bonus: 80% of base salary (\"Target Bonus\")', 'Incentive Bonus: 125% of base salary (\"Incentive Bonus\")', 'Sale Bonus: 2.5% of the equity value of the company, up to $1,000,000', 'Transition Bonus: $350,000, payable on October 1, 2013 and October 1, 2014']\n",
"update: iterating through source batches - 4 - ['25 vacation days', '7 fixed holidays', '7 floating holidays', 'reimbursement of reasonable expenses', 'Initial stock options grant', 'Severance of up to 12 months salary and severance bonus if employment is terminated without Cause or resignation for Good Reason']\n",
"update: iterating through source batches - 5 - ['Severance Executive will receive severance in monthly installments in accordance with Employers regular payroll practices during the Severance Period', 'Sale Bonus If the Executives employment is terminated by the Company without Cause or due to Executives resignation for Good Reason prior to the payment of the First Transition Bonus then the Company will pay the Executive a lump-sum cash amount equal to the First Transition Bonus on the First Payment Date', 'Health Care Continuation If Executive is eligible for and elects to receive continuation group health coverage mandated by Section 4980B of the Internal Revenue Code or similar state laws (', ') during the Severance Period then the Company will reimburse Executive for the amount of the COBRA premiums and the Company will reimburse Executive for the amount of the COBRA premiums']\n",
"update: iterating through source batches - 6 - ['Not Found']\n",
"update: iterating through source batches - 7 - ['Not Found']\n",
"update: iterating through source batches - 8 - ['Not Found']\n",
"update: iterating through source batches - 9 - ['Not Found']\n",
"update: iterating through source batches - 10 - ['Not Found']\n",
"update: iterating through source batches - 11 - ['Not Found']\n",
"update: iterating through source batches - 12 - ['Notwithstanding any provision of the agreement to the contrary, if the Executive is a \"specified employee\" at the time of separation from service, then the payments or benefits otherwise payable pursuant to the agreement will be postponed to prevent any accelerated or additional tax under Section 409A of the Code', 'If any payments or benefits are postponed due to Section 409A of the Code, then the Executive will be entitled to be paid interest on such amount at an annual rate equal to the prime rate plus 2%', 'If the Executive dies during the postponement period, then the amounts withheld on account of Section 409A of the Code will be paid to the estate of the Executive within sixty days after the date of death']\n",
"update: iterating through source batches - 13 - ['Not Found']\n",
"\n",
"Document summary completed - 23 Points\n",
"0 Accrued Compensation - all compensation reimbursements and other amounts earned by payable to or accrued and vested for Executive through and including Executives Date of Termination including but not limited to (i) Base Salary; (ii) Executives Incentive Bonus for the fiscal year that ended immediately prior to Executives Date of Termination to the extent such Incentive Bonus was accrued and earned by but not yet paid to Executive as of Executives Date of Termination; (iii) pay for accrued but unused vacation; and (iv) reimbursable business expenses incurred by Executive on behalf of Employer\n",
"1 Not to exceed 5% of base salary\n",
"2 Material Competitor\n",
"3 Sale of the Company\n",
"4 Material breach of the terms of the agreement\n",
"5 Base Salary: $500,000 annual rate\n",
"6 Target Bonus: 80% of base salary (\"Target Bonus\")\n",
"7 Incentive Bonus: 125% of base salary (\"Incentive Bonus\")\n",
"8 Sale Bonus: 2.5% of the equity value of the company, up to $1,000,000\n",
"9 Transition Bonus: $350,000, payable on October 1, 2013 and October 1, 2014\n",
"10 25 vacation days\n",
"11 7 fixed holidays\n",
"12 7 floating holidays\n",
"13 reimbursement of reasonable expenses\n",
"14 Initial stock options grant\n",
"15 Severance of up to 12 months salary and severance bonus if employment is terminated without Cause or resignation for Good Reason\n",
"16 Severance Executive will receive severance in monthly installments in accordance with Employers regular payroll practices during the Severance Period\n",
"17 Sale Bonus If the Executives employment is terminated by the Company without Cause or due to Executives resignation for Good Reason prior to the payment of the First Transition Bonus then the Company will pay the Executive a lump-sum cash amount equal to the First Transition Bonus on the First Payment Date\n",
"18 Health Care Continuation If Executive is eligible for and elects to receive continuation group health coverage mandated by Section 4980B of the Internal Revenue Code or similar state laws (\n",
"19 ) during the Severance Period then the Company will reimburse Executive for the amount of the COBRA premiums and the Company will reimburse Executive for the amount of the COBRA premiums\n",
"20 Notwithstanding any provision of the agreement to the contrary, if the Executive is a \"specified employee\" at the time of separation from service, then the payments or benefits otherwise payable pursuant to the agreement will be postponed to prevent any accelerated or additional tax under Section 409A of the Code\n",
"21 If any payments or benefits are postponed due to Section 409A of the Code, then the Executive will be entitled to be paid interest on such amount at an annual rate equal to the prime rate plus 2%\n",
"22 If the Executive dies during the postponement period, then the amounts withheld on account of Section 409A of the Code will be paid to the estate of the Executive within sixty days after the date of death\n"
]
}
],
"source": [
"if __name__ == \"__main__\":\n",
"\n",
" print(f\"\\nExample: Summarize Documents\\n\")\n",
"\n",
" # 4 examples - [\"jd salinger\", \"employment terms\", \"just the comp\", \"un resolutions\"]\n",
" # -- \"jd salinger\" - summarizes key points about jd salinger from short biography document\n",
" # -- \"employment terms\" - summarizes the executive compensation terms across 15 page document\n",
" # -- \"just the comp\" - queries to find subset of document and then summarizes the key terms\n",
" # -- \"un resolutions\" - summarizes the un resolutions document\n",
"\n",
" summary_direct = test_summarize_document(example=\"employment terms\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We're given an output of 23 points summarizing the `employment terms` sample document!\n",
"\n",
"Note that several batches gave us an output of `Not Found`, indicating that there were no relevant points related to the topic of `executive compensation terms`. This is the topic that was passed in to the `summarize_document_fc()` function call in our worker function. The model gave us the output of `Not Found` rather than unrelated points with no meaning to our topic!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,275 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "90ab87c1-278b-48c9-a7b4-57473f051cd2",
"metadata": {},
"outputs": [],
"source": [
"# If you are using Colab for free, we highly recommend you activate the T4 GPU\n",
"# hardware accelerator. Our models are designed to run with at least 16GB\n",
"# of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as opposed\n",
"# to the ~13GB Colab gives automatically.\n",
"# To activate T4:\n",
"# 1. click on the \"Runtime\" tab\n",
"# 2. click on \"Change runtime type\"\n",
"# 3. select T4 GPU under \"Hardware Accelerator\"\n",
"# NOTE: there is a weekly usage limit on using T4 for free"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "42caad5f-939b-4e42-a207-b79000c02179",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: llmware in /opt/homebrew/lib/python3.11/site-packages (0.2.15)\n",
"Requirement already satisfied: boto3==1.24.53 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (1.24.53)\n",
"Requirement already satisfied: huggingface-hub>=0.19.4 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.23.2)\n",
"Requirement already satisfied: numpy>=1.23.2 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (1.26.4)\n",
"Requirement already satisfied: openai>=1.0.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (1.30.3)\n",
"Requirement already satisfied: pymongo>=4.7.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (4.7.2)\n",
"Requirement already satisfied: tokenizers>=0.15.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.15.2)\n",
"Requirement already satisfied: torch>=1.13.1 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (2.2.1)\n",
"Requirement already satisfied: transformers>=4.36.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (4.39.1)\n",
"Requirement already satisfied: Wikipedia-API==0.6.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.6.0)\n",
"Requirement already satisfied: psycopg-binary==3.1.17 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: psycopg==3.1.17 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: pgvector==0.2.4 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.2.4)\n",
"Requirement already satisfied: colorama==0.4.6 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.4.6)\n",
"Requirement already satisfied: einops==0.7.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.7.0)\n",
"Requirement already satisfied: librosa>=0.10.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.10.2.post1)\n",
"Requirement already satisfied: botocore<1.28.0,>=1.27.53 in /opt/homebrew/lib/python3.11/site-packages (from boto3==1.24.53->llmware) (1.27.96)\n",
"Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /opt/homebrew/lib/python3.11/site-packages (from boto3==1.24.53->llmware) (1.0.1)\n",
"Requirement already satisfied: s3transfer<0.7.0,>=0.6.0 in /opt/homebrew/lib/python3.11/site-packages (from boto3==1.24.53->llmware) (0.6.2)\n",
"Requirement already satisfied: typing-extensions>=4.1 in /opt/homebrew/lib/python3.11/site-packages (from psycopg==3.1.17->llmware) (4.10.0)\n",
"Requirement already satisfied: requests in /opt/homebrew/lib/python3.11/site-packages (from Wikipedia-API==0.6.0->llmware) (2.32.2)\n",
"Requirement already satisfied: filelock in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.19.4->llmware) (3.14.0)\n",
"Requirement already satisfied: fsspec>=2023.5.0 in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.19.4->llmware) (2024.3.1)\n",
"Requirement already satisfied: packaging>=20.9 in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.19.4->llmware) (24.0)\n",
"Requirement already satisfied: pyyaml>=5.1 in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.19.4->llmware) (6.0.1)\n",
"Requirement already satisfied: tqdm>=4.42.1 in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.19.4->llmware) (4.66.2)\n",
"Requirement already satisfied: audioread>=2.1.9 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (3.0.1)\n",
"Requirement already satisfied: scipy>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (1.12.0)\n",
"Requirement already satisfied: scikit-learn>=0.20.0 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (1.5.0)\n",
"Requirement already satisfied: joblib>=0.14 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (1.4.2)\n",
"Requirement already satisfied: decorator>=4.3.0 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (5.1.1)\n",
"Requirement already satisfied: numba>=0.51.0 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (0.59.1)\n",
"Requirement already satisfied: soundfile>=0.12.1 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (0.12.1)\n",
"Requirement already satisfied: pooch>=1.1 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (1.8.1)\n",
"Requirement already satisfied: soxr>=0.3.2 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (0.3.7)\n",
"Requirement already satisfied: lazy-loader>=0.1 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (0.4)\n",
"Requirement already satisfied: msgpack>=1.0 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (1.0.8)\n",
"Requirement already satisfied: anyio<5,>=3.5.0 in /opt/homebrew/lib/python3.11/site-packages (from openai>=1.0.0->llmware) (4.4.0)\n",
"Requirement already satisfied: distro<2,>=1.7.0 in /opt/homebrew/lib/python3.11/site-packages (from openai>=1.0.0->llmware) (1.9.0)\n",
"Requirement already satisfied: httpx<1,>=0.23.0 in /opt/homebrew/lib/python3.11/site-packages (from openai>=1.0.0->llmware) (0.27.0)\n",
"Requirement already satisfied: pydantic<3,>=1.9.0 in /opt/homebrew/lib/python3.11/site-packages (from openai>=1.0.0->llmware) (2.7.1)\n",
"Requirement already satisfied: sniffio in /opt/homebrew/lib/python3.11/site-packages (from openai>=1.0.0->llmware) (1.3.1)\n",
"Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in /opt/homebrew/lib/python3.11/site-packages (from pymongo>=4.7.0->llmware) (2.6.1)\n",
"Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.13.1->llmware) (1.12)\n",
"Requirement already satisfied: networkx in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.13.1->llmware) (3.3)\n",
"Requirement already satisfied: jinja2 in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.13.1->llmware) (3.1.4)\n",
"Requirement already satisfied: regex!=2019.12.17 in /opt/homebrew/lib/python3.11/site-packages (from transformers>=4.36.0->llmware) (2024.5.15)\n",
"Requirement already satisfied: safetensors>=0.4.1 in /opt/homebrew/lib/python3.11/site-packages (from transformers>=4.36.0->llmware) (0.4.3)\n",
"Requirement already satisfied: idna>=2.8 in /opt/homebrew/lib/python3.11/site-packages (from anyio<5,>=3.5.0->openai>=1.0.0->llmware) (3.7)\n",
"Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /opt/homebrew/lib/python3.11/site-packages (from botocore<1.28.0,>=1.27.53->boto3==1.24.53->llmware) (2.9.0.post0)\n",
"Requirement already satisfied: urllib3<1.27,>=1.25.4 in /opt/homebrew/lib/python3.11/site-packages (from botocore<1.28.0,>=1.27.53->boto3==1.24.53->llmware) (1.26.18)\n",
"Requirement already satisfied: certifi in /opt/homebrew/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai>=1.0.0->llmware) (2024.2.2)\n",
"Requirement already satisfied: httpcore==1.* in /opt/homebrew/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai>=1.0.0->llmware) (1.0.5)\n",
"Requirement already satisfied: h11<0.15,>=0.13 in /opt/homebrew/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai>=1.0.0->llmware) (0.14.0)\n",
"Requirement already satisfied: llvmlite<0.43,>=0.42.0dev0 in /opt/homebrew/lib/python3.11/site-packages (from numba>=0.51.0->librosa>=0.10.0->llmware) (0.42.0)\n",
"Requirement already satisfied: platformdirs>=2.5.0 in /opt/homebrew/lib/python3.11/site-packages (from pooch>=1.1->librosa>=0.10.0->llmware) (4.2.2)\n",
"Requirement already satisfied: annotated-types>=0.4.0 in /opt/homebrew/lib/python3.11/site-packages (from pydantic<3,>=1.9.0->openai>=1.0.0->llmware) (0.7.0)\n",
"Requirement already satisfied: pydantic-core==2.18.2 in /opt/homebrew/lib/python3.11/site-packages (from pydantic<3,>=1.9.0->openai>=1.0.0->llmware) (2.18.2)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests->Wikipedia-API==0.6.0->llmware) (3.3.2)\n",
"Requirement already satisfied: threadpoolctl>=3.1.0 in /opt/homebrew/lib/python3.11/site-packages (from scikit-learn>=0.20.0->librosa>=0.10.0->llmware) (3.4.0)\n",
"Requirement already satisfied: cffi>=1.0 in /opt/homebrew/lib/python3.11/site-packages (from soundfile>=0.12.1->librosa>=0.10.0->llmware) (1.16.0)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /opt/homebrew/lib/python3.11/site-packages (from jinja2->torch>=1.13.1->llmware) (2.1.5)\n",
"Requirement already satisfied: mpmath>=0.19 in /opt/homebrew/lib/python3.11/site-packages (from sympy->torch>=1.13.1->llmware) (1.3.0)\n",
"Requirement already satisfied: pycparser in /opt/homebrew/lib/python3.11/site-packages (from cffi>=1.0->soundfile>=0.12.1->librosa>=0.10.0->llmware) (2.22)\n",
"Requirement already satisfied: six>=1.5 in /opt/homebrew/lib/python3.11/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.28.0,>=1.27.53->boto3==1.24.53->llmware) (1.16.0)\n"
]
}
],
"source": [
"!pip install llmware"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "809f3d5a-37f9-4843-b3dd-080a10e9d8c9",
"metadata": {},
"outputs": [],
"source": [
"from llmware.models import ModelCatalog\n",
"from llmware.parsers import WikiParser\n",
"\n",
"\n",
"# our input - financial news article - note: the 'company founding date' is not mentioned in the text\n",
"# -- our worry is that the model will either 'make up' an answer from background knowledge (which may or may not\n",
"# be accurate), or try to incorrectly use a date in the text, e.g, February 29, 2024.\n",
"\n",
"\n",
"text =(\"BEAVERTON, Ore.--(BUSINESS WIRE)--NIKE, Inc. (NYSE:NKE) today reported fiscal 2024 financial results for its \"\n",
" \"third quarter ended February 29, 2024.) “We are making the necessary adjustments to drive NIKEs next chapter \"\n",
" \"of growth Post this Third quarter revenues were slightly up on both a reported and currency-neutral basis* \"\n",
" \"at $12.4 billion NIKE Direct revenues were $5.4 billion, slightly up on a reported and currency-neutral basis \"\n",
" \"NIKE Brand Digital sales decreased 3 percent on a reported basis and 4 percent on a currency-neutral basis \"\n",
" \"Wholesale revenues were $6.6 billion, up 3 percent on a reported and currency-neutral basis Gross margin \"\n",
" \"increased 150 basis points to 44.8 percent, including a detriment of 50 basis points due to restructuring charges \"\n",
" \"Selling and administrative expense increased 7 percent to $4.2 billion, including $340 million of restructuring \"\n",
" \"charges Diluted earnings per share was $0.77, including $0.21 of restructuring charges. Excluding these \"\n",
" \"charges, Diluted earnings per share would have been $0.98* “We are making the necessary adjustments to \"\n",
" \"drive NIKEs next chapter of growth,” said John Donahoe, President & CEO, NIKE, Inc. “Were encouraged by \"\n",
" \"the progress weve seen, as we build a multiyear cycle of new innovation, sharpen our brand storytelling and \"\n",
" \"work with our wholesale partners to elevate and grow the marketplace.\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b6c5f0ab-e5d7-41da-8487-15cd1647962e",
"metadata": {},
"outputs": [],
"source": [
"def not_found_then_triage_lookup():\n",
"\n",
" print(\"\\nNot Found Example - if info not found, then lookup in another source.\\n\")\n",
"\n",
" extract_key = \"company founding date\"\n",
" dict_key = extract_key.replace(\" \", \"_\")\n",
"\n",
" company_founding_date = \"\"\n",
"\n",
" # step 1 - run an extract function call on the text\n",
" model = ModelCatalog().load_model(\"slim-extract-tool\", temperature=0.0, sample=False)\n",
" response = model.function_call(text, function=\"extract\", params=[extract_key])\n",
" llm_response = response[\"llm_response\"]\n",
"\n",
" print(f\"update: first text reviewed for {extract_key} - llm response: \", llm_response)\n",
"\n",
" # unpack the output\n",
" if dict_key in llm_response:\n",
"\n",
" company_founding_date = llm_response[dict_key]\n",
"\n",
" if len(company_founding_date) > 0:\n",
"\n",
" # in this case, the value is a list with at least one element, so an 'answer' was found\n",
" company_founding_date = company_founding_date[0]\n",
" print(f\"update: found the {extract_key} value - \", company_founding_date)\n",
" return company_founding_date\n",
"\n",
" else:\n",
"\n",
" # step 2 - could not find the answer in the original source materials\n",
" # e.g., the len of the list associated with the key is zero, or []\n",
"\n",
" print(f\"update: did not find the target value in the text - {company_founding_date}\")\n",
" print(\"update: initiating a secondary process to try to find the information\")\n",
"\n",
" # look up the company name from the original text\n",
" response = model.function_call(text, function=\"extract\", params=[\"company name\"])\n",
"\n",
" if \"company_name\" in response[\"llm_response\"]:\n",
" company_name = response[\"llm_response\"][\"company_name\"][0]\n",
"\n",
" if company_name:\n",
" print(f\"\\nupdate: found the company name - {company_name} - now using to lookup in secondary source\")\n",
"\n",
" # use the company name to lookup materials in Wikipedia secondary source\n",
" output = WikiParser().add_wiki_topic(company_name,target_results=1)\n",
"\n",
" if output:\n",
"\n",
" supplemental_text = output[\"articles\"][0][\"summary\"]\n",
"\n",
" if len(supplemental_text) > 150:\n",
" supplemental_text_pp = supplemental_text[0:150] + \" ... \"\n",
" else:\n",
" supplemental_text_pp = supplemental_text\n",
"\n",
" print(f\"update: using lookup - {company_name} - found secondary source article \"\n",
" f\"(extract displayed) - \", supplemental_text_pp)\n",
"\n",
" # finally, try to get the company founding date from the supplemental text\n",
" new_response = model.function_call(supplemental_text,params=[\"company founding date\"])\n",
"\n",
" print(\"\\nupdate: reviewed second source article - \", new_response[\"llm_response\"])\n",
"\n",
" if \"company_founding_date\" in new_response[\"llm_response\"]:\n",
" company_founding_date = new_response[\"llm_response\"][\"company_founding_date\"]\n",
" if company_founding_date:\n",
" print(\"update: success - found the answer - \", company_founding_date)\n",
"\n",
" return company_founding_date"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "296a32b1-db9f-4e09-9c6f-ab4c40046634",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Not Found Example - if info not found, then lookup in another source.\n",
"\n",
"update: first text reviewed for company founding date - llm response: {'company_founding_date': []}\n",
"update: did not find the target value in the text - []\n",
"update: initiating a secondary process to try to find the information\n",
"\n",
"update: found the company name - NIKE, Inc. - now using to lookup in secondary source\n",
"update: using lookup - NIKE, Inc. - found secondary source article (extract displayed) - Nike, Inc. (stylized as NIKE) is an American athletic footwear and apparel corporation headquartered near Beaverton, Oregon, United States. It is the ... \n",
"\n",
"update: reviewed second source article - {'company_founding_date': ['January 25, 1964']}\n",
"update: success - found the answer - ['January 25, 1964']\n"
]
}
],
"source": [
"if __name__ == \"__main__\":\n",
"\n",
" founding_date = not_found_then_triage_lookup()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ddd92bbb-1f24-4684-b0ca-61d0e0a26ff9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@@ -0,0 +1,229 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Voice Transcription with CPU Friendly AI Models Example (Greatest Speeches of 20th Century)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we were given an audio file, is there any way we could identify the time stamps where specific words were said? Is there any way we could extract all the key words mentioned about a topic?\n",
"\n",
"With AI 🤖, we can do all of this and much more! The key lies in being able to parse audio into text, allowing us to then harness the natural language processing capabilities of language models to perform sophisticated analyses and inferences on our data.\n",
"\n",
"Regardless of who you are, such an approach to audio transcription and analysis will augment how you interact with and extract knowledge from audio files.\n",
"\n",
"Let's see how we can do this with llmware, using the Whisper model and the SLIM Extract Tool."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### For Google Colab Users"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you are using Colab for free, we highly recommend you activate the T4 GPU hardware accelerator. Our models are designed to run with at least 16GB of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as apposed to the ~13GB Colab gives automatically.\n",
"\n",
"To activate T4:\n",
"1. click on the \"Runtime\" tab\n",
"2. click on \"Change runtime type\"\n",
"3. select T4 GPU under Hardware Accelerator\n",
"\n",
"NOTE: there is a weekly usage limit on using T4 for free"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Installing and importing dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install llmware"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"from llmware.parsers import Parser\n",
"from llmware.setup import Setup\n",
"from llmware.util import Utilities\n",
"from llmware.models import ModelCatalog"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Worker function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Our main worker function will do the following:\n",
"1. Load in sample audio files with the `Setup` class\n",
"2. Parse the audio into text by calling the WhisperCPP model via the `Parser` class\n",
"3. Perform a text search for the word \"president\" (these specific text chunks are used in the next step)\n",
"4. Use the SLIM Extract Tool to extract the president names from these chunks\n",
"5. Check if the extracted name matches someone in a specified list (`various_american_presidents`)\n",
" - If there is a match, append information about the location of the extracted name and the time stamp in the audio to a `final_list`\n",
"6. Output the content of the `final_list`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def greatest_speeches_example():\n",
"\n",
" # four sample file sets - \"famous_quotes\" | \"greatest_speeches\" | \"youtube_demos\" | \"earnings_calls\"\n",
" voice_sample_files = Setup().load_voice_sample_files(small_only=False)\n",
"\n",
" input_folder = os.path.join(voice_sample_files, \"greatest_speeches\")\n",
"\n",
" print(\"\\nStep 1 - converting, parsing and text chunking ~56 WAV files\")\n",
"\n",
" parser_output = Parser(chunk_size=400, max_chunk_size=600).parse_voice(input_folder,\n",
" write_to_db=False,\n",
" copy_to_library=False,\n",
" remove_segment_markers=True,\n",
" chunk_by_segment=True,\n",
" real_time_progress=False)\n",
"\n",
" print(\"\\nStep 2- look at the text chunks with all of the metadata\")\n",
"\n",
" for i, entries in enumerate(parser_output):\n",
" print(\"all parsed blocks: \", i, entries)\n",
"\n",
" print(\"\\nStep 3- run an inline text search for 'president'\")\n",
"\n",
" results = Utilities().fast_search_dicts(\"president\", parser_output)\n",
"\n",
" for i, res in enumerate(results):\n",
" print(\"search results: \", i, res)\n",
"\n",
" print(\"\\nStep 4- use LLM to review each search result - and if specific U.S. presidents found, then display the source\")\n",
"\n",
" extract_model = ModelCatalog().load_model(\"slim-extract-tool\", sample=False, temperature=0.0, max_output=200)\n",
"\n",
" final_list = []\n",
"\n",
" for i, res in enumerate(results):\n",
"\n",
" response = extract_model.function_call(res[\"text\"], params=[\"president name\"])\n",
"\n",
" various_american_presidents = [\"kennedy\", \"carter\", \"nixon\", \"reagan\", \"clinton\", \"obama\"]\n",
"\n",
" extracted_name = \"\"\n",
" if \"president_name\" in response[\"llm_response\"]:\n",
" if len(response[\"llm_response\"][\"president_name\"]) > 0:\n",
" extracted_name = response[\"llm_response\"][\"president_name\"][0].lower()\n",
" else:\n",
" print(\"\\nupdate: skipping result - no president name found - \", response[\"llm_response\"], res[\"text\"])\n",
"\n",
" for president in various_american_presidents:\n",
" if president in extracted_name:\n",
" print(\"\\nextracted american president text: \", i, extracted_name)\n",
" print(\"file source: \", res[\"file_source\"])\n",
" print(\"time stamp: \", res[\"coords_x\"], res[\"coords_y\"], res[\"coords_cx\"], res[\"coords_cy\"])\n",
" print(\"text: \", i, res[\"text\"])\n",
" final_list.append({\"key\": president, \"source\": res[\"file_source\"], \"time_start\": res[\"coords_x\"],\n",
" \"text\": res[\"text\"]})\n",
"\n",
" print(\"\\nStep 5 - final results\")\n",
" for i, f in enumerate(final_list):\n",
" print(\"final results: \", i, f)\n",
"\n",
" return final_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Main block"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, we call the worker function."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
"\n",
" greatest_speeches_example()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The output, `final_list`, is a Python dictionary with information about the instances where a president name mentioned in the audio matches one of our specified presidents in the `various_american_presidents_list`. It has the following keys:\n",
"- key: the name of the president identified\n",
"- source: the audio file this was found in\n",
"- time_start: the time stamp in seconds where the president was mentioned\n",
"- text: which contains the text chunk the name was found in."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,183 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "5DKDTnMGHTGn"
},
"source": [
"# If you are using Colab for free, we highly recommend you activate the T4 GPU\n",
"# hardware accelerator. Our models are designed to run with at least 16GB\n",
"# of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as opposed\n",
"# to the ~13GB Colab gives automatically.\n",
"# To activate T4:\n",
"# 1. click on the \"Runtime\" tab\n",
"# 2. click on \"Change runtime type\"\n",
"# 3. select T4 GPU under Hardware Accelerator\n",
"# NOTE: there is a weekly usage limit on using T4 for free"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aBE4SLY5LYiq"
},
"source": [
"##This example aims to show how to run multiple specific queries on text using our slim extract tool, and ouput it as a dictionary"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "oanCf6Dn8UlA",
"outputId": "84e6207b-40bb-47b6-d756-ae6ead743bc1"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: llmware in /usr/local/lib/python3.10/dist-packages (0.3.0)\n",
"Requirement already satisfied: boto3>=1.24.53 in /usr/local/lib/python3.10/dist-packages (from llmware) (1.34.129)\n",
"Requirement already satisfied: huggingface-hub>=0.19.4 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.23.3)\n",
"Requirement already satisfied: numpy>=1.23.2 in /usr/local/lib/python3.10/dist-packages (from llmware) (1.25.2)\n",
"Requirement already satisfied: pymongo>=4.7.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (4.7.3)\n",
"Requirement already satisfied: tokenizers>=0.15.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.19.1)\n",
"Requirement already satisfied: psycopg-binary==3.1.17 in /usr/local/lib/python3.10/dist-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: psycopg==3.1.17 in /usr/local/lib/python3.10/dist-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: pgvector==0.2.4 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.2.4)\n",
"Requirement already satisfied: colorama==0.4.6 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.4.6)\n",
"Requirement already satisfied: librosa>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.10.2.post1)\n",
"Requirement already satisfied: typing-extensions>=4.1 in /usr/local/lib/python3.10/dist-packages (from psycopg==3.1.17->llmware) (4.12.2)\n",
"Requirement already satisfied: botocore<1.35.0,>=1.34.129 in /usr/local/lib/python3.10/dist-packages (from boto3>=1.24.53->llmware) (1.34.129)\n",
"Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from boto3>=1.24.53->llmware) (1.0.1)\n",
"Requirement already satisfied: s3transfer<0.11.0,>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from boto3>=1.24.53->llmware) (0.10.1)\n",
"Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (3.14.0)\n",
"Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (2023.6.0)\n",
"Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (24.1)\n",
"Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (6.0.1)\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (2.31.0)\n",
"Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (4.66.4)\n",
"Requirement already satisfied: audioread>=2.1.9 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (3.0.1)\n",
"Requirement already satisfied: scipy>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.11.4)\n",
"Requirement already satisfied: scikit-learn>=0.20.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.2.2)\n",
"Requirement already satisfied: joblib>=0.14 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.4.2)\n",
"Requirement already satisfied: decorator>=4.3.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (4.4.2)\n",
"Requirement already satisfied: numba>=0.51.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.58.1)\n",
"Requirement already satisfied: soundfile>=0.12.1 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.12.1)\n",
"Requirement already satisfied: pooch>=1.1 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.8.2)\n",
"Requirement already satisfied: soxr>=0.3.2 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.3.7)\n",
"Requirement already satisfied: lazy-loader>=0.1 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.4)\n",
"Requirement already satisfied: msgpack>=1.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.0.8)\n",
"Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in /usr/local/lib/python3.10/dist-packages (from pymongo>=4.7.0->llmware) (2.6.1)\n",
"Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /usr/local/lib/python3.10/dist-packages (from botocore<1.35.0,>=1.34.129->boto3>=1.24.53->llmware) (2.8.2)\n",
"Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in /usr/local/lib/python3.10/dist-packages (from botocore<1.35.0,>=1.34.129->boto3>=1.24.53->llmware) (2.0.7)\n",
"Requirement already satisfied: llvmlite<0.42,>=0.41.0dev0 in /usr/local/lib/python3.10/dist-packages (from numba>=0.51.0->librosa>=0.10.0->llmware) (0.41.1)\n",
"Requirement already satisfied: platformdirs>=2.5.0 in /usr/local/lib/python3.10/dist-packages (from pooch>=1.1->librosa>=0.10.0->llmware) (4.2.2)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.4->llmware) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.4->llmware) (3.7)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.4->llmware) (2024.6.2)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=0.20.0->librosa>=0.10.0->llmware) (3.5.0)\n",
"Requirement already satisfied: cffi>=1.0 in /usr/local/lib/python3.10/dist-packages (from soundfile>=0.12.1->librosa>=0.10.0->llmware) (1.16.0)\n",
"Requirement already satisfied: pycparser in /usr/local/lib/python3.10/dist-packages (from cffi>=1.0->soundfile>=0.12.1->librosa>=0.10.0->llmware) (2.22)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.35.0,>=1.34.129->boto3>=1.24.53->llmware) (1.16.0)\n"
]
}
],
"source": [
"!pip install llmware\n",
"from llmware.models import ModelCatalog\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "erpOjVoUGZPU"
},
"source": [
"To add more strings to analyze, add them to Text_to_analyze.\n",
"To add more queries, add them to queries_list.\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"id": "Ls6GHIEqF1uq"
},
"outputs": [],
"source": [
"Text_to_analyze = [\"I am John Doe, I visited Peter, a therapist in the year 2019. I travelled to NYC for around 5 therapy sessions, I was advised to be more outgoing.\"]\n",
"queries_list = [\"consultant specialty\", \"Consultant name\", \"Consultant location\", \"Consultant Advice\"]\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aWw7uro5C1ym",
"outputId": "b33fbf63-171c-4b5b-f33a-b102282b42d2"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Query 1: consultant specialty\n",
"extract response: {'consultant_specialty': ['Therapist']}\n",
"Query 2: Consultant name\n",
"extract response: {'Consultant_name': ['Peter']}\n",
"Query 3: Consultant location\n",
"extract response: {'Consultant_location': ['NYC']}\n",
"Query 4: Consultant Advice\n",
"extract response: {'Consultant_Advice': ['Be more outgoing.']}\n",
"output dictionary: \n",
"{'consultant_specialty': ['Therapist'], 'Consultant_name': ['Peter'], 'Consultant_location': ['NYC'], 'Consultant_Advice': ['Be more outgoing.']}\n"
]
}
],
"source": [
"# load the model\n",
"model = ModelCatalog().load_model(\"slim-extract-tool\",sample=False, temperature=0.0, max_output=200)\n",
"# loop through the text and queries, calling the model each time\n",
"output_dict = {}\n",
"for i, sample in enumerate(Text_to_analyze):\n",
" for j, query in enumerate(queries_list):\n",
" print(f\"Query {j+1}: {query}\")\n",
" response = model.function_call(sample, function=\"extract\", params=[query])\n",
" output_dict.update(response[\"llm_response\"])\n",
" if response[\"llm_response\"] == []:\n",
" print(\"No response\")\n",
" print(\"extract response: \", response[\"llm_response\"])\n",
"\n",
"\n",
" # display the response on the screen\n",
"print(\"output dictionary: \" )\n",
"print(output_dict)"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"gpuType": "T4",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@@ -0,0 +1,557 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Natural Language Queries for SQL using SLIM (Full End-to-End Example)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You've might have heard of SQL. It's a widely used programming language for storing and processing information in relational databases - simply put, relational databases store data in tables, where each row stores an entity and each column stores an attribute for that entity.\n",
"\n",
"Let's say we have a table called customers in a relational database. If I wanted to access the names of all customers (customer_names) that have an annual_spend of at least $1000, I would have to formulate an SQL query like this:\n",
"```SQL\n",
"SELECT customer_names FROM customers WHERE annual_spend >= 1000\n",
"```\n",
"I would then run this query against the database to access my results.\n",
"\n",
"But what if AI 🤖 could do all this for us?\n",
"\n",
"LLMWare allows us to do just that, making use of small language models. These are models of a smaller scale, with fewer and less precise parameters that don't require much computational power to run. Their main advantage is that they run locally on a CPU, without an internet connection or a GPU. This enables use to make our queries entirely in natural language and still get accurate results!\n",
"\n",
"Let's look at an example of how to do this from start to finish."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### For Google Colab users"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you are using Colab for free, we highly recommend you activate the T4 GPU hardware accelerator. Our models are designed to run with at least 16GB of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as apposed to the ~13GB Colab gives automatically.\n",
"\n",
"To activate T4:\n",
"1. click on the \"Runtime\" tab\n",
"2. click on \"Change runtime type\"\n",
"3. select T4 GPU under Hardware Accelerator\n",
"\n",
"NOTE: there is a weekly usage limit on using T4 for free"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Installing and importing dependencies"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Defaulting to user installation because normal site-packages is not writeable\n",
"Requirement already satisfied: llmware in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (0.2.14)\n",
"Requirement already satisfied: boto3==1.24.53 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (1.24.53)\n",
"Requirement already satisfied: huggingface-hub>=0.19.4 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.19.4)\n",
"Requirement already satisfied: numpy>=1.23.2 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (1.26.0)\n",
"Requirement already satisfied: openai>=1.0.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (1.13.3)\n",
"Requirement already satisfied: pymongo>=4.7.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (4.7.2)\n",
"Requirement already satisfied: tokenizers>=0.15.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.15.2)\n",
"Requirement already satisfied: torch>=1.13.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (2.1.0+cu121)\n",
"Requirement already satisfied: transformers>=4.36.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (4.38.2)\n",
"Requirement already satisfied: Wikipedia-API==0.6.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.6.0)\n",
"Requirement already satisfied: psycopg-binary==3.1.17 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: psycopg==3.1.17 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: pgvector==0.2.4 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.2.4)\n",
"Requirement already satisfied: colorama==0.4.6 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.4.6)\n",
"Requirement already satisfied: einops==0.7.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.7.0)\n",
"Requirement already satisfied: librosa>=0.10.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.10.2.post1)\n",
"Requirement already satisfied: botocore<1.28.0,>=1.27.53 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from boto3==1.24.53->llmware) (1.27.96)\n",
"Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from boto3==1.24.53->llmware) (1.0.1)\n",
"Requirement already satisfied: s3transfer<0.7.0,>=0.6.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from boto3==1.24.53->llmware) (0.6.2)\n",
"Requirement already satisfied: typing-extensions>=4.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from psycopg==3.1.17->llmware) (4.8.0)\n",
"Requirement already satisfied: tzdata in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from psycopg==3.1.17->llmware) (2023.3)\n",
"Requirement already satisfied: requests in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from Wikipedia-API==0.6.0->llmware) (2.31.0)\n",
"Requirement already satisfied: filelock in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (3.13.1)\n",
"Requirement already satisfied: fsspec>=2023.5.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (2023.10.0)\n",
"Requirement already satisfied: tqdm>=4.42.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (4.66.1)\n",
"Requirement already satisfied: pyyaml>=5.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (6.0.1)\n",
"Requirement already satisfied: packaging>=20.9 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (23.1)\n",
"Requirement already satisfied: audioread>=2.1.9 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (3.0.1)\n",
"Requirement already satisfied: scipy>=1.2.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.11.3)\n",
"Requirement already satisfied: scikit-learn>=0.20.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.3.1)\n",
"Requirement already satisfied: joblib>=0.14 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.3.2)\n",
"Requirement already satisfied: decorator>=4.3.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (5.1.1)\n",
"Requirement already satisfied: numba>=0.51.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (0.59.1)\n",
"Requirement already satisfied: soundfile>=0.12.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (0.12.1)\n",
"Requirement already satisfied: pooch>=1.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.8.1)\n",
"Requirement already satisfied: soxr>=0.3.2 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (0.3.7)\n",
"Requirement already satisfied: lazy-loader>=0.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (0.4)\n",
"Requirement already satisfied: msgpack>=1.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.0.8)\n",
"Requirement already satisfied: anyio<5,>=3.5.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from openai>=1.0.0->llmware) (4.0.0)\n",
"Requirement already satisfied: distro<2,>=1.7.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from openai>=1.0.0->llmware) (1.9.0)\n",
"Requirement already satisfied: httpx<1,>=0.23.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from openai>=1.0.0->llmware) (0.23.3)\n",
"Requirement already satisfied: pydantic<3,>=1.9.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from openai>=1.0.0->llmware) (2.6.4)\n",
"Requirement already satisfied: sniffio in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from openai>=1.0.0->llmware) (1.3.0)\n",
"Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from pymongo>=4.7.0->llmware) (2.6.1)\n",
"Requirement already satisfied: sympy in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from torch>=1.13.1->llmware) (1.12)\n",
"Requirement already satisfied: networkx in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from torch>=1.13.1->llmware) (3.2.1)\n",
"Requirement already satisfied: jinja2 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from torch>=1.13.1->llmware) (3.1.2)\n",
"Requirement already satisfied: regex!=2019.12.17 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from transformers>=4.36.0->llmware) (2023.12.25)\n",
"Requirement already satisfied: safetensors>=0.4.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from transformers>=4.36.0->llmware) (0.4.2)\n",
"Requirement already satisfied: idna>=2.8 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from anyio<5,>=3.5.0->openai>=1.0.0->llmware) (2.10)\n",
"Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from botocore<1.28.0,>=1.27.53->boto3==1.24.53->llmware) (2.8.2)\n",
"Requirement already satisfied: urllib3<1.27,>=1.25.4 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from botocore<1.28.0,>=1.27.53->boto3==1.24.53->llmware) (1.26.18)\n",
"Requirement already satisfied: certifi in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from httpx<1,>=0.23.0->openai>=1.0.0->llmware) (2023.7.22)\n",
"Requirement already satisfied: httpcore<0.17.0,>=0.15.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from httpx<1,>=0.23.0->openai>=1.0.0->llmware) (0.16.3)\n",
"Requirement already satisfied: rfc3986<2,>=1.3 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from rfc3986[idna2008]<2,>=1.3->httpx<1,>=0.23.0->openai>=1.0.0->llmware) (1.5.0)\n",
"Requirement already satisfied: llvmlite<0.43,>=0.42.0dev0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from numba>=0.51.0->librosa>=0.10.0->llmware) (0.42.0)\n",
"Requirement already satisfied: platformdirs>=2.5.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from pooch>=1.1->librosa>=0.10.0->llmware) (3.10.0)\n",
"Requirement already satisfied: annotated-types>=0.4.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from pydantic<3,>=1.9.0->openai>=1.0.0->llmware) (0.6.0)\n",
"Requirement already satisfied: pydantic-core==2.16.3 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from pydantic<3,>=1.9.0->openai>=1.0.0->llmware) (2.16.3)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from requests->Wikipedia-API==0.6.0->llmware) (3.2.0)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from scikit-learn>=0.20.0->librosa>=0.10.0->llmware) (3.2.0)\n",
"Requirement already satisfied: cffi>=1.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from soundfile>=0.12.1->librosa>=0.10.0->llmware) (1.15.1)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from jinja2->torch>=1.13.1->llmware) (2.1.3)\n",
"Requirement already satisfied: mpmath>=0.19 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from sympy->torch>=1.13.1->llmware) (1.3.0)\n",
"Requirement already satisfied: pycparser in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from cffi>=1.0->soundfile>=0.12.1->librosa>=0.10.0->llmware) (2.21)\n",
"Requirement already satisfied: h11<0.15,>=0.13 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from httpcore<0.17.0,>=0.15.0->httpx<1,>=0.23.0->openai>=1.0.0->llmware) (0.14.0)\n",
"Requirement already satisfied: six>=1.5 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.28.0,>=1.27.53->boto3==1.24.53->llmware) (1.16.0)\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install llmware"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"from llmware.agents import SQLTables, LLMfx\n",
"from llmware.models import ModelCatalog\n",
"from llmware.configs import LLMWareConfig"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Worker function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The process to generate an output for the given input query is as follows:\n",
"1. If `create_new_table` is set to `True`, then:\n",
" - Check to see if the SLIM SQL tool is already downloaded, if not, download it using the `ModelCatalog` class\n",
" - Load in the sample CSV file `customer_table.csv`\n",
" - Create an SQL table from the CSV file using the `SQLTables` class\n",
"2. Create an agent, an instance of the `LLMfx` class, and load in the SQL tool\n",
"3. For each query in the `query_list`, call the `query_db` function on the agent, which:\n",
" - looks up the table schema in the db using the table_name\n",
" - packages the text-2-sql query prompt\n",
" - executes sql method to convert the prompt into a sql query\n",
" - attempts to execute the sql query on the db\n",
" - returns the db results as 'research' output\n",
"4. Print the result from the `agent`'s `research_list`."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def sql_e2e_test_script(table_name=\"customers1\",create_new_table=False):\n",
"\n",
" \"\"\" This is the end-to-end execution script. \"\"\"\n",
"\n",
" # create table if needed to set up\n",
" if create_new_table:\n",
"\n",
" # looks to pull sample csv 'customer_table.csv' from slim-sql-tool model package files\n",
" sql_tool_repo_path = os.path.join(LLMWareConfig().get_model_repo_path(), \"slim-sql-tool\")\n",
"\n",
" if not os.path.exists(sql_tool_repo_path):\n",
" ModelCatalog().load_model(\"llmware/slim-sql-tool\")\n",
"\n",
" files = os.listdir(sql_tool_repo_path)\n",
" csv_file = \"customer_table.csv\"\n",
"\n",
" if csv_file in files:\n",
"\n",
" # to create a testing table from a csv\n",
" sql_db = SQLTables(experimental=True)\n",
" sql_db.create_new_table_from_csv(sql_tool_repo_path, csv_file, table_name=table_name)\n",
" # end - creating table\n",
"\n",
" print(\"update: successfully created new db table\")\n",
" else:\n",
" print(\"something has gone wrong - could not find customer_table.csv inside the slim-sql-tool file package\")\n",
"\n",
" # query starts here\n",
" agent = LLMfx()\n",
" agent.load_tool(\"sql\", sample=False, get_logits=True, temperature=0.0)\n",
"\n",
" # Pass direct queries to the DB\n",
"\n",
" query_list = [\"Which customers have vip customer status of yes?\",\n",
" \"What is the highest annual spend of any customer?\",\n",
" \"Which customer has account number 1234953\",\n",
" \"Which customer has the lowest annual spend?\",\n",
" \"Is Susan Soinsin a vip customer?\"]\n",
"\n",
" for i, query in enumerate(query_list):\n",
"\n",
" # query_db method is doing all of the work\n",
" # -- looks up the table schema in the db using the table_name\n",
" # -- packages the text-2-sql query prompt\n",
" # -- executes sql method to convert the prompt into a sql query\n",
" # -- attempts to execute the sql query on the db\n",
" # -- returns the db results as 'research' output\n",
"\n",
" response = agent.query_db(query, table=table_name)\n",
"\n",
" for x in range(0,len(agent.research_list)):\n",
" print(\"research: \", x, agent.research_list[x])\n",
"\n",
" return 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Helper functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want to delete a table in the experimental database, you can use the `delete_table()` function."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def delete_table(table_name):\n",
"\n",
" \"\"\" Start fresh in testing - delete table in experimental local SQLite DB \"\"\"\n",
"\n",
" sql_db = SQLTables(experimental=True)\n",
" sql_db.delete_table(table_name, confirm_delete=True)\n",
"\n",
" return True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want to delete the entire database, you use the `delete_db()` function."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def delete_db():\n",
"\n",
" \"\"\" Start fresh in testing - deletes SQLite DB and starts over. \"\"\"\n",
"\n",
" sql_db = SQLTables(experimental=True)\n",
" sql_db.delete_experimental_db(confirm_delete=True)\n",
"\n",
" return True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Main block"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now run our code by calling the `sql_e2e_test_script` function."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2d9d5f328d9b4b0f8bac965385d61d89",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Fetching 6 files: 0%| | 0/6 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3d9518d0bdb742469d7efb5f2a589db8",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"library_table.csv: 0%| | 0.00/317 [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1b12a5ddf963431eb01893edb1bd2071",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
".gitattributes: 0%| | 0.00/1.57k [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3de118e4004e4d8fbde1b8114b5e80f4",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"README.md: 0%| | 0.00/1.98k [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "bb89b15e1d9f45e7a927858b2906354e",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"config.json: 0%| | 0.00/3.96k [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e5c4e076065b4a01a73e0168ad6bed71",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"customer_table.csv: 0%| | 0.00/929 [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "76d4ca969d11404fa50bb1822fe07e90",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"slim-sql.gguf: 0%| | 0.00/669M [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"update: successfully created new db table\n",
"update: Launching LLMfx process\n",
"step - \t1 - \tcreating object - ready to start processing.\n",
"step - \t2 - \tloading tool - sql\n",
"step - \t3 - \tloading new processing text - 1 new entries\n",
"step - \t4 - \texecuting function call - deploying - text-to-sql\n",
"\t\t\t\t -- query - Which customers have vip customer status of yes?\n",
"\t\t\t\t -- table_schema - CREATE TABLE customer1 (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )\n",
"step - \t5 - \texecuting function call - getting response - sql\n",
"\t\t\t\t -- llm_response - SELECT customer_name FROM customer1 WHERE vip_customer = 'yes'\n",
"\t\t\t\t -- output type - text\n",
"\t\t\t\t -- usage - {'input': 57, 'output': 16, 'total': 73, 'metric': 'tokens', 'processing_time': 0.877871036529541}\n",
"step - \t6 - \texecuting research call - executing query on db\n",
"\t\t\t\t -- db - C:\\Users\\prash\\llmware_data\\accounts\\sqlite_experimental.db\n",
"\t\t\t\t -- sql_query - SELECT customer_name FROM customer1 WHERE vip_customer = 'yes'\n",
"step - \t7 - \texecuting research - getting response - sql\n",
"\t\t\t\t -- result - [('Martha Williams',), ('John Jones',), ('Arvind Arora',), ('Vinod Aggarwal',), ('Allison Winters',), ('Olivia Smith',), ('Alan Wang',), ('Wilmer Rodriguez',), ('Eliza Listron',), ('Douglas Hudson',)]\n",
"step - \t8 - \tloading new processing text - 1 new entries\n",
"step - \t9 - \texecuting function call - deploying - text-to-sql\n",
"\t\t\t\t -- query - What is the highest annual spend of any customer?\n",
"\t\t\t\t -- table_schema - CREATE TABLE customer1 (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )\n",
"step - \t10 - \texecuting function call - getting response - sql\n",
"\t\t\t\t -- llm_response - SELECT MAX(annual_spend) FROM customer1\n",
"\t\t\t\t -- output type - text\n",
"\t\t\t\t -- usage - {'input': 56, 'output': 12, 'total': 68, 'metric': 'tokens', 'processing_time': 0.4298675060272217}\n",
"step - \t11 - \texecuting research call - executing query on db\n",
"\t\t\t\t -- db - C:\\Users\\prash\\llmware_data\\accounts\\sqlite_experimental.db\n",
"\t\t\t\t -- sql_query - SELECT MAX(annual_spend) FROM customer1\n",
"step - \t12 - \texecuting research - getting response - sql\n",
"\t\t\t\t -- result - [(93540,)]\n",
"step - \t13 - \tloading new processing text - 1 new entries\n",
"step - \t14 - \texecuting function call - deploying - text-to-sql\n",
"\t\t\t\t -- query - Which customer has account number 1234953\n",
"\t\t\t\t -- table_schema - CREATE TABLE customer1 (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )\n",
"step - \t15 - \texecuting function call - getting response - sql\n",
"\t\t\t\t -- llm_response - SELECT customer_name FROM customer1 WHERE account_number = 1234953\n",
"\t\t\t\t -- output type - text\n",
"\t\t\t\t -- usage - {'input': 60, 'output': 20, 'total': 80, 'metric': 'tokens', 'processing_time': 0.6454498767852783}\n",
"step - \t16 - \texecuting research call - executing query on db\n",
"\t\t\t\t -- db - C:\\Users\\prash\\llmware_data\\accounts\\sqlite_experimental.db\n",
"\t\t\t\t -- sql_query - SELECT customer_name FROM customer1 WHERE account_number = 1234953\n",
"step - \t17 - \texecuting research - getting response - sql\n",
"\t\t\t\t -- result - [('Melinda Lyons',)]\n",
"step - \t18 - \tloading new processing text - 1 new entries\n",
"step - \t19 - \texecuting function call - deploying - text-to-sql\n",
"\t\t\t\t -- query - Which customer has the lowest annual spend?\n",
"\t\t\t\t -- table_schema - CREATE TABLE customer1 (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )\n",
"step - \t20 - \texecuting function call - getting response - sql\n",
"\t\t\t\t -- llm_response - SELECT customer_name FROM customer1 ORDER BY annual_spend LIMIT 1\n",
"\t\t\t\t -- output type - text\n",
"\t\t\t\t -- usage - {'input': 55, 'output': 16, 'total': 71, 'metric': 'tokens', 'processing_time': 0.5111117362976074}\n",
"step - \t21 - \texecuting research call - executing query on db\n",
"\t\t\t\t -- db - C:\\Users\\prash\\llmware_data\\accounts\\sqlite_experimental.db\n",
"\t\t\t\t -- sql_query - SELECT customer_name FROM customer1 ORDER BY annual_spend LIMIT 1\n",
"step - \t22 - \texecuting research - getting response - sql\n",
"\t\t\t\t -- result - [('Michael Rogers',)]\n",
"step - \t23 - \tloading new processing text - 1 new entries\n",
"step - \t24 - \texecuting function call - deploying - text-to-sql\n",
"\t\t\t\t -- query - Is Susan Soinsin a vip customer?\n",
"\t\t\t\t -- table_schema - CREATE TABLE customer1 (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[37mWARNING: update: delete sqlite experimental db - table - customer1 \u001b[39m\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"step - \t25 - \texecuting function call - getting response - sql\n",
"\t\t\t\t -- llm_response - SELECT vip_customer FROM customer1 WHERE customer_name = 'Susan Soinsin'\n",
"\t\t\t\t -- output type - text\n",
"\t\t\t\t -- usage - {'input': 56, 'output': 21, 'total': 77, 'metric': 'tokens', 'processing_time': 0.6235740184783936}\n",
"step - \t26 - \texecuting research call - executing query on db\n",
"\t\t\t\t -- db - C:\\Users\\prash\\llmware_data\\accounts\\sqlite_experimental.db\n",
"\t\t\t\t -- sql_query - SELECT vip_customer FROM customer1 WHERE customer_name = 'Susan Soinsin'\n",
"step - \t27 - \texecuting research - getting response - sql\n",
"\t\t\t\t -- result - [('no',)]\n",
"research: 0 {'step': 6, 'tool': 'sql', 'db_response': [('Martha Williams',), ('John Jones',), ('Arvind Arora',), ('Vinod Aggarwal',), ('Allison Winters',), ('Olivia Smith',), ('Alan Wang',), ('Wilmer Rodriguez',), ('Eliza Listron',), ('Douglas Hudson',)], 'sql_query': \"SELECT customer_name FROM customer1 WHERE vip_customer = 'yes'\", 'query': 'Which customers have vip customer status of yes?', 'db': 'C:\\\\Users\\\\prash\\\\llmware_data\\\\accounts\\\\sqlite_experimental.db', 'work_item': 'CREATE TABLE customer1 (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'}\n",
"research: 1 {'step': 11, 'tool': 'sql', 'db_response': [(93540,)], 'sql_query': 'SELECT MAX(annual_spend) FROM customer1', 'query': 'What is the highest annual spend of any customer?', 'db': 'C:\\\\Users\\\\prash\\\\llmware_data\\\\accounts\\\\sqlite_experimental.db', 'work_item': 'CREATE TABLE customer1 (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'}\n",
"research: 2 {'step': 16, 'tool': 'sql', 'db_response': [('Melinda Lyons',)], 'sql_query': 'SELECT customer_name FROM customer1 WHERE account_number = 1234953', 'query': 'Which customer has account number 1234953', 'db': 'C:\\\\Users\\\\prash\\\\llmware_data\\\\accounts\\\\sqlite_experimental.db', 'work_item': 'CREATE TABLE customer1 (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'}\n",
"research: 3 {'step': 21, 'tool': 'sql', 'db_response': [('Michael Rogers',)], 'sql_query': 'SELECT customer_name FROM customer1 ORDER BY annual_spend LIMIT 1', 'query': 'Which customer has the lowest annual spend?', 'db': 'C:\\\\Users\\\\prash\\\\llmware_data\\\\accounts\\\\sqlite_experimental.db', 'work_item': 'CREATE TABLE customer1 (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'}\n",
"research: 4 {'step': 26, 'tool': 'sql', 'db_response': [('no',)], 'sql_query': \"SELECT vip_customer FROM customer1 WHERE customer_name = 'Susan Soinsin'\", 'query': 'Is Susan Soinsin a vip customer?', 'db': 'C:\\\\Users\\\\prash\\\\llmware_data\\\\accounts\\\\sqlite_experimental.db', 'work_item': 'CREATE TABLE customer1 (customer_name text, account_number integer, customer_level text, vip_customer text, annual_spend integer, user_name text )'}\n"
]
}
],
"source": [
"if __name__ == \"__main__\":\n",
"\n",
" ModelCatalog().get_llm_toolkit(tool_list=[\"sql\"])\n",
"\n",
" # run an end-to-end test\n",
" sql_e2e_test_script(table_name=\"customer1\",create_new_table=True)\n",
"\n",
" # third - delete and start fresh for further testing\n",
" delete_table(\"customer1\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now have the output of our program. The output for each natural language question is a dictionary containing a lot of detailed information about the steps carried out by the agent, but here are some of the more interesting parts of it:\n",
"- `db_response` gives us what we want, the answer to the question we ask\n",
"- `sql_query` shows us the SQL query that was generated from our natural language question using the SLIM SQL tool."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,474 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Local Ask and Answer Dueling Bots with Small Language Models"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You've probably asked a question to a language model before and then had it give you an answer. After all, this is what we most commonly use language models for.\n",
"\n",
"But have you ever received a question from a language model? While not as common, this application of AI has diverse use cases in areas like education, where you might want a model to give you practice questions for a test, and in sales enablement, where you question your business's sales team about your products to improve their ability make sales.\n",
"\n",
"Now, what if we had a face off⚔️ between two different models: one that asked questions about a topic and another that answered them? All without human intervention?\n",
"\n",
"In this notebook, we're going to look at exactly that. We'll provide a sample passage about OpenAI's AI safety team as context to our models. We'll then let our models duel it out! One model will ask questions based on this passage, and another model will respond!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### For Google Colab users"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you are using Colab for free, we highly recommend you activate the T4 GPU hardware accelerator. Our models are designed to run with at least 16GB of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as apposed to the ~13GB Colab gives automatically.\n",
"\n",
"To activate T4:\n",
"1. click on the \"Runtime\" tab\n",
"2. click on \"Change runtime type\"\n",
"3. select T4 GPU under Hardware Accelerator\n",
"\n",
"NOTE: there is a weekly usage limit on using T4 for free"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Streamlit example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have an [interactive Streamlit program](https://github.com/llmware-ai/llmware/blob/main/examples/UI/dueling_chatbot.py) for this example in our repository. To run it, navigate to the directory the file is located in, and run `streamlit run dueling_chatbot.py` in a terminal."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Installing and importing dependencies"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Defaulting to user installation because normal site-packages is not writeable\n",
"Requirement already satisfied: llmware in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (0.3.3)\n",
"Requirement already satisfied: boto3>=1.24.53 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (1.24.53)\n",
"Requirement already satisfied: huggingface-hub>=0.19.4 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.19.4)\n",
"Requirement already satisfied: numpy>=1.23.2 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (1.26.0)\n",
"Requirement already satisfied: pymongo>=4.7.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (4.7.2)\n",
"Requirement already satisfied: tokenizers>=0.15.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.15.2)\n",
"Requirement already satisfied: psycopg-binary==3.1.17 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: psycopg==3.1.17 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: pgvector==0.2.4 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.2.4)\n",
"Requirement already satisfied: colorama==0.4.6 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.4.6)\n",
"Requirement already satisfied: librosa>=0.10.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from llmware) (0.10.2.post1)\n",
"Requirement already satisfied: typing-extensions>=4.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from psycopg==3.1.17->llmware) (4.8.0)\n",
"Requirement already satisfied: tzdata in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from psycopg==3.1.17->llmware) (2023.3)\n",
"Requirement already satisfied: botocore<1.28.0,>=1.27.53 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from boto3>=1.24.53->llmware) (1.27.96)\n",
"Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from boto3>=1.24.53->llmware) (1.0.1)\n",
"Requirement already satisfied: s3transfer<0.7.0,>=0.6.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from boto3>=1.24.53->llmware) (0.6.2)\n",
"Requirement already satisfied: filelock in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (3.13.1)\n",
"Requirement already satisfied: fsspec>=2023.5.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (2023.10.0)\n",
"Requirement already satisfied: requests in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (2.31.0)\n",
"Requirement already satisfied: tqdm>=4.42.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (4.66.1)\n",
"Requirement already satisfied: pyyaml>=5.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (6.0.1)\n",
"Requirement already satisfied: packaging>=20.9 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from huggingface-hub>=0.19.4->llmware) (23.1)\n",
"Requirement already satisfied: audioread>=2.1.9 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (3.0.1)\n",
"Requirement already satisfied: scipy>=1.2.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.11.3)\n",
"Requirement already satisfied: scikit-learn>=0.20.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.3.1)\n",
"Requirement already satisfied: joblib>=0.14 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.3.2)\n",
"Requirement already satisfied: decorator>=4.3.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (5.1.1)\n",
"Requirement already satisfied: numba>=0.51.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (0.59.1)\n",
"Requirement already satisfied: soundfile>=0.12.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (0.12.1)\n",
"Requirement already satisfied: pooch>=1.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.8.1)\n",
"Requirement already satisfied: soxr>=0.3.2 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (0.3.7)\n",
"Requirement already satisfied: lazy-loader>=0.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (0.4)\n",
"Requirement already satisfied: msgpack>=1.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from librosa>=0.10.0->llmware) (1.0.8)\n",
"Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from pymongo>=4.7.0->llmware) (2.6.1)\n",
"Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from botocore<1.28.0,>=1.27.53->boto3>=1.24.53->llmware) (2.8.2)\n",
"Requirement already satisfied: urllib3<1.27,>=1.25.4 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from botocore<1.28.0,>=1.27.53->boto3>=1.24.53->llmware) (1.26.18)\n",
"Requirement already satisfied: llvmlite<0.43,>=0.42.0dev0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from numba>=0.51.0->librosa>=0.10.0->llmware) (0.42.0)\n",
"Requirement already satisfied: platformdirs>=2.5.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from pooch>=1.1->librosa>=0.10.0->llmware) (3.10.0)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from requests->huggingface-hub>=0.19.4->llmware) (3.2.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from requests->huggingface-hub>=0.19.4->llmware) (2.10)\n",
"Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from requests->huggingface-hub>=0.19.4->llmware) (2023.7.22)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from scikit-learn>=0.20.0->librosa>=0.10.0->llmware) (3.2.0)\n",
"Requirement already satisfied: cffi>=1.0 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from soundfile>=0.12.1->librosa>=0.10.0->llmware) (1.15.1)\n",
"Requirement already satisfied: pycparser in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from cffi>=1.0->soundfile>=0.12.1->librosa>=0.10.0->llmware) (2.21)\n",
"Requirement already satisfied: six>=1.5 in c:\\users\\prash\\appdata\\roaming\\python\\python311\\site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.28.0,>=1.27.53->boto3>=1.24.53->llmware) (1.16.0)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"[notice] A new release of pip is available: 24.0 -> 24.1.2\n",
"[notice] To update, run: python.exe -m pip install --upgrade pip\n"
]
}
],
"source": [
"!pip install llmware"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from llmware.models import ModelCatalog"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Generating questions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this function, we will see how we can generate questions about the `source_passage` using the slim-q-gen-tiny-tool. The workflow for this function is as follows:\n",
"- Load in the the model using `ModelCatalog`\n",
"- Use `function_call()` to generate questions from the model `number_of_tries` times\n",
"- Add the generated question to the `questions` list only if it is unique and is not already in the list\n",
"- Output the `questions`."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def hello_world_test(source_passage, q_model=\"slim-q-gen-tiny-tool\", number_of_tries=10, question_type=\"question\", temperature=0.5):\n",
"\n",
" \"\"\" Shows a basic example of generating questions from a text passage, running a number of inferences,\n",
" and then keeping only the unique questions generated.\n",
"\n",
" -- source_passage = text passage\n",
" -- number_of_tries = integer number of times to call the model to generate a question\n",
" -- question_type = \"question\" | \"boolean\" | \"multiple choice\"\n",
"\n",
" \"\"\"\n",
"\n",
" # recommend using temperature of 0.2 - 0.8 - for multiple choice, use lower end of the range\n",
" q_model = ModelCatalog().load_model(q_model, sample=True, temperature=temperature)\n",
"\n",
" questions = []\n",
"\n",
" for x in range(0, number_of_tries):\n",
"\n",
" response = q_model.function_call(source_passage, params=[question_type], get_logits=False)\n",
"\n",
" # expect response in the form of: \"llm_response\": {\"question\": [\"generated question?\"] }\n",
"\n",
" if response:\n",
" if \"llm_response\" in response:\n",
" if \"question\" in response[\"llm_response\"]:\n",
" new_q = response[\"llm_response\"][\"question\"]\n",
"\n",
" # keep only new questions\n",
" if new_q not in questions:\n",
" questions.append(new_q)\n",
"\n",
" print(f\"inference {x} - response: {response['llm_response']}\")\n",
"\n",
" print(f\"\\nDe-duped list of questions created\\n\")\n",
" for i, question in enumerate(questions):\n",
"\n",
" print(f\"new generated questions: {i} - {question}\")\n",
"\n",
" return questions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dueling AIs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This function will allow us to generate the questions using the same process as above, but then have a different model answer those questions. The process is as follows:\n",
"- Generate the `questions` list using the same steps as the previous function.\n",
"- Load in the answer model using `ModelCatalog`\n",
"- Answer each question in `questions` using the `inference()` function of the answer model."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def ask_and_answer_game(source_passage, q_model=\"slim-q-gen-tiny-tool\", number_of_tries=10, question_type=\"question\",\n",
" temperature=0.5):\n",
"\n",
" \"\"\" Shows a simple two model game of using q-gen model to generate a question, and then a second model\n",
" to answer the question generated. \"\"\"\n",
"\n",
" # this is the model that will generate the 'question'\n",
" q_model = ModelCatalog().load_model(q_model, sample=True, temperature=temperature)\n",
"\n",
" # this will be the model used to 'answer' the question\n",
" answer_model = ModelCatalog().load_model(\"bling-phi-3-gguf\")\n",
"\n",
" questions = []\n",
"\n",
" print(f\"\\nGenerating a set of questions automatically from the source passage.\\n\")\n",
"\n",
" for x in range(0,number_of_tries):\n",
"\n",
" response = q_model.function_call(source_passage, params=[question_type], get_logits=False)\n",
"\n",
" if response:\n",
" if \"llm_response\" in response:\n",
" if \"question\" in response[\"llm_response\"]:\n",
" new_q = response[\"llm_response\"][\"question\"]\n",
"\n",
" # only keep new questions\n",
" if new_q and new_q not in questions:\n",
" questions.append(new_q)\n",
"\n",
" print(f\"inference - {x} - response: {response['llm_response']}\")\n",
"\n",
" print(\"\\nAnswering the generated questions\\n\")\n",
" for i, question in enumerate(questions):\n",
"\n",
" print(f\"\\nquestion: {i} - {question}\")\n",
" if isinstance(question, list) and len(question) > 0:\n",
" response = answer_model.inference(question[0], add_context=source_passage)\n",
" print(f\"response: \", response[\"llm_response\"])\n",
"\n",
" return True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Main block"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, we state our source text and call both functions above."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# test passage pulled from CNBC news story on Tuesday, May 28, 2024\n",
"test_passage = (\"OpenAI said Tuesday it has established a new committee to make recommendations to the \"\n",
" \"companys board about safety and security, weeks after dissolving a team focused on AI safety. \"\n",
" \"In a blog post, OpenAI said the new committee would be led by CEO Sam Altman as well as \"\n",
" \"Bret Taylor, the companys board chair, and board member Nicole Seligman. The announcement \"\n",
" \"follows the high-profile exit this month of an OpenAI executive focused on safety, \"\n",
" \"Jan Leike. Leike resigned from OpenAI leveling criticisms that the company had \"\n",
" \"under-invested in AI safety work and that tensions with OpenAIs leadership had \"\n",
" \"reached a breaking point.\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"inference 0 - response: {'question': ['Who are the two members of the new safety and security committee?']}\n",
"inference 1 - response: {'question': ['What is the name of the new committee?']}\n",
"inference 2 - response: {'question': ['What is the name of the executive who resigned?']}\n",
"inference 3 - response: {'question': ['What is the name of the CEO of the company?']}\n",
"inference 4 - response: {'question': ['What is the name of the board chair?']}\n",
"inference 5 - response: {'question': ['Who is the chairman of OpenAI?']}\n",
"inference 6 - response: {'question': ['What is a list of the key points in the announcement?']}\n",
"inference 7 - response: {'question': ['What is a list of three key points?']}\n",
"inference 8 - response: {'question': ['What is the name of the executive who resigned?']}\n",
"inference 9 - response: {'question': ['What is the name of the executive who resigned from OpenAI?']}\n",
"\n",
"De-duped list of questions created\n",
"\n",
"new generated questions: 0 - ['Who are the two members of the new safety and security committee?']\n",
"new generated questions: 1 - ['What is the name of the new committee?']\n",
"new generated questions: 2 - ['What is the name of the executive who resigned?']\n",
"new generated questions: 3 - ['What is the name of the CEO of the company?']\n",
"new generated questions: 4 - ['What is the name of the board chair?']\n",
"new generated questions: 5 - ['Who is the chairman of OpenAI?']\n",
"new generated questions: 6 - ['What is a list of the key points in the announcement?']\n",
"new generated questions: 7 - ['What is a list of three key points?']\n",
"new generated questions: 8 - ['What is the name of the executive who resigned from OpenAI?']\n"
]
},
{
"data": {
"text/plain": [
"[['Who are the two members of the new safety and security committee?'],\n",
" ['What is the name of the new committee?'],\n",
" ['What is the name of the executive who resigned?'],\n",
" ['What is the name of the CEO of the company?'],\n",
" ['What is the name of the board chair?'],\n",
" ['Who is the chairman of OpenAI?'],\n",
" ['What is a list of the key points in the announcement?'],\n",
" ['What is a list of three key points?'],\n",
" ['What is the name of the executive who resigned from OpenAI?']]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hello_world_test(test_passage,q_model=\"slim-q-gen-tiny-tool\",number_of_tries=10, question_type=\"question\", temperature=0.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see each question that was generated `number_of_tries` times, and then the final question list with the duplicates removed."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Generating a set of questions automatically from the source passage.\n",
"\n",
"inference - 0 - response: {'question': ['What is the name of the executive who resigned?']}\n",
"inference - 1 - response: {'question': ['When was this announcement made?']}\n",
"inference - 2 - response: {'question': ['What is the name of one of the members of the new committee?']}\n",
"inference - 3 - response: {'question': ['What is one of the names of the people who will lead the new committee?']}\n",
"inference - 4 - response: {'question': ['What is the name of the person who will lead the new committee?']}\n",
"inference - 5 - response: {'question': ['Who is leading the new advisory group?']}\n",
"inference - 6 - response: {'question': ['What is the name of the executive who resigned?']}\n",
"inference - 7 - response: {'question': ['What is the name of the person who is leading the new committee?']}\n",
"inference - 8 - response: {'question': ['What is one role of Nicole Seligman?']}\n",
"inference - 9 - response: {'question': ['What is one of the names of one of the members of this new committee?']}\n",
"\n",
"Answering the generated questions\n",
"\n",
"\n",
"question: 0 - ['What is the name of the executive who resigned?']\n",
"response: Jan Leike\n",
"\n",
"question: 1 - ['When was this announcement made?']\n",
"response: Tuesday\n",
"\n",
"question: 2 - ['What is the name of one of the members of the new committee?']\n",
"response: Bret Taylor\n",
"\n",
"question: 3 - ['What is one of the names of the people who will lead the new committee?']\n",
"response: Sam Altman\n",
"\n",
"question: 4 - ['What is the name of the person who will lead the new committee?']\n",
"response: Sam Altman\n",
"\n",
"question: 5 - ['Who is leading the new advisory group?']\n",
"response: CEO Sam Altman\n",
"\n",
"question: 6 - ['What is the name of the person who is leading the new committee?']\n",
"response: Sam Altman\n",
"\n",
"question: 7 - ['What is one role of Nicole Seligman?']\n",
"response: Board Chair of OpenAI\n",
"\n",
"question: 8 - ['What is one of the names of one of the members of this new committee?']\n",
"response: Bret Taylor\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ask_and_answer_game(test_passage,q_model=\"slim-q-gen-phi-3-tool\", number_of_tries=10, question_type=\"question\", temperature=0.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, we can see each question generated, then the responses to each unique (non-duplicate) question."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,387 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "jdUlCWCXWX-C"
},
"source": [
"This example shows how to use llmware provided sample files for testing with WhisperCPP, integrated as of\n",
" llmware 0.2.11.\n",
"\n",
" # examples - \"famous_quotes\" | \"greatest_speeches\" | \"youtube_demos\" | \"earnings_calls\"\n",
"\n",
" -- famous_quotes - approximately 20 small .wav files with clips from old movies and speeches\n",
" -- greatest_speeches - approximately 60 famous historical speeches in english\n",
" -- youtube_videos - wav files of ~3 llmware youtube videos\n",
" -- earnings_calls - wav files of ~4 public company earnings calls (gathered from public investor relations)\n",
"\n",
" These sample files are hosted in a non-restricted AWS S3 bucket, and downloaded via the Setup method\n",
" `load_sample_voice_files`. There are two options:\n",
"\n",
" -- small_only = True: only pulls the 'famous_quotes' samples\n",
" -- small_only = False: pulls all of the samples (requires ~1.9 GB in total)\n",
"\n",
" Please note that all of these samples have been pulled from open public domain sources, including the\n",
" Internet Archives, e.g., https://archive.org. These sample files are being provided solely for the purpose of\n",
" testing the code scripts below. Please do not use them for any other purpose.\n",
"\n",
" To run these examples, please make sure to `pip install librosa`\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WM0bYHGyWpGu"
},
"source": [
"# If you are using Colab for free, we highly recommend you activate the T4 GPU\n",
"# hardware accelerator. Our models are designed to run with at least 16GB\n",
"# of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as opposed\n",
"# to the ~13GB Colab gives automatically.\n",
"# To activate T4:\n",
"# 1. click on the \"Runtime\" tab\n",
"# 2. click on \"Change runtime type\"\n",
"# 3. select T4 GPU under Hardware Accelerator\n",
"# NOTE: there is a weekly usage limit on using T4 for free"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4vi_LENqVGHp",
"outputId": "4b296c7b-e67a-4dee-faad-da3636c79858"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: llmware in /usr/local/lib/python3.10/dist-packages (0.3.0)\n",
"Requirement already satisfied: boto3>=1.24.53 in /usr/local/lib/python3.10/dist-packages (from llmware) (1.34.129)\n",
"Requirement already satisfied: huggingface-hub>=0.19.4 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.23.3)\n",
"Requirement already satisfied: numpy>=1.23.2 in /usr/local/lib/python3.10/dist-packages (from llmware) (1.25.2)\n",
"Requirement already satisfied: pymongo>=4.7.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (4.7.3)\n",
"Requirement already satisfied: tokenizers>=0.15.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.19.1)\n",
"Requirement already satisfied: psycopg-binary==3.1.17 in /usr/local/lib/python3.10/dist-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: psycopg==3.1.17 in /usr/local/lib/python3.10/dist-packages (from llmware) (3.1.17)\n",
"Requirement already satisfied: pgvector==0.2.4 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.2.4)\n",
"Requirement already satisfied: colorama==0.4.6 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.4.6)\n",
"Requirement already satisfied: librosa>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.10.2.post1)\n",
"Requirement already satisfied: typing-extensions>=4.1 in /usr/local/lib/python3.10/dist-packages (from psycopg==3.1.17->llmware) (4.12.2)\n",
"Requirement already satisfied: botocore<1.35.0,>=1.34.129 in /usr/local/lib/python3.10/dist-packages (from boto3>=1.24.53->llmware) (1.34.129)\n",
"Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from boto3>=1.24.53->llmware) (1.0.1)\n",
"Requirement already satisfied: s3transfer<0.11.0,>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from boto3>=1.24.53->llmware) (0.10.1)\n",
"Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (3.14.0)\n",
"Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (2023.6.0)\n",
"Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (24.1)\n",
"Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (6.0.1)\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (2.31.0)\n",
"Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.4->llmware) (4.66.4)\n",
"Requirement already satisfied: audioread>=2.1.9 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (3.0.1)\n",
"Requirement already satisfied: scipy>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.11.4)\n",
"Requirement already satisfied: scikit-learn>=0.20.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.2.2)\n",
"Requirement already satisfied: joblib>=0.14 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.4.2)\n",
"Requirement already satisfied: decorator>=4.3.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (4.4.2)\n",
"Requirement already satisfied: numba>=0.51.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.58.1)\n",
"Requirement already satisfied: soundfile>=0.12.1 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.12.1)\n",
"Requirement already satisfied: pooch>=1.1 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.8.2)\n",
"Requirement already satisfied: soxr>=0.3.2 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.3.7)\n",
"Requirement already satisfied: lazy-loader>=0.1 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (0.4)\n",
"Requirement already satisfied: msgpack>=1.0 in /usr/local/lib/python3.10/dist-packages (from librosa>=0.10.0->llmware) (1.0.8)\n",
"Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in /usr/local/lib/python3.10/dist-packages (from pymongo>=4.7.0->llmware) (2.6.1)\n",
"Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /usr/local/lib/python3.10/dist-packages (from botocore<1.35.0,>=1.34.129->boto3>=1.24.53->llmware) (2.8.2)\n",
"Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in /usr/local/lib/python3.10/dist-packages (from botocore<1.35.0,>=1.34.129->boto3>=1.24.53->llmware) (2.0.7)\n",
"Requirement already satisfied: llvmlite<0.42,>=0.41.0dev0 in /usr/local/lib/python3.10/dist-packages (from numba>=0.51.0->librosa>=0.10.0->llmware) (0.41.1)\n",
"Requirement already satisfied: platformdirs>=2.5.0 in /usr/local/lib/python3.10/dist-packages (from pooch>=1.1->librosa>=0.10.0->llmware) (4.2.2)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.4->llmware) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.4->llmware) (3.7)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.4->llmware) (2024.6.2)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=0.20.0->librosa>=0.10.0->llmware) (3.5.0)\n",
"Requirement already satisfied: cffi>=1.0 in /usr/local/lib/python3.10/dist-packages (from soundfile>=0.12.1->librosa>=0.10.0->llmware) (1.16.0)\n",
"Requirement already satisfied: pycparser in /usr/local/lib/python3.10/dist-packages (from cffi>=1.0->soundfile>=0.12.1->librosa>=0.10.0->llmware) (2.22)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.35.0,>=1.34.129->boto3>=1.24.53->llmware) (1.16.0)\n",
"Requirement already satisfied: librosa in /usr/local/lib/python3.10/dist-packages (0.10.2.post1)\n",
"Requirement already satisfied: audioread>=2.1.9 in /usr/local/lib/python3.10/dist-packages (from librosa) (3.0.1)\n",
"Requirement already satisfied: numpy!=1.22.0,!=1.22.1,!=1.22.2,>=1.20.3 in /usr/local/lib/python3.10/dist-packages (from librosa) (1.25.2)\n",
"Requirement already satisfied: scipy>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from librosa) (1.11.4)\n",
"Requirement already satisfied: scikit-learn>=0.20.0 in /usr/local/lib/python3.10/dist-packages (from librosa) (1.2.2)\n",
"Requirement already satisfied: joblib>=0.14 in /usr/local/lib/python3.10/dist-packages (from librosa) (1.4.2)\n",
"Requirement already satisfied: decorator>=4.3.0 in /usr/local/lib/python3.10/dist-packages (from librosa) (4.4.2)\n",
"Requirement already satisfied: numba>=0.51.0 in /usr/local/lib/python3.10/dist-packages (from librosa) (0.58.1)\n",
"Requirement already satisfied: soundfile>=0.12.1 in /usr/local/lib/python3.10/dist-packages (from librosa) (0.12.1)\n",
"Requirement already satisfied: pooch>=1.1 in /usr/local/lib/python3.10/dist-packages (from librosa) (1.8.2)\n",
"Requirement already satisfied: soxr>=0.3.2 in /usr/local/lib/python3.10/dist-packages (from librosa) (0.3.7)\n",
"Requirement already satisfied: typing-extensions>=4.1.1 in /usr/local/lib/python3.10/dist-packages (from librosa) (4.12.2)\n",
"Requirement already satisfied: lazy-loader>=0.1 in /usr/local/lib/python3.10/dist-packages (from librosa) (0.4)\n",
"Requirement already satisfied: msgpack>=1.0 in /usr/local/lib/python3.10/dist-packages (from librosa) (1.0.8)\n",
"Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from lazy-loader>=0.1->librosa) (24.1)\n",
"Requirement already satisfied: llvmlite<0.42,>=0.41.0dev0 in /usr/local/lib/python3.10/dist-packages (from numba>=0.51.0->librosa) (0.41.1)\n",
"Requirement already satisfied: platformdirs>=2.5.0 in /usr/local/lib/python3.10/dist-packages (from pooch>=1.1->librosa) (4.2.2)\n",
"Requirement already satisfied: requests>=2.19.0 in /usr/local/lib/python3.10/dist-packages (from pooch>=1.1->librosa) (2.31.0)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=0.20.0->librosa) (3.5.0)\n",
"Requirement already satisfied: cffi>=1.0 in /usr/local/lib/python3.10/dist-packages (from soundfile>=0.12.1->librosa) (1.16.0)\n",
"Requirement already satisfied: pycparser in /usr/local/lib/python3.10/dist-packages (from cffi>=1.0->soundfile>=0.12.1->librosa) (2.22)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->pooch>=1.1->librosa) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->pooch>=1.1->librosa) (3.7)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->pooch>=1.1->librosa) (2.0.7)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->pooch>=1.1->librosa) (2024.6.2)\n"
]
}
],
"source": [
"!pip install llmware\n",
"!pip install librosa"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YlMbZEubW3P7"
},
"source": [
"\n",
"**Set the verbose config to TRUE if you would like a more detailed readout of the results.**\n",
"\n",
"**Set your language, English (en) is default.**"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "baMdUNuIwlrm"
},
"outputs": [],
"source": [
"\n",
"\n",
"import os\n",
"from llmware.models import ModelCatalog\n",
"from llmware.gguf_configs import GGUFConfigs\n",
"from llmware.setup import Setup\n",
"\n",
"# optional / to adjust various parameters of the model\n",
"GGUFConfigs().set_config(\"whisper_cpp_verbose\", \"OFF\")\n",
"GGUFConfigs().set_config(\"whisper_cpp_realtime_display\", True)\n",
"\n",
"# note: english is default output - change to 'es' | 'fr' | 'de' | 'it' ...\n",
"GGUFConfigs().set_config(\"whisper_language\", \"en\")\n",
"GGUFConfigs().set_config(\"whisper_remove_segment_markers\", True)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "cWTYJLfXWOzH"
},
"outputs": [],
"source": [
"def sample_files(example=\"famous_quotes\", small_only=False):\n",
"\n",
" \"\"\" Execute a basic inference on Voice-to-Text model passing a file_path string \"\"\"\n",
"\n",
" voice_samples = Setup().load_voice_sample_files(small_only=small_only)\n",
"\n",
" examples = [\"famous_quotes\", \"greatest_speeches\", \"youtube_demos\", \"earnings_calls\"]\n",
"\n",
" if example not in examples:\n",
" print(\"choose one of the following - \", examples)\n",
" return 0\n",
"\n",
" fp = os.path.join(voice_samples,example)\n",
"\n",
" files = os.listdir(fp)\n",
"\n",
" # these are the two key lines\n",
" whisper_base_english = \"whisper-cpp-base-english\"\n",
"\n",
" model = ModelCatalog().load_model(whisper_base_english)\n",
"\n",
" for f in files:\n",
"\n",
" if f.endswith(\".wav\"):\n",
"\n",
" prompt = os.path.join(fp,f)\n",
"\n",
" print(f\"\\n\\nPROCESSING: prompt = {prompt}\")\n",
"\n",
" response = model.inference(prompt)\n",
"\n",
" print(\"\\nllm response: \", response[\"llm_response\"])\n",
" print(\"usage: \", response[\"usage\"])\n",
"\n",
" return 0"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fAf9KlzBXhSm"
},
"source": [
"**Change the audio you want transcribed from our collection of sample files, or alter the code to point towards your own audio files.**"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vo7_pq32WMz7",
"outputId": "aae49981-9fda-4374-c42a-c9610bcbb4b3"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.10/dist-packages/huggingface_hub/file_download.py:1194: UserWarning: `local_dir_use_symlinks` parameter is deprecated and will be ignored. The process to download files to a local folder has been updated and do not rely on symlinks anymore. You only need to pass a destination folder as`local_dir`.\n",
"For more details, check out https://huggingface.co/docs/huggingface_hub/main/en/guides/download#download-files-to-local-folder.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/cusack_say_anything_career.wav\n",
"\n",
"llm response: i want to sell anything by anything or process anything as a career i want to sell anything but a process to buy anything so the process process anything so bought or process repair anything so bought a process you know it's a career i want to\n",
"usage: {'duration-seconds': 16.8298125, 'segments': 4, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/all_presidents_money.wav\n",
"\n",
"llm response: supposedly he's got a lawyer with twenty five thousand dollars and a brown paper bag. \"Hey follow the money.\" What do you mean? Well, I can't tell you that. But you could tell me that. No, I have to do this my way. You tell me what you know and I'll confirm. I'll keep you in the right direction if I can, but that's all. Just follow the money.\n",
"usage: {'duration-seconds': 31.0826875, 'segments': 9, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/cant_handle.wav\n",
"\n",
"llm response: I said I want the truth. You can't handle the truth. I said I want the truth. You can't handle the truth.\n",
"usage: {'duration-seconds': 7.096625, 'segments': 1, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/none_shall_pass.wav\n",
"\n",
"llm response: None shall pass. What none shall pass?\n",
"usage: {'duration-seconds': 4.8298125, 'segments': 1, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/field_dreams_he_will_come.wav\n",
"\n",
"llm response: if you build it, he will come.\n",
"usage: {'duration-seconds': 3.2624375, 'segments': 1, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/usual_suspects_smarter2.wav\n",
"\n",
"llm response: Let me get right to the point. I'm smarter than you. And I'm going to find out what I want to know. And I'm going to get it from you whether you like it or not.\n",
"usage: {'duration-seconds': 6.77225, 'segments': 1, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/life_moves_fast.wav\n",
"\n",
"llm response: life moves pretty fast you don't stop and look around once in a while\n",
"usage: {'duration-seconds': 4.5989375, 'segments': 2, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/bonds.wav\n",
"\n",
"llm response: Bond. James Bond. My name's Bond. James Bond. My name is Bond. James Bond. James Bond.\n",
"usage: {'duration-seconds': 12.57825, 'segments': 1, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/willy_wonka_questions.wav\n",
"\n",
"llm response: I'm sorry, but all questions must be submitted in writing.\n",
"usage: {'duration-seconds': 2.66875, 'segments': 1, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/wall_street_greed.wav\n",
"\n",
"llm response: Greed is good. Greed is right. Greed works.\n",
"usage: {'duration-seconds': 5.7818125, 'segments': 1, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/optimistic.wav\n",
"\n",
"llm response: The emperor does not share your optimistic appraisal of the situation.\n",
"usage: {'duration-seconds': 4.6734375, 'segments': 1, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/bond_james_bond.wav\n",
"\n",
"llm response: I admire your luck, Mr. Bond. Jamie's Bond.\n",
"usage: {'duration-seconds': 7.6161875, 'segments': 2, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/t3_judgment_day.wav\n",
"\n",
"llm response: the end of the world is today three hours from now two hours and fifty-three minutes.\n",
"usage: {'duration-seconds': 12.201375, 'segments': 1, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/t1_be_back.wav\n",
"\n",
"llm response: i'll be back.\n",
"usage: {'duration-seconds': 1.053375, 'segments': 1, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/jfk.wav\n",
"\n",
"llm response: And so my fellow Americans, ask not what your country can do for you, ask what you can do for your country.\n",
"usage: {'duration-seconds': 11.0, 'segments': 1, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/apologize.wav\n",
"\n",
"llm response: Alright, alright, I apologize. I'm really, really sorry. I apologize unreservedly. I offer a complete and utter retraction. The imputation was totally without basis in fact and was in no way fair comment and was motivated purely by malice. And I deeply regret any distress that my comments may have caused you or your family, and I hereby undertake not to repeat any such slander at any time in the future.<|endoftext|>\n",
"usage: {'duration-seconds': 28.143, 'segments': 1, 'language': 'en'}\n",
"\n",
"\n",
"PROCESSING: prompt = /root/llmware_data/voice_sample_files/famous_quotes/indiana_last_situation.wav\n",
"\n",
"llm response: Our situation has not improved.\n",
"usage: {'duration-seconds': 2.249625, 'segments': 1, 'language': 'en'}\n"
]
}
],
"source": [
"if __name__ == \"__main__\":\n",
"\n",
" # pick among the four examples: famous_quotes | greatest_speeches | youtube_demos | earnings_calls\n",
"\n",
" sample_files(example=\"famous_quotes\", small_only=False)"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"gpuType": "T4",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}