{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9a23d507",
   "metadata": {},
   "source": [
    "# Similarity Search Lab"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d9c0fd0",
   "metadata": {},
   "source": [
    "## Import modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f21afd9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94f0105b",
   "metadata": {},
   "source": [
    "### Set the OpenAI API Key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52babb99",
   "metadata": {},
   "outputs": [],
   "source": [
    "#openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "openai.api_key = \"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7497e59",
   "metadata": {},
   "source": [
    "## Define embedding function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df205121",
   "metadata": {},
   "outputs": [],
   "source": [
    "def text_embedding(text) -> None:\n",
    "    response = openai.Embedding.create(model=\"text-embedding-ada-002\", input=text)\n",
    "    return response[\"data\"][0][\"embedding\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ed11dae",
   "metadata": {},
   "source": [
    "## Sample text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "500d9ee7",
   "metadata": {},
   "outputs": [],
   "source": [
    "phrases=[\n",
    "    \"Most of the websites provide the users with the choice of accepting or denying cookies\",\n",
    "    \"Olivia went to the bank to open a savings account\",\n",
    "    \"Sam sat under a tree that was on the bank of a river\",\n",
    "    \"John's cookies were only half-baked but he still carries them for Mary\"\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "daa7ceaa",
   "metadata": {},
   "source": [
    "## Generate Embeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02ee693a",
   "metadata": {},
   "outputs": [],
   "source": [
    "embeddings=list(map(text_embedding,phrases))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55d78bfe",
   "metadata": {},
   "outputs": [],
   "source": [
    "embeddings[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac980aa1",
   "metadata": {},
   "source": [
    "## Define dot product function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa5937fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def vector_similarity(vec1, vec2):\n",
    "    return np.dot(np.squeeze(np.array(vec1)),np.squeeze(np.array(vec2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0eb6cf8",
   "metadata": {},
   "source": [
    "## Search phrases"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0abf3b99",
   "metadata": {},
   "source": [
    "#### Uncomment each line to check similarity score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea64381f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#phrase=\"Sam sat under a tree that was on the bank of a river\"\n",
    "phrase=\"Mary got the biscuits from John that were not fully baked\"\n",
    "#phrase=\"It's recommended to put your savings in a financial institue\"\n",
    "#phrase=\"You get refreshed when you spend time with the nature\"\n",
    "#phrase=\"cookies are covered by GDPR if they collect information about users that could be used to identify them\"\n",
    "phrase_embedding=text_embedding(phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0080bd9",
   "metadata": {},
   "source": [
    "## Calculate similarity score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3eb17dcc",
   "metadata": {},
   "outputs": [],
   "source": [
    "similarity_scores=[]\n",
    "for embedding in embeddings:\n",
    "    similarity_scores.append(vector_similarity(phrase_embedding,embedding))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e720e25",
   "metadata": {},
   "outputs": [],
   "source": [
    "similarity_scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5ddb1d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,(phrase,embedding,similarity_score) in enumerate(zip(phrases,embeddings,similarity_scores)):\n",
    "    print(i,phrase,\"\\n\\t\"+str(similarity_score))    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03af66a4",
   "metadata": {},
   "source": [
    "## Print the phrase that similar meaning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d165574f",
   "metadata": {},
   "outputs": [],
   "source": [
    "res = {similarity_scores[i]:phrases[i]  for i in range(len(similarity_scores))}\n",
    "print(sorted(res.items(), key=lambda item: item[0],reverse=True)[0][1])"
   ]
  }
 ],
 "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
