{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Load the dataset\n", "\n", "The dataset used in this example is [fine-food reviews](https://www.kaggle.com/snap/amazon-fine-food-reviews) from Amazon. The dataset contains a total of 568,454 food reviews Amazon users left up to October 2012. We will use a subset of this dataset, consisting of 1,000 most recent reviews for illustration purposes. The reviews are in English and tend to be positive or negative. Each review has a ProductId, UserId, Score, review title (Summary) and review body (Text).\n", "\n", "We will combine the review summary and review text into a single combined text. The model will encode this combined text and it will output a single vector embedding." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TimeProductIdUserIdScoreSummaryTextcombined
01351123200B003XPF9BOA3R7JR3FMEBXQB5where does one start...and stop... with a tre...Wanted to save some to bring to my Chicago fam...Title: where does one start...and stop... wit...
11351123200B003JK537SA3JBPC3WFUT5ZP1Arrived in piecesNot pleased at all. When I opened the box, mos...Title: Arrived in pieces; Content: Not pleased...
\n", "
" ], "text/plain": [ " Time ProductId UserId Score \\\n", "0 1351123200 B003XPF9BO A3R7JR3FMEBXQB 5 \n", "1 1351123200 B003JK537S A3JBPC3WFUT5ZP 1 \n", "\n", " Summary \\\n", "0 where does one start...and stop... with a tre... \n", "1 Arrived in pieces \n", "\n", " Text \\\n", "0 Wanted to save some to bring to my Chicago fam... \n", "1 Not pleased at all. When I opened the box, mos... \n", "\n", " combined \n", "0 Title: where does one start...and stop... wit... \n", "1 Title: Arrived in pieces; Content: Not pleased... " ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "input_datapath = 'data/fine_food_reviews_1k.csv' # to save space, we provide a pre-filtered dataset\n", "df = pd.read_csv(input_datapath, index_col=0)\n", "df = df[['Time', 'ProductId', 'UserId', 'Score', 'Summary', 'Text']]\n", "df = df.dropna()\n", "df['combined'] = \"Title: \" + df.Summary.str.strip() + \"; Content: \" + df.Text.str.strip()\n", "df.head(2)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1000" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# subsample to 1k most recent reviews and remove samples that are too long\n", "df = df.sort_values('Time').tail(1_100)\n", "df.drop('Time', axis=1, inplace=True)\n", "\n", "from transformers import GPT2TokenizerFast\n", "tokenizer = GPT2TokenizerFast.from_pretrained(\"gpt2\")\n", "\n", "# remove reviews that are too long\n", "df['n_tokens'] = df.combined.apply(lambda x: len(tokenizer.encode(x)))\n", "df = df[df.n_tokens<2000].tail(1_000)\n", "len(df)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. Get embeddings and save them for future reuse" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from openai.embeddings_utils import get_embedding\n", "\n", "# This will take just under 10 minutes\n", "df['babbage_similarity'] = df.combined.apply(lambda x: get_embedding(x, engine='text-similarity-babbage-001'))\n", "df['babbage_search'] = df.combined.apply(lambda x: get_embedding(x, engine='text-search-babbage-doc-001'))\n", "df.to_csv('data/fine_food_reviews_with_embeddings_1k.csv')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.9 ('openai')", "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.9.9" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "365536dcbde60510dc9073d6b991cd35db2d9bac356a11f5b64279a5e6708b97" } } }, "nbformat": 4, "nbformat_minor": 2 }