diff --git a/pgml-cms/blog/semantic-search-in-postgres-in-15-minutes.md b/pgml-cms/blog/semantic-search-in-postgres-in-15-minutes.md index a31fcc086..ce4abb919 100644 --- a/pgml-cms/blog/semantic-search-in-postgres-in-15-minutes.md +++ b/pgml-cms/blog/semantic-search-in-postgres-in-15-minutes.md @@ -1,7 +1,6 @@ - --- description: >- - Learn how to implement semantic search in postgres with nothing but SQL. + Learn how to implement semantic search in PostgreSQL with nothing but SQL. featured: true tags: ["Engineering"] --- @@ -18,13 +17,13 @@ Silas Marvin June 15, 2024 -## What is and is not Semantic Search +## What is and is not semantic search Semantic search uses machine learning to understand the meaning of text by converting it into numerical vectors, allowing for more accurate and context-aware search results. It is not a complete replacement for full-text search. In many cases, full-text search can outperform semantic search. Specifically, if a user knows the exact phrase they want to match in a document, full-text search is faster and guaranteed to return the correct result, whereas semantic search is only likely to return the correct result. Full-text search and semantic search can be combined to create powerful and robust search systems. -Semantic search is not just for machine learning engineers. The system behind semantic search is relatively easy to implement, and thanks to new Postgres extensions like pgml and pgvector, it is readily available to SQL developers. Just as modern SQL developers are expected to be familiar with and capable of implementing full-text search, they will soon be expected to implement semantic search as well. +Semantic search is not just for machine learning engineers. The system behind semantic search is relatively easy to implement, and thanks to new Postgres extensions like _pgml_ and _pgvector_, it is readily available to SQL developers. Just as modern SQL developers are expected to be familiar with and capable of implementing full-text search, they will soon be expected to implement semantic search as well. ## Embeddings 101 @@ -34,10 +33,10 @@ Embeddings are vectors. Given some text and some embedding model, we can convert !!! generic -!!! code_block time="19.080 ms" +!!! code_block time="14.125 ms" ```postgresql -SELECT pgml.embed('mixedbread-ai/mxbai-embed-large-v1', 'test'); +SELECT pgml.embed('mixedbread-ai/mxbai-embed-large-v1', 'Generating embeddings in Postgres is fun!'); ``` !!! @@ -45,31 +44,31 @@ SELECT pgml.embed('mixedbread-ai/mxbai-embed-large-v1', 'test'); !!! results ```text -{0.100442916,0.19116051,0.031572577,0.4392428,-0.4684339,0.2671335,0.000183642,0.402029,0.52400684,0.9102567,0.3289586,0.13825876,0.060483586,-0.9315942,-0. 36723328,-0.34614784,-0.5203485,-0.0026301902,-0.4359727,-0.2078317,-0.3624561,0.151347,-1.0850769,0.03073607,-0.38463902,0.5746146,-0.065853976,-0.02959722, 0.3181093,0.60477436,-0.20975977,-0.112029605,0.32066098,-0.92783266,0.17003687,-0.6294021,0.94078255,-0.32587636,-0.06733026,-0.41903132,-0.29940385,-0.0473 7147,0.7173805,-0.4461215,-1.2713308,-0.44129986,-0.46632472,-0.89888424,-0.22231846,-0.34233224,0.09881798,0.17341912,0.27128124,-0.7020756,0.113429464,-0.2 2964618,-0.22798245,0.1105072,-0.8441625,1.238711,0.8674123,-0.14600402,0.391594,-0.9928256,0.24864249,-0.11477054,0.23513256,-0.366138,-0.13302355,-0.449127 64,-0.45309332,0.4775117,-0.19158679,-0.6634198,-0.21402365,0.2285473,0.09665201,0.47793895,-0.456355,0.33732873,0.2820914,0.17230554,0.14925064,0.23560016,- 1.2823433,-0.8188313,0.07958572,0.758208,0.39241728,-0.021326406,-0.0026611753,0.4960972,-0.5743201,-0.10779899,-0.53800243,0.11743014,0.17272875,-0.537756,0 .15774709,-0.024241826,0.75601554,0.5569049,-0.098995246,1.0593386,-0.90425104,0.3956237,0.024354521,-0.32476613,-0.5950871,-0.75371516,-0.31561607,0.0696320 8,0.6516349,0.5434117,-0.7673086,0.7324196,0.15175763,1.1354275,-0.56910944,-0.09738582,0.35705066,0.018214416,-0.091416046,-0.19074695,-0.34592274,-0.115972 71,-0.5033031,0.6735635,-0.05835747,-0.21572702,-0.58285874,0.095334634,0.8742985,0.6349386,0.4706169,-0.029405594,-0.50637966,0.4569466,0.2924249,-0.9321604 ,0.34013036,1.1258447,-0.28096777,1.2910426,0.32090122,0.5956652,0.22290495,0.08063537,-0.3783538,0.71436244,-0.90230185,-0.4399799,0.24639784,0.3069413,-0.4 8032463,0.27206388,-0.43469447,-0.2339563,0.12732148,0.22685277,-0.7924011,0.3359629,-0.30172998,0.43736732,-0.521733,1.324045,-0.28834093,-0.15974034,0.2684 1715,-0.33593872,0.73629487,-0.1049053,0.16749644,0.3264093,-0.101803474,0.22606595,1.2974273,0.22830595,0.39088526,0.4486965,-0.57037145,-0.09293561,-0.0394 99372,0.47220317,0.74698365,0.2392712,0.23049277,-0.52685314,-0.5007666,-0.03302433,-0.2098883,0.47145832,-0.6392486,0.58358306,-0.15019979,0.32308426,-0.506 2344,-0.16731891,-0.55598915,-1.7701503,-0.3798853,0.54786783,-0.71652645,-0.1773671,0.2289979,-1.0015582,0.5309544,0.81240565,-0.17937969,-0.3966378,0.60281 52,0.8962739,-0.176342,-0.010436469,0.02249392,0.09129296,-0.105494745,0.970157,-0.26875457,0.10241943,0.6148784,-0.35458624,0.5211534,0.61402124,0.48477444, -0.16437246,-0.28179103,1.2025942,-0.22813843,-0.09890138,0.043852188,1.0050704,-0.17958593,1.3325024,0.59157765,0.4212076,1.0721352,0.095619194,0.26288888,0 .42549643,0.2535346,0.35668525,0.82613224,0.30157906,-0.567903,0.32422608,-0.046756506,0.08393835,-0.31040233,0.7402205,0.7880251,0.5210311,1.0603857,0.41067 ,-0.3616352,-0.25297007,0.97518605,0.85333014,0.16857263,0.040858276,0.09388767,-0.19449936,0.38802677,0.164354,-0.017545663,0.15570965,-0.31904343,0.2223094 4,0.6248201,-0.5483591,-0.36983973,-0.38050756,-1.925645,-1.037537,-0.6157122,-0.53581315,0.2836596,-0.643354,0.07323658,-0.93136156,-0.20392789,-0.72027314, -0.33667037,0.91866046,0.23589604,0.9972664,-0.29671007,0.08811699,0.24376874,0.82748646,-0.604533,-0.67664343,-0.32924688,-0.37375167,0.33761302,-0.19614917 ,-0.21015668,0.46505967,-0.28253073,-1.0112746,1.1360632,0.8825793,-1.0680563,0.0655969,-1.034352,0.5267044,0.91949135,-0.031119794,0.60942674,0.54940313,-0. 3630888,0.44943437,0.66361815,0.073895305,-0.59853613,0.18480797,0.49640504,-0.13335773,-0.66213644,0.08816239,-0.52057326,-0.48232892,-0.2665552,-0.10339267 ,-0.30988455,0.46449667,-0.022207903,-1.6161236,0.27622652,-0.5909109,-1.0504522,0.052266315,-0.66712016,1.038967,-0.21038829,-0.30632204,-0.63056785,-0.0326 83205,0.8322449,0.43663988,0.8234027,-0.69451404,-0.29506078,0.8947272,0.36536238,-0.06769319,-0.21281374,0.1542073,-1.0177101,0.1798313,-0.38755146,0.353291 33,-0.1736927,0.2708998,0.36253256,0.55142975,-0.25388694,0.2749728,1.0570686,0.14571312,0.14165263,-0.18871498,0.2701316,0.6352345,-0.1975502,-1.0767723,-0. 0899109,0.06417123,0.16973273,-1.4618999,0.75780666,-0.37219298,0.34675384,-0.21044762,0.3230924,-0.59562063,0.57655936,-0.24317324,0.4706862,-1.0036217,0.27 595782,-0.18632303,-0.024258574,0.36281094,0.72106606,0.4534661,0.10037945,0.49504414,-0.9208432,-0.8387544,-0.17667033,0.44228357,0.36593667,-0.3061421,-1.2 638136,-1.1484053,0.5236616,0.020920075,0.2590868,-0.017210491,0.48833278,-0.34420222,0.35703135,1.0728164,-0.51129043,0.0902225,-0.42320332,0.19660714,-0.28 81061,-0.15664813,-0.99245757,0.06579208,-1.5574676,0.16405283,0.46488675,-0.15788008,-1.01791,0.84872925,0.035253655,0.40218765,-0.59924084,-0.2960986,-0.27 4478,-0.17835106,0.6479293,-0.42014578,-0.15515843,-0.62578845,0.2247606,1.153755,-0.033114456,-0.8774578,-0.021032173,-0.54359645,-1.0827168,-0.4298837,0.39 979023,-0.031404667,-0.25790605,-0.55900896,0.85690576,-0.23558116,-0.64585954,-0.18867598,-0.016098155,-0.021867584,0.5298315,0.65620464,-0.45029733,-1.0737 212,-0.25292996,-1.8820043,0.78425264,0.049297262,0.033368483,-0.13924618,-0.08540384,0.26575288,0.3641497,-0.5929729,0.012706397,-0.14115371,0.7092089,-0.29 87519,-0.50846523,1.1529989,-0.007935073,-0.39666718,0.66540664,-0.43792737,-0.14657505,0.013367422,0.59577924,-0.31825122,0.3546381,0.11212899,0.5804333,-0. 72722685,-0.58012086,-0.25618848,-0.3021478,0.3090123,0.39833462,-0.1964222,-1.0031091,-0.7377774,-0.37093028,-0.268894,-0.16332497,0.8644577,0.5592706,0.175 96069,-0.28468367,-0.11259709,-0.3321775,0.12905857,-0.4623798,-0.2466813,-0.39571014,0.8273027,0.3286372,-0.42084447,-0.6982525,0.51819134,-0.4211214,-0.450 2746,-0.58659184,0.9362978,-0.24028268,-0.07863556,0.03276802,0.31117234,-0.61217594,0.29426676,0.5394515,0.096639164,-0.17290285,-0.100368135,-1.1184332,0.6 5379685,0.21017732,-0.48588675,-0.42309326,0.78154176,0.11492857,0.9659768,0.85164833,-0.510996,-0.4957692,-1.0045063,0.41195333,-0.25961345,-0.06390433,-0.8 0765647,-0.5750627,-0.004215756,0.6570266,0.021791002,-0.2851547,0.33010367,-1.0438038,0.64198446,-0.3170377,-0.21503314,-0.7744095,0.34140953,-0.123576835,1 .2228137,0.3193732,0.097345576,0.013826016,0.490495,0.16021223,0.3592192,-0.64754117,-1.2467206,0.20728661,-0.040293045,0.18149051,-0.3889212,-1.2567233,-0.2 7512074,-0.8875311,-0.4562278,-0.14274459,-0.7154212,-0.9517362,-0.42942467,0.34255636,-0.25662944,-0.071650766,-0.2570997,0.97032154,0.55209476,0.9512633,-0 .78840256,-0.87641865,-0.31667447,-1.1845096,0.61095214,-0.4934745,-0.090470426,-0.8589016,0.16191132,1.3353379,0.36014295,0.6354017,1.6015769,-0.15028308,-0 .35953638,-0.46233898,0.7056889,0.44098303,-0.2561036,-0.38414526,-0.85254925,-0.35759622,0.32756907,-1.1055855,-0.9486638,-0.75697213,-0.18819816,0.91543293 ,0.046453375,0.8660134,-0.7937197,-0.72757536,-0.8235348,0.8263684,0.84975964,0.6188537,1.0370533,0.8713266,-0.17223643,0.74872315,-0.087729014,0.027644658,- 0.41663802,0.86366785,0.45966265,-0.7807239,0.72492,0.7516153,-1.4882706,-0.7965106,0.44769654,0.04745266,-0.3665682,-1.1761265,-0.16592331,-0.49482864,-0.18 829915,0.079323344,0.5283898,-0.25911674,0.49787715,0.040334962,0.6457638,-0.9161095,-0.52021873,0.3950836,-0.8869649,0.61957175,-0.8694589,-0.14945404,0.331 69168,0.2645687,0.45321828,-0.20752133,-0.00011348105,0.7114366,0.36253646,0.94113743,0.27327093,-0.279275,0.74158365,-0.7394054,-0.9920889,-0.5790354,0.4460 0168,0.6965152,0.055897847,-0.7247457,-0.23232944,1.0741904,-0.103388265,0.3405134,-0.6539511,-0.51377046,-0.7043648,-0.61793834,-0.7072252,-0.34909388,-0.05 701723,0.6294965,-0.30765653,0.03854165,0.032257613,0.8844775,-0.12016908,0.45807433,-0.8181472,0.5738447,-0.08459999,-0.5052286,-0.322389,0.16923045,-0.5340 384,0.82369304,-0.6654957,0.09066754,0.23323251,0.75676244,-0.07526736,0.18891658,-0.58411753,-0.5459881,0.31472534,0.22671345,0.15036865,0.5497431,0.6759999 4,-0.17044614,0.3315073,-0.07908476,0.3493545,-1.3477355,0.56133074,0.6158089,-0.15612105,-0.15391739,-1.6920619,-0.45604506,-0.9460573,0.1832847,-0.9812012, -0.037437357,0.23665366,0.20942298,0.12745716,0.3055677,0.4899028,0.1521983,-0.4412764,0.44380093,-0.24363151,0.049277242,-0.03479184,0.34719813,0.34336445,0 .44446415,-0.2509871,-0.07174216,0.16965394,0.40415984,-0.50963897,-0.4655299,0.59960693,-0.3961361,0.17242691,0.71643007,-0.012265541,0.07691683,1.2442924,0 .22043933,-1.2103993,0.61401594,-0.541842,-0.33357695,0.3074923,0.065326504,-0.27286193,0.6154859,-0.69564784,-0.11709106,-0.1545567,-0.11896704,-0.007217975 ,0.23488984,0.5601741,0.4612949,-0.28685024,-0.01752333,0.09766184,1.3614978,-0.9316589,-0.62082577,-0.17708167,-0.14922938,0.6017379,0.20790131,-0.17358595, 0.51986843,-0.8632079,-0.23630512,0.5615771,0.12942453,-0.55579686,-0.28877118,-0.023886856,0.6346819,0.11919484,0.112735756,-0.2105418,-1.0274605,-0.2215069 7,0.6296189,0.528352,-0.27940798,0.5474754,0.14160539,0.38373166,0.5457794,-0.7958526,-0.53057015,1.2145486,0.12005539,0.9229809,0.11178251,0.35618028,0.8680 126,-0.14047255,-0.022312704,0.6335968,0.22576317,0.63063693,0.077043116,-0.3592758,0.14797379,0.37010187,-0.14920035,-0.303325,-0.68384075,-0.22196415,-0.48 251563,0.085435614,1.0682561,-0.28910154,0.0547357,-0.49188855,0.07103363,0.23165464,0.7919816,-0.31917652,-0.11256474,0.22344519,0.202349,-0.042141877,0.487 33395,-0.6330437,0.18770827,-0.8534354,0.24361372,0.05912281,-0.14594407,-0.3065622,-0.13557081,-1.4080516,0.60802686,0.7874556,-0.8090863,0.5354539,-0.86377 89,-0.2529881,-0.76151496,0.39836842,-0.3637328,0.16363671,0.5599722,-0.24072857,0.09546083,0.831411,0.09562837,0.31388548,0.103111275,1.1427172,0.694476,0.9 3155265,0.64801776,-0.33954978,-0.0988641,0.473648,-0.2811673,-0.3996959,-0.33468047,-0.21153395,0.886874,-0.8678805,-0.10753187,-0.19310957,0.4603335,-0.122 70494,-1.0267254,-0.53114897,0.004987782,-0.7938769,0.40439928,0.4829653,1.5288875,0.6414294,-0.6214873,-0.65656304,0.47653323,0.16301247,-0.12008583,1.03255 62,0.13527338,-0.927417,-0.35502926,-0.17070319,-0.0011159402,0.15795147,-0.3817831,-0.99539477,0.44974712,0.623257,0.032141224,0.20115706,-0.753747,-0.03541 0736,0.317427,0.7414546,-0.41621342,1.4412553,0.088434115,-0.29406205,0.019276256,-0.66831887,0.39378297,-0.15091878,-0.33501017,0.012463322,0.26902023,-0.85 676277,-0.08205583,-0.13279751,0.8540507,-0.07071759,0.67416996,-1.0808998,-0.7537985,-1.1090854,-0.42881688,-0.545489,1.0022873,-0.34716064,-0.3511107,0.611 6534,-1.0079868,3.7511525,0.4171535,0.504542,-0.051603127,-0.071831375,0.44832432,-0.21127303,-0.57512856,-0.19024895,0.23094098,0.16914046,0.21540225,-0.077 53263,0.19773084,0.8750281,0.55822086,-0.46648705,-0.44413725,0.23833762,-0.6311006,-0.5150255,0.014071045,-0.043874096,0.40925947,-0.082470596,0.4262907,1.2 440436,-0.123832524,-0.09172271,-0.42539525,1.0193819,-0.20638897,-0.055872787,-0.12540375,-0.058966316,0.73125196,0.3050278,0.25579217,0.118471175,-0.148029 91,-0.33583203,0.11730125,1.5576597,-0.17712794,-0.2750745,0.11848973,-0.48632467,0.8594597,0.21705948,-0.04919338,0.8793258,-0.6851242,1.2830902,-0.226695,- 1.6696168,-0.4619705,-0.080957085,-0.53974324,-0.77588433,0.103437446,0.015129212,0.2896572,-0.28889287,-0.266523,-0.5023567,-0.0604841,0.57056016,0.5261334, -0.18631883,-0.5122663,-0.055830136,0.56574637,-0.5704402,-0.4263674,0.24019304,0.082071595,-0.31298077,0.30196336,-0.011113114,-0.5608543,0.3951217,-0.26592 582,0.41811758,-0.7411703,0.30873746,0.5664615,-0.98191136,-0.49090472,-1.0648257,0.97027993,0.9559882,-0.019431114,-0.07921166,-0.120092966,-0.13082835} +{-0.12269165,0.79433846,0.1909454,-0.8607215,-0.5526149,-0.48317516,0.48356333,0.40197256,0.6542712,0.20637313,0.68719935,-0.11798598,0.3924242,-0.3669872,-0.37829298,-0.57285887,-0.42399693,-0.57672346,-0.5584913,-0.25157344,-0.26103315,0.8435066,-1.3652948,-0.060239665,0.053472117,0.61965233,0.70429814,0.21168475,2.1243148,0.54657197,0.44898787,0.5141667,0.25056657,-0.7296713,-0.21511579,-0.26193422,0.18050511,0.42497447,0.10701023,-0.47321296,0.88108975,-0.23380123,0.097806804,-0.7617625,-1.7238936,0.0734859,0.5393925,0.08824284,0.6490631,-0.6999467,-0.04020539,0.34580526,-0.22457539,-0.1596002,0.30769205,0.10054478,-0.21030527,-0.6795052,-0.49133295,0.64051557,0.729387,-0.28649548,0.6304755,-1.2938358,0.18542609,-0.1447736,0.26269862,-0.7243509,-0.3743654,0.32034853,-0.033665977,-0.101480104,-0.40238166,-0.13823868,-0.08293891,0.18822464,0.614725,-0.51620704,-0.9493647,0.34618157,-0.045119785,0.5292574,0.24998534,0.50182945,-0.66819376,-0.69498116,1.0365546,0.7618454,0.22734495,-0.3371644,0.18830177,0.65933335,0.90198004,0.62203044,-0.18297921,0.80193377,-0.3250604,0.7243765,0.42883193,0.21042423,-0.01517533,0.5617572,-0.1593908,0.25845265,-0.07747603,0.4637758,0.3156056,-0.8067281,0.20704024,0.26316988,0.26273122,-0.32277155,0.16489738,-0.025123874,-0.8421937,0.42238364,-0.20360216,0.7395353,-0.28297424,-0.58514386,-1.1276962,-0.57587785,0.7367427,-1.183229,-0.17403314,-1.3642671,0.06204233,0.83101535,-0.8367251,0.4434241,0.13569412,-0.5018109,-0.24702606,0.2925449,-0.30402657,0.30018607,-0.8272239,0.7552851,0.71613544,-0.5800097,0.4300131,-0.3769249,0.15121885,1.4300121,-0.70190847,-0.014502372,1.1501042,-0.91252214,-1.299539,1.5988679,0.29511172,-0.3301541,0.10612632,0.48639655,-0.67100185,-0.18592787,-0.0610746,-0.40246755,0.34081936,0.26820442,-0.1269026,-0.02156586,0.10375944,0.6626627,-0.18523005,0.96837664,-0.5868682,0.081125714,-0.62061644,-1.010315,-0.18992952,-0.034805447,0.3482115,0.10850326,0.7015801,1.181063,0.51085556,-0.3421162,1.1605215,0.34367874,-0.45851547,-0.23464307,0.22397688,0.5295375,-0.067920305,0.38869885,-0.764097,0.08183036,-0.74270236,0.1314034,-0.09241337,0.7889378,-0.4487391,0.2671574,-0.057286393,0.23383318,-0.64422816,0.31305853,-0.5284081,-0.8764228,-1.0072867,0.7426642,0.20632008,0.19519271,-0.20781143,-0.55022776,-0.7449971,0.8095787,-1.1823708,-0.12114787,0.7764435,-0.4102213,-0.5614735,-1.151166,0.453138,-0.124295816,-0.7787184,0.8213192,0.19523725,-0.3429081,-0.5960741,0.05939262,0.6634549,-0.10354193,-0.16674386,0.23894079,0.5281129,0.4417929,-0.052335966,0.26073328,-0.5175538,0.43219882,0.42117482,0.9145017,0.62297195,0.5059562,1.0199716,0.33026397,0.10540544,1.4194826,0.2387192,-0.24473047,-0.12635238,0.38584706,0.06950318,0.13178644,0.4950382,0.58716995,-0.22241667,0.28335956,-1.4205463,-0.37189013,-0.006335424,0.674547,-0.35189858,-0.06895771,0.33660728,0.6581518,-0.5726849,0.20706958,-0.63431185,0.55616635,-0.3150213,0.18246625,0.6179018,0.3199304,0.1705371,0.40476194,-0.49592853,-0.00519022,-0.98531955,-0.8100823,-0.58652925,0.10230886,-0.7235388,-0.6156084,0.2809807,-0.2967379,-0.3508671,-1.1141659,-0.22769807,0.08822136,-0.23333925,0.6282077,1.0215682,0.38222972,-1.1630126,0.4021485,-0.064744614,1.0170162,-0.6086199,0.32332307,0.3160495,0.37213752,0.23822482,-0.24534902,-0.35759526,0.16281769,0.20119011,-0.7505329,-0.53170776,0.52023965,0.34757367,-0.3365119,-1.090554,0.74303913,0.7576997,0.1850476,0.38377324,0.6341742,0.0035892723,0.17847057,-0.52225345,0.4744198,-0.7825479,0.85714924,1.2160783,0.05176344,-0.34153363,-0.9228027,-0.45701292,-0.31697652,0.18669243,-0.080539,-0.97618884,0.44975403,0.12266389,-1.5476696,0.10114262,0.2652986,-0.6647504,-0.11139665,0.09672374,0.3067969,0.124992974,-0.075039916,-0.945483,-0.08019136,0.33150327,0.79691124,0.32509813,-0.7345915,0.49151382,0.8019188,0.054724086,0.3824057,0.54616,-1.338427,-0.17915602,0.29255223,-0.1312647,0.17714119,0.9686431,0.5271556,-0.09237713,-0.14801571,-0.8311881,0.4603313,1.173417,-0.17329413,1.1544656,1.2609864,0.6680077,-0.7116551,-0.26211533,-0.6321865,-0.4512319,0.30350694,0.7740681,-1.0377058,0.5507171,0.08685625,-0.4665991,1.0912793,-0.4253514,-1.3324647,0.6247509,0.17459206,0.64427835,-0.1543753,-0.4854082,0.42142552,0.41042453,0.80998975,-0.025750212,0.8487763,0.29716644,-0.8283788,-0.702183,-0.15909031,-0.4065299,1.064912,-0.25737965,-0.22743805,-1.1570827,0.17145145,0.38430393,0.82506144,0.46196732,-0.101009764,0.7100557,0.37232363,0.2594003,0.19210479,0.36719602,0.75960565,-0.65713775,0.23913959,0.692282,-0.41791838,0.47484493,0.17821907,-0.60062724,0.29957938,-0.11593854,0.32937768,-0.45972684,0.01129646,0.18534593,0.62680054,-0.028435916,0.251009,-0.71900076,0.44056803,0.16914998,-1.0019057,-0.55680645,0.059508275,0.20963086,0.06784629,0.07168728,-0.93063635,-0.045650747,-0.007684426,-0.7944553,0.79666996,0.9232027,-0.0643565,0.6617379,-1.1071137,0.35533053,-0.5851006,0.7480103,0.18149409,0.42977095,0.28515843,-0.29686522,0.9553224,0.7197761,-0.6413751,-0.17099445,-0.544606,0.06221392,-0.24136083,-0.5460586,-0.40875596,-0.057024892,-0.31573594,-0.01389576,-0.010156465,0.5784532,-0.44803303,0.38007888,-0.38199085,-0.43404552,0.91768897,-0.09181415,-0.44456294,0.28143787,0.6168798,-0.34374133,0.43424013,0.39190337,-0.56925493,0.8975914,-0.27520975,0.82481575,-0.16046512,-0.21151508,0.013323051,-0.60130703,0.19633308,-0.07837379,-0.16391036,-0.80348927,-1.6232564,-0.123514965,-0.15926442,-0.9025081,0.47055957,-0.078078784,-0.30613127,1.0725194,-0.5127652,-0.26803625,0.2473333,-0.43352637,0.26197925,0.47239286,0.3917152,0.13200012,-0.021115797,-1.3560157,-0.15067065,-0.23412828,0.24189733,-0.7706759,-0.3094795,-0.17276037,0.11040486,-1.122779,-0.8549858,-0.8815358,0.36725566,0.4391438,0.14913401,-0.044919793,-0.90855205,-1.2868156,0.86806804,0.013447602,-1.3518908,-1.0878333,1.1056291,-0.6054898,0.8732615,0.090048715,0.3439396,-0.43436176,-1.4296948,0.21427931,-0.56683505,-0.7287918,-0.66875815,-1.2414092,0.14564492,0.14575684,1.6843026,-0.7691825,-0.8857156,-0.59383214,0.1526336,-0.40446484,-0.093765385,-0.57902026,0.7115043,-0.2987314,1.4434578,-0.7507225,-0.14864576,0.09993563,0.3642726,0.39022216,1.4126799,-0.39582014,-0.46609184,-0.119693935,-0.7797329,0.8846008,-0.008525363,-1.1169624,0.28791374,-0.64548826,-0.14354923,-0.9195319,0.5042809,-0.64800096,-0.566263,0.31473473,-1.3200041,0.066968784,-1.2279652,0.6596321,-0.22676139,0.05292237,-0.44841886,-0.14407255,-1.1879731,-0.9624812,0.3520917,-0.8199045,-0.23614404,0.057054248,0.2774532,0.56673276,-0.68772894,0.8464806,1.0946864,0.7181479,-0.08149687,-0.033113156,-0.45337513,0.6593971,0.040748913,0.25708768,0.2444611,-0.6291184,0.2154976,-1.0344702,-0.57461023,-0.22907877,0.20212884,1.5542895,-0.69493115,0.76096123,-0.27198875,-0.28636566,-0.80702794,-0.09504783,0.5880213,0.52442694,0.88963073,-0.113876544,0.44108576,0.5131936,-0.51199615,-0.5373556,-0.50712276,0.7119059,0.26809675,-0.624161,0.50190353,0.45905492,-0.7560234,-0.36166972,-0.11057704,-0.93385667,0.14702824,-0.5007164,0.062319282,0.14635088,-0.60926783,0.44830725,0.5508014,-0.18144712,0.8553549,0.4763656,-0.06791675,-0.7282673,0.5312333,0.29696235,-0.32435995,0.11339427,-0.3156661,0.21376118,0.101174735,0.49239466,0.31915516,0.7523039,0.015413809,1.1970537,1.2595433,0.7877007,-0.77948576,-0.07308315,-0.005401653,-0.9297423,-0.6518283,-0.5235209,-0.08294889,-0.32686272,0.81800294,0.28346354,0.23243074,1.211297,0.5740814,-0.23115727,-1.0199192,-0.11423441,-1.2686234,-0.3610325,-0.13443044,-0.09186939,-0.46258482,-0.2746501,0.039179135,-0.6018465,-0.8123009,0.65863043,-1.4951158,0.04137505,-0.39956668,-0.21086998,-0.16921428,-0.12892427,-0.07058203,0.22937924,0.1872652,0.24946518,0.06469146,0.69964784,-0.14188632,0.57223684,0.26891342,-0.27864167,-0.5591145,-0.79737157,-1.0706135,-0.2231602,-1.108503,-0.34735858,-0.032272782,-0.38188872,0.32032675,0.6364613,-0.38768604,-1.1507906,-0.913829,0.36491016,0.25496644,-0.06781126,-0.84842575,0.0793298,0.0049917502,0.07099934,-0.5054571,-0.55416757,-0.4953387,0.47616813,0.13400371,1.3912268,0.30719018,-0.16337638,0.18637846,-0.19401097,0.71916217,-0.21031788,0.61066073,-0.43263736,-0.54376316,-0.36609605,0.30756727,0.3625213,0.30662173,-0.109407134,-0.26726124,-0.10782864,-0.5728887,0.35624364,0.23127197,1.0006613,-0.18430339,0.24659279,-0.1414664,-0.9362831,-0.14328903,-0.76222867,-1.6322204,-0.23277596,1.1940688,-0.5248364,0.6987823,0.36069974,-0.38930154,0.31739354,0.8688939,0.25019056,-0.45539424,0.5829257,-0.35556546,-0.23837212,-0.74019665,-0.49967116,0.20733729,0.18190496,-0.84233344,-0.9670267,0.29291785,0.18208896,0.26272357,0.076004505,0.16490388,0.23035681,-0.05491554,-0.35777965,-0.06495173,0.84074193,-0.06649489,0.5308439,-0.27389482,0.52712023,-0.70385605,1.582289,0.3533609,0.6537309,-0.11627128,1.1282475,-0.12714477,0.61138934,1.0615714,0.6239467,0.54578096,-0.56903726,-0.09996867,0.29148775,0.4719238,0.52982926,-0.122312695,-0.59448034,1.1922164,-0.102847695,0.015887707,-0.46900386,0.9373753,0.5174408,0.107704684,0.33192438,-0.73113894,-0.07725855,-0.21073207,-0.53892136,-0.41692436,0.04440565,-0.7362955,-0.18671799,-0.617404,0.11175289,-0.03757055,-0.9091465,-0.4772941,0.115955085,-0.109630615,0.27334505,-0.15329921,-0.40542892,0.6577188,-0.14270602,0.028438624,0.7158844,-0.04260146,0.14211391,0.36379516,-0.16956282,-0.32750866,0.7697329,-0.31624234,-0.81320703,-0.18005963,0.6081982,0.23052801,-0.20143141,0.24865282,-0.5117264,-0.64896625,-0.664304,0.4412688,-0.74262285,0.31758395,1.0110188,-0.0542792,-0.12961724,0.038787734,-0.019657299,0.3522628,0.88944745,0.7572078,0.4543937,0.31338966,2.1305785,0.11285806,0.9827753,0.4258123,0.46003717,0.01849649,-0.050423466,-0.7171815,-0.31475943,-0.48302308,-1.342478,0.017705658,0.3137204,0.43893284,-0.31969646,0.26008397,0.86090857,-0.9084142,0.47359383,1.2101759,0.25754166,0.071290456,-0.19756663,-0.07539108,-0.6719409,0.404817,-0.992041,0.48930237,0.83036274,-1.0315892,-0.06564829,0.00026013568,-0.43265438,-0.55953914,-0.06504767,-0.6801495,0.57494533,0.6398298,0.46862775,0.04649162,-0.70052904,-0.24009219,0.52453166,0.79875654,-0.09534484,0.82706153,0.96052814,0.1742728,0.057494655,-0.21722038,0.21895333,-0.15573184,0.5323167,-0.11215742,0.23329657,-0.566671,-0.7952302,0.31211463,0.40420142,0.32071197,-0.9692792,-0.27738753,0.35658348,-0.23604108,-0.5778135,-1.2452201,0.18487398,0.28343126,0.034852847,-0.42560938,-0.87293553,3.3916373,0.37104064,0.95921576,0.30020702,0.43176678,0.4746065,0.8066563,0.02344249,0.6768376,-1.243408,0.013419566,0.26038718,0.052325014,0.40021995,0.69684315,0.17993873,-0.6125471,0.39728552,0.1287264,-0.821042,-0.6356886,0.04368836,0.58837336,0.2951825,0.80620193,-0.55552566,-0.27555013,-0.86757773,-0.33467183,0.07901353,0.20590094,0.095205106,0.5052767,-0.3156328,-0.054386012,0.29206502,-0.26267004,-1.1437016,0.037064184,0.5587826,-0.23018162,-0.9855164,0.007280944,-0.5550629,-0.46999946,0.58497715,-0.1522534,0.4508725,0.37664524,-0.72747505,-0.52117777,-0.8577786,0.77468944,-1.2249953,-0.85298705,-0.8583468,-0.5801342,-0.817326,0.16878682,1.3681034,-0.6309237,0.42270342,-0.11961653,0.36134583,0.459141,0.24535258,0.21466772,-0.45898587,-0.20054409,-0.92821646,-0.05238323,0.17994325,0.82358634,-1.1087554,0.55523217,-0.29262337,-0.7871331,0.7758087,-0.2988389,-0.14875472,-0.731297,-0.46911976,-0.5939936,0.39334157,-0.2833826,0.64205635,-0.21212497,0.31960186,0.25826675,0.94142056,-0.15007028,0.7186352,-0.13642757,0.4422678,-0.106289506} ``` !!! !!! -Above we used the `pgml.embed` SQL function to generate an embedding of the word `test` using the `mixedbread-ai/mxbai-embed-large-v1` model. +We used the [pgml.embed](/docs/api/sql-extension/pgml.embed) PostresML function to generate an embedding of the sentence "Generating embeddings in Postgres is fun!" using the [mixedbread-ai/mxbai-embed-large-v1](https://huggingface.co/mixedbread-ai/mxbai-embed-large-v1) model from mixedbread.ai. -The output size of the vector varies per model. This specific model outputs vectors with 1024 dimensions. This means each vector contains 1024 floating point numbers. +The output size of the vector varies per model, and in _mxbai-embed-large-v1_ outputs vectors with 1024 dimensions: each vector contains 1024 floating point numbers. -The vector this model outputs is not random. It is designed to capture the semantic meaning of the text. What this really means, is that sentences that are closer together in meaning will be closer together in vector space. +The vector this model outputs is not random. It is designed to capture the semantic meaning of the text. What this really means, is that sentences which are closer together in meaning will be closer together in vector space. -Let’s look at a more simple example. Assume we have a model called `simple-embedding-model`, and it outputs vectors with 2 dimensions. Let’s embed the following three phrases: `I like Postgres`, `I like SQL`, `Rust is the best`. +Let’s look at a more simple example. Let's assume we have a model called _simple-embedding-model_, and it outputs vectors with only 2 dimensions. Let’s embed the following three phrases: "I like Postgres", "I like SQL" and "Rust is the best": !!! generic !!! code_block ```postgresql -SELECT pgml.embed('simple-embedding-model', 'I like Postgres') as embedding; +SELECT pgml.embed('simple-embedding-model', 'I like Postgres') AS embedding; -SELECT pgml.embed('simple-embedding-model', 'I like SQL') as embedding; +SELECT pgml.embed('simple-embedding-model', 'I like SQL') AS embedding; -SELECT pgml.embed('simple-embedding-model', 'Rust is the best') as embedding; +SELECT pgml.embed('simple-embedding-model', 'Rust is the best') AS embedding; ``` !!! @@ -94,34 +93,26 @@ embedding for 'Rust is the best' !!! -Notice how similar the vectors produced by the text `I like Postgres` and `I like SQL` are compared to `Rust is the best`. - -This is a simple example, but the same idea holds true when translating to real models like `mixedbread-ai/mxbai-embed-large-v1`. +You'll notice how similar the vectors produced by the text "I like Postgres" and "I like SQL" are compared to "Rust is the best". This is a artificial example, but the same idea holds true when translating to real models like _mixedbread-ai/mxbai-embed-large-v1_. -## What Does it Mean to be Close? +## What does it mean to be "close"? -We can use the idea that text that is more similar in meaning will be closer together in the vector space to perform search. +We can use the idea that text that is more similar in meaning will be closer together in the vector space to build our semantic search engine. For instance let’s say that we have the following documents: +| Document ID | Document text | +-----|----------| +| 1 | The pgml.transform function is a PostgreSQL function for calling LLMs in the database. | +| 2 | I think tomatos are incredible on burgers. | -!!! generic - -!!! code_block - -```text -Document1: The pgml.transform function is a PostgreSQL function for calling LLMs in the database. - -Document2: I think tomatos are incredible on burgers. -``` -!!! +and a user is looking for the answer to the question: "What is the pgml.transform function?". If we embed the search query and all of the documents using a model like _mixedbread-ai/mxbai-embed-large-v1_, we can compare the query embedding to all of the document embeddings, and select the document that has the closest embedding in vector space, and therefore in meaning, to the to the answer. -!!! +These are big embeddings, so we can’t simply estimate which one is closest. So, how do we actually measure the similarity (distance) between different vectors? -And a user is looking for the answer to the question: `What is the pgml.transform function?`. If we embed the user query and all of the documents using a model like `mixedbread-ai/mxbai-embed-large-v1`, we can compare the query embedding to all of the document embeddings, and select the document that has the closest embedding to the answer. +_pgvector_ as of this writing supports four different measurements of vector similarity: -These are big embeddings, and we can’t simply eyeball which one is the closest. How do we actually measure the similarity / distance between different vectors? There are four popular methods for measuring the distance between vectors available in PostgresML: - L2 distance - (negative) inner product - cosine distance @@ -131,9 +122,9 @@ For most use cases we recommend using the cosine distance as defined by the form
cosine similarity formula
-Where A and B are two vectors. +where A and B are two vectors. -This is a somewhat confusing formula but luckily for us pgvector provides an operator that computes the cosine distance for us. +This is a somewhat confusing formula but luckily _pgvector_ provides an operator that computes the cosine distance for us: !!! generic @@ -157,21 +148,34 @@ SELECT '[1,2,3]'::vector <=> '[2,3,4]'::vector; !!! -!!! note +Other distance functions have similar formulas and provide convenient operators to use as well. It may be worth testing other operators and to see which performs better for your use case. For more information on the other distance functions, take a look at our [Embeddings guide](https://postgresml.org/docs/guides/embeddings/vector-similarity). -The other distance functions have similar formulas and also provide convenient operators to use. It may be worth testing the other operators and seeing which performs better for your use case. For more information on the other distance functions see our guide on [embeddings](https://postgresml.org/docs/guides/embeddings/vector-similarity). - -!!! - -Back to our search example outlined above, we can compute the cosine distance between our query embedding and our documents. +Going back to our searchn example, we can compute the cosine distance between our query embedding and our documents: !!! generic !!! code_block ```postgresql -SELECT pgml.embed('mixedbread-ai/mxbai-embed-large-v1', 'What is the pgml.transform function?')::vector <=> pgml.embed('mixedbread-ai/mxbai-embed-large-v1', 'The pgml.transform function is a PostgreSQL function for calling LLMs in the database.')::vector as cosine_distance; -SELECT pgml.embed('mixedbread-ai/mxbai-embed-large-v1', 'What is the pgml.transform function?')::vector <=> pgml.embed('mixedbread-ai/mxbai-embed-large-v1', 'I think tomatos are incredible on burgers.')::vector as cosine_distance; +SELECT pgml.embed( + 'mixedbread-ai/mxbai-embed-large-v1', + 'What is the pgml.transform function?' +)::vector + <=> +pgml.embed( + 'mixedbread-ai/mxbai-embed-large-v1', + 'The pgml.transform function is a PostgreSQL function for calling LLMs in the database.' +)::vector AS cosine_distance; + +SELECT pgml.embed( + 'mixedbread-ai/mxbai-embed-large-v1', + 'What is the pgml.transform function?' +)::vector + <=> +pgml.embed( + 'mixedbread-ai/mxbai-embed-large-v1', + 'I think tomatos are incredible on burgers.' +)::vector AS cosine_distance; ``` !!! @@ -192,13 +196,13 @@ cosine_distance !!! -Notice that the cosine distance between `What is the pgml.transform function?` and `The pgml.transform function is a PostgreSQL function for calling LLMs in the database.` is much smaller than the cosine distance between `What is the pgml.transform function?` and `I think tomatos are incredible on burgers.`. +You'll notice that the distance between "What is the pgml.transform function?" and "The pgml.transform function is a PostgreSQL function for calling LLMs in the database." is much smaller than the cosine distance between "What is the pgml.transform function?" and "I think tomatos are incredible on burgers". -## Making it Fast! +## Making it fast! -It is inefficient to compute the embeddings for our documents for every search request. Instead, we want to embed our documents once, and search against our stored embeddings. +It is inefficient to compute embeddings for all the documents every time we search the dataset. Instead, we should embed our documents once and search against precomputed embeddings. -We can store our embedding vectors with the vector type given by pgvector. +_pgvector_ provides us with the `vector` data type for storing embeddings in regular PostgreSQL tables: !!! generic @@ -210,34 +214,52 @@ CREATE TABLE text_and_embeddings ( text text, embedding vector (1024) ); -INSERT INTO text_and_embeddings(text, embedding) + +INSERT INTO text_and_embeddings (text, embedding) VALUES - ('The pgml.transform function is a PostgreSQL function for calling LLMs in the database.', pgml.embed('mixedbread-ai/mxbai-embed-large-v1', 'The pgml.transform function is a PostgreSQL function for calling LLMs in the database.')), - ('I think tomatos are incredible on burgers.', pgml.embed('mixedbread-ai/mxbai-embed-large-v1', 'I think tomatos are incredible on burgers.')) -; + ( + 'The pgml.transform function is a PostgreSQL function for calling LLMs in the database.', + pgml.embed( + 'mixedbread-ai/mxbai-embed-large-v1', + 'The pgml.transform function is a PostgreSQL function for calling LLMs in the database.' + ) + ), + + ( + 'I think tomatos are incredible on burgers.', + pgml.embed( + 'mixedbread-ai/mxbai-embed-large-v1', + 'I think tomatos are incredible on burgers.' + ) + ); ``` !!! !!! -We can search this table using the following query: +Once our table has some data, we can search it using the following query: !!! generic !!! code_block time="19.864 ms" ```postgresql -WITH embedded_query AS ( +WITH query_embedding AS ( SELECT - pgml.embed('mixedbread-ai/mxbai-embed-large-v1', 'What is the pgml.transform function?', '{"prompt": "Represent this sentence for searching relevant passages: "}')::vector embedding + pgml.embed( + 'mixedbread-ai/mxbai-embed-large-v1', + 'What is the pgml.transform function?', + '{"prompt": "Represent this sentence for searching relevant passages: "}' + )::vector embedding ) SELECT text, ( SELECT - embedding - FROM embedded_query) <=> text_and_embeddings.embedding cosine_distance + embedding + FROM query_embedding + ) <=> text_and_embeddings.embedding cosine_distance FROM text_and_embeddings ORDER BY cosine_distance @@ -258,9 +280,9 @@ LIMIT 1; !!! -This query is fast for now, but as the table scales it will greatly slow down because we have not indexed the embedding column. +This query is fast for now, but as we add more data to the the table, it will slow down because we have not indexed the embedding column. -Let's insert 100,000 embeddings. +Let's demonstrate this by inserting 100,000 additional embeddings: !!! generic @@ -268,7 +290,12 @@ Let's insert 100,000 embeddings. ```postgresql INSERT INTO text_and_embeddings (text, embedding) -SELECT md5(random()::text), pgml.embed('mixedbread-ai/mxbai-embed-large-v1', md5(random()::text)) +SELECT + md5(random()::text), + pgml.embed( + 'mixedbread-ai/mxbai-embed-large-v1', + md5(random()::text) + ) FROM generate_series(1, 100000); ``` @@ -276,7 +303,7 @@ FROM generate_series(1, 100000); !!! -Now let's try our search again. +Now trying our search engine again: !!! generic @@ -313,27 +340,29 @@ LIMIT 1; !!! -This somewhat less than ideal performance can be fixed by indexing the embedding column. There are two types of indexes available in pgvector: IVFFlat and HNSW. +This somewhat less than ideal performance can be fixed by indexing the embedding column. There are two types of indexes available in _pgvector_: IVFFlat and HNSW. -IVFFlat indexes clusters the table into sublists, and when searching, only searches over a fixed number of the sublists. In the case above, if we were to add an IVFFlat index with 10 lists: +IVFFlat indexes clusters the table into sublists, and when searching, only searches over a fixed number of sublists. In oour example, if we were to add an IVFFlat index with 10 lists: !!! generic !!! code_block time="4989.398 ms" ```postgresql -CREATE INDEX ON text_and_embeddings USING ivfflat (embedding vector_cosine_ops) WITH (lists = 10); +CREATE INDEX ON text_and_embeddings +USING ivfflat (embedding vector_cosine_ops) +WITH (lists = 10); ``` !!! !!! -And search again: +and search again, we would get much better perfomance: !!! generic -!!! code_block time = "29.191" +!!! code_block time="29.191 ms" ```postgresql WITH embedded_query AS ( @@ -368,9 +397,9 @@ LIMIT 1; We can see it is a massive speedup because we are only searching over 1/10th of the original vectors! -HNSW indexes are a bit more complicated. It is essentially a graph with edges linked by proximity in the vector space. For more information you can check out this [writeup](https://www.pinecone.io/learn/series/faiss/hnsw/). +HNSW indexes are a bit more complicated. It is essentially a graph with edges linked by proximity in vector space. For more information you can check out this [write-up](https://www.pinecone.io/learn/series/faiss/hnsw/). -HNSW indexes typically have better and faster recall but require more compute when inserting. We recommend using HNSW indexes for most use cases. +HNSW indexes typically have better and faster recall but require more compute when adding new vectors. That being said, we recommend using HNSW indexes for most use cases. !!! generic @@ -379,14 +408,15 @@ HNSW indexes typically have better and faster recall but require more compute wh ```postgresql DROP index text_and_embeddings_embedding_idx; -CREATE INDEX ON text_and_embeddings USING hnsw (embedding vector_cosine_ops); +CREATE INDEX ON text_and_embeddings +USING hnsw (embedding vector_cosine_ops); ``` !!! !!! -Now let's try searching again. +Now let's try searching again: !!! generic @@ -395,14 +425,19 @@ Now let's try searching again. ```postgresql WITH embedded_query AS ( SELECT - pgml.embed('mixedbread-ai/mxbai-embed-large-v1', 'What is the pgml.transform function?', '{"prompt": "Represent this sentence for searching relevant passages: "}')::vector embedding + pgml.embed( + 'mixedbread-ai/mxbai-embed-large-v1', + 'What is the pgml.transform function?', + '{"prompt": "Represent this sentence for searching relevant passages: "}' + )::vector embedding ) SELECT text, ( SELECT - embedding - FROM embedded_query) <=> text_and_embeddings.embedding cosine_distance + embedding + FROM embedded_query + ) <=> text_and_embeddings.embedding cosine_distance FROM text_and_embeddings ORDER BY cosine_distance pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy