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-en.json content:

[
    {
        "id": "laravel.md",
        "text": "Laravel is used to build\nmodern web applications.",
        "embedding": [
            -0.37033358216285706,
            -0.46652400493621826,
            -0.3326549530029297,
            -0.18774469196796417,
            -0.30043330788612366,
            -0.21563872694969177,
            -0.49084827303886414,
            0.08537927269935608,
            0.05056249722838402,
            0.31977835297584534,
            -0.03479998558759689,
            0.4523378014564514,
            0.36973991990089417,
            0.013962987810373306,
            0.3741021454334259,
            0.17941772937774658,
            -0.04809941351413727,
            -0.02613629586994648,
            0.021748824045062065,
            -0.11050545424222946,
            -0.08685814589262009,
            -0.12554897367954254,
            -0.30327078700065613,
            -0.01952793262898922,
            0.13253818452358246,
            -0.18361106514930725,
            0.15083947777748108,
            0.16806206107139587,
            0.06092476844787598,
            -0.32321178913116455,
            0.3433237373828888,
            0.03749905899167061,
            0.03711754083633423,
            -0.03721301630139351,
            -0.2649605870246887,
            0.0842873603105545,
            -0.14011314511299133,
            -0.3726276755332947,
            -0.36362314224243164,
            -0.21967734396457672,
            -0.5726264715194702,
            -0.07588007301092148,
            0.04700228571891785,
            0.16133861243724823,
            0.080459825694561,
            -0.3244065046310425,
            -0.24962840974330902,
            0.09321364015340805,
            -0.05677751079201698,
            0.2763603627681732,
            0.05220410227775574,
            -0.4899885356426239,
            -0.28786811232566833,
            -0.025321736931800842,
            0.0945492684841156,
            0.09753835201263428,
            0.0132632777094841,
            0.39632514119148254,
            -0.2324288785457611,
            -0.15569964051246643,
            0.2643176019191742,
            -0.3147953152656555,
            0.25637713074684143,
            0.27025285363197327,
            -0.33880168199539185,
            0.12171073257923126,
            0.03541068732738495,
            -0.15169286727905273,
            0.09775688499212265,
            -0.37493428587913513,
            -0.7846778035163879,
            -0.29524868726730347,
            -0.13186924159526825,
            -0.0896221473813057,
            0.022143984213471413,
            0.029995659366250038,
            0.0974198579788208,
            0.4636126756668091,
            0.3379855453968048,
            0.03351187705993652,
            0.493393212556839,
            0.07764489203691483,
            0.2735965847969055,
            0.2993449568748474,
            0.20689508318901062,
            -0.0006224786047823727,
            -0.10368838161230087,
            -0.08741486072540283,
            -0.13537287712097168,
            -0.016248555853962898,
            -0.018610944971442223,
            0.19021053612232208,
            -0.03231305256485939,
            0.04575556889176369,
            -0.1012420505285263,
            -0.042863599956035614,
            0.09407922625541687,
            0.4427919387817383,
            0.16542933881282806,
            0.007786276284605265,
            -0.14091616868972778,
            -0.027888761833310127,
            0.08368919044733047,
            -0.17982690036296844,
            0.20309729874134064,
            0.12419885396957397,
            0.34226521849632263,
            0.3102644085884094,
            0.1804654747247696,
            0.1867396980524063,
            0.3099501430988312,
            0.0015783124836161733,
            -0.03936745598912239,
            -0.03723129630088806,
            0.2233041226863861,
            -0.09386484324932098,
            -0.3474904000759125,
            -0.21659298241138458,
            -0.32003605365753174,
            0.33211010694503784,
            -0.12936045229434967,
            0.050032082945108414,
            0.0810093805193901,
            -0.12758848071098328,
            0.223921000957489,
            -0.3048390746116638,
            0.20785664021968842,
            0.08845902979373932,
            0.05509305000305176,
            0.1295652687549591,
            0.12395398318767548,
            -0.09000668674707413,
            -0.1597244292497635,
            -0.376003235578537,
            0.12575998902320862,
            -0.1036844551563263,
            -0.32967808842658997,
            -0.36775171756744385,
            -0.12887892127037048,
            0.29581084847450256,
            -0.18160484731197357,
            0.31008633971214294,
            0.6019099354743958,
            -0.14285880327224731,
            0.012965531088411808,
            -0.1254866123199463,
            0.397487074136734,
            -0.2377920150756836,
            0.1228441596031189,
            -0.32567232847213745,
            0.00881581474095583,
            -0.03601540997624397,
            0.02711012028157711,
            0.206815704703331,
            0.5069355964660645,
            0.1758858561515808,
            -0.5015609264373779,
            -0.15658637881278992,
            0.22117553651332855,
            -0.3625197112560272,
            0.0890381708741188,
            0.14386242628097534,
            -0.022722791880369186,
            0.19669845700263977,
            -0.31017258763313293,
            -0.15514656901359558,
            -0.1752898097038269,
            0.5196915864944458,
            -0.16236136853694916,
            0.21266108751296997,
            -0.0013254008954390883,
            0.2367781549692154,
            -0.26779407262802124,
            -0.11069577187299728,
            0.3033474087715149,
            0.30118250846862793,
            0.14566339552402496,
            -0.09141995757818222,
            -0.1438848227262497,
            0.24691970646381378,
            0.0671493411064148,
            0.2825212776660919,
            0.17774802446365356,
            0.1296948343515396,
            0.01846633106470108,
            -0.3052142560482025,
            -0.05825105309486389,
            0.18800391256809235,
            0.027803504839539528,
            -0.3439631164073944,
            -0.3419971764087677,
            -0.23458071053028107,
            -0.2957276403903961,
            -0.1583116501569748,
            0.4957204759120941,
            -0.1799822747707367,
            0.11460988968610764,
            0.14291509985923767,
            0.034142330288887024,
            0.3101637065410614,
            -0.17354504764080048,
            0.31034231185913086,
            -0.10433836281299591,
            -0.010079745203256607,
            0.015226642601191998,
            0.1805173009634018,
            -0.21825052797794342,
            0.1235417053103447,
            -0.13064822554588318,
            -0.2543208599090576,
            0.21009238064289093,
            0.2518792152404785,
            -0.26632973551750183,
            0.12599904835224152,
            0.004908978473395109,
            -0.09848631918430328,
            -0.3190583288669586,
            -0.24034370481967926,
            -0.3524119555950165,
            0.423913836479187,
            0.011366651393473148,
            -0.24887949228286743,
            0.18407556414604187,
            -0.19869999587535858,
            0.04075340926647186,
            0.26936572790145874,
            -0.3445685803890228,
            0.5224525332450867,
            0.27878043055534363,
            -0.05642831698060036,
            -0.09123887121677399,
            -0.28685158491134644,
            -0.11668731272220612,
            0.45302748680114746,
            0.3073740005493164,
            -0.3208317756652832,
            0.007881958968937397,
            -0.2628985345363617,
            0.1587846577167511,
            -0.3579860329627991,
            -0.02113097347319126,
            -0.27634117007255554,
            -0.29064780473709106,
            -0.019878646358847618,
            -0.07235653698444366,
            0.22961129248142242,
            -0.43859991431236267,
            -0.6269044876098633,
            0.4239151179790497,
            0.21601304411888123,
            -0.20387212932109833,
            -0.42446959018707275,
            0.12100952863693237,
            0.2554318904876709,
            -0.12761877477169037,
            0.31701070070266724,
            -0.12224671989679337,
            -0.16766144335269928,
            0.29227539896965027,
            0.3156890273094177,
            0.0032881218940019608,
            0.2994457185268402,
            0.12272404879331589,
            -0.0386345311999321,
            0.4514995813369751,
            -0.07388485968112946,
            -0.5608351230621338,
            0.1372442990541458,
            -0.4745928645133972,
            0.10598865896463394,
            -0.24060268700122833,
            0.335906982421875,
            0.3127780854701996,
            0.24668726325035095,
            0.43093207478523254,
            -0.04982848837971687,
            0.19997993111610413,
            -0.31015509366989136,
            -0.20873846113681793,
            0.0959862768650055,
            -0.13734549283981323,
            0.1528327614068985,
            -0.6208135485649109,
            -0.17616847157478333,
            -0.41966867446899414,
            -0.20825116336345673,
            0.11711572855710983,
            -0.04237328842282295,
            -0.42761316895484924,
            0.1887967735528946,
            0.02091265469789505,
            0.17765076458454132,
            -0.3337383270263672,
            -0.051466163247823715,
            -0.2098025530576706,
            -0.3336694538593292,
            0.19078688323497772,
            -0.12696333229541779,
            0.0825057327747345,
            -0.050920743495225906,
            -0.07480727136135101,
            -0.19611020386219025,
            0.01610015332698822,
            0.11341995745897293,
            0.14189209043979645,
            0.1328478753566742,
            0.2015373706817627,
            -0.02041003108024597,
            0.12692099809646606,
            0.19287414848804474,
            -0.5497334003448486,
            -0.09733513742685318,
            -0.2814742922782898,
            0.4017796814441681,
            -0.017601631581783295,
            0.15609094500541687,
            -0.32444778084754944,
            -0.02805660478770733,
            -0.16996929049491882,
            -0.23744647204875946,
            -0.12863750755786896,
            -0.15267843008041382,
            -0.5675036907196045,
            -0.031515005975961685,
            0.09979165345430374,
            0.20743024349212646,
            -0.2003813087940216,
            0.5897457599639893,
            -0.24110278487205505,
            -0.1994236409664154,
            -0.15951213240623474,
            0.06233430653810501,
            -0.21529008448123932,
            0.18855410814285278,
            0.3663169741630554,
            0.15285533666610718,
            0.5610155463218689,
            0.057339899241924286,
            -0.08779483288526535,
            -0.252385675907135,
            -0.285332053899765,
            0.2578606903553009,
            -0.22658047080039978,
            -0.1842372715473175,
            -0.16686367988586426,
            0.06500426679849625,
            0.19796431064605713,
            0.510665774345398,
            0.09791475534439087,
            -0.09056595712900162,
            0.15894968807697296,
            0.5401940941810608,
            -0.8040503859519958,
            -0.24413847923278809,
            0.10529199242591858,
            0.2522907257080078,
            0.11875889450311661,
            0.23381707072257996,
            -0.09627069532871246,
            0.4963266849517822,
            -0.12151134759187698,
            -0.01520814374089241,
            0.42652133107185364,
            -0.027966968715190887,
            0.056382253766059875,
            0.318821519613266,
            0.017409691587090492,
            -0.1414400190114975,
            -0.24308297038078308,
            0.16323502361774445,
            0.006056798156350851,
            -0.08495866507291794,
            0.4248453974723816,
            0.391553670167923,
            0.10588763654232025,
            0.37389591336250305,
            0.09916182607412338,
            -0.5833578109741211,
            -0.07984542101621628,
            0.573759138584137,
            -0.3594571650028229,
            -0.11113623529672623,
            0.6381022930145264,
            0.03485475853085518
        ]
    },
    {
        "id": "machine-learning.txt",
        "text": "Machine learning helps build models\nthat detect patterns in data\nand improve search and recommendation quality.",
        "embedding": [
            -0.5629966855049133,
            -0.221602663397789,
            0.009386405348777771,
            -0.20168957114219666,
            -0.1591426134109497,
            0.15363529324531555,
            -0.10579442232847214,
            -0.010096987709403038,
            -0.11800454556941986,
            -0.1995270848274231,
            -0.05802559852600098,
            0.3061692714691162,
            0.022920355200767517,
            -0.20757271349430084,
            -0.2336738407611847,
            -0.11997029185295105,
            0.003847663290798664,
            0.39103731513023376,
            -0.051048390567302704,
            -0.25384676456451416,
            -0.15453790128231049,
            -0.19185595214366913,
            -0.17365095019340515,
            0.08250381052494049,
            -0.30305883288383484,
            0.13861608505249023,
            0.37550660967826843,
            -0.1050449013710022,
            0.18572182953357697,
            0.00832690391689539,
            0.21541756391525269,
            0.08458946645259857,
            0.2908341586589813,
            0.03877966105937958,
            -0.151374951004982,
            0.16109898686408997,
            -0.190956249833107,
            0.18179360032081604,
            0.21261188387870789,
            -0.028923051431775093,
            -0.0739675834774971,
            -0.18974845111370087,
            0.2770295739173889,
            0.20682668685913086,
            -0.15013642609119415,
            -0.07155375927686691,
            -0.3012692332267761,
            0.13724695146083832,
            0.06835193932056427,
            -0.35124948620796204,
            -0.2559238076210022,
            -0.5693178772926331,
            -0.0491948127746582,
            -0.1681138128042221,
            0.3739222586154938,
            0.05657224357128143,
            0.017977533861994743,
            -0.2407054901123047,
            -0.3384736478328705,
            -0.051446449011564255,
            0.03163020685315132,
            -0.1815306395292282,
            0.23947586119174957,
            0.12342940270900726,
            0.37263908982276917,
            0.3193211555480957,
            0.025042610242962837,
            0.21145661175251007,
            0.19422100484371185,
            0.10362713783979416,
            -0.04590177908539772,
            0.1586964726448059,
            -0.17881283164024353,
            0.028781628236174583,
            -0.31870973110198975,
            0.3916720151901245,
            0.09261532127857208,
            -0.490744411945343,
            0.29238465428352356,
            0.1042027398943901,
            0.1243833377957344,
            -0.06254429370164871,
            0.1757815182209015,
            0.3433718979358673,
            -0.02969677932560444,
            -0.10345502197742462,
            -0.04841594770550728,
            -0.3162868320941925,
            0.060068756341934204,
            0.03588380664587021,
            0.03679078072309494,
            0.4675089120864868,
            -0.3573817014694214,
            -0.09959051012992859,
            -0.25165605545043945,
            0.1721525490283966,
            -0.11958889663219452,
            -0.2720579206943512,
            0.12818947434425354,
            0.17469877004623413,
            -0.24273903667926788,
            -0.09903765469789505,
            -0.25038257241249084,
            -0.17975279688835144,
            0.22292867302894592,
            -0.11117913573980331,
            -0.032218895852565765,
            -0.13870298862457275,
            -0.07122726738452911,
            -0.17811907827854156,
            0.05504131317138672,
            0.034740645438432693,
            -0.001709277625195682,
            0.18099075555801392,
            -0.0541098527610302,
            -0.02628585323691368,
            0.24790385365486145,
            -0.003689378034323454,
            -0.10152930021286011,
            0.08608367294073105,
            0.1830286681652069,
            0.02221010811626911,
            0.2638441026210785,
            0.08544670790433884,
            0.35683730244636536,
            -0.17005082964897156,
            -0.3691869080066681,
            0.1069808378815651,
            0.05323774367570877,
            -0.49227434396743774,
            0.2098713219165802,
            -0.19194753468036652,
            0.27567335963249207,
            0.2611832916736603,
            0.0690542533993721,
            -0.1095048040151596,
            -0.17041200399398804,
            0.09077461063861847,
            -0.2793370485305786,
            -0.07064282149076462,
            0.0018781296676024795,
            0.4264315068721771,
            0.3722095191478729,
            0.04538271203637123,
            -0.16472288966178894,
            -0.08328355848789215,
            -0.13448207080364227,
            -0.19326741993427277,
            0.10061822831630707,
            -0.43104487657546997,
            0.19121325016021729,
            -0.29351601004600525,
            0.07361722737550735,
            -0.1388043761253357,
            0.1643507033586502,
            0.1899375170469284,
            0.14388629794120789,
            0.01870221458375454,
            -0.24108904600143433,
            -0.23553481698036194,
            -0.28154465556144714,
            0.10615161806344986,
            -0.16021816432476044,
            -0.22890692949295044,
            0.07477451115846634,
            -0.05573279410600662,
            0.26966986060142517,
            -0.27433130145072937,
            0.45251360535621643,
            -0.0762164443731308,
            0.166842982172966,
            -0.23582537472248077,
            -0.48112791776657104,
            0.0004957862547598779,
            0.082693912088871,
            0.23676790297031403,
            -0.11097273230552673,
            -0.18862086534500122,
            0.22228652238845825,
            -0.2200545221567154,
            0.04801717773079872,
            -0.22052016854286194,
            0.2503017783164978,
            0.046852193772792816,
            0.009679346345365047,
            0.10459509491920471,
            -0.1923433542251587,
            -0.03767039254307747,
            -0.06841050833463669,
            0.10304683446884155,
            0.25581619143486023,
            0.1617731750011444,
            0.3897489607334137,
            -0.24222546815872192,
            0.1295694261789322,
            -0.26576846837997437,
            0.38880831003189087,
            -0.153329998254776,
            -0.046923454850912094,
            -0.004308215342462063,
            -0.30757394433021545,
            -0.2909962236881256,
            -0.36319637298583984,
            -0.30916526913642883,
            -0.31399065256118774,
            0.03666413575410843,
            0.1750865876674652,
            0.02146955393254757,
            0.19685456156730652,
            0.22829358279705048,
            -0.24757793545722961,
            0.3903534710407257,
            -0.2242218554019928,
            -0.2132667750120163,
            0.029487935826182365,
            -0.18357205390930176,
            -0.08026931434869766,
            -0.2958744168281555,
            0.19207529723644257,
            -0.05279054865241051,
            -0.12331753969192505,
            -0.032096900045871735,
            -0.18363553285598755,
            0.05792371556162834,
            -0.5611134171485901,
            0.11515526473522186,
            0.3915771543979645,
            0.0898231714963913,
            0.16158097982406616,
            -0.1709124594926834,
            -0.2176334112882614,
            -0.094123974442482,
            0.09224342554807663,
            0.23893195390701294,
            -0.0681857317686081,
            -0.05880323424935341,
            -0.042546723037958145,
            0.02130616270005703,
            0.10724245756864548,
            -0.13876193761825562,
            -0.10349131375551224,
            -0.007120619527995586,
            0.07916964590549469,
            -0.27411022782325745,
            -0.08165474981069565,
            0.2831336259841919,
            -0.43301212787628174,
            -0.1336662769317627,
            0.0013011684641242027,
            -0.22607946395874023,
            0.019005469977855682,
            -0.3466031551361084,
            0.2814195454120636,
            -0.07969753444194794,
            0.11184901744127274,
            -0.35699716210365295,
            -0.05876163765788078,
            0.34598514437675476,
            0.0684249997138977,
            0.37389951944351196,
            0.27481943368911743,
            -0.2898086607456207,
            0.1843729317188263,
            0.18812359869480133,
            -0.020271020010113716,
            -0.019338324666023254,
            -0.3724002540111542,
            -0.35063645243644714,
            -0.30228564143180847,
            -0.09806274622678757,
            0.07722107321023941,
            0.4027124047279358,
            0.23782145977020264,
            0.20339712500572205,
            0.1084844321012497,
            0.23099730908870697,
            -0.14173488318920135,
            0.10769499838352203,
            0.12995292246341705,
            -0.03179531544446945,
            -0.00504688173532486,
            0.021051928400993347,
            -0.15549980103969574,
            0.26357904076576233,
            -0.24320146441459656,
            -0.12308763712644577,
            0.14008782804012299,
            0.14224983751773834,
            -0.1830918937921524,
            -0.11109694838523865,
            -0.061863042414188385,
            -0.04630360007286072,
            0.1429510861635208,
            0.0003488597576506436,
            -0.031104208901524544,
            0.05892975255846977,
            0.06175379082560539,
            0.24186718463897705,
            0.15954849123954773,
            -0.2111409306526184,
            -0.1599397361278534,
            0.23252122104167938,
            -0.0033506506588310003,
            0.2464085966348648,
            -0.10811729729175568,
            0.025421474128961563,
            0.2887844145298004,
            0.2495633363723755,
            -0.20382720232009888,
            0.24213442206382751,
            0.022283002734184265,
            0.1003815233707428,
            0.14191751182079315,
            0.1429697424173355,
            -0.010119432583451271,
            -0.3936193585395813,
            -0.10698149353265762,
            0.061870548874139786,
            -0.03398832306265831,
            0.06609990447759628,
            -0.018098605796694756,
            0.10490574687719345,
            0.7794854044914246,
            0.24042190611362457,
            -0.2682453989982605,
            -0.3584897816181183,
            -0.1388101875782013,
            0.2605900466442108,
            -0.21038365364074707,
            -0.09931236505508423,
            -0.10107708722352982,
            0.08474870771169662,
            -0.20917560160160065,
            0.27379119396209717,
            0.03877270966768265,
            0.28065699338912964,
            0.26499107480049133,
            0.1468534916639328,
            -0.07629819214344025,
            -0.04573754593729973,
            0.061969105154275894,
            -0.12123981863260269,
            -0.3002799451351166,
            0.17415012419223785,
            0.1676115095615387,
            0.08115722239017487,
            -0.23435242474079132,
            0.35889139771461487,
            0.04890598729252815,
            -0.050056345760822296,
            0.19182021915912628,
            0.3658101558685303,
            0.2763752043247223,
            -0.05470386520028114,
            0.5457322597503662,
            0.41616785526275635,
            -0.2656914293766022,
            0.0942726656794548,
            -0.19296933710575104,
            -0.09195534884929657,
            -0.04539575055241585,
            0.07524373382329941,
            0.030919115990400314,
            -0.31932613253593445,
            0.06505513191223145,
            0.014534300193190575,
            0.3067471385002136,
            -0.4114149808883667,
            -0.04944116994738579,
            -0.3618197441101074,
            -0.132491797208786,
            -0.36330899596214294,
            0.21430498361587524,
            0.09294554591178894,
            0.3924057185649872,
            0.17255884408950806,
            0.025419125333428383,
            -0.18260037899017334,
            0.37306302785873413,
            0.18434275686740875,
            -0.12748683989048004,
            0.21985794603824615,
            -0.21190334856510162,
            -0.10690052062273026
        ]
    },
    {
        "id": "php-ai.md",
        "text": "PHP is gradually becoming part of AI infrastructure.\nDevelopers use PHP to build applications\nwith language models and intelligent features.",
        "embedding": [
            -0.1030479148030281,
            -0.8104873895645142,
            -0.06902304291725159,
            -0.0988733097910881,
            -0.09162896871566772,
            0.21525560319423676,
            -0.05346231162548065,
            -0.05412030965089798,
            0.18397264182567596,
            0.5889009833335876,
            0.030199307948350906,
            0.4305153787136078,
            0.18689295649528503,
            0.3409609794616699,
            0.09483484923839569,
            0.2558214068412781,
            -0.23940344154834747,
            -0.2106475681066513,
            -0.37309518456459045,
            -0.10311855375766754,
            -0.030002400279045105,
            -0.02652042545378208,
            0.36693498492240906,
            -0.05843339115381241,
            0.25394633412361145,
            -0.00458022253587842,
            0.32099923491477966,
            -0.12992292642593384,
            0.1965271681547165,
            0.15174898505210876,
            0.21710029244422913,
            0.2866215109825134,
            -0.13616368174552917,
            0.11970560997724533,
            -0.3372712731361389,
            0.3828805387020111,
            -0.3613539934158325,
            -0.2872529923915863,
            -0.2819957733154297,
            0.036408066749572754,
            -0.14576482772827148,
            -0.25741443037986755,
            0.07919686287641525,
            -0.018119560554623604,
            0.2202560305595398,
            0.09712144732475281,
            -0.19846037030220032,
            -0.06606542319059372,
            -0.23456601798534393,
            -0.1109006255865097,
            0.2643255889415741,
            -0.35003426671028137,
            -0.17110027372837067,
            0.06977421790361404,
            0.13610070943832397,
            0.22137388586997986,
            0.07189197838306427,
            -0.09386124461889267,
            -0.23341242969036102,
            0.03414801135659218,
            0.10764658451080322,
            -0.18799200654029846,
            0.5430157780647278,
            0.2900431454181671,
            -0.43455180525779724,
            0.11060892045497894,
            0.3490932583808899,
            -0.11764809489250183,
            -0.0623478963971138,
            -0.5262750387191772,
            -0.2926337420940399,
            -0.1653849482536316,
            -0.12373114377260208,
            -0.17200273275375366,
            -0.20431403815746307,
            0.08873005956411362,
            0.13036076724529266,
            0.08820068091154099,
            0.5604996085166931,
            0.43445226550102234,
            0.18827851116657257,
            0.17862467467784882,
            0.5320486426353455,
            0.34393084049224854,
            -0.2563231885433197,
            0.021941324695944786,
            0.06423608213663101,
            -0.03348373994231224,
            -0.05663454160094261,
            -0.1889011412858963,
            0.04886138439178467,
            0.22547808289527893,
            -0.1925298273563385,
            -0.012894543819129467,
            0.4314559996128082,
            -0.2169860452413559,
            0.008192242123186588,
            0.07834815233945847,
            0.011063151061534882,
            0.19761887192726135,
            -0.4038625955581665,
            0.04220579192042351,
            -0.048847492784261703,
            -0.054385364055633545,
            0.17313921451568604,
            0.3502654731273651,
            0.31368544697761536,
            -0.14706340432167053,
            -0.010131102055311203,
            -0.12978611886501312,
            0.11234153062105179,
            0.014074968174099922,
            -0.015536321327090263,
            0.19726897776126862,
            0.05877930670976639,
            -0.131642147898674,
            -0.5910166501998901,
            -0.3106832206249237,
            -0.057558417320251465,
            0.4171296954154968,
            -0.016901593655347824,
            -0.09560688585042953,
            0.02041623927652836,
            0.16367782652378082,
            0.13245078921318054,
            0.10968708246946335,
            -0.13590207695960999,
            -0.2591647803783417,
            -0.005010109860450029,
            -0.12227590382099152,
            0.1393638402223587,
            -0.03880314528942108,
            0.13935276865959167,
            0.33532193303108215,
            0.2284204661846161,
            -0.38428109884262085,
            -0.36376672983169556,
            -0.310011088848114,
            -0.42654627561569214,
            -0.014864962548017502,
            0.11525597423315048,
            0.3199574947357178,
            0.1913813203573227,
            -0.14803794026374817,
            -0.025487078353762627,
            0.1635519564151764,
            0.04476572200655937,
            -0.04067724570631981,
            -0.27345743775367737,
            0.01444640476256609,
            0.3548438251018524,
            -0.11645011603832245,
            -0.03815878927707672,
            0.16901612281799316,
            0.2402835488319397,
            0.019147062674164772,
            0.38183292746543884,
            -0.25131553411483765,
            -0.15982450544834137,
            -0.32036149501800537,
            -0.18921875953674316,
            -0.06394506245851517,
            -0.38234463334083557,
            -0.15892156958580017,
            -0.07791218906641006,
            -0.14282198250293732,
            -0.18337604403495789,
            0.05100288614630699,
            0.16218188405036926,
            0.3557925224304199,
            0.005000139586627483,
            0.03521057590842247,
            -0.29813352227211,
            -0.13457347452640533,
            0.1875254511833191,
            0.07035259902477264,
            -0.1917078047990799,
            -0.20180575549602509,
            0.06523793935775757,
            -0.16604594886302948,
            0.05227762460708618,
            0.11194075644016266,
            0.42976975440979004,
            0.08157114684581757,
            -0.12036508321762085,
            -0.21148712933063507,
            0.005582895129919052,
            0.1745283454656601,
            -0.21686820685863495,
            -0.10225141793489456,
            -0.16459457576274872,
            0.08740545809268951,
            0.3705698549747467,
            -0.1101444661617279,
            0.04682910069823265,
            -0.28686341643333435,
            0.5568324327468872,
            0.2034505158662796,
            -0.11506563425064087,
            -0.13274534046649933,
            -0.2184530645608902,
            0.18417273461818695,
            -0.27620577812194824,
            0.07779548317193985,
            -0.40118080377578735,
            0.12950170040130615,
            -0.061657100915908813,
            0.20262643694877625,
            -0.4125658869743347,
            0.10398072749376297,
            -0.14476223289966583,
            0.2053854912519455,
            -0.2866629660129547,
            -0.09495801478624344,
            -0.11540379375219345,
            -0.6059826016426086,
            -0.3765602707862854,
            -0.007567862048745155,
            0.05465809255838394,
            -0.04881767928600311,
            -0.07993839681148529,
            -0.13612934947013855,
            0.03188997134566307,
            0.16069816052913666,
            -0.25085097551345825,
            -0.07683482021093369,
            -0.38166308403015137,
            0.28460440039634705,
            0.04375017434358597,
            -0.06056211516261101,
            0.13042959570884705,
            -0.18285349011421204,
            0.04242698848247528,
            0.12922245264053345,
            -0.09732823073863983,
            0.2735718786716461,
            -0.026251934468746185,
            0.016664808616042137,
            0.1756022721529007,
            0.05444801226258278,
            0.23738786578178406,
            0.1643649935722351,
            0.13858579099178314,
            0.09949281811714172,
            0.033504780381917953,
            0.12032339721918106,
            -0.23486822843551636,
            -0.5865503549575806,
            0.05564042180776596,
            0.1848856657743454,
            -0.49811461567878723,
            -0.16731935739517212,
            0.06350670009851456,
            0.054303545504808426,
            0.07418454438447952,
            -0.23990803956985474,
            -0.3667161166667938,
            0.2500030994415283,
            -0.008248072117567062,
            0.2934640645980835,
            -0.03240331634879112,
            -0.20456863939762115,
            0.527934730052948,
            0.44656404852867126,
            0.4046526849269867,
            -0.1504458487033844,
            -0.3722043037414551,
            -0.10855167359113693,
            -0.2174186110496521,
            -0.17419090867042542,
            -0.3492697477340698,
            0.23114840686321259,
            0.19257429242134094,
            0.054620321840047836,
            -0.09905162453651428,
            0.06277880072593689,
            0.32000765204429626,
            -0.5264943242073059,
            0.07093186676502228,
            -0.18785159289836884,
            0.2995108962059021,
            0.033272817730903625,
            -0.1441873461008072,
            0.09635826200246811,
            -0.1733369529247284,
            -0.19601812958717346,
            0.30351170897483826,
            0.14046204090118408,
            -0.33394336700439453,
            0.21907047927379608,
            -0.5313506722450256,
            -0.12829379737377167,
            0.15644286572933197,
            -0.2538778483867645,
            0.08856236934661865,
            0.18282195925712585,
            0.18903301656246185,
            0.25321531295776367,
            -0.14858977496623993,
            -0.12177774310112,
            -0.16482441127300262,
            0.11754938960075378,
            0.23500901460647583,
            -0.17906326055526733,
            -0.2907086908817291,
            -0.008578070439398289,
            0.0753917321562767,
            -0.14488279819488525,
            -0.26482442021369934,
            -0.06546684354543686,
            0.052248261868953705,
            -0.20151843130588531,
            -0.1744348406791687,
            0.050734248012304306,
            -0.19624881446361542,
            -0.07147762924432755,
            -0.09726051241159439,
            0.26261717081069946,
            -0.2049722969532013,
            0.10304351896047592,
            -0.22896291315555573,
            0.23405195772647858,
            0.011104372330009937,
            0.049827754497528076,
            -0.11064115911722183,
            0.014518649317324162,
            -0.15270668268203735,
            0.35127267241477966,
            -0.09730551391839981,
            -0.018356777727603912,
            0.12390352040529251,
            -0.06580126285552979,
            0.06338705122470856,
            0.003989344462752342,
            0.33474496006965637,
            0.2049836963415146,
            0.15335968136787415,
            0.03185566887259483,
            0.00975364912301302,
            -0.2711649239063263,
            0.28542718291282654,
            0.01381057221442461,
            -0.6451457142829895,
            0.10553216189146042,
            -0.00396967027336359,
            -0.03967614471912384,
            0.44243529438972473,
            0.7434820532798767,
            -0.04143737256526947,
            -0.12463027983903885,
            0.21226607263088226,
            0.2938382029533386,
            -0.45060649514198303,
            0.051779963076114655,
            0.17812852561473846,
            -0.08974258601665497,
            -0.2647152245044708,
            -0.12013837695121765,
            0.10713152587413788,
            0.060597583651542664,
            -0.1376824975013733,
            -0.04519288241863251,
            0.12311037629842758,
            -0.3919932246208191,
            0.42937982082366943,
            0.07362933456897736,
            0.2105981558561325,
            -0.06988681852817535,
            -0.059152308851480484,
            0.15634821355342865,
            -0.07455789297819138,
            0.18029333651065826,
            -0.0823991671204567,
            0.4696787893772125,
            0.23802471160888672,
            -0.09022035449743271,
            0.09475826472043991,
            -0.058759186416864395,
            0.017324328422546387,
            0.17116893827915192,
            -0.4641529619693756,
            0.3257032632827759,
            0.38085225224494934,
            -0.4340960383415222
        ]
    },
    {
        "id": "transformers.md",
        "text": "Transformers make it possible to build models\nthat understand text context and produce\nsemantic representations of data.",
        "embedding": [
            -0.6830356121063232,
            -0.1310495287179947,
            -0.12315614521503448,
            -0.30611327290534973,
            -0.6136655807495117,
            -0.018671438097953796,
            0.005505668465048075,
            0.13166089355945587,
            -0.25197634100914,
            0.21102936565876007,
            -0.007516247220337391,
            -0.06004631146788597,
            0.24960558116436005,
            0.09716396033763885,
            -0.043021686375141144,
            0.01936982199549675,
            0.21531358361244202,
            0.2966875433921814,
            -0.36880382895469666,
            -0.18658342957496643,
            0.4251406788825989,
            0.026756582781672478,
            0.09809648990631104,
            -0.1858365833759308,
            -0.1480737030506134,
            0.5320762991905212,
            0.016803482547402382,
            -0.18081681430339813,
            0.17663626372814178,
            0.025423839688301086,
            -0.016232173889875412,
            0.3179301917552948,
            0.2978987991809845,
            0.1377626210451126,
            -0.11375082284212112,
            0.09142280369997025,
            -0.1083027645945549,
            0.12096761912107468,
            0.1900145560503006,
            -0.0819023847579956,
            -0.21572089195251465,
            0.21632510423660278,
            -0.08328455686569214,
            -0.075624480843544,
            -0.1521066576242447,
            -0.324945867061615,
            -0.20926281809806824,
            -0.05789877474308014,
            -0.0897664949297905,
            -0.49942561984062195,
            -0.15863290429115295,
            -0.3424508571624756,
            0.0032978986855596304,
            -0.08290174603462219,
            -0.027237214148044586,
            0.17417678236961365,
            -0.04535501450300217,
            -0.11149994283914566,
            -0.08425572514533997,
            -0.5775423645973206,
            -0.04017169028520584,
            -0.03438117355108261,
            0.28825482726097107,
            0.4449937641620636,
            0.008882575668394566,
            0.3021492063999176,
            -0.005834203213453293,
            0.17046965658664703,
            0.12935402989387512,
            -0.2724182903766632,
            -0.5120145082473755,
            0.007412131875753403,
            0.1307211071252823,
            -0.10056145489215851,
            0.043600887060165405,
            0.1411864161491394,
            -0.20135267078876495,
            -0.2625839412212372,
            0.31471940875053406,
            -0.2578077018260956,
            0.31219926476478577,
            0.06987305730581284,
            0.004399968311190605,
            0.34152770042419434,
            -0.02716844528913498,
            0.1005464494228363,
            0.1838410496711731,
            -0.3056332468986511,
            -0.28825512528419495,
            0.027123257517814636,
            -0.02461279183626175,
            -0.1582123190164566,
            0.08849211037158966,
            0.11999586224555969,
            -0.5485939383506775,
            -0.25955119729042053,
            -0.29951468110084534,
            -0.031043879687786102,
            0.7420569658279419,
            0.3019391894340515,
            -0.11755402386188507,
            0.008441631682217121,
            0.061503976583480835,
            -0.08144108206033707,
            0.281554251909256,
            -0.2864290773868561,
            -0.0044241612777113914,
            0.09261105209589005,
            0.0609213151037693,
            -0.05635121837258339,
            0.034040216356515884,
            0.015622653067111969,
            -0.05564190074801445,
            -0.1704828143119812,
            0.1114436611533165,
            -0.08221706002950668,
            0.21594944596290588,
            -0.32006531953811646,
            0.0785403698682785,
            0.12015587091445923,
            0.25776517391204834,
            -0.008515981957316399,
            -0.04154105857014656,
            0.23990347981452942,
            0.35337692499160767,
            -0.15802514553070068,
            -0.2011423259973526,
            0.03706412389874458,
            0.15056009590625763,
            -0.11783909052610397,
            0.04858069121837616,
            0.21033501625061035,
            0.2878126800060272,
            0.35872891545295715,
            -0.09870628267526627,
            -0.02835722826421261,
            -0.09596913307905197,
            -0.05621431767940521,
            -0.02537679858505726,
            0.22255881130695343,
            0.09538589417934418,
            0.5511302947998047,
            0.16744588315486908,
            0.2196912169456482,
            -0.37436074018478394,
            0.013606103137135506,
            -0.06381844729185104,
            -0.3845541477203369,
            -0.15302741527557373,
            0.06839809566736221,
            0.2627237141132355,
            0.0904417335987091,
            -0.2750733196735382,
            0.2663227319717407,
            0.49295762181282043,
            -0.23259513080120087,
            -0.10047683119773865,
            -0.20574608445167542,
            -0.37923794984817505,
            -0.20003628730773926,
            0.1332598179578781,
            0.45153653621673584,
            -0.06761106848716736,
            0.027364831417798996,
            -0.0014811096480116248,
            0.03362337127327919,
            0.2030804455280304,
            0.22361893951892853,
            0.4198054075241089,
            0.23764485120773315,
            0.23432394862174988,
            -0.13352257013320923,
            -0.021538013592362404,
            -0.26245084404945374,
            0.310550332069397,
            0.188322514295578,
            -0.22950135171413422,
            -0.06152535229921341,
            0.21683131158351898,
            0.032774001359939575,
            0.2899174392223358,
            -0.06094679981470108,
            0.14781507849693298,
            0.12832066416740417,
            -0.08444433659315109,
            -0.4032599627971649,
            0.0379202663898468,
            0.0824393481016159,
            -0.3231630027294159,
            0.31946492195129395,
            0.06602614372968674,
            0.32055607438087463,
            -0.07958913594484329,
            0.003624111879616976,
            0.06591116636991501,
            -0.45108315348625183,
            0.4156884551048279,
            0.21967685222625732,
            -0.49367475509643555,
            -0.2507328391075134,
            -0.46464937925338745,
            -0.0708707943558693,
            -0.5718653202056885,
            -0.31703850626945496,
            -0.3251365125179291,
            0.05905284732580185,
            -0.307499498128891,
            0.2853480875492096,
            -0.07795156538486481,
            -0.09144110232591629,
            -0.10132252424955368,
            -0.04374802112579346,
            -0.2029445469379425,
            -0.5314295291900635,
            0.2078513205051422,
            -0.11196111887693405,
            -0.279171884059906,
            0.0012894186656922102,
            -0.12624859809875488,
            -0.09212008118629456,
            0.014700659550726414,
            -0.019413121044635773,
            0.13036604225635529,
            -0.13250316679477692,
            -0.4145163595676422,
            0.5713692903518677,
            -0.33611997961997986,
            -0.05377623811364174,
            -0.27742844820022583,
            0.016633586958050728,
            -0.20369957387447357,
            -0.09745538979768753,
            0.06744955480098724,
            -0.1050674095749855,
            -0.4546860456466675,
            -0.021625500172376633,
            -0.012726878747344017,
            0.009713051840662956,
            -0.26342666149139404,
            -0.29322153329849243,
            0.22885803878307343,
            -0.1082514151930809,
            -0.13232222199440002,
            -0.2387170046567917,
            -0.15053045749664307,
            0.2730480432510376,
            -0.5017227530479431,
            -0.3002815544605255,
            0.2999657392501831,
            0.4112837314605713,
            -0.04501878470182419,
            0.08602995425462723,
            0.23619858920574188,
            0.11998365074396133,
            -0.3192884027957916,
            0.10616748034954071,
            -0.20931822061538696,
            0.02543024905025959,
            -0.0615108460187912,
            0.03222102299332619,
            0.06487655639648438,
            0.1394416242837906,
            0.05647314339876175,
            -0.07372643053531647,
            0.005370700266212225,
            -0.09483533352613449,
            -0.33000898361206055,
            0.08526530861854553,
            -0.14934368431568146,
            -0.10611596703529358,
            -0.20836104452610016,
            0.04168004170060158,
            0.04029814898967743,
            0.16463105380535126,
            0.09498374164104462,
            -0.0337938629090786,
            0.049613192677497864,
            -0.4420541226863861,
            -0.15590526163578033,
            -0.08708146214485168,
            0.0354195274412632,
            -0.2888961732387543,
            0.04610954597592354,
            0.04367731139063835,
            -0.04793807119131088,
            -0.1827298104763031,
            0.12909534573554993,
            -0.060546230524778366,
            0.013924358412623405,
            -0.1623094230890274,
            0.13804949820041656,
            -0.23386625945568085,
            0.07276923954486847,
            -0.2691619396209717,
            0.22677069902420044,
            0.04997982829809189,
            -0.005571046378463507,
            -0.03139476478099823,
            0.17132505774497986,
            -0.12473470717668533,
            -0.3489539623260498,
            0.1213926300406456,
            -0.2251269519329071,
            0.23001298308372498,
            -0.00020174134988337755,
            -0.10074703395366669,
            0.47767943143844604,
            0.1120697632431984,
            0.0397619754076004,
            0.011789821088314056,
            -0.1671038419008255,
            0.04462156444787979,
            -0.04420384764671326,
            0.1300911158323288,
            -0.10055090487003326,
            -0.11498256027698517,
            -0.17230196297168732,
            0.1924908310174942,
            -0.2671211063861847,
            0.0034350964706391096,
            -0.12641538679599762,
            0.17583921551704407,
            0.35133102536201477,
            -0.10258275270462036,
            -0.04084300249814987,
            -0.3190176784992218,
            -0.0901430994272232,
            0.07162552326917648,
            -0.003676064545288682,
            -0.05740814656019211,
            0.1700529307126999,
            0.17481285333633423,
            0.09663218259811401,
            0.13326507806777954,
            0.10840333998203278,
            0.5838716626167297,
            0.025106187909841537,
            0.22766552865505219,
            -0.17402777075767517,
            0.41028979420661926,
            0.18720391392707825,
            0.21704719960689545,
            -0.6276405453681946,
            0.3440190255641937,
            0.015545771457254887,
            0.11415549367666245,
            -0.08448130637407303,
            0.44880759716033936,
            0.02837299183011055,
            -0.19871950149536133,
            0.22745801508426666,
            0.4232048988342285,
            0.18652544915676117,
            0.12910057604312897,
            0.07948002964258194,
            -0.32643014192581177,
            -0.05592727288603783,
            -0.00462170597165823,
            0.03472663834691048,
            0.36795639991760254,
            0.2916737496852875,
            -0.14637349545955658,
            0.038769081234931946,
            -0.109405018389225,
            0.3030121624469757,
            -0.02049443870782852,
            0.1835634857416153,
            -0.4249577820301056,
            0.12812437117099762,
            0.33591774106025696,
            -0.021862484514713287,
            -0.11124454438686371,
            0.15085387229919434,
            0.19214046001434326,
            0.2530134320259094,
            -0.243207648396492,
            0.43239009380340576,
            0.07984699308872223,
            0.41718772053718567,
            0.34948939085006714,
            -0.1964477300643921,
            0.46866515278816223,
            0.056785352528095245,
            -0.18100477755069733
        ]
    }
]

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-en.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));
}