{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Zero-shot classification with 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",
    "# If you have not run the \"Obtain_dataset.ipynb\" notebook, you can download the datafile from here: https://cdn.openai.com/API/examples/data/fine_food_reviews_with_embeddings_1k.csv\n",
    "datafile_path = \"./data/fine_food_reviews_with_embeddings_1k.csv\"\n",
    "\n",
    "df = pd.read_csv(datafile_path)\n",
    "df['ada_similarity'] = df.ada_similarity.apply(eval).apply(np.array)\n",
    "df['ada_search'] = df.ada_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": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "    negative       0.62      0.88      0.73       136\n",
      "    positive       0.98      0.90      0.94       789\n",
      "\n",
      "    accuracy                           0.90       925\n",
      "   macro avg       0.80      0.89      0.83       925\n",
      "weighted avg       0.92      0.90      0.91       925\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 39884 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from openai.embeddings_utils import cosine_similarity, get_embedding\n",
    "from sklearn.metrics import PrecisionRecallDisplay\n",
    "\n",
    "def evaluate_embeddings_approach(\n",
    "    labels = ['negative', 'positive'], \n",
    "    engine = 'text-embedding-ada-002',\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 = 'ada_similarity'\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_embeddings_approach(labels=['negative', 'positive'], engine='text-embedding-ada-002')"
   ]
  },
  {
   "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": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "    negative       0.98      0.73      0.84       136\n",
      "    positive       0.96      1.00      0.98       789\n",
      "\n",
      "    accuracy                           0.96       925\n",
      "   macro avg       0.97      0.86      0.91       925\n",
      "weighted avg       0.96      0.96      0.96       925\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 37220 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "evaluate_embeddings_approach(labels=['An Amazon review with a negative sentiment.', 'An Amazon review with a positive sentiment.'], engine='text-embedding-ada-002')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the search embeddings and descriptive names leads to an additional improvement in performance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "    negative       0.98      0.73      0.84       136\n",
      "    positive       0.96      1.00      0.98       789\n",
      "\n",
      "    accuracy                           0.96       925\n",
      "   macro avg       0.97      0.86      0.91       925\n",
      "weighted avg       0.96      0.96      0.96       925\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 37220 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "evaluate_embeddings_approach(labels=['An Amazon review with a negative sentiment.', 'An Amazon review with a positive sentiment.'], engine='text-embedding-ada-002')"
   ]
  },
  {
   "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": {
  "kernelspec": {
   "display_name": "openai-cookbook",
   "language": "python",
   "name": "openai-cookbook"
  },
  "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.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
