{ "cells": [ { "cell_type": "markdown", "id": "374b67d0-b446-4d6f-8e07-59e97716c55a", "metadata": {}, "source": [ "# Sales Bot with Llama3 - A Summarization and RAG Use Case" ] }, { "cell_type": "markdown", "id": "add4953d-07c3-4480-ad91-7d0ea9c9fb55", "metadata": {}, "source": [ "## Overview\n", "\n", "In this notebook you'll take an Amazon product reviews dataset from Kaggle and use Llama3 to obtain product review summaries, upsert those summaries in a vector database, then use Retrieval Augmented Generation (RAG) to power a sales chatbot that can make targeted product recommendations.\n", "\n", "Let's take a look at the overall workflow:\n", "1. We start with a dataset that contains over 10,000 reviews across 900 Amazon musical instruments and accessories.\n", "2. Using Llama2 70B chat (hosted on OctoAI), we generate summaries of product reviews for each product from the 20 most recent reviews. We format the summaries in JSON format.\n", "3. We then take the summaries and upsert them into a vector database (Weaviate in this case)\n", "4. We then use this vector database and Llama3 70B instruct (hosted on OctoAI) to build a RAG-based sales chatbot that provides targeted recommendations to the user based on the products that are present in the inventory.\n", "\n", "Note: at the time of writing this tutorial, JSON mode formatting isn't supported for Llama 3 on OctoAI via constrained sampling which is why we are falling back onto Llama 2. This tutorial will be updated when the feature becomes available to rely on Llama 3 exclusively.\n", "\n", "### OctoAI\n", "We'll use [OctoAI](https://octo.ai/) to power all of the GenAI model needs of this notebook: LLMs, image gen, image animation.\n", "* To use OctoAI, you'll need to go to https://octoai.cloud/ and sign in using your Google or GitHub account.\n", "* Next you'll need to generate an OctoAI API token by following these [instructions](https://octo.ai/docs/getting-started/how-to-create-an-octoai-access-token). Keep the API token in hand, we'll need it further down in this notebook.\n", "\n", "In this example we will use the Llama 3 70b instruct model. You can find more on Llama models on the [OctoAI text generation solution page](https://octoai.cloud/text).\n", "\n", "At the time of writing this notebook the following Llama models are available on OctoAI:\n", "* meta-llama-3-8b-instruct\n", "* meta-llama-3-70b-instruct\n", "* codellama-7b-instruct\n", "* codellama-13b-instruct\n", "* codellama-34b-instruct\n", "* llama-2-13b-chat\n", "* llama-2-70b-chat\n", "* llamaguard-7b\n", "\n", "### Weaviate\n", "We'll use Weaviate Cloud Services (WCS) for our vector database. You can create an account and Weaviate clusters easily at the following link: https://console.weaviate.cloud/.\n", "You can then create a cluster, from which you can obtain the REST Endpoint URL and the API key to use the cluster endpoint.\n", "\n", "### OpenAI\n", "We'll be using OpenAI for its embedding model to upsert our vectors into the Weaviate vector database. Create an account and obtain an API key here: https://openai.com/api/\n", "\n", "### Local Python Notebook\n", "We highly recommend launching this notebook from a fresh python environment, for instance you can run the following:\n", "```\n", "python3 -m venv .venv \n", "source .venv/bin/activate\n", "```\n", "All you need to run this notebook is to install jupyter notebook with `python3 -m pip install notebook` then run `jupyter notebook` ([link](https://jupyter.org/install)) in the same directory as this `.ipynb` file.\n", "You don't need to install additional pip packages ahead of running the notebook, since those will be installed right at the beginning. You will need to ensure your system has `imagemagick` installed by following the [instructions](https://imagemagick.org/script/download.php)." ] }, { "cell_type": "code", "execution_count": null, "id": "133c2ea4-0256-49cf-9f5a-a9e5bb0bb63f", "metadata": {}, "outputs": [], "source": [ "# Let's start by installing the appropriate python packages\n", "! pip install octoai===1.0.2 openai weaviate-client pandas gradio pydantic" ] }, { "cell_type": "markdown", "id": "75341227-43f8-4a68-b3cb-31e8216f874e", "metadata": {}, "source": [ "## Part 1: Review Summarization" ] }, { "cell_type": "markdown", "id": "793c06d7-fa67-4c67-a380-081ed3a7a7bf", "metadata": {}, "source": [ "Let's start by importing all of the packages we need for this example" ] }, { "cell_type": "code", "execution_count": null, "id": "edd366c8-4f0b-4211-83d3-c16e88cbd5c8", "metadata": {}, "outputs": [], "source": [ "import gradio\n", "import json\n", "import langchain\n", "import os\n", "import openai\n", "import weaviate\n", "from getpass import getpass\n", "from json import loads\n", "from pandas import DataFrame, concat, read_csv\n", "from pydantic import BaseModel, Field\n", "from typing import List\n", "import weaviate.classes as wvc" ] }, { "cell_type": "markdown", "id": "cd171a7c-c5e7-46d5-8a04-a0f7863609be", "metadata": {}, "source": [ "Enter your OctoAI, Weaviate, and OpenAI tokens below" ] }, { "cell_type": "code", "execution_count": null, "id": "3af09686-a654-45b0-98c5-dee6f30440c7", "metadata": {}, "outputs": [], "source": [ "# Get OctoAI API token for Llama 2 & 3\n", "OCTOAI_API_TOKEN = getpass()\n", "os.environ[\"OCTOAI_API_TOKEN\"] = OCTOAI_API_TOKEN" ] }, { "cell_type": "code", "execution_count": null, "id": "31c3e684-6e5e-41ad-81d4-970b06522553", "metadata": {}, "outputs": [], "source": [ "# Get WCS API key\n", "WCS_API_KEY = getpass()\n", "os.environ[\"WCS_API_KEY\"] = WCS_API_KEY" ] }, { "cell_type": "code", "execution_count": null, "id": "a44f7b71-c4f9-4fd6-9a3b-1322c2fd0c35", "metadata": {}, "outputs": [], "source": [ "# Get WCS URL\n", "WCS_URL = getpass()\n", "os.environ[\"WCS_URL\"] = WCS_URL" ] }, { "cell_type": "code", "execution_count": null, "id": "e4502dfa-c369-4085-a697-fdcda00f970b", "metadata": {}, "outputs": [], "source": [ "# Get OpenAI API key for the embedding model\n", "OPENAI_API_KEY = getpass()\n", "os.environ[\"OPENAI_API_KEY\"] = OPENAI_API_KEY" ] }, { "cell_type": "code", "execution_count": null, "id": "883986ad-9f60-44d8-ab64-3f566261e055", "metadata": {}, "outputs": [], "source": [ "# First let's load the dataset from Kaggle: https://www.kaggle.com/datasets/eswarchandt/amazon-music-reviews\n", "df = read_csv('Musical_instruments_reviews.csv')" ] }, { "cell_type": "markdown", "id": "c05865a7-307a-425e-a6ee-f057d63db77b", "metadata": {}, "source": [ "Set `product_record_limit` to a lower number if you just want to do a test run" ] }, { "cell_type": "code", "execution_count": null, "id": "22f024e7-3976-425f-b684-8b2c2c1ed191", "metadata": {}, "outputs": [], "source": [ "# Set a product record limit\n", "product_record_limit = 900" ] }, { "cell_type": "markdown", "id": "06554f51-5983-42fc-8a8e-684ae82099db", "metadata": { "scrolled": true }, "source": [ "# List all of the unique ASIN:\n", "asin_list = df.asin.unique()\n", "print(\"There are {} unique products in the music product inventory\".format(len(asin_list)))" ] }, { "cell_type": "markdown", "id": "4941baa1-107b-4f39-8d04-1daa5acd465b", "metadata": {}, "source": [ "For each one of the unique products, let's group the reviews together and sort them by how recent they are" ] }, { "cell_type": "code", "execution_count": null, "id": "38147b91-2425-46a7-b6c0-221173d81024", "metadata": {}, "outputs": [], "source": [ "# Get the reviews for the product ASIN, sorted by recency and store in dict\n", "review_dict = {}\n", "for asin in asin_list[0:product_record_limit]:\n", " reviews = df.loc[df['asin'] == asin]\\\n", " .sort_values([\"unixReviewTime\"], axis=0, ascending=False)\\\n", " .reviewText.tolist()\n", " review_dict[asin] = reviews" ] }, { "cell_type": "markdown", "id": "7d5fb78d-808a-4753-abba-4a3066d76ba7", "metadata": {}, "source": [ "To be able to store our summaries into our vector DB, we need to have the fields formatted into a JSON object. We use Pydantic base class model here to define our formatting." ] }, { "cell_type": "code", "execution_count": null, "id": "b786cde1-116a-47eb-8478-3fa2285dcf9d", "metadata": {}, "outputs": [], "source": [ "# Define the Pydantic model that specifies how our output should be formatted\n", "class ProductRecord(BaseModel):\n", " \"\"\"The record of a given product\"\"\"\n", " description: str = Field(description=\"Description of the product\")\n", " name: str = Field(description=\"Name of the product\")\n", " review_summary: str = Field(description=\"Summary of all of the reviews\")\n", " ASIN: str = Field(description=\"ASIN of the product\")\n", " features: str = Field(description=\"Features of the product based on the reviews\")" ] }, { "cell_type": "markdown", "id": "08226a6e-f994-454b-9a1d-6246b34bfca2", "metadata": {}, "source": [ "We define our prompt template below." ] }, { "cell_type": "code", "execution_count": null, "id": "1cc3fe69-bf0c-4a50-8d9c-1ae6cb99a9ca", "metadata": {}, "outputs": [], "source": [ "# Prepare a prompt template\n", "template = '''\n", "Here are product reviews for a music product with an ID of {asin}.\n", " - Respond back only as only JSON!\n", " - Provide:\n", " - the product \"description\",\n", " - the product \"name\",\n", " - a summary of all the reviews as \"review_summary\",\n", " - the \"ASIN\" and\n", " - and the product \"features\" based on the content of these reviews. \n", " - The \"features\" should be a string describing the features and NOT JSON. \n", " - Do not include the ASIN in the description field.\n", " \n", "The reviews for the product are: {reviews}\n", "'''" ] }, { "cell_type": "markdown", "id": "9b8dc3fa-4ad9-4329-96a0-353b05a1c43e", "metadata": {}, "source": [ "We initialize the OctoAI client using OpenAI's API. All we have to do is override the `base_url` and `api_key`." ] }, { "cell_type": "code", "execution_count": null, "id": "57c2ff0a-8029-41a6-a06f-41e560b92230", "metadata": {}, "outputs": [], "source": [ "# Init OctoAI client\n", "client = openai.OpenAI(\n", " base_url=\"https://text.octoai.run/v1\",\n", " api_key=os.environ[\"OCTOAI_API_TOKEN\"]\n", ")" ] }, { "cell_type": "markdown", "id": "bd0eb425-ceea-4258-a52d-814b7335febb", "metadata": {}, "source": [ "Iterate over all product ASINs and summarize the top 20 most recent reviews. Note: this takes a while to run unless we parallelize it." ] }, { "cell_type": "code", "execution_count": null, "id": "1a55839e-a824-4919-b755-730eaac48d83", "metadata": {}, "outputs": [], "source": [ "# Produce the 900 product summaries\n", "review_summaries = []\n", "counter = 0\n", "\n", "# This can take a while to process serially (30min+)\n", "# TODO: Optimize to run in a few parallel threads to run faster while meeting the 240RPM limit\n", "for asin, review_list in review_dict.items():\n", " print(f'Getting review summary {counter} of {len(review_dict)}, ASIN: {asin}')\n", " try:\n", " response = client.chat.completions.create(\n", " model=\"llama-2-70b-chat\",\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": template.format(\n", " asin = asin,\n", " reviews = review_list[0:20]\n", " )},\n", " ],\n", " temperature=0,\n", " response_format={\"type\": \"json_object\", \"schema\": ProductRecord.model_json_schema()},\n", " max_tokens=1024\n", " )\n", " print(\"\\n{}\\n\".format(response.choices[0].message.content))\n", " summary = loads(response.choices[0].message.content)\n", " summary[\"ASIN\"] = asin\n", " review_summaries.append(summary)\n", " except:\n", " print(f'Issue with ASIN {asin}, skipping')\n", " pass\n", " counter += 1\n", "\n", "review_summaries = DataFrame(review_summaries)\n", "\n", "print(review_summaries)" ] }, { "cell_type": "markdown", "id": "4772d1c1-c9c4-466e-9c80-259804a4286b", "metadata": {}, "source": [ "# Part 2: Retrieval Augmented Generation" ] }, { "cell_type": "markdown", "id": "ccd97408-d47f-46f3-b601-f66f8a3b20ff", "metadata": {}, "source": [ "For our RAG use case we're going to rely on Weaviate vector database and on an OpenAI embedding model. \n", "\n", "When you define your collection, you'll need to provide properties, i.e. object attributes that you want to store in the collection. These properties map 1:1 to the JSON dictionary keys defined earlier for the `ProductRecord` Pydantic base model." ] }, { "cell_type": "code", "execution_count": null, "id": "5dad98ec-531d-4fc2-aed9-9f337b957feb", "metadata": {}, "outputs": [], "source": [ "# Connect to WCS\n", "wcs_client = weaviate.connect_to_wcs(\n", " cluster_url=os.getenv(\"WCS_URL\"),\n", " auth_credentials=weaviate.auth.AuthApiKey(os.getenv(\"WCS_API_KEY\")),\n", " headers={\n", " \"X-OpenAI-Api-Key\": os.environ[\"OPENAI_API_KEY\"]\n", " }\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "02953f7b-0149-4c13-a7cc-c4dd1da45d43", "metadata": {}, "outputs": [], "source": [ "# Create the collection if it doesn't already exist\n", "try:\n", " collection = wcs_client.collections.get(\"Products\")\n", "except:\n", " # Create the collection for products\n", " collection = wcs_client.collections.create(\n", " name=\"Products\",\n", " vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_openai(),\n", " properties=[\n", " wvc.config.Property(\n", " name=\"ASIN\",\n", " data_type=wvc.config.DataType.TEXT\n", " ),\n", " wvc.config.Property(\n", " name=\"name\",\n", " data_type=wvc.config.DataType.TEXT\n", " ),\n", " wvc.config.Property(\n", " name=\"review_summary\",\n", " data_type=wvc.config.DataType.TEXT\n", " ),\n", " wvc.config.Property(\n", " name=\"features\",\n", " data_type=wvc.config.DataType.TEXT\n", " ),\n", " wvc.config.Property(\n", " name=\"description\",\n", " data_type=wvc.config.DataType.TEXT\n", " ),\n", " ]\n", " )\n", " print(\"Collection Created!\")" ] }, { "cell_type": "markdown", "id": "1551fd74-b143-4c02-9b56-364d33683fd3", "metadata": {}, "source": [ "Now we upsert all of the vectors into the databse using OpenAI's embedding model." ] }, { "cell_type": "code", "execution_count": null, "id": "53f779e7-b875-4a19-9f9c-74b45992608e", "metadata": {}, "outputs": [], "source": [ "# Convert df to JSON string and then to a list of dictionaries\n", "data = review_summaries.to_json(orient='records')\n", "data_list = json.loads(data)\n", "\n", "items_to_insert = []\n", "\n", "for d in data_list:\n", " new_item = {\n", " \"ASIN\": d[\"ASIN\"],\n", " \"name\": d[\"name\"],\n", " \"description\": d[\"description\"], \\\n", " \"features\": d[\"features\"],\n", " \"review_summary\": d[\"review_summary\"]\n", " }\n", " items_to_insert.append(new_item)\n", "\n", " # Insert every 100 items\n", " if len(items_to_insert) == 100:\n", " collection.data.insert_many(items_to_insert)\n", " items_to_insert.clear()\n", "\n", "# Insert remaining items\n", "if len(items_to_insert) > 0:\n", " collection.data.insert_many(items_to_insert)" ] }, { "cell_type": "markdown", "id": "35079318-41a5-46fc-8475-5d728550fb88", "metadata": {}, "source": [ "Let's now try to run a hybrid search on the following query below.\n", "Hybrid search combines the results of a vector search and a keyword (BM25F) search by fusing the two result sets.\n", "It will return the 3 closest entries in the database according to the search criteria." ] }, { "cell_type": "code", "execution_count": null, "id": "5f707954-c36b-4a83-874b-f817bd33c39a", "metadata": {}, "outputs": [], "source": [ "# Hybrid search\n", "response = collection.query.hybrid(\n", " query=\"easy to learn instrument\",\n", " limit=3\n", ")\n", "for o in response.objects:\n", " print(o.properties)" ] }, { "cell_type": "markdown", "id": "04d39507-5e8e-4374-a33c-53e57db6ef99", "metadata": {}, "source": [ "Let's now define a helper function that gives us the relevant context given a string query. Let's see what it returns based on the question: \"What is a good beginner harmonica\"" ] }, { "cell_type": "code", "execution_count": null, "id": "a1ca51c7-83e5-4896-acc9-753060592ba0", "metadata": {}, "outputs": [], "source": [ "# Helper function to run hybrid search on a user query and return the closest\n", "# product review summaries relevant to the user query\n", "def get_context(question, limit=3):\n", " response = collection.query.hybrid(\n", " query=question,\n", " limit=limit\n", " )\n", " return \"\\n\".join([str(o.properties) for o in response.objects])\n", "\n", "print(get_context(\"What is a good beginner harmonica\"))" ] }, { "cell_type": "markdown", "id": "677f534c-8be4-4b6b-82d9-2df8e2ad12d4", "metadata": {}, "source": [ "Great, we're now ready to build a sales assistant helper function.\n", "\n", "We first define a prompt template for Llama 3 - based on the context provided by the vector hybrid search (i.e. collection of product summaries of relevance to the question), provide a helpful recommendation to the customer. \n", "\n", "Also provide links to the product that the user can click on to view the product on Amazon's website. For that we use the fact that any product referenced by its aSIN can be accessed at the following url: `https://www.amazon.com/exec/obidos/ASIN/`" ] }, { "cell_type": "code", "execution_count": null, "id": "856d021a-add5-48f4-a09c-258d2a617095", "metadata": {}, "outputs": [], "source": [ "sales_template = \"\"\"\n", "You are a sales assistant. Answer the user questions as helpfully as possible.\n", "Only recommend the products that are provided in the context provided below.\n", "\n", "Provide a reference to each product you mention with hyperlinks:\n", "* Provide the name of the product\n", "* Embed the hyperlink in the name of the product as follows\n", " * If the product name is \"Solid Electric Guitar Case with Accessories Compartment\"\n", " * And the aSIN is \"B001EL6I8W\"\n", " * Format the reference as follows: \n", " [Solid Electric Guitar Case with Accessories Compartment](https://www.amazon.com/exec/obidos/ASIN/B001EL6I8W)\n", "\n", "Finish with a references section.\n", "\n", "Customer question: {}\n", "\n", "Product context: {}\n", "\n", "AI:\n", "\"\"\"\n", "\n", "def sales_assistant(question): \n", " response = client.chat.completions.create(\n", " model=\"meta-llama-3-70b-instruct\",\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": sales_template.format(question, get_context(question, limit=10))},\n", " ],\n", " temperature=0,\n", " max_tokens=1024\n", " )\n", " \n", " return response.choices[0].message.content\n", "\n", "print(sales_assistant(\"what is must have accessory for my new electric guitar\"))" ] }, { "cell_type": "markdown", "id": "faccba14-9216-4420-b6c5-ddf4029d7904", "metadata": {}, "source": [ "# Part 3: Gradio-based sales assistant demo" ] }, { "cell_type": "markdown", "id": "3e2b73b5-6bdf-4c87-b044-2690fd52605f", "metadata": {}, "source": [ "In this section we build a simple an interactive sales bot assistant using Gradio." ] }, { "cell_type": "code", "execution_count": null, "id": "53805acb-3e8d-40fa-8045-c589cb14eadd", "metadata": {}, "outputs": [], "source": [ "import gradio as gr\n", "\n", "def predict(message, history):\n", " history_openai_format = []\n", " for human, assistant in history:\n", " history_openai_format.append({\"role\": \"user\", \"content\": human})\n", " history_openai_format.append({\"role\": \"assistant\", \"content\": assistant})\n", " history_openai_format.append({\"role\": \"user\", \"content\": sales_template.format(message, get_context(message, limit=5))})\n", "\n", " response = client.chat.completions.create(\n", " model = 'meta-llama-3-70b-instruct',\n", " messages = history_openai_format,\n", " temperature = 0.0,\n", " stream = True\n", " )\n", "\n", " partial_message = \"\"\n", " for chunk in response:\n", " if chunk.choices[0].delta.content is not None:\n", " partial_message = partial_message + chunk.choices[0].delta.content\n", " yield partial_message\n", "\n", "gr.ChatInterface(predict).launch()" ] }, { "cell_type": "markdown", "id": "6d4e65fe-0246-40b7-adb6-9091cccbc486", "metadata": {}, "source": [ "**Authors**\n", "- Thierry Moreau, OctoAI - tmoreau@octo.ai\n", "- Jonathan Tuite, Weaviate - jon@weaviate.io" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 5 }