{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://supabase.com/blog/openai-embeddings-postgres-vector\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install supabase pgvector vecs openai -q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "!pip install python-dotenv -q\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()  # Load environment variables from .env file\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from supabase import create_client, Client\n",
    "import os\n",
    "\n",
    "url = os.getenv(\"SUPABASE_URL\")\n",
    "key = os.getenv(\"SUPABASE_KEY\")\n",
    "supabase: Client = create_client(url, key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1536\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.005132982973009348,\n",
       " 0.017242904752492905,\n",
       " -0.018698474392294884,\n",
       " -0.01855851523578167,\n",
       " -0.047250036150217056,\n",
       " -0.03028704971075058,\n",
       " 0.027669822797179222,\n",
       " 0.003614431945607066,\n",
       " 0.011217685416340828,\n",
       " 0.006434598471969366,\n",
       " -0.0016865016659721732,\n",
       " 0.01581532694399357,\n",
       " -0.0012849952327087522,\n",
       " -0.00785867776721716,\n",
       " 0.05990229919552803,\n",
       " 0.050357118248939514,\n",
       " -0.02754385955631733,\n",
       " 0.00991606991738081,\n",
       " -0.040392063558101654,\n",
       " 0.04999322444200516,\n",
       " -0.0003859796852339059,\n",
       " 0.030231066048145294,\n",
       " -0.013729942962527275,\n",
       " 0.032974254339933395,\n",
       " 0.01731288433074951,\n",
       " 0.01680903322994709,\n",
       " -0.0017661030869930983,\n",
       " 0.020391974598169327,\n",
       " 0.040811941027641296,\n",
       " -0.03770485892891884,\n",
       " -0.02617226541042328,\n",
       " -0.0500212162733078,\n",
       " 0.024184852838516235,\n",
       " -0.05519968643784523,\n",
       " -0.03224647045135498,\n",
       " 0.04232349246740341,\n",
       " 0.06466089189052582,\n",
       " 0.014695657417178154,\n",
       " -0.015619384124875069,\n",
       " -0.04134378209710121,\n",
       " 0.02218344435095787,\n",
       " 0.007361824624240398,\n",
       " 0.04495471343398094,\n",
       " 0.007074909284710884,\n",
       " -0.024072885513305664,\n",
       " 0.0523725226521492,\n",
       " -0.020056072622537613,\n",
       " -0.032190486788749695,\n",
       " 0.016249198466539383,\n",
       " 0.046494260430336,\n",
       " 0.02384895272552967,\n",
       " -0.01886642538011074,\n",
       " -0.00532542634755373,\n",
       " 0.043919019401073456,\n",
       " -0.006175674963742495,\n",
       " 0.007326834835112095,\n",
       " -0.014947582967579365,\n",
       " 0.07261054217815399,\n",
       " -0.0055143702775239944,\n",
       " -0.037676867097616196,\n",
       " 0.016095243394374847,\n",
       " 0.03428986668586731,\n",
       " 0.007116897031664848,\n",
       " -0.004758593626320362,\n",
       " -0.026704108342528343,\n",
       " 0.028999431058764458,\n",
       " -0.03857260197401047,\n",
       " 0.023219138383865356,\n",
       " -0.00515747582539916,\n",
       " 0.01186849270015955,\n",
       " 0.02551445923745632,\n",
       " -0.014611681923270226,\n",
       " -0.011049734428524971,\n",
       " 0.003782382234930992,\n",
       " 0.0005786414840258658,\n",
       " -0.023275122046470642,\n",
       " -0.01633317396044731,\n",
       " -0.004394701216369867,\n",
       " 0.020755866542458534,\n",
       " -0.004216254223138094,\n",
       " 0.040196120738983154,\n",
       " -0.002821916015818715,\n",
       " -0.025332514196634293,\n",
       " -0.020154044032096863,\n",
       " -0.04817376285791397,\n",
       " 0.020643899217247963,\n",
       " -0.07563365250825882,\n",
       " 0.027963737025856972,\n",
       " -0.017074953764677048,\n",
       " 0.00835553091019392,\n",
       " -0.018348578363656998,\n",
       " -0.012064434587955475,\n",
       " -0.022603319957852364,\n",
       " -0.007074909284710884,\n",
       " -0.01581532694399357,\n",
       " 0.005552859045565128,\n",
       " -0.03275032341480255,\n",
       " 0.0040692975744605064,\n",
       " -0.005850271321833134,\n",
       " -0.03837665915489197,\n",
       " 0.04711007699370384,\n",
       " -0.012071432545781136,\n",
       " -0.02004207670688629,\n",
       " 0.002688955282792449,\n",
       " -0.03154667839407921,\n",
       " 0.011119714006781578,\n",
       " 0.011952468194067478,\n",
       " -0.002426533028483391,\n",
       " -0.046858154237270355,\n",
       " -0.021329697221517563,\n",
       " -0.08543075621128082,\n",
       " 0.007256855256855488,\n",
       " 0.006256151013076305,\n",
       " 0.020643899217247963,\n",
       " 0.0012814962537959218,\n",
       " 0.004226751159876585,\n",
       " 0.06885965168476105,\n",
       " -0.014499715529382229,\n",
       " 0.021665597334504128,\n",
       " -0.022645307704806328,\n",
       " 0.025262534618377686,\n",
       " -0.0027974233962595463,\n",
       " 0.03185458481311798,\n",
       " -0.0568791925907135,\n",
       " 0.0036739143542945385,\n",
       " -0.0410078801214695,\n",
       " 0.005626337137073278,\n",
       " -0.03549351170659065,\n",
       " -0.037956781685352325,\n",
       " 0.015465429984033108,\n",
       " 0.021259717643260956,\n",
       " -0.048509664833545685,\n",
       " -0.011189693585038185,\n",
       " -0.00932824332267046,\n",
       " -0.05018916726112366,\n",
       " -0.007123894989490509,\n",
       " -0.04585045203566551,\n",
       " 0.04123181477189064,\n",
       " -0.03056696616113186,\n",
       " 0.04268738627433777,\n",
       " -0.015451434068381786,\n",
       " -0.029727214947342873,\n",
       " -0.0033432620111852884,\n",
       " -0.011602571234107018,\n",
       " -0.02463272027671337,\n",
       " -0.018306590616703033,\n",
       " -0.03227446228265762,\n",
       " -0.006161679048091173,\n",
       " -0.024268828332424164,\n",
       " 0.018740462139248848,\n",
       " -0.035605479031801224,\n",
       " 0.03479371964931488,\n",
       " -0.06639637798070908,\n",
       " 0.03398195654153824,\n",
       " 0.027040008455514908,\n",
       " -0.013897893019020557,\n",
       " 0.026214253157377243,\n",
       " -0.02450675703585148,\n",
       " -0.04445086419582367,\n",
       " 0.004692113492637873,\n",
       " -0.021763568744063377,\n",
       " -0.057019151747226715,\n",
       " -0.004968531895428896,\n",
       " 0.013506008312106133,\n",
       " -0.03350609913468361,\n",
       " -0.04604639112949371,\n",
       " 0.03792878985404968,\n",
       " 0.024366799741983414,\n",
       " -0.04366709664463997,\n",
       " 0.02334510162472725,\n",
       " -0.013072136789560318,\n",
       " -0.0410078801214695,\n",
       " -0.049293432384729385,\n",
       " -0.005419898312538862,\n",
       " -0.008642446249723434,\n",
       " 0.042155541479587555,\n",
       " 0.021231725811958313,\n",
       " -0.01007002405822277,\n",
       " -0.021371684968471527,\n",
       " 0.048397697508335114,\n",
       " 0.021077770739793777,\n",
       " 0.04047603905200958,\n",
       " 0.00820157676935196,\n",
       " -0.00869842991232872,\n",
       " -0.004135777708142996,\n",
       " -0.006700519938021898,\n",
       " 0.03692108765244484,\n",
       " 0.022939221933484077,\n",
       " 0.00810360535979271,\n",
       " -0.013254082761704922,\n",
       " 0.007991638034582138,\n",
       " -0.005755798891186714,\n",
       " 0.019426260143518448,\n",
       " 0.0011170449433848262,\n",
       " -0.01925830915570259,\n",
       " 0.02072787471115589,\n",
       " 0.03428986668586731,\n",
       " -0.013624973595142365,\n",
       " 0.03988821059465408,\n",
       " 0.06662031263113022,\n",
       " 0.03585740178823471,\n",
       " 0.06348524242639542,\n",
       " -0.02461872436106205,\n",
       " -0.005042009986937046,\n",
       " -0.08576665818691254,\n",
       " -0.0288314800709486,\n",
       " -0.0447307825088501,\n",
       " 0.015129528939723969,\n",
       " 0.013212095946073532,\n",
       " 0.026158269494771957,\n",
       " 0.09119705110788345,\n",
       " -0.019594209268689156,\n",
       " -0.009356235153973103,\n",
       " -0.019888123497366905,\n",
       " -0.0502171590924263,\n",
       " -0.03798477351665497,\n",
       " 0.003490218659862876,\n",
       " 0.019286300987005234,\n",
       " 0.012344351969659328,\n",
       " -0.06113393232226372,\n",
       " -0.018320586532354355,\n",
       " 0.029475290328264236,\n",
       " 0.0600142665207386,\n",
       " 0.024954626336693764,\n",
       " 0.028495579957962036,\n",
       " 0.007270851172506809,\n",
       " 0.025262534618377686,\n",
       " 0.022295411676168442,\n",
       " -0.0031945558730512857,\n",
       " 0.03602535277605057,\n",
       " 0.03778883442282677,\n",
       " -0.02688605524599552,\n",
       " 0.009594164788722992,\n",
       " -0.03770485892891884,\n",
       " 0.023317109793424606,\n",
       " 0.015115533024072647,\n",
       " 0.0003822620492428541,\n",
       " -0.010279961861670017,\n",
       " 0.02139967679977417,\n",
       " 0.019272305071353912,\n",
       " -0.07182677090167999,\n",
       " -0.025360506027936935,\n",
       " 0.08039224147796631,\n",
       " 0.045766476541757584,\n",
       " 0.019958101212978363,\n",
       " -0.01574534736573696,\n",
       " -0.024002905935049057,\n",
       " -0.027207959443330765,\n",
       " 0.026158269494771957,\n",
       " 0.03605334460735321,\n",
       " 0.02180555649101734,\n",
       " 0.015759343281388283,\n",
       " 0.020601911470294,\n",
       " -0.0035899390932172537,\n",
       " 0.02599032036960125,\n",
       " -0.017466837540268898,\n",
       " -0.02053193189203739,\n",
       " 0.031042825430631638,\n",
       " 0.04778188094496727,\n",
       " -0.0419875904917717,\n",
       " -0.033282164484262466,\n",
       " -0.034233883023262024,\n",
       " -0.03778883442282677,\n",
       " -0.0024947626516222954,\n",
       " -0.004160270560532808,\n",
       " -0.00038969735032878816,\n",
       " 0.025164563208818436,\n",
       " 0.0038593595381826162,\n",
       " -0.0004340904706623405,\n",
       " -0.03837665915489197,\n",
       " -0.04229550063610077,\n",
       " -0.004618634935468435,\n",
       " 0.028061706572771072,\n",
       " 0.040811941027641296,\n",
       " -0.03311421349644661,\n",
       " 0.06617244333028793,\n",
       " -0.010881784372031689,\n",
       " -0.020545927807688713,\n",
       " -0.002933882875367999,\n",
       " 0.0196222010999918,\n",
       " -0.04263140261173248,\n",
       " 0.0112806661054492,\n",
       " 0.010825800709426403,\n",
       " -0.018586507067084312,\n",
       " -0.028999431058764458,\n",
       " -0.02167959325015545,\n",
       " 0.028369616717100143,\n",
       " -0.0030720920767635107,\n",
       " 0.009797104634344578,\n",
       " 0.021049778908491135,\n",
       " 0.0027501871809363365,\n",
       " -0.03241442143917084,\n",
       " 0.022449366748332977,\n",
       " -0.008327539078891277,\n",
       " -0.027026012539863586,\n",
       " 0.03851661831140518,\n",
       " -0.018054664134979248,\n",
       " 0.024170856922864914,\n",
       " 0.013387043960392475,\n",
       " 0.05240051448345184,\n",
       " 0.03037102520465851,\n",
       " 0.05377211049199104,\n",
       " 0.0045486558228731155,\n",
       " 0.0027221955824643373,\n",
       " -0.02463272027671337,\n",
       " 0.017452841624617577,\n",
       " 0.03398195654153824,\n",
       " -0.023177150636911392,\n",
       " 0.044310905039310455,\n",
       " -0.013939880765974522,\n",
       " 0.01547942589968443,\n",
       " -0.027585847303271294,\n",
       " -0.004408697132021189,\n",
       " -0.03462576866149902,\n",
       " -0.07512979954481125,\n",
       " -0.010454910807311535,\n",
       " 0.06342925876379013,\n",
       " 0.021945515647530556,\n",
       " -0.0410078801214695,\n",
       " -0.0519806407392025,\n",
       " 0.05268043279647827,\n",
       " -0.01952422969043255,\n",
       " 0.041483741253614426,\n",
       " -0.006480085197836161,\n",
       " 0.005136481951922178,\n",
       " 0.04288332909345627,\n",
       " 0.05298834294080734,\n",
       " 0.01573135145008564,\n",
       " 0.014485719613730907,\n",
       " -0.010468906722962856,\n",
       " 0.01787271909415722,\n",
       " -0.07238660752773285,\n",
       " 0.026410195976495743,\n",
       " -0.01612323522567749,\n",
       " 0.0026032307650893927,\n",
       " -0.012155408039689064,\n",
       " 0.0011747778626158834,\n",
       " 0.0203079991042614,\n",
       " 0.04044804722070694,\n",
       " 0.04632630944252014,\n",
       " -0.005741802975535393,\n",
       " -0.02365300990641117,\n",
       " 0.015129528939723969,\n",
       " 0.008831390179693699,\n",
       " -0.006700519938021898,\n",
       " -0.03294626250863075,\n",
       " -0.03515760973095894,\n",
       " 0.04789384827017784,\n",
       " 0.006392610725015402,\n",
       " 0.011665552854537964,\n",
       " 0.0070294225588440895,\n",
       " 0.028271645307540894,\n",
       " 0.0273619145154953,\n",
       " -0.04193160682916641,\n",
       " 0.04361111298203468,\n",
       " -0.08072814345359802,\n",
       " -0.019818143919110298,\n",
       " 0.011749527417123318,\n",
       " 0.00909031368792057,\n",
       " 0.008334537036716938,\n",
       " -0.024492761120200157,\n",
       " -0.011301659978926182,\n",
       " 0.018012676388025284,\n",
       " 0.0061126938089728355,\n",
       " -0.009783108718693256,\n",
       " 0.048117779195308685,\n",
       " -0.020377978682518005,\n",
       " -0.0030983344186097383,\n",
       " 0.04229550063610077,\n",
       " -0.025374501943588257,\n",
       " -0.004216254223138094,\n",
       " 0.0036354255862534046,\n",
       " 0.022883238270878792,\n",
       " 0.013009155169129372,\n",
       " -0.051252853125333786,\n",
       " 0.002804421354085207,\n",
       " -0.03370204195380211,\n",
       " 0.04111984744668007,\n",
       " 0.03095885179936886,\n",
       " 0.007305840961635113,\n",
       " -0.03168663755059242,\n",
       " 0.027040008455514908,\n",
       " -0.0417916513979435,\n",
       " -0.016263194382190704,\n",
       " 0.025920340791344643,\n",
       " -0.008432508446276188,\n",
       " 0.006857973523437977,\n",
       " 0.028201665729284286,\n",
       " -0.037956781685352325,\n",
       " 0.009776110760867596,\n",
       " 0.031714629381895065,\n",
       " 0.014093834906816483,\n",
       " -0.01465366967022419,\n",
       " 0.0286355372518301,\n",
       " -0.048005811870098114,\n",
       " 0.03666916489601135,\n",
       " 0.006913956720381975,\n",
       " 0.01079780887812376,\n",
       " 0.03342212364077568,\n",
       " -0.04822974652051926,\n",
       " 0.0784888043999672,\n",
       " -0.014821619726717472,\n",
       " -0.008677436038851738,\n",
       " -0.04008415341377258,\n",
       " -0.03916042670607567,\n",
       " 0.005090995691716671,\n",
       " -0.04487074166536331,\n",
       " -0.03345011547207832,\n",
       " 0.004933542106300592,\n",
       " -0.030818892642855644,\n",
       " 0.056515298783779144,\n",
       " -0.030818892642855644,\n",
       " 0.03888050839304924,\n",
       " 0.012106422334909439,\n",
       " -0.000954342947807163,\n",
       " 0.04408697038888931,\n",
       " 0.007165882270783186,\n",
       " 0.009041327983140945,\n",
       " 0.01549342181533575,\n",
       " -0.007319836877286434,\n",
       " -0.009706132113933563,\n",
       " -0.024394791573286057,\n",
       " -0.03350609913468361,\n",
       " -0.026844067499041557,\n",
       " 0.000995455775409937,\n",
       " 0.05175670608878136,\n",
       " 0.0015937789576128125,\n",
       " -0.05794287845492363,\n",
       " 0.0288314800709486,\n",
       " -0.0670681819319725,\n",
       " -0.012281370349228382,\n",
       " -0.014863607473671436,\n",
       " 0.01787271909415722,\n",
       " -0.0010925520909950137,\n",
       " -0.015017561614513397,\n",
       " -0.04397500306367874,\n",
       " -0.008474495261907578,\n",
       " 0.026228249073028564,\n",
       " -0.030231066048145294,\n",
       " -0.018012676388025284,\n",
       " -0.011217685416340828,\n",
       " 0.021273713558912277,\n",
       " -0.04632630944252014,\n",
       " -0.014009859412908554,\n",
       " 0.005720809567719698,\n",
       " 0.010000044479966164,\n",
       " 0.05483579635620117,\n",
       " 0.028103694319725037,\n",
       " -0.02726394310593605,\n",
       " -0.027235951274633408,\n",
       " 0.01529747899621725,\n",
       " -0.024954626336693764,\n",
       " -0.05069302022457123,\n",
       " -0.02012605220079422,\n",
       " 0.02775379829108715,\n",
       " 0.01235134992748499,\n",
       " 0.04260341078042984,\n",
       " 0.023876944556832314,\n",
       " -0.009874082170426846,\n",
       " -0.023904936388134956,\n",
       " -0.007242859806865454,\n",
       " -0.0033817505463957787,\n",
       " -0.03342212364077568,\n",
       " -0.027991728857159615,\n",
       " -0.008271555416285992,\n",
       " -0.038236699998378754,\n",
       " -0.009979050606489182,\n",
       " 0.031882576644420624,\n",
       " 0.059454429894685745,\n",
       " -0.028117690235376358,\n",
       " -0.019090358167886734,\n",
       " 0.035129617899656296,\n",
       " 0.03734096512198448,\n",
       " 0.026354212313890457,\n",
       " 0.004653624724596739,\n",
       " -0.03664117306470871,\n",
       " 0.013813917525112629,\n",
       " -0.014975574798882008,\n",
       " -0.019972097128629684,\n",
       " -0.011497601866722107,\n",
       " -0.0001862106000771746,\n",
       " 0.03594137728214264,\n",
       " -0.03028704971075058,\n",
       " 0.02747388184070587,\n",
       " -0.025430485606193542,\n",
       " 0.10412922501564026,\n",
       " -0.0296152476221323,\n",
       " -0.013044144958257675,\n",
       " -0.0019016880542039871,\n",
       " -0.0044261920265853405,\n",
       " -0.04190361499786377,\n",
       " 0.000921977567486465,\n",
       " 0.03773285076022148,\n",
       " -0.01610923931002617,\n",
       " -0.024254832416772842,\n",
       " -0.028565557673573494,\n",
       " -0.013869901187717915,\n",
       " -0.017424849793314934,\n",
       " 0.011154703795909882,\n",
       " -0.0368930958211422,\n",
       " 0.01865648664534092,\n",
       " 0.0030231066048145294,\n",
       " 0.004622133914381266,\n",
       " 0.03812473267316818,\n",
       " 0.02522054687142372,\n",
       " 0.00894335750490427,\n",
       " -0.030902868136763573,\n",
       " 0.012519299983978271,\n",
       " 0.028943447396159172,\n",
       " 0.022743279114365578,\n",
       " 0.012792219407856464,\n",
       " -0.018292594701051712,\n",
       " 0.010608864948153496,\n",
       " -0.008054619655013084,\n",
       " -0.0018981890752911568,\n",
       " 0.04554254189133644,\n",
       " 0.014009859412908554,\n",
       " 0.009874082170426846,\n",
       " -0.011189693585038185,\n",
       " -0.03557748720049858,\n",
       " -0.014107830822467804,\n",
       " 0.012281370349228382,\n",
       " 0.02891545556485653,\n",
       " -0.04624233394861221,\n",
       " -0.0030685930978506804,\n",
       " -0.04366709664463997,\n",
       " -0.02081185020506382,\n",
       " -0.0555075965821743,\n",
       " 0.025822369381785393,\n",
       " -0.0035601980052888393,\n",
       " -0.013939880765974522,\n",
       " -0.010979754850268364,\n",
       " 0.04249144345521927,\n",
       " 0.0022638309746980667,\n",
       " -0.006525571458041668,\n",
       " -0.007634743582457304,\n",
       " -0.0037403947208076715,\n",
       " 0.0026277233846485615,\n",
       " 0.033758025616407394,\n",
       " 0.02502460405230522,\n",
       " 0.023219138383865356,\n",
       " 0.013408037833869457,\n",
       " 0.010370935313403606,\n",
       " -0.012876194901764393,\n",
       " -0.043415170162916183,\n",
       " 0.015549404546618462,\n",
       " -0.01065085269510746,\n",
       " -0.0047096083872020245,\n",
       " 0.005923749413341284,\n",
       " 0.012386339716613293,\n",
       " -0.008313543163239956,\n",
       " -0.019314292818307877,\n",
       " 0.0023757978342473507,\n",
       " 0.015269487164914608,\n",
       " 0.01806866005063057,\n",
       " 0.009559174999594688,\n",
       " -0.026718104258179665,\n",
       " -0.015885306522250175,\n",
       " 0.007711721118539572,\n",
       " 0.026158269494771957,\n",
       " 0.022813258692622185,\n",
       " -0.0008314417791552842,\n",
       " 0.0029601252172142267,\n",
       " 0.005626337137073278,\n",
       " -0.03818071633577347,\n",
       " 0.02021002769470215,\n",
       " -0.006280643865466118,\n",
       " 0.05004920810461044,\n",
       " 0.05444391071796417,\n",
       " 0.010825800709426403,\n",
       " -0.0025035100989043713,\n",
       " 0.007071410305798054,\n",
       " -0.0572710745036602,\n",
       " 0.026116281747817993,\n",
       " 0.03359007462859154,\n",
       " -0.03876854479312897,\n",
       " -0.009363233111798763,\n",
       " 0.032582372426986694,\n",
       " -0.015955284237861633,\n",
       " -0.02072787471115589,\n",
       " 0.0026802080683410168,\n",
       " 0.012449321337044239,\n",
       " 0.014044849202036858,\n",
       " 0.010461908765137196,\n",
       " -0.04397500306367874,\n",
       " 0.013296070508658886,\n",
       " -0.001736361882649362,\n",
       " -0.03694907948374748,\n",
       " 0.004016812890768051,\n",
       " 0.014527706429362297,\n",
       " 0.047138068825006485,\n",
       " -0.011882488615810871,\n",
       " 0.008516483008861542,\n",
       " 0.05226055532693863,\n",
       " 0.011350645683705807,\n",
       " -0.028747504577040672,\n",
       " -0.008635448291897774,\n",
       " -0.03459777683019638,\n",
       " -0.016879012808203697,\n",
       " 0.016263194382190704,\n",
       " -0.008544474840164185,\n",
       " -0.029867174103856087,\n",
       " 0.02082584612071514,\n",
       " -0.04209955781698227,\n",
       " 0.018250606954097748,\n",
       " 0.0017757252790033817,\n",
       " -0.018348578363656998,\n",
       " -0.0017949695466086268,\n",
       " 0.009888078086078167,\n",
       " -0.02755785547196865,\n",
       " -0.04111984744668007,\n",
       " -0.0016165223205462098,\n",
       " -0.009216275997459888,\n",
       " -0.028411604464054108,\n",
       " 0.003974825609475374,\n",
       " 0.017172925174236298,\n",
       " -0.006137186195701361,\n",
       " 0.011231680400669575,\n",
       " -0.007998635992407799,\n",
       " -0.019692180678248405,\n",
       " 0.008397518657147884,\n",
       " 0.004979028832167387,\n",
       " 0.01661309041082859,\n",
       " -0.0021378681994974613,\n",
       " -0.03331015631556511,\n",
       " 0.001287619466893375,\n",
       " 0.011147705838084221,\n",
       " -0.03280630707740784,\n",
       " 0.017830731347203255,\n",
       " 0.0054723829962313175,\n",
       " 0.013652965426445007,\n",
       " 0.03339413180947304,\n",
       " 0.011049734428524971,\n",
       " -0.028271645307540894,\n",
       " -0.007648739498108625,\n",
       " -0.03417789936065674,\n",
       " 0.05500374734401703,\n",
       " 0.0018509529763832688,\n",
       " -0.009174289181828499,\n",
       " -0.017522821202874184,\n",
       " 0.01114070788025856,\n",
       " -0.027319926768541336,\n",
       " 0.029391314834356308,\n",
       " -0.013736940920352936,\n",
       " 0.0001765884371707216,\n",
       " 0.02734791859984398,\n",
       " 0.008607456460595131,\n",
       " 0.03916042670607567,\n",
       " 0.011098720133304596,\n",
       " 0.0003118453314527869,\n",
       " 0.006616544909775257,\n",
       " 0.0300071332603693,\n",
       " 0.02677408792078495,\n",
       " -0.0006035716505721211,\n",
       " 0.0025839863810688257,\n",
       " 0.024590732529759407,\n",
       " -0.021637605503201485,\n",
       " -0.028369616717100143,\n",
       " -0.0044891731813549995,\n",
       " -0.02470269985496998,\n",
       " 0.004611636977642775,\n",
       " 0.026648124679923058,\n",
       " -0.007228863891214132,\n",
       " -0.029559263959527016,\n",
       " -0.0119664641097188,\n",
       " -0.0008611829834990203,\n",
       " -0.03426187485456467,\n",
       " -0.015689363703131676,\n",
       " 0.012624269351363182,\n",
       " -0.00981809850782156,\n",
       " -0.0074527976103127,\n",
       " 0.03417789936065674,\n",
       " 0.020951807498931885,\n",
       " 0.021161746233701706,\n",
       " -0.005633335094898939,\n",
       " 0.013051142916083336,\n",
       " -0.022827254608273506,\n",
       " -0.051252853125333786,\n",
       " 0.010370935313403606,\n",
       " 0.006095198914408684,\n",
       " -5.8881036238744855e-05,\n",
       " 0.03235843777656555,\n",
       " -0.012085428461432457,\n",
       " -0.010573875159025192,\n",
       " 0.013107126578688622,\n",
       " -0.0026767090894281864,\n",
       " 0.036389246582984924,\n",
       " -0.008474495261907578,\n",
       " 0.01055288128554821,\n",
       " 0.01249830611050129,\n",
       " -0.02014004811644554,\n",
       " -0.01886642538011074,\n",
       " 0.00613368721678853,\n",
       " 0.0054723829962313175,\n",
       " 0.027795786038041115,\n",
       " -0.003247040556743741,\n",
       " 0.013156112283468246,\n",
       " 0.004331720061600208,\n",
       " -0.008859382010996342,\n",
       " -0.05181268975138664,\n",
       " -0.009454206563532352,\n",
       " 0.004468179773539305,\n",
       " -0.005787289701402187,\n",
       " 0.025766385719180107,\n",
       " -0.0007662735297344625,\n",
       " -0.04307926818728447,\n",
       " -0.004083293490111828,\n",
       " -0.0201120562851429,\n",
       " 0.05156076326966286,\n",
       " 0.007795696146786213,\n",
       " 0.00948219746351242,\n",
       " -0.018390566110610962,\n",
       " -0.010195987299084663,\n",
       " -0.00011524719593580812,\n",
       " -0.009251265786588192,\n",
       " -0.0065605612471699715,\n",
       " 0.0067914929240942,\n",
       " -0.027040008455514908,\n",
       " -0.01699097827076912,\n",
       " -0.029391314834356308,\n",
       " 0.003273282665759325,\n",
       " 0.02608828991651535,\n",
       " 0.009160293266177177,\n",
       " -0.015087541192770004,\n",
       " -0.011147705838084221,\n",
       " -0.01945425011217594,\n",
       " 0.009048325940966606,\n",
       " 0.0014337013708427548,\n",
       " -0.0022410876117646694,\n",
       " 0.029363323003053665,\n",
       " 0.005283438600599766,\n",
       " 0.035409536212682724,\n",
       " 0.0029951150063425303,\n",
       " 0.02941930666565895,\n",
       " -0.0050665028393268585,\n",
       " 0.0050070201978087425,\n",
       " -0.03157467022538185,\n",
       " -0.0607980340719223,\n",
       " -0.013121122494339943,\n",
       " -0.01904837042093277,\n",
       " -0.03417789936065674,\n",
       " -0.01549342181533575,\n",
       " 0.018530523404479027,\n",
       " 0.03538154438138008,\n",
       " -0.018054664134979248,\n",
       " 0.007060913369059563,\n",
       " 0.0037683863192796707,\n",
       " -0.011252674274146557,\n",
       " -0.03308622166514397,\n",
       " -0.011945470236241817,\n",
       " -0.018782449886202812,\n",
       " 0.012736236676573753,\n",
       " 0.0011030490277335048,\n",
       " -0.02286924235522747,\n",
       " -0.011924476362764835,\n",
       " -0.0018387066666036844,\n",
       " 0.011812509037554264,\n",
       " 0.019384272396564484,\n",
       " 0.03596936911344528,\n",
       " -0.003526957705616951,\n",
       " -0.009608160704374313,\n",
       " 0.014695657417178154,\n",
       " 0.00967114232480526,\n",
       " 0.02628423273563385,\n",
       " -0.060518115758895874,\n",
       " -0.012302364222705364,\n",
       " -0.0026259738951921463,\n",
       " 0.01728489249944687,\n",
       " -0.002146615646779537,\n",
       " 0.010944765992462635,\n",
       " 0.023904936388134956,\n",
       " -0.048425689339637756,\n",
       " -0.0121134202927351,\n",
       " -0.008537476882338524,\n",
       " 0.02228141576051712,\n",
       " -0.02639620006084442,\n",
       " -0.08011232316493988,\n",
       " 0.03613732010126114,\n",
       " 0.021343693137168884,\n",
       " 0.012785221450030804,\n",
       " -0.0017766000237315893,\n",
       " -0.008999340236186981,\n",
       " -0.004338718019425869,\n",
       " 0.0006250027799978852,\n",
       " 0.01249830611050129,\n",
       " -0.048201754689216614,\n",
       " 0.033674050122499466,\n",
       " -0.004615135956555605,\n",
       " 0.010049030184745789,\n",
       " -0.028201665729284286,\n",
       " -0.013023151084780693,\n",
       " 0.0017932200571522117,\n",
       " 0.013995864428579807,\n",
       " -0.01748083345592022,\n",
       " -0.008663440123200417,\n",
       " -0.00932824332267046,\n",
       " -0.005731306504458189,\n",
       " -0.05111289396882057,\n",
       " -0.004405198153108358,\n",
       " 0.019692180678248405,\n",
       " -0.021637605503201485,\n",
       " -0.02950328215956688,\n",
       " -0.008215571753680706,\n",
       " 0.017522821202874184,\n",
       " 0.03496166691184044,\n",
       " 0.004023810848593712,\n",
       " -0.01818062737584114,\n",
       " 0.04711007699370384,\n",
       " -0.017914706841111183,\n",
       " -0.015129528939723969,\n",
       " 0.0607980340719223,\n",
       " 0.015675367787480354,\n",
       " -0.02862154133617878,\n",
       " 0.040783949196338654,\n",
       " -0.007172880228608847,\n",
       " -0.006504577584564686,\n",
       " -0.036585189402103424,\n",
       " -0.03182659298181534,\n",
       " 0.015437438152730465,\n",
       " 0.020965803414583206,\n",
       " 0.014527706429362297,\n",
       " -0.03398195654153824,\n",
       " -0.023611022159457207,\n",
       " -0.0031368229538202286,\n",
       " 0.01708894968032837,\n",
       " 0.024884646758437157,\n",
       " -0.003810374066233635,\n",
       " 0.018292594701051712,\n",
       " -0.0037578893825411797,\n",
       " -0.025668414309620857,\n",
       " -0.014443731866776943,\n",
       " -0.02187553606927395,\n",
       " 0.0003715464554261416,\n",
       " -0.013114124536514282,\n",
       " -0.0029758706223219633,\n",
       " 0.02365300990641117,\n",
       " -0.003338013542816043,\n",
       " -0.003855860559269786,\n",
       " -0.04408697038888931,\n",
       " -0.018096651881933212,\n",
       " 0.053296249359846115,\n",
       " -0.006728511769324541,\n",
       " 0.044198937714099884,\n",
       " 0.008327539078891277,\n",
       " -0.011539589613676071,\n",
       " -0.0007448424003086984,\n",
       " -0.021511642262339592,\n",
       " 0.007305840961635113,\n",
       " 0.015675367787480354,\n",
       " 0.03949632868170738,\n",
       " 0.011679548770189285,\n",
       " -0.03566146269440651,\n",
       " 0.020657895132899284,\n",
       " -0.04652225226163864,\n",
       " -0.020154044032096863,\n",
       " 0.0063996086828410625,\n",
       " 0.0031735622324049473,\n",
       " 0.0016900005284696817,\n",
       " -0.0212877094745636,\n",
       " -0.0410078801214695,\n",
       " 0.009867084212601185,\n",
       " 0.003161315806210041,\n",
       " 0.007057414390146732,\n",
       " -0.05430395156145096,\n",
       " 0.0276558268815279,\n",
       " 0.0235410425812006,\n",
       " 4.871216879109852e-05,\n",
       " 0.009055323898792267,\n",
       " 0.045290615409612656,\n",
       " 0.008236565627157688,\n",
       " -0.003180560190230608,\n",
       " 0.022421374917030334,\n",
       " -0.010105013847351074,\n",
       " -0.011084724217653275,\n",
       " 0.022883238270878792,\n",
       " 0.032078519463539124,\n",
       " 0.01708894968032837,\n",
       " 0.034737735986709595,\n",
       " 0.03636125475168228,\n",
       " 0.008516483008861542,\n",
       " 0.0429673008620739,\n",
       " 0.00016160849190782756,\n",
       " 0.005073500797152519,\n",
       " -0.0031018333975225687,\n",
       " -0.02688605524599552,\n",
       " -0.00029413183801807463,\n",
       " 0.02891545556485653,\n",
       " 0.07417807728052139,\n",
       " 0.024688703939318657,\n",
       " 0.011014744639396667,\n",
       " 0.011574579402804375,\n",
       " -0.026214253157377243,\n",
       " -0.03792878985404968,\n",
       " 0.001764353597536683,\n",
       " -0.021763568744063377,\n",
       " -0.014079838991165161,\n",
       " -0.00720787001773715,\n",
       " 0.03812473267316818,\n",
       " 0.0069769383408129215,\n",
       " 0.0010890531120821834,\n",
       " -0.0008183206664398313,\n",
       " -0.011231680400669575,\n",
       " 0.003152568358927965,\n",
       " 0.008950355462729931,\n",
       " -0.006028718315064907,\n",
       " -0.018978390842676163,\n",
       " -0.05150477960705757,\n",
       " -0.017522821202874184,\n",
       " -0.003009110689163208,\n",
       " 0.029755206778645515,\n",
       " 0.012652261182665825,\n",
       " -0.0419875904917717,\n",
       " 0.04565450921654701,\n",
       " 0.008530478924512863,\n",
       " -0.009510189294815063,\n",
       " -0.0061126938089728355,\n",
       " -0.045682501047849655,\n",
       " 0.0026784585788846016,\n",
       " -0.003105332376435399,\n",
       " 0.020545927807688713,\n",
       " -0.053884077817201614,\n",
       " -0.0235410425812006,\n",
       " 0.001231636037118733,\n",
       " -0.032190486788749695,\n",
       " -0.008173584938049316,\n",
       " -0.008915365673601627,\n",
       " -0.006843977607786655,\n",
       " -0.0031368229538202286,\n",
       " 0.007564764469861984,\n",
       " 0.019804148003458977,\n",
       " -0.0032138002570718527,\n",
       " 0.004118282813578844,\n",
       " -0.02237938717007637,\n",
       " -0.006630540359765291,\n",
       " 0.028593549504876137,\n",
       " -0.00894335750490427,\n",
       " -0.0006643661763519049,\n",
       " 0.018208619207143784,\n",
       " -0.0022708289325237274,\n",
       " 0.016753049567341805,\n",
       " -0.02706800028681755,\n",
       " 0.014709653332829475,\n",
       " 0.005899256560951471,\n",
       " -0.006438097450882196,\n",
       " -0.006161679048091173,\n",
       " -0.03997218608856201,\n",
       " -0.039636287838220596,\n",
       " -0.004958034958690405,\n",
       " 0.016291186213493347,\n",
       " -0.03129475191235542,\n",
       " -0.029055412858724594,\n",
       " -0.04279935359954834,\n",
       " -0.015451434068381786,\n",
       " -0.027235951274633408,\n",
       " 0.003574193688109517,\n",
       " -0.0047096083872020245,\n",
       " 0.025388497859239578,\n",
       " -0.007508780807256699,\n",
       " 0.02050394006073475,\n",
       " -0.00672151381149888,\n",
       " -0.02666212059557438,\n",
       " 0.007753708865493536,\n",
       " -0.014247789978981018,\n",
       " -0.014009859412908554,\n",
       " 0.01689300872385502,\n",
       " -0.004170767497271299,\n",
       " -0.006256151013076305,\n",
       " -0.008208573795855045,\n",
       " -0.020000088959932327,\n",
       " -0.004583645612001419,\n",
       " -0.022225432097911835,\n",
       " 0.024898642674088478,\n",
       " 0.011511597782373428,\n",
       " 0.061021968722343445,\n",
       " -0.03526957705616951,\n",
       " 0.013254082761704922,\n",
       " 0.035017650574445724,\n",
       " 0.011917478404939175,\n",
       " -0.03163065388798714,\n",
       " -0.044114962220191956,\n",
       " 0.006025219336152077,\n",
       " 0.032778315246105194,\n",
       " -0.014485719613730907,\n",
       " -0.0028813984245061874,\n",
       " 0.007578760385513306,\n",
       " -0.011399631388485432,\n",
       " 0.03482171148061752,\n",
       " 0.0028009223751723766,\n",
       " -0.0144157400354743,\n",
       " 0.020769862458109856,\n",
       " -0.007648739498108625,\n",
       " 0.021455658599734306,\n",
       " -0.024842659011483192,\n",
       " 0.056319355964660645,\n",
       " 0.030846884474158287,\n",
       " -0.0127502316609025,\n",
       " -0.022127460688352585,\n",
       " 0.0300071332603693,\n",
       " -0.0024685205426067114,\n",
       " 0.0017608546186238527,\n",
       " ...]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from openai import OpenAI\n",
    "\n",
    "# Initialize OpenAI client\n",
    "client = OpenAI(api_key=os.getenv(\"OPENAI_API_KEY\"))\n",
    "MODEL = \"gpt-4.1-mini\"\n",
    "\n",
    "# Function to get the vector embedding for a given text\n",
    "def get_vector_embeddings(text):\n",
    "    response = client.embeddings.create(\n",
    "        input=text,\n",
    "        model=\"text-embedding-3-small\"  # Using latest embedding model\n",
    "    )\n",
    "    embeddings = response.data[0].embedding\n",
    "    return embeddings\n",
    "\n",
    "\n",
    "query_embedding = get_vector_embeddings(\"Your text string goes here\")\n",
    "print(len(query_embedding))\n",
    "query_embedding"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "CREATE TABLE items (\n",
    "    id serial PRIMARY KEY,\n",
    "    embedding vector(1536),\n",
    "    text TEXT\n",
    ");\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of paragraphs: 6\n",
      "\n",
      "Paragraph 0:\n",
      "Welcome to the \"Unicorn Enterprises: Where Magic Happens\" Employee Handbook! We're thrilled to have you join our team of dreamers, doers, and unicorn enthusiasts. At Unicorn Enterprises, we believe that work should be as enchanting as it is productive. This handbook is your ticket to the magical world of our company, where we'll outline the principles, policies, and practices that guide us on this extraordinary journey. So, fasten your seatbelts and get ready to embark on an adventure like no other!\n",
      "\n",
      "Paragraph 1:\n",
      "**1: Our Magical Culture**\n",
      "At Unicorn Enterprises, we take pride in our unique and enchanting company culture. We believe that creativity and innovation flourish best when people are happy and inspired. From our weekly \"Wear Your Favorite Mythical Creature Costume\" day on Fridays to our in-house unicorn petting zoo, we aim to infuse magic into every corner of our workplace. So, don't be surprised if you find a fairy tale book in the breakroom or a gnome guiding you to the restroom. Our culture is designed to spark your imagination and encourage collaboration among our magical team.\n",
      "\n",
      "Paragraph 2:\n",
      "**2: Unicorn Code of Conduct**\n",
      "While we embrace creativity, we also value professionalism. Our Unicorn Code of Conduct ensures that we maintain a harmonious and respectful environment. Treating all team members, regardless of their unicorn species, with kindness and respect is essential. We also encourage open communication and constructive feedback because, in our world, every opinion matters, just like every horn on a unicorn's head!\n",
      "\n",
      "Paragraph 3:\n",
      "**3: Magical Work-Life Balance**\n",
      "At Unicorn Enterprises, we understand the importance of maintaining a balanced life. We offer flexible work hours, magical mental health days, and even an on-site wizard to provide stress-relief spells when needed. We believe that a happy and well-rested employee is a creative and productive employee. So, don't hesitate to use our relaxation chambers or join a group meditation session under the office rainbow.\n",
      "\n",
      "Paragraph 4:\n",
      "**4: Enchanted Benefits**\n",
      "Our commitment to your well-being extends to our magical benefits package. You'll enjoy a treasure chest of perks, including unlimited unicorn rides, a bottomless cauldron of coffee and potions, and access to our company library filled with spellbinding books. We also offer competitive health and dental plans, ensuring your physical well-being is as robust as your magical spirit.\n",
      "\n",
      "Paragraph 5:\n",
      "**5: Continuous Learning and Growth**\n",
      "At Unicorn Enterprises, we believe in continuous learning and growth. We provide access to a plethora of online courses, enchanted workshops, and wizard-led training sessions. Whether you're aspiring to master new spells or conquer new challenges, we're here to support your personal and professional development.\n",
      "As we conclude this handbook, remember that at Unicorn Enterprises, the pursuit of excellence is a never-ending quest. Our company's success depends on your passion, creativity, and commitment to making the impossible possible. We encourage you to always embrace the magic within and outside of work, and to share your ideas and innovations to keep our enchanted journey going. Thank you for being a part of our mystical family, and together, we'll continue to create a world where magic and business thrive hand in hand!\n"
     ]
    }
   ],
   "source": [
    "employee_handbook = \"\"\"\n",
    "Welcome to the \"Unicorn Enterprises: Where Magic Happens\" Employee Handbook! We're thrilled to have you join our team of dreamers, doers, and unicorn enthusiasts. At Unicorn Enterprises, we believe that work should be as enchanting as it is productive. This handbook is your ticket to the magical world of our company, where we'll outline the principles, policies, and practices that guide us on this extraordinary journey. So, fasten your seatbelts and get ready to embark on an adventure like no other!\n",
    "\n",
    "**1: Our Magical Culture**\n",
    "At Unicorn Enterprises, we take pride in our unique and enchanting company culture. We believe that creativity and innovation flourish best when people are happy and inspired. From our weekly \"Wear Your Favorite Mythical Creature Costume\" day on Fridays to our in-house unicorn petting zoo, we aim to infuse magic into every corner of our workplace. So, don't be surprised if you find a fairy tale book in the breakroom or a gnome guiding you to the restroom. Our culture is designed to spark your imagination and encourage collaboration among our magical team.\n",
    "\n",
    "**2: Unicorn Code of Conduct**\n",
    "While we embrace creativity, we also value professionalism. Our Unicorn Code of Conduct ensures that we maintain a harmonious and respectful environment. Treating all team members, regardless of their unicorn species, with kindness and respect is essential. We also encourage open communication and constructive feedback because, in our world, every opinion matters, just like every horn on a unicorn's head!\n",
    "\n",
    "**3: Magical Work-Life Balance**\n",
    "At Unicorn Enterprises, we understand the importance of maintaining a balanced life. We offer flexible work hours, magical mental health days, and even an on-site wizard to provide stress-relief spells when needed. We believe that a happy and well-rested employee is a creative and productive employee. So, don't hesitate to use our relaxation chambers or join a group meditation session under the office rainbow.\n",
    "\n",
    "**4: Enchanted Benefits**\n",
    "Our commitment to your well-being extends to our magical benefits package. You'll enjoy a treasure chest of perks, including unlimited unicorn rides, a bottomless cauldron of coffee and potions, and access to our company library filled with spellbinding books. We also offer competitive health and dental plans, ensuring your physical well-being is as robust as your magical spirit.\n",
    "\n",
    "**5: Continuous Learning and Growth**\n",
    "At Unicorn Enterprises, we believe in continuous learning and growth. We provide access to a plethora of online courses, enchanted workshops, and wizard-led training sessions. Whether you're aspiring to master new spells or conquer new challenges, we're here to support your personal and professional development.\n",
    "As we conclude this handbook, remember that at Unicorn Enterprises, the pursuit of excellence is a never-ending quest. Our company's success depends on your passion, creativity, and commitment to making the impossible possible. We encourage you to always embrace the magic within and outside of work, and to share your ideas and innovations to keep our enchanted journey going. Thank you for being a part of our mystical family, and together, we'll continue to create a world where magic and business thrive hand in hand!\n",
    "\"\"\"\n",
    "\n",
    "# Split the employee handbook into paragraphs\n",
    "chunks = [p.strip() for p in employee_handbook.split('\\n\\n') if p.strip()]\n",
    "print(f\"Number of paragraphs: {len(chunks)}\")\n",
    "\n",
    "# Print each paragraph with its index\n",
    "for i, p in enumerate(chunks):\n",
    "    print(f\"\\nParagraph {i}:\")\n",
    "    print(p)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Successfully inserted all chunks into Supabase\n"
     ]
    }
   ],
   "source": [
    "# Get embeddings for each chunk\n",
    "embeddings = []\n",
    "for chunk in chunks:\n",
    "    embedding = get_vector_embeddings(chunk)\n",
    "    embeddings.append(embedding)\n",
    "\n",
    "# Insert chunks and their embeddings into Supabase\n",
    "for chunk, embedding in zip(chunks, embeddings):\n",
    "    data = supabase.table('items').insert({\n",
    "        \"text\": chunk,\n",
    "        \"embedding\": embedding\n",
    "    }).execute()\n",
    "    \n",
    "print(\"Successfully inserted all chunks into Supabase\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "-- Ensure the pgvector extension is enabled\n",
    "create extension if not exists vector;\n",
    "\n",
    "-- Function to search for similar items\n",
    "create or replace function match_items (\n",
    "  query_embedding vector(1536), -- Match the dimension of your embeddings\n",
    "  match_threshold float,        -- Minimum similarity score (e.g., 0.7)\n",
    "  match_count int               -- Max number of results to return\n",
    ")\n",
    "returns table (\n",
    "  id bigint,                  -- Match the type of your 'id' column\n",
    "  text text,                  -- Match the type of your 'text' column\n",
    "  similarity float            -- Calculated similarity score\n",
    ")\n",
    "language sql stable\n",
    "as $$\n",
    "  select\n",
    "    items.id,\n",
    "    items.text,\n",
    "    1 - (items.embedding <=> query_embedding) as similarity -- '<=>' is cosine distance, 1 - distance = similarity\n",
    "  from items\n",
    "  where 1 - (items.embedding <=> query_embedding) > match_threshold\n",
    "  order by items.embedding <=> query_embedding -- Order by distance (ascending)\n",
    "  limit match_count;\n",
    "$$;\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Document ID: 2\n",
      "Similarity Score: 0.503\n",
      "Text:\n",
      "**1: Our Magical Culture**\n",
      "At Unicorn Enterprises, we take pride in our unique and enchanting company culture. We believe that creativity and innovation flourish best when people are happy and inspired. From our weekly \"Wear Your Favorite Mythical Creature Costume\" day on Fridays to our in-house unicorn petting zoo, we aim to infuse magic into every corner of our workplace. So, don't be surprised if you find a fairy tale book in the breakroom or a gnome guiding you to the restroom. Our culture is designed to spark your imagination and encourage collaboration among our magical team.\n",
      "--------------------------------------------------------------------------------\n",
      "\n",
      "Document ID: 5\n",
      "Similarity Score: 0.494\n",
      "Text:\n",
      "**4: Enchanted Benefits**\n",
      "Our commitment to your well-being extends to our magical benefits package. You'll enjoy a treasure chest of perks, including unlimited unicorn rides, a bottomless cauldron of coffee and potions, and access to our company library filled with spellbinding books. We also offer competitive health and dental plans, ensuring your physical well-being is as robust as your magical spirit.\n",
      "--------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# Your query text\n",
    "query_text = \"Do we get free Unicorn rides?\"\n",
    "\n",
    "# Generate the embedding for the query text\n",
    "query_embedding = get_vector_embeddings(query_text)\n",
    "\n",
    "# Call the database function using rpc\n",
    "try:\n",
    "    results = supabase.rpc('match_items', {\n",
    "        'query_embedding': query_embedding,\n",
    "        'match_threshold': 0.4,  # Adjust this threshold as needed\n",
    "        'match_count': 2         # Number of results you want\n",
    "    }).execute()\n",
    "\n",
    "    # Process the results\n",
    "    # Print each result with its similarity score\n",
    "    for result in results.data:\n",
    "        print(f\"\\nDocument ID: {result['id']}\")\n",
    "        print(f\"Similarity Score: {result['similarity']:.3f}\")\n",
    "        print(\"Text:\")\n",
    "        print(result['text'])\n",
    "        print(\"-\" * 80)\n",
    "\n",
    "except Exception as e:\n",
    "    print(f\"An error occurred: {e}\")\n",
    "    # Check if the error message gives clues, e.g., function not found\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'id': 2,\n",
       "  'similarity': 0.502970903226465,\n",
       "  'text': '**1: Our Magical Culture**\\nAt Unicorn Enterprises, we take pride in our unique and enchanting company culture. We believe that creativity and innovation flourish best when people are happy and inspired. From our weekly \"Wear Your Favorite Mythical Creature Costume\" day on Fridays to our in-house unicorn petting zoo, we aim to infuse magic into every corner of our workplace. So, don\\'t be surprised if you find a fairy tale book in the breakroom or a gnome guiding you to the restroom. Our culture is designed to spark your imagination and encourage collaboration among our magical team.'},\n",
       " {'id': 5,\n",
       "  'similarity': 0.493960173017949,\n",
       "  'text': \"**4: Enchanted Benefits**\\nOur commitment to your well-being extends to our magical benefits package. You'll enjoy a treasure chest of perks, including unlimited unicorn rides, a bottomless cauldron of coffee and potions, and access to our company library filled with spellbinding books. We also offer competitive health and dental plans, ensuring your physical well-being is as robust as your magical spirit.\"}]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def retrieve(query_text, match_count=2):\n",
    "    # Get vector embedding for the query text\n",
    "    query_embedding = get_vector_embeddings(query_text)\n",
    "    \n",
    "    # Query using pgvector's cosine distance search\n",
    "    response = supabase.rpc(\n",
    "        'match_items',  # Use vector similarity search\n",
    "        {\n",
    "            'query_embedding': query_embedding,\n",
    "            'match_threshold': 0.4,  # Similarity threshold\n",
    "            'match_count': match_count  # Number of matches to return\n",
    "        }\n",
    "    ).execute()\n",
    "    \n",
    "    # Process and format results\n",
    "    results = []\n",
    "    for result in response.data:\n",
    "        results.append({\n",
    "            'id': result['id'],\n",
    "            'similarity': result['similarity'],\n",
    "            'text': result['text']\n",
    "        })\n",
    "    \n",
    "    return results\n",
    "\n",
    "\n",
    "query = \"Do we get free Unicorn rides?\"\n",
    "retrieve(query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "User Query:\n",
      "Do we get free Unicorn rides?\n",
      "--------------------------------------------------------------------------------\n",
      "\n",
      "Retrieved Context:\n",
      "Document 2 (Similarity: 0.503)\n",
      "**1: Our Magical Culture**\n",
      "At Unicorn Enterprises, we take pride in our unique and enchanting company culture. We believe that creativity and innovation flourish best when people are happy and inspired. From our weekly \"Wear Your Favorite Mythical Creature Costume\" day on Fridays to our in-house unicorn petting zoo, we aim to infuse magic into every corner of our workplace. So, don't be surprised if you find a fairy tale book in the breakroom or a gnome guiding you to the restroom. Our culture is designed to spark your imagination and encourage collaboration among our magical team.\n",
      "\n",
      "Document 5 (Similarity: 0.494)\n",
      "**4: Enchanted Benefits**\n",
      "Our commitment to your well-being extends to our magical benefits package. You'll enjoy a treasure chest of perks, including unlimited unicorn rides, a bottomless cauldron of coffee and potions, and access to our company library filled with spellbinding books. We also offer competitive health and dental plans, ensuring your physical well-being is as robust as your magical spirit.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\n",
      "AI Response:\n",
      "Yes, you will enjoy unlimited unicorn rides as part of the magical benefits package.\n",
      "--------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "def search_and_chat(chat_prompt, k=2):\n",
    "    # Print the user query\n",
    "    print(\"\\nUser Query:\")\n",
    "    print(chat_prompt)\n",
    "    print(\"-\" * 80)\n",
    "    \n",
    "    # Perform the vector search using retrieve()\n",
    "    search_results = retrieve(chat_prompt, k)\n",
    "    \n",
    "    # Print the retrieved context\n",
    "    print(\"\\nRetrieved Context:\")\n",
    "    for result in search_results:\n",
    "        print(f\"Document {result['id']} (Similarity: {result['similarity']:.3f})\")\n",
    "        print(result['text'])\n",
    "        print()\n",
    "    print(\"-\" * 80)\n",
    "    \n",
    "    prompt_with_context = f\"\"\"Context: {search_results}\\n\\n\n",
    "    Answer the question: {chat_prompt}\"\"\"\n",
    "\n",
    "    # Create a list of messages for the chat\n",
    "    messages = [\n",
    "        {\"role\": \"system\", \"content\": \"Please answer the questions provided by the user. Use only the context provided to you, if you don't know the answer say \\\"I don't know\\\".\"},\n",
    "        {\"role\": \"user\", \"content\": prompt_with_context},\n",
    "    ]\n",
    "\n",
    "    # Get the model's response\n",
    "    response = client.chat.completions.create(\n",
    "        model=\"gpt-4o-mini\", # Using standard model since no MODEL constant defined\n",
    "        messages=messages\n",
    "    )\n",
    "\n",
    "    # Print the model's response\n",
    "    print(\"\\nAI Response:\")\n",
    "    print(response.choices[0].message.content)\n",
    "    print(\"-\" * 80)\n",
    "\n",
    "# Example usage\n",
    "search_and_chat(\"Do we get free Unicorn rides?\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
