Case 1. Semantic search over text documents (no DB)
Implementation with TransformersPHP
In this case we build a minimal semantic search in PHP: documents are transformed into embeddings, saved into a simple JSON index, and then ranked by cosine similarity to the query. This demonstrates the core engineering cycle (indexing -> query embedding -> similarity search -> top-N results) without a database or vector store.
Documents and their content:
laravel.md
Laravel is used to build
modern web applications.
machine-learning.txt
Machine learning helps build models
that detect patterns in data
and improve search and recommendation quality.
php-ai.md
PHP is gradually becoming part of AI infrastructure.
Developers use PHP to build applications
with language models and intelligent features.
transformers.md
Transformers make it possible to build models
that understand text context and produce
semantic representations of data.
embeddings.json content:
File: embeddings.json
[
{
"id": "laravel.md",
"text": "Laravel используется для создания\nсовременных веб-приложений.",
"embedding": [
-0.40971067547798157,
-0.4850393533706665,
-0.17117373645305634,
-0.22870801389217377,
-0.23326469957828522,
-0.18515624105930328,
-0.47585374116897583,
0.03902299702167511,
0.08855549991130829,
0.2927566170692444,
0.0230246651917696,
0.4993246793746948,
0.41529250144958496,
0.00761334178969264,
0.3068586587905884,
0.1331215649843216,
-0.015800556167960167,
0.014076695777475834,
0.10331287235021591,
-0.03553016483783722,
-0.028532467782497406,
-0.10930192470550537,
-0.28067949414253235,
-0.027204135432839394,
0.18413642048835754,
-0.28656792640686035,
0.16963283717632294,
0.13550010323524475,
0.008107210509479046,
-0.3241754174232483,
0.39014509320259094,
0.020385265350341797,
-0.10760948061943054,
-0.020267285406589508,
-0.3388162851333618,
0.09377319365739822,
-0.2062847763299942,
-0.36919689178466797,
-0.34555110335350037,
-0.2435772567987442,
-0.47138547897338867,
-0.10526683181524277,
-0.006735246162861586,
0.17177702486515045,
0.03922387212514877,
-0.2696598470211029,
-0.18668435513973236,
0.04265028238296509,
-0.10048038512468338,
0.39937901496887207,
0.12892787158489227,
-0.47725990414619446,
-0.28196293115615845,
0.15106631815433502,
0.13816948235034943,
0.14320717751979828,
-0.012157829478383064,
0.3717527389526367,
-0.1715792864561081,
-0.08405905961990356,
0.30191728472709656,
-0.23639865219593048,
0.20389094948768616,
0.30148157477378845,
-0.344173401594162,
0.2356669306755066,
0.047037187963724136,
-0.22320377826690674,
0.09841075539588928,
-0.4065189063549042,
-0.7845395803451538,
-0.35730844736099243,
-0.09564441442489624,
-0.032204631716012955,
0.010646962560713291,
-0.020488880574703217,
0.07394463568925858,
0.4673950672149658,
0.2746923565864563,
-0.05875521898269653,
0.494578093290329,
0.16082347929477692,
0.24124197661876678,
0.22971563041210175,
0.24790872633457184,
0.007428489625453949,
-0.15345148742198944,
-0.12063829600811005,
-0.10775556415319443,
-0.008260439150035381,
-0.038709551095962524,
0.21815405786037445,
0.07762842625379562,
-0.03187134489417076,
-0.11882966756820679,
-0.03626090660691261,
0.05455166846513748,
0.41665467619895935,
0.33981242775917053,
0.06636170297861099,
-0.22585110366344452,
0.07269502431154251,
0.02293977327644825,
-0.2701854407787323,
0.14191173017024994,
0.03118213079869747,
0.27933892607688904,
0.34144628047943115,
0.15904876589775085,
0.286647230386734,
0.24118979275226593,
-0.028180746361613274,
-0.04845191538333893,
-0.1133069396018982,
0.1843053251504898,
-0.08545186370611191,
-0.2987278997898102,
-0.1720791906118393,
-0.20317593216896057,
0.2917333245277405,
-0.07455750554800034,
0.12681572139263153,
0.10643833875656128,
-0.08635859191417694,
0.20603056252002716,
-0.21716827154159546,
0.22813735902309418,
0.10680974274873734,
0.015713103115558624,
0.21496808528900146,
0.08769802004098892,
-0.14015573263168335,
-0.23181132972240448,
-0.3540875017642975,
0.048398371785879135,
-0.16666491329669952,
-0.38066408038139343,
-0.3665935695171356,
-0.06882045418024063,
0.2690936028957367,
-0.2403956949710846,
0.2561172544956207,
0.46524202823638916,
-0.08190225809812546,
0.015541342087090015,
-0.13825693726539612,
0.41601163148880005,
-0.2431211769580841,
0.10444619506597519,
-0.2055814266204834,
0.0035641102585941553,
-0.013008448295295238,
0.033802445977926254,
0.1775645911693573,
0.45544764399528503,
0.22516028583049774,
-0.45273640751838684,
-0.1779676228761673,
0.2634236812591553,
-0.3095298707485199,
0.07298919558525085,
0.0003150163101963699,
0.11929429322481155,
0.0823688805103302,
-0.2373218834400177,
-0.21791547536849976,
-0.20376154780387878,
0.4279122054576874,
-0.15899035334587097,
0.13746275007724762,
-0.0978756994009018,
0.170104518532753,
-0.30762946605682373,
-0.06145705655217171,
0.2634982764720917,
0.29163020849227905,
0.25728338956832886,
-0.056818637996912,
-0.17352616786956787,
0.20117396116256714,
0.004068449139595032,
0.22303378582000732,
0.09120393544435501,
0.18792806565761566,
-0.029730722308158875,
-0.3309624493122101,
-0.022838901728391647,
0.1305960714817047,
0.10018495470285416,
-0.2432572990655899,
-0.3833889067173004,
-0.2340179681777954,
-0.2038394957780838,
-0.13973349332809448,
0.4958367645740509,
-0.14599332213401794,
0.048956140875816345,
0.12410542368888855,
0.09851229190826416,
0.23028792440891266,
-0.1954449564218521,
0.3412320613861084,
-0.14085562527179718,
-0.01089036650955677,
-0.1353209912776947,
0.1514194756746292,
-0.1558733433485031,
0.14318199455738068,
-0.26209163665771484,
-0.32141515612602234,
0.26773592829704285,
0.3150095045566559,
-0.14110396802425385,
0.12420696020126343,
0.043061040341854095,
-0.10852604359388351,
-0.36517369747161865,
-0.1858520656824112,
-0.31698986887931824,
0.40437039732933044,
-0.08348919451236725,
-0.24296718835830688,
0.25741446018218994,
-0.22066977620124817,
0.038735564798116684,
0.2950422763824463,
-0.2645411491394043,
0.5620144009590149,
0.21749597787857056,
-0.05275172367691994,
-0.10658726841211319,
-0.19940906763076782,
-0.11091041564941406,
0.48226645588874817,
0.29204675555229187,
-0.2998897135257721,
-0.0070288535207509995,
-0.30152738094329834,
0.11184697598218918,
-0.24791689217090607,
-0.05453670397400856,
-0.20503298938274384,
-0.23877263069152832,
-0.06689031422138214,
-0.0071290419436991215,
0.15562956035137177,
-0.35080474615097046,
-0.47605419158935547,
0.28752753138542175,
0.2710033059120178,
-0.08330894261598587,
-0.3867705762386322,
0.05270460247993469,
0.08565651625394821,
-0.059314314275979996,
0.2805802822113037,
-0.1754865199327469,
-0.27698802947998047,
0.23329845070838928,
0.28211328387260437,
-0.02845640666782856,
0.2764645218849182,
0.06854268908500671,
-0.02066253311932087,
0.44129863381385803,
-0.024075450375676155,
-0.5069231390953064,
0.16665171086788177,
-0.4251512587070465,
0.0829644650220871,
-0.18575125932693481,
0.294951468706131,
0.3219844102859497,
0.19159150123596191,
0.42160072922706604,
-0.024967791512608528,
0.25313907861709595,
-0.2741076350212097,
-0.12773281335830688,
0.1452418714761734,
-0.07531525939702988,
0.14545968174934387,
-0.6057812571525574,
-0.13414475321769714,
-0.4480648934841156,
-0.09581853449344635,
0.05961506813764572,
0.019955458119511604,
-0.33351561427116394,
0.12136194854974747,
0.037388578057289124,
0.13587620854377747,
-0.28042155504226685,
-0.0009071527165360749,
-0.18771077692508698,
-0.36064884066581726,
0.2339169681072235,
-0.21851077675819397,
0.13235469162464142,
-0.0862109512090683,
0.0020514808129519224,
-0.1331457495689392,
0.038232628256082535,
0.06963584572076797,
0.16641858220100403,
0.11227890104055405,
0.16744549572467804,
-0.08996515721082687,
0.16353179514408112,
0.09277454763650894,
-0.5186083912849426,
-0.11286193877458572,
-0.1981370449066162,
0.3936913311481476,
-0.07859796285629272,
0.16286465525627136,
-0.3482476770877838,
-0.10148853808641434,
-0.16594848036766052,
-0.30828651785850525,
-0.10323426127433777,
-0.10945887863636017,
-0.4472099542617798,
0.01590345986187458,
0.21659734845161438,
0.21303367614746094,
-0.1833048164844513,
0.5556674003601074,
-0.24388040602207184,
-0.12281755357980728,
-0.1293460726737976,
0.03198612108826637,
-0.1888839155435562,
0.0807214081287384,
0.41400906443595886,
0.09326005727052689,
0.5266225934028625,
0.08209428936243057,
-0.12283344566822052,
-0.23073478043079376,
-0.37879666686058044,
0.21042829751968384,
-0.07217330485582352,
-0.24832484126091003,
-0.15353338420391083,
-0.003662529168650508,
0.1307632029056549,
0.4902600944042206,
0.05827512964606285,
-0.10735204070806503,
0.03169899806380272,
0.47411292791366577,
-0.71307373046875,
-0.24408088624477386,
-0.01780964620411396,
0.21974924206733704,
0.12424274533987045,
0.17421171069145203,
-0.053413115441799164,
0.49040597677230835,
-0.1208701878786087,
0.07022683322429657,
0.417603462934494,
0.062093887478113174,
0.0063330549746751785,
0.2940008342266083,
0.022648828104138374,
-0.13052964210510254,
-0.16976654529571533,
0.15082678198814392,
0.03203475475311279,
-0.11749690771102905,
0.3776596188545227,
0.37420010566711426,
0.012389094568789005,
0.29091906547546387,
0.14823758602142334,
-0.505608320236206,
-0.18088510632514954,
0.5438819527626038,
-0.28679805994033813,
-0.15724579989910126,
0.6000832319259644,
-0.03547869250178337
]
},
{
"id": "machine-learning.txt",
"text": "Машинное обучение помогает строить модели,\nкоторые находят закономерности в данных\nи улучшают качество поиска и рекомендаций.",
"embedding": [
-0.41719067096710205,
-0.21071042120456696,
-0.1726614385843277,
-0.2914014756679535,
-0.1084723025560379,
0.1662265807390213,
-0.1764022707939148,
-0.008102288469672203,
-0.16323472559452057,
-0.12128476798534393,
-0.03321289271116257,
0.2667699158191681,
0.12474337965250015,
-0.22815334796905518,
-0.20025619864463806,
-0.0575876384973526,
0.007166453171521425,
0.3148920238018036,
-0.12992967665195465,
-0.25496476888656616,
-0.20463603734970093,
-0.16616566479206085,
-0.01672969199717045,
-0.03544355556368828,
-0.28913819789886475,
0.08082977682352066,
0.3400025963783264,
-0.08517013490200043,
0.20468929409980774,
-0.02837389148771763,
0.25956180691719055,
0.09143446385860443,
0.22497346997261047,
-0.002878668252378702,
-0.16439469158649445,
0.24705137312412262,
-0.20063506066799164,
0.17910520732402802,
0.10134327411651611,
-0.12278562039136887,
-0.04173813760280609,
-0.09972424805164337,
0.19512422382831573,
0.12269995361566544,
-0.19897869229316711,
-0.10097052901983261,
-0.3582198917865753,
0.11070886254310608,
-0.004221053794026375,
-0.20872953534126282,
-0.31311699748039246,
-0.5831272602081299,
0.006315828301012516,
-0.19492368400096893,
0.283607542514801,
0.12404490262269974,
0.16591082513332367,
-0.1815071702003479,
-0.27861636877059937,
-0.0995454341173172,
0.013373678550124168,
-0.11697092652320862,
0.24183928966522217,
0.19251306354999542,
0.32786205410957336,
0.27042925357818604,
-0.03858305886387825,
0.178300678730011,
0.20321129262447357,
0.07605535537004471,
-0.07596256583929062,
0.085565946996212,
-0.17880330979824066,
0.035719528794288635,
-0.228177011013031,
0.247501403093338,
0.06428810954093933,
-0.3309275507926941,
0.3549242913722992,
0.08535516262054443,
0.20726805925369263,
0.007754384074360132,
0.14722463488578796,
0.2998007535934448,
-0.05678325518965721,
-0.09176163375377655,
0.029343463480472565,
-0.3012451231479645,
0.09143833816051483,
0.15375752747058868,
0.03782300278544426,
0.46918073296546936,
-0.2728911340236664,
-0.14304408431053162,
-0.25963717699050903,
0.10088963061571121,
-0.13539953529834747,
-0.1817530244588852,
0.14895035326480865,
0.18114513158798218,
-0.28522297739982605,
-0.1362038105726242,
-0.1502300351858139,
-0.13300755620002747,
0.15383416414260864,
-0.14950749278068542,
-0.03116091527044773,
-0.20452381670475006,
-0.08254816383123398,
-0.1642797887325287,
0.046746496111154556,
0.11523177474737167,
-0.004691906273365021,
0.1077212542295456,
-0.06466035544872284,
0.022450944408774376,
0.10386925935745239,
0.0339614674448967,
-0.06860116124153137,
0.15513908863067627,
0.13051995635032654,
-0.08059107512235641,
0.23172686994075775,
0.07887236028909683,
0.4138686954975128,
-0.19986723363399506,
-0.3788604438304901,
-0.026937659829854965,
0.117331862449646,
-0.4166153073310852,
0.29298070073127747,
-0.11019612848758698,
0.2784464359283447,
0.18559661507606506,
0.07857222855091095,
-0.17734983563423157,
-0.16746369004249573,
-0.002359452424570918,
-0.2321060448884964,
0.08980628103017807,
0.02784542366862297,
0.4292677640914917,
0.23678043484687805,
0.029291590675711632,
-0.15375736355781555,
0.04962708428502083,
-0.17051823437213898,
-0.17668402194976807,
0.08218055963516235,
-0.37672579288482666,
0.21165871620178223,
-0.24863660335540771,
0.07822071015834808,
-0.13398581743240356,
0.18417136371135712,
0.2254953682422638,
0.2088874727487564,
-0.024366913363337517,
-0.2122959941625595,
-0.16654497385025024,
-0.28114593029022217,
0.09897834807634354,
-0.13588176667690277,
-0.1433718055486679,
-0.023109195753932,
-0.11313439160585403,
0.23407243192195892,
-0.14540807902812958,
0.45546427369117737,
0.04075872525572777,
0.2476692646741867,
-0.21185237169265747,
-0.4088905155658722,
-0.09429135918617249,
0.10132390260696411,
0.250923216342926,
-0.12079679220914841,
-0.04561231657862663,
0.20528976619243622,
-0.23841802775859833,
0.15631474554538727,
-0.2679246962070465,
0.23072996735572815,
0.0024591106921434402,
-0.05156084895133972,
0.03565499559044838,
-0.07538513094186783,
-0.013393586501479149,
-0.11722326278686523,
0.06334371864795685,
0.21337555348873138,
0.12960989773273468,
0.4066784083843231,
-0.1907288283109665,
0.12804310023784637,
-0.3104704022407532,
0.466739296913147,
-0.004921451210975647,
-0.1075386330485344,
0.020474664866924286,
-0.2037811577320099,
-0.18553172051906586,
-0.2680482864379883,
-0.283479243516922,
-0.24588802456855774,
0.014437255449593067,
0.13663533329963684,
0.035415031015872955,
0.12887579202651978,
0.20208632946014404,
-0.25953245162963867,
0.39885035157203674,
-0.13347968459129333,
-0.20924273133277893,
-0.10574083030223846,
-0.22383686900138855,
-0.09445876628160477,
-0.22693315148353577,
0.22770245373249054,
-0.02628977783024311,
-0.18265779316425323,
0.015589730814099312,
-0.17612691223621368,
0.022004934027791023,
-0.5129923224449158,
0.13813437521457672,
0.2706989347934723,
0.12296593189239502,
0.14424161612987518,
-0.1559276580810547,
-0.19605384767055511,
-0.10619354248046875,
0.10972951352596283,
0.23650285601615906,
-0.08567164093255997,
-0.1801961064338684,
0.035969749093055725,
-0.02335534431040287,
0.03654225915670395,
-0.05864301323890686,
-0.0797477662563324,
0.011683295480906963,
0.07015535980463028,
-0.17795409262180328,
-0.0950719490647316,
0.3026427626609802,
-0.32135501503944397,
-0.17501384019851685,
0.09794653952121735,
-0.09935368597507477,
-0.04931236058473587,
-0.27267199754714966,
0.16153055429458618,
-0.16325528919696808,
-0.00391458161175251,
-0.34431931376457214,
-0.08423719555139542,
0.2976517975330353,
-0.11139308661222458,
0.3331696093082428,
0.2697104215621948,
-0.22022795677185059,
0.20869915187358856,
0.27602487802505493,
-0.04128715768456459,
-0.05793795362114906,
-0.36269640922546387,
-0.2817317247390747,
-0.2641266882419586,
-0.05810245871543884,
0.07390104979276657,
0.31648629903793335,
0.21316863596439362,
0.25063151121139526,
0.05433419346809387,
0.2227461338043213,
-0.22373396158218384,
-0.01988280564546585,
0.11041068285703659,
-0.10776874423027039,
-0.024039704352617264,
0.027125272899866104,
-0.19267123937606812,
0.2153213620185852,
-0.2799861431121826,
-0.14601092040538788,
0.1823505461215973,
0.055888399481773376,
-0.2564294636249542,
-0.0573599673807621,
-0.061781492084264755,
-0.0859651118516922,
-0.12184968590736389,
-0.023173276335000992,
-0.009629241190850735,
0.010406781919300556,
0.23027324676513672,
0.10196633636951447,
0.1218603253364563,
-0.18896418809890747,
-0.040329404175281525,
0.16920790076255798,
0.05378038436174393,
0.13385018706321716,
-0.1260208934545517,
-0.07196622341871262,
0.16961464285850525,
0.1950104683637619,
-0.050513725727796555,
0.2081775963306427,
-0.0954165831208229,
-0.024273253977298737,
0.12909336388111115,
0.23172342777252197,
-0.05526453256607056,
-0.3950845003128052,
-0.10756676644086838,
0.11432807892560959,
-0.05005626380443573,
0.031022317707538605,
0.06086597964167595,
0.21910138428211212,
0.5275847911834717,
0.209954634308815,
-0.118577741086483,
-0.22924646735191345,
-0.21603867411613464,
0.260873407125473,
-0.07686027884483337,
-0.037456214427948,
0.05001378059387207,
0.06822440773248672,
-0.10369204729795456,
0.35313090682029724,
-0.029592646285891533,
0.2921326160430908,
0.21868018805980682,
0.24811924993991852,
-0.052454084157943726,
-0.06360316276550293,
0.1158403679728508,
-0.04919123649597168,
-0.40885335206985474,
0.1685253530740738,
0.14861135184764862,
-0.018400128930807114,
-0.1898849904537201,
0.34626927971839905,
-0.0010209853062406182,
-0.0184552650898695,
0.08261793851852417,
0.226775661110878,
0.26882436871528625,
-0.1056867465376854,
0.4512932300567627,
0.3179524838924408,
-0.2297619879245758,
0.0908081904053688,
-0.18883025646209717,
-0.10661463439464569,
0.0012527487706393003,
0.20956973731517792,
-0.010367306880652905,
-0.27350157499313354,
0.02457820624113083,
0.06593324989080429,
0.2166205495595932,
-0.4113916754722595,
0.04977602884173393,
-0.22812797129154205,
-0.07027945667505264,
-0.27445656061172485,
0.1342013031244278,
0.16176579892635345,
0.41833746433258057,
0.22593849897384644,
0.0726829245686531,
-0.10610358417034149,
0.3243841826915741,
0.22652019560337067,
-0.18305286765098572,
0.20258477330207825,
-0.10057109594345093,
-0.11351841688156128
]
},
{
"id": "php-ai.md",
"text": "PHP постепенно становится частью AI-инфраструктуры.\nРазработчики используют PHP для создания приложений\nс языковыми моделями и интеллектуальными функциями.",
"embedding": [
-0.08343562483787537,
-0.6788528561592102,
-0.009684866294264793,
-0.057727646082639694,
-0.13794401288032532,
0.13074974715709686,
-0.02139340713620186,
-0.07929274439811707,
0.19891728460788727,
0.5104467868804932,
0.13036149740219116,
0.4403701722621918,
0.17795155942440033,
0.22233588993549347,
0.16069422662258148,
0.18824182450771332,
-0.26609522104263306,
-0.1251007318496704,
-0.3827344477176666,
-0.15085166692733765,
0.058750852942466736,
-0.0058038923889398575,
0.27982640266418457,
-0.043990232050418854,
0.16890667378902435,
0.045928191393613815,
0.2504737675189972,
-0.14402396976947784,
0.24217306077480316,
0.14616575837135315,
0.22842521965503693,
0.2907588481903076,
-0.14990241825580597,
0.11957915872335434,
-0.4275616407394409,
0.3091815114021301,
-0.3993431031703949,
-0.28089597821235657,
-0.2869609296321869,
0.029112154617905617,
-0.11658693104982376,
-0.12983892858028412,
0.09166279435157776,
-0.012881135568022728,
0.21000882983207703,
0.10980301350355148,
-0.13267216086387634,
-0.14889782667160034,
-0.17219451069831848,
-0.10115400701761246,
0.1984819620847702,
-0.28942999243736267,
-0.1852763146162033,
0.16786730289459229,
0.19237037003040314,
0.20390987396240234,
0.08959595859050751,
-0.1039271205663681,
-0.19481922686100006,
-0.03702815622091293,
0.08246079087257385,
-0.23367749154567719,
0.4357103407382965,
0.3699149489402771,
-0.3757053315639496,
0.09860994666814804,
0.31672319769859314,
-0.11244750022888184,
-0.09148689359426498,
-0.4208962619304657,
-0.32202908396720886,
-0.14698649942874908,
-0.12306345999240875,
-0.07124541699886322,
-0.13687822222709656,
0.06923726946115494,
0.08350376784801483,
-0.04834812134504318,
0.4675767123699188,
0.4225856363773346,
0.24427665770053864,
0.12447953969240189,
0.5143826007843018,
0.34777429699897766,
-0.2243185192346573,
0.028111616149544716,
0.10176989436149597,
-0.08785474300384521,
-0.06138940900564194,
-0.12645848095417023,
-0.060548268258571625,
0.046271584928035736,
-0.16312798857688904,
-0.004837371874600649,
0.41968753933906555,
-0.22884754836559296,
0.031216464936733246,
0.12770649790763855,
0.12869538366794586,
0.09909836947917938,
-0.34594663977622986,
0.019393369555473328,
-0.08071719855070114,
-0.08727849274873734,
0.13402295112609863,
0.19840078055858612,
0.2941763401031494,
-0.16788741946220398,
-0.055936526507139206,
-0.12019021064043045,
0.056203458458185196,
-0.03284795209765434,
0.11376364529132843,
0.07613962143659592,
0.03801840543746948,
-0.1458331197500229,
-0.4817737936973572,
-0.3222391605377197,
0.012967446818947792,
0.37715888023376465,
0.0009330576285719872,
-0.06805768609046936,
0.006072803866118193,
0.22471769154071808,
0.1309206187725067,
0.1094418466091156,
-0.11168726533651352,
-0.185281902551651,
-0.07738441228866577,
-0.03826912119984627,
0.11984159797430038,
-0.09893441945314407,
0.123658187687397,
0.27056050300598145,
0.27001360058784485,
-0.421785444021225,
-0.32692328095436096,
-0.22419297695159912,
-0.3019472062587738,
0.047397226095199585,
0.09318487346172333,
0.39146196842193604,
0.12613806128501892,
-0.05203940346837044,
-0.02169884368777275,
0.1655750721693039,
0.023786189034581184,
-0.06044589355587959,
-0.22684811055660248,
0.09388196468353271,
0.26389679312705994,
-0.10024861991405487,
-0.12393061816692352,
0.12106934934854507,
0.17722350358963013,
0.04283343255519867,
0.32651734352111816,
-0.2525550127029419,
-0.18925131857395172,
-0.3221761882305145,
-0.16227227449417114,
-0.09978852421045303,
-0.36513033509254456,
-0.14865991473197937,
0.0299893319606781,
-0.016670528799295425,
-0.136736199259758,
0.03989057615399361,
0.09206093847751617,
0.3584708869457245,
0.01520921103656292,
0.01120991725474596,
-0.351491779088974,
-0.1095336526632309,
0.11704404652118683,
0.05443580448627472,
-0.1674560159444809,
-0.16052678227424622,
0.09708432853221893,
-0.13882069289684296,
0.11215656250715256,
0.07707101851701736,
0.35825279355049133,
0.12360553443431854,
-0.16348135471343994,
-0.22977088391780853,
0.033154863864183426,
0.16870024800300598,
-0.18789970874786377,
-0.07608292251825333,
-0.16712675988674164,
0.1418447047472,
0.277707040309906,
-0.10300711542367935,
0.025501159951090813,
-0.2873336672782898,
0.5613657236099243,
0.20898333191871643,
-0.1450185328722,
-0.11550171673297882,
-0.21370340883731842,
0.22285529971122742,
-0.22478577494621277,
0.06980002671480179,
-0.382585346698761,
0.08339972794055939,
-0.2412019670009613,
0.17459818720817566,
-0.3253079950809479,
0.08232296258211136,
-0.14074553549289703,
0.21971336007118225,
-0.3084874153137207,
-0.15327785909175873,
-0.15589314699172974,
-0.5484622716903687,
-0.3510808050632477,
-0.07179488241672516,
0.026492072269320488,
-0.08770885318517685,
-0.061293140053749084,
-0.10481841117143631,
-0.023022247478365898,
0.09999462962150574,
-0.17881183326244354,
-0.06572113186120987,
-0.42845574021339417,
0.20701119303703308,
-0.025124719366431236,
-0.1073000580072403,
0.0599207878112793,
-0.07597403973340988,
0.019566047936677933,
0.05419105663895607,
-0.04441441595554352,
0.2186247855424881,
0.044674500823020935,
0.045136455446481705,
0.11794130504131317,
0.062394119799137115,
0.21237778663635254,
0.19004598259925842,
0.1615813970565796,
0.02771833725273609,
0.013431776314973831,
0.15159061551094055,
-0.21899105608463287,
-0.5163456201553345,
0.05675904080271721,
0.2115928828716278,
-0.33870673179626465,
-0.03584520146250725,
0.03311006352305412,
-0.022596055641770363,
0.05300687998533249,
-0.21994809806346893,
-0.3091369867324829,
0.23630152642726898,
-0.024425527080893517,
0.28703027963638306,
-0.013552344404160976,
-0.0746857300400734,
0.4350196421146393,
0.4085598587989807,
0.38800615072250366,
-0.12013831734657288,
-0.30516549944877625,
-0.07250814884901047,
-0.12211558222770691,
-0.08068253099918365,
-0.3571772277355194,
0.17994458973407745,
0.22605499625205994,
-0.04762199893593788,
-0.07473407685756683,
0.040933676064014435,
0.32505735754966736,
-0.6054646372795105,
0.07235612720251083,
-0.14712902903556824,
0.2846355140209198,
-0.061061155050992966,
-0.1034357100725174,
0.03726030886173248,
-0.17769958078861237,
-0.16540645062923431,
0.3354615569114685,
0.24564780294895172,
-0.3683759272098541,
0.11929027736186981,
-0.47153541445732117,
-0.09607980400323868,
0.12281732261180878,
-0.3328752815723419,
0.0030921900179237127,
0.20303840935230255,
0.13630375266075134,
0.19349148869514465,
-0.1583154946565628,
-0.1131390854716301,
-0.14406731724739075,
0.07751783728599548,
0.15338918566703796,
-0.11257515102624893,
-0.21321183443069458,
-0.06355677545070648,
0.07437710464000702,
-0.1008080467581749,
-0.289795845746994,
-0.0026944829151034355,
0.04235881567001343,
-0.06619349867105484,
-0.13859014213085175,
0.08019573241472244,
-0.19773298501968384,
-0.10414978116750717,
-0.07571154087781906,
0.2584339678287506,
-0.2672462463378906,
0.03175308182835579,
-0.18711063265800476,
0.17919966578483582,
0.12980175018310547,
0.09702792018651962,
-0.10959101468324661,
0.0956263542175293,
-0.09916133433580399,
0.26083144545555115,
-0.08324386924505234,
-0.007883762009441853,
0.08343333005905151,
-0.04578745365142822,
0.107866570353508,
-0.039274685084819794,
0.30642154812812805,
0.2537986636161804,
0.13014540076255798,
0.030323976650834084,
0.015246259048581123,
-0.21273957192897797,
0.32618603110313416,
0.045559871941804886,
-0.5926967859268188,
0.08071546256542206,
-0.05879639834165573,
0.07758352905511856,
0.3309161961078644,
0.4682011008262634,
-0.02495233714580536,
0.014817826449871063,
0.1533190757036209,
0.29311344027519226,
-0.35755655169487,
0.021017538383603096,
0.13025803864002228,
-0.14484018087387085,
-0.2494497150182724,
-0.1411125212907791,
0.1397620141506195,
0.13580311834812164,
-0.11410421878099442,
0.009700694121420383,
0.12509562075138092,
-0.37906885147094727,
0.4276134669780731,
0.010049312375485897,
0.18559630215168,
-0.1531882882118225,
0.08457371592521667,
0.11736174672842026,
-0.06388913840055466,
0.17746679484844208,
-0.04678787663578987,
0.4342843294143677,
0.2229575514793396,
-0.036289721727371216,
0.09975477308034897,
-0.054499417543411255,
0.02304134890437126,
0.17774604260921478,
-0.3816559314727783,
0.33375608921051025,
0.285653680562973,
-0.40240582823753357
]
},
{
"id": "transformers.md",
"text": "Трансформеры позволяют создавать модели,\nкоторые понимают контекст текста и строят\nсемантические представления данных.",
"embedding": [
-0.4200153946876526,
-0.20608003437519073,
-0.13104304671287537,
-0.2604610025882721,
-0.2536422908306122,
-0.02012365497648716,
0.03859695792198181,
-0.01450788788497448,
-0.2552964389324188,
0.1774282306432724,
-0.05905825272202492,
-0.048631828278303146,
0.27536967396736145,
-0.2931104004383087,
-0.006522543262690306,
-0.09990675002336502,
0.10906382650136948,
0.2266838699579239,
-0.3597877323627472,
-0.3200390338897705,
0.08972921967506409,
-0.006736686918884516,
-0.1121247261762619,
-0.21722152829170227,
-0.19133567810058594,
0.29956793785095215,
-0.03647548332810402,
-0.10662734508514404,
0.13057704269886017,
-0.06231167912483215,
-0.06379177421331406,
0.307940274477005,
0.1595264971256256,
0.1484154760837555,
-0.2586863934993744,
0.18094797432422638,
-0.14252664148807526,
0.2318643182516098,
0.027743635699152946,
0.03089205175638199,
-0.03511177375912666,
0.1980254352092743,
-0.06114838272333145,
0.07455754280090332,
-0.20524835586547852,
-0.23175516724586487,
-0.2520796060562134,
-0.057163823395967484,
-0.20861709117889404,
-0.2581096589565277,
-0.2590135931968689,
-0.25843629240989685,
0.04068365693092346,
0.18766292929649353,
-0.10745590180158615,
0.39130932092666626,
0.15743374824523926,
0.041702330112457275,
-0.10053591430187225,
-0.46580755710601807,
-0.05348014459013939,
-0.10322678834199905,
0.10244246572256088,
0.3923357427120209,
0.08831796795129776,
0.19127331674098969,
-0.013114459812641144,
0.18924033641815186,
0.05861397087574005,
-0.1534292995929718,
-0.5190916657447815,
-0.02669944427907467,
0.06821934878826141,
-0.10334689170122147,
-0.046774011105298996,
-0.05329960212111473,
-0.1431030035018921,
0.00069862772943452,
0.24825690686702728,
-0.12349972128868103,
0.135357066988945,
0.12401492148637772,
0.11746953427791595,
0.23212333023548126,
0.021650664508342743,
0.017656607553362846,
-0.00558319129049778,
-0.19678433239459991,
-0.1713370978832245,
0.1105058491230011,
0.02127924934029579,
-0.054041266441345215,
0.20775683224201202,
0.16040846705436707,
-0.2702142000198364,
-0.06037024036049843,
-0.20780020952224731,
0.08415940403938293,
0.5942713618278503,
0.3315695822238922,
-0.02539019100368023,
0.12344086915254593,
0.17121608555316925,
0.08394701033830643,
0.13617098331451416,
-0.3340037167072296,
-0.042234934866428375,
-0.01872868463397026,
0.06554874777793884,
-0.14053067564964294,
0.13788415491580963,
0.18924196064472198,
-0.011858368292450905,
-0.13000266253948212,
-0.06383128464221954,
-0.17949306964874268,
0.02258380874991417,
-0.14403265714645386,
0.1863320767879486,
0.17995037138462067,
0.1434091031551361,
0.038362495601177216,
-0.19280944764614105,
0.15415135025978088,
0.03577401489019394,
-0.16315919160842896,
-0.33183008432388306,
-0.1494522988796234,
-0.027982231229543686,
-0.21878866851329803,
0.10292009264230728,
0.1334911733865738,
0.1987008899450302,
0.09009477496147156,
-0.19800631701946259,
-0.24137014150619507,
-0.16424934566020966,
-0.10871431976556778,
-0.06064796447753906,
0.16293106973171234,
0.07000099122524261,
0.32913336157798767,
0.16890278458595276,
0.09939277917146683,
-0.2852921783924103,
0.3012411892414093,
-0.18247199058532715,
-0.3010571599006653,
-0.1339268833398819,
0.034453026950359344,
0.16232803463935852,
0.14651522040367126,
-0.13585224747657776,
0.09220139682292938,
0.20068985223770142,
-0.07702609896659851,
-0.07474927604198456,
-0.26132699847221375,
-0.4289133548736572,
-0.10054835677146912,
0.0283996369689703,
0.22421695291996002,
-0.16527390480041504,
-0.12242884188890457,
0.12579087913036346,
-0.09985841810703278,
0.11525298655033112,
0.10433357208967209,
0.38925591111183167,
0.19549520313739777,
0.28454655408859253,
-0.21329928934574127,
-0.1070333942770958,
-0.23821966350078583,
0.18779443204402924,
0.27066442370414734,
-0.02017049305140972,
-0.09896109998226166,
0.2159968465566635,
-0.053653355687856674,
0.43867847323417664,
-0.21697913110256195,
0.015534122474491596,
0.147608682513237,
-0.2204422801733017,
-0.30482861399650574,
-0.0949026420712471,
0.1435173749923706,
-0.24234969913959503,
0.30537331104278564,
0.2310710996389389,
0.1894116848707199,
0.24122457206249237,
-0.07943862676620483,
-0.1528579741716385,
-0.45523008704185486,
0.3975929319858551,
0.2937304675579071,
-0.47267305850982666,
-0.22660571336746216,
-0.2507115602493286,
0.3342741131782532,
-0.1730806678533554,
-0.3387313485145569,
-0.28519901633262634,
-0.01626029796898365,
-0.1248670220375061,
0.2023238092660904,
-0.024703025817871094,
0.1372736096382141,
-0.09904059767723083,
0.23097200691699982,
-0.11955574154853821,
-0.3837045431137085,
-0.008980969898402691,
-0.13687579333782196,
-0.06200026720762253,
-0.1534782499074936,
-0.13111910223960876,
0.04549789056181908,
-0.10446479171514511,
-0.026916367933154106,
0.10877267271280289,
-0.14496438205242157,
-0.2992415726184845,
0.47275716066360474,
-0.289067804813385,
0.009642837569117546,
0.00022595992777496576,
-0.07297366112470627,
-0.09342152625322342,
-0.0432622916996479,
0.21697735786437988,
0.024350326508283615,
-0.13064171373844147,
-0.01481185294687748,
0.011645951308310032,
-0.22293347120285034,
-0.16741594672203064,
-0.18989992141723633,
0.3418892025947571,
-0.019365858286619186,
-0.13809505105018616,
-0.2063024491071701,
-0.041829776018857956,
0.08903803676366806,
-0.4111694395542145,
-0.3049190044403076,
0.16884924471378326,
0.2338239997625351,
-0.00058924057520926,
0.038171447813510895,
0.24646267294883728,
0.041545186191797256,
-0.2705078125,
0.12310157716274261,
-0.28155726194381714,
0.16442088782787323,
-0.21983011066913605,
0.08357919007539749,
0.1532200127840042,
-0.054856155067682266,
0.01580221951007843,
0.4360128343105316,
0.08329349756240845,
0.0326538160443306,
-0.19333313405513763,
-0.07775571197271347,
-0.05517618730664253,
0.06629238277673721,
-0.1078139916062355,
0.19942806661128998,
0.027238376438617706,
-0.0667204037308693,
0.20370790362358093,
0.10318279266357422,
0.17279985547065735,
-0.3796752393245697,
-0.1394818276166916,
-0.1207740530371666,
0.11580432951450348,
-0.3936024606227875,
0.066373810172081,
0.022344445809721947,
-0.029954325407743454,
-0.061925407499074936,
-0.03220473602414131,
0.1264064610004425,
-0.12808114290237427,
-0.17690657079219818,
-0.026002157479524612,
-0.28373345732688904,
-0.10164628177881241,
-0.2585029900074005,
0.14392124116420746,
-0.14033113420009613,
0.025464816018939018,
0.06993116438388824,
0.17798040807247162,
-0.1794339418411255,
-0.19927340745925903,
-0.110469751060009,
-0.17420193552970886,
0.10334213078022003,
0.07654841244220734,
-0.36109426617622375,
0.2684655487537384,
0.2118074595928192,
0.1870587319135666,
0.12992893159389496,
-0.23325535655021667,
0.022861970588564873,
-0.04265013337135315,
0.252792090177536,
-0.2045402079820633,
-0.2915451228618622,
-0.20487375557422638,
0.24408559501171112,
-0.2863543927669525,
-0.17084543406963348,
-0.18569280207157135,
0.04289575666189194,
0.15784652531147003,
0.0016249134205281734,
0.026813318952918053,
-0.015461426228284836,
-0.08949702978134155,
0.14066968858242035,
-0.07732905447483063,
0.001840264187194407,
-0.048466674983501434,
0.14780393242835999,
0.03474986180663109,
0.20613543689250946,
0.004812710918486118,
0.5358919501304626,
0.1658172607421875,
0.33810535073280334,
-0.08918821811676025,
0.21456372737884521,
0.48552364110946655,
0.17902526259422302,
-0.46386298537254333,
0.2726276218891144,
0.04355999082326889,
0.11352930963039398,
-0.06024544686079025,
0.462933748960495,
0.13126885890960693,
-0.1652868688106537,
0.1221337541937828,
0.39081627130508423,
0.2597801089286804,
0.08073138445615768,
0.12889231741428375,
-0.2481824904680252,
0.07795034348964691,
0.11659373342990875,
0.027458852156996727,
0.31528040766716003,
0.16310347616672516,
-0.03353795036673546,
-0.0012978948652744293,
-0.07820703834295273,
0.1860896795988083,
-0.05619204044342041,
0.10173989832401276,
-0.18734489381313324,
0.17157842218875885,
0.22416570782661438,
0.050335392355918884,
-0.11757266521453857,
0.3065244257450104,
0.09456104040145874,
0.03569773957133293,
-0.20303568243980408,
0.40790873765945435,
0.17619551718235016,
0.2882513403892517,
0.2481195330619812,
-0.24331045150756836,
0.5699385404586792,
-0.046807605773210526,
-0.235049769282341
]
}
]
Example of code:
<?php
use function Codewithkyrian\Transformers\Pipelines\pipeline;
$embedder = pipeline(
task: 'embeddings',
modelName: 'Xenova/paraphrase-multilingual-MiniLM-L12-v2'
);
$files = glob(__DIR__ . '/documents-en/*');
$documents = [];
foreach ($files as $file) {
$documentText = file_get_contents($file);
if ($documentText === false) {
continue;
}
$result = $embedder($documentText, normalize: true, pooling: 'mean');
$documentEmbedding = array_map(static fn ($v): float => (float) $v, $result[0]);
$documents[] = [
'id' => basename($file),
'text' => trim($documentText),
'embedding' => $documentEmbedding,
];
}
file_put_contents(__DIR__ . '/embeddings.json', json_encode($documents, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
function cosineSimilarity(array $a, array $b): float
{
$dot = 0.0;
$normA = 0.0;
$normB = 0.0;
$n = min(count($a), count($b));
for ($i = 0; $i < $n; $i++) {
$x = (float) $a[$i];
$y = (float) $b[$i];
$dot += $x * $y;
$normA += $x * $x;
$normB += $y * $y;
}
if ($normA <= 0.0 || $normB <= 0.0) {
return 0.0;
}
return $dot / (sqrt($normA) * sqrt($normB));
}