{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Zero-shot classification using the embeddings\n",
    "\n",
    "In this notebook we will classify the sentiment of reviews using embeddings and zero labeled data! The dataset is created in the [Obtain_dataset Notebook](Obtain_dataset.ipynb).\n",
    "\n",
    "We'll define positive sentiment to be 4 and 5-star reviews, and negative sentiment to be 1 and 2-star reviews. 3-star reviews are considered neutral and we won't use them for this example.\n",
    "\n",
    "We will perform zero-shot classification by embedding descriptions of each class and then comparing new samples to those class embeddings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn.metrics import classification_report\n",
    "\n",
    "df = pd.read_csv('output/embedded_1k_reviews.csv')\n",
    "df['babbage_similarity'] = df.babbage_similarity.apply(eval).apply(np.array)\n",
    "df['babbage_search'] = df.babbage_search.apply(eval).apply(np.array)\n",
    "\n",
    "df= df[df.Score!=3]\n",
    "df['sentiment'] = df.Score.replace({1:'negative', 2:'negative', 4:'positive', 5:'positive'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Zero-Shot Classification\n",
    "To perform zero shot classification, we want to predict labels for our samples without any training. To do this, we can simply embed short descriptions of each label, such as positive and negative, and then compare the cosine distance between embeddings of samples and label descriptions. \n",
    "\n",
    "The highest similarity label to the sample input is the predicted label. We can also define a prediction score to be the difference between the cosine distance to the positive and to the negative label. This score can be used for plotting a precision-recall curve, which can be used to select a different tradeoff between precision and recall, by selecting a different threshold."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "    negative       0.67      0.88      0.76       136\n",
      "    positive       0.98      0.93      0.95       789\n",
      "\n",
      "    accuracy                           0.92       925\n",
      "   macro avg       0.82      0.90      0.86       925\n",
      "weighted avg       0.93      0.92      0.92       925\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17308 characters]",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from openai.embeddings_utils import cosine_similarity, get_embedding\n",
    "from sklearn.metrics import PrecisionRecallDisplay\n",
    "\n",
    "def evaluate_emeddings_approach(\n",
    "    labels = ['negative', 'positive'], \n",
    "    engine = 'text-similarity-babbage-001',\n",
    "):\n",
    "    label_embeddings = [get_embedding(label, engine=engine) for label in labels]\n",
    "\n",
    "    def label_score(review_embedding, label_embeddings):\n",
    "        return cosine_similarity(review_embedding, label_embeddings[1]) - cosine_similarity(review_embedding, label_embeddings[0])\n",
    "\n",
    "    engine_col_name = engine.replace('-','_').replace('_query','')\n",
    "    probas = df[engine_col_name].apply(lambda x: label_score(x, label_embeddings))\n",
    "    preds = probas.apply(lambda x: 'positive' if x>0 else 'negative')\n",
    "\n",
    "    report = classification_report(df.sentiment, preds)\n",
    "    print(report)\n",
    "\n",
    "    display = PrecisionRecallDisplay.from_predictions(df.sentiment, probas, pos_label='positive')\n",
    "    _ = display.ax_.set_title(\"2-class Precision-Recall curve\")\n",
    "\n",
    "evaluate_emeddings_approach(labels=['negative', 'positive'], engine='text-similarity-babbage-001')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that this classifier already performs extremely well. We used similarity embeddings, and the simplest possible label name. Let's try to improve on this by using more descriptive label names, and search embeddings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "    negative       0.65      0.93      0.76       136\n",
      "    positive       0.99      0.91      0.95       789\n",
      "\n",
      "    accuracy                           0.92       925\n",
      "   macro avg       0.82      0.92      0.86       925\n",
      "weighted avg       0.94      0.92      0.92       925\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16796 characters]",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "evaluate_emeddings_approach(labels=['An Amazon review with a negative sentiment.', 'An Amazon review with a positive sentiment.'], engine='text-similarity-babbage-001')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the search embeddings and descriptive names leads to an additional improvement in performance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "    negative       0.77      0.79      0.78       136\n",
      "    positive       0.96      0.96      0.96       789\n",
      "\n",
      "    accuracy                           0.94       925\n",
      "   macro avg       0.87      0.88      0.87       925\n",
      "weighted avg       0.94      0.94      0.94       925\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 15244 characters]",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "evaluate_emeddings_approach(labels=['An Amazon review with a negative sentiment.', 'An Amazon review with a positive sentiment.'], engine='text-similarity-babbage-001')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As shown above, zero-shot classification with embeddings can lead to great results, especially when the labels are more descriptive than just simple words."
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "be4b5d5b73a21c599de40d6deb1129796d12dc1cc33a738f7bac13269cfcafe8"
  },
  "kernelspec": {
   "display_name": "Python 3.7.3 64-bit ('base': conda)",
   "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.7.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
