{"id":2945,"date":"2024-04-13T09:41:56","date_gmt":"2024-04-13T01:41:56","guid":{"rendered":"https:\/\/www.aqwu.net\/wp\/?p=2945"},"modified":"2024-04-28T19:55:34","modified_gmt":"2024-04-28T11:55:34","slug":"%e4%ba%86%e8%a7%a3-openai-%e7%9a%84%e5%b5%8c%e5%85%a5embeddings","status":"publish","type":"post","link":"https:\/\/www.aqwu.net\/wp\/?p=2945","title":{"rendered":"\u4e86\u89e3 OpenAI \u7684\u5d4c\u5165(Embeddings)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>1. \u4ec0\u4e48\u662f\u5d4c\u5165\uff1f<\/strong><\/h2>\n\n\n\n<p>OpenAI\u7684\u6587\u672c\u5d4c\u5165\u8861\u91cf\u6587\u672c\u5b57\u7b26\u4e32\u7684\u76f8\u5173\u6027\u3002\u5d4c\u5165\u901a\u5e38\u7528\u4e8e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u641c\u7d22(Search)<\/strong> \u5176\u4e2d\u7ed3\u679c\u6309\u4e0e\u67e5\u8be2\u5b57\u7b26\u4e32\u7684\u76f8\u5173\u6027\u6392\u540d<\/li>\n\n\n\n<li><strong>\u805a\u7c7b\u5206\u6790(Clustering)<\/strong> \u5176\u4e2d\u6587\u672c\u5b57\u7b26\u4e32\u6309\u76f8\u4f3c\u6027\u5206\u7ec4<\/li>\n\n\n\n<li><strong>\u5efa\u8bae(Recommendations<\/strong>) \u5efa\u8bae\u4f7f\u7528\u5177\u6709\u76f8\u5173\u6587\u672c\u5b57\u7b26\u4e32\u7684\u9879\u76ee<\/li>\n\n\n\n<li><strong>\u5f02\u5e38\u68c0\u6d4b(Anomaly detection)<\/strong> \u8bc6\u522b\u76f8\u5173\u6027\u4e0d\u5927\u7684\u5f02\u5e38\u503c<\/li>\n\n\n\n<li><strong>\u591a\u6837\u6027\u6d4b\u91cf(Diversity measurement)<\/strong> \u5206\u6790\u76f8\u4f3c\u6027\u5206\u5e03<\/li>\n\n\n\n<li><strong>\u5206\u7c7b(Classification)<\/strong> \u5176\u4e2d\u6587\u672c\u5b57\u7b26\u4e32\u6309\u5176\u6700\u76f8\u4f3c\u7684\u6807\u7b7e\u5206\u7c7b<\/li>\n<\/ul>\n\n\n\n<p>\u5d4c\u5165\u662f\u6d6e\u70b9\u6570\u7684\u5411\u91cf\uff08\u5217\u8868\uff09\u3002\u4e24\u4e2a\u5411\u91cf\u4e4b\u95f4\u7684\u8ddd\u79bb\u8861\u91cf\u5b83\u4eec\u7684\u76f8\u5173\u6027\u3002\u5c0f\u8ddd\u79bb\u8868\u793a\u9ad8\u76f8\u5173\u6027\uff0c\u5927\u8ddd\u79bb\u8868\u793a\u4f4e\u76f8\u5173\u6027\u3002<\/p>\n\n\n\n<p>\u8bf7\u8bbf\u95eeOpenAI\u7684<a href=\"https:\/\/openai.com\/api\/pricing\/\">\u5b9a\u4ef7\u9875\u9762<\/a>\uff0c\u4e86\u89e3\u5d4c\u5165\u5b9a\u4ef7\u3002\u8bf7\u6c42\u6839\u636e<a href=\"https:\/\/platform.openai.com\/docs\/api-reference\/embeddings\/create#embeddings\/create-input\">\u8f93\u5165<\/a>\u4e2d\u7684<a href=\"https:\/\/platform.openai.com\/tokenizer\">\u4ee4\u724c<\/a>\u6570\u91cf\u8ba1\u8d39\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u5982\u4f55\u83b7\u53d6\u5d4c\u5165<\/strong><\/h2>\n\n\n\n<p>\u8981\u83b7\u53d6\u5d4c\u5165\uff0c\u8bf7\u5c06\u6587\u672c\u5b57\u7b26\u4e32\u4e0e\u5d4c\u5165\u6a21\u578b\u540d\u79f0\uff08\u4f8b\u5982&nbsp;<code>text-embedding-3-small<\/code>&nbsp;\uff09\u4e00\u8d77\u53d1\u9001\u5230\u5d4c\u5165 API \u7aef\u70b9\uff08\u4f8b\u5982 \uff09\u3002\u54cd\u5e94\u5c06\u5305\u542b\u4e00\u4e2a\u5d4c\u5165\uff08\u6d6e\u70b9\u6570\u5217\u8868\uff09\uff0c\u60a8\u53ef\u4ee5\u5c06\u5176\u63d0\u53d6\u3001\u4fdd\u5b58\u5728\u77e2\u91cf\u6570\u636e\u5e93\u4e2d\uff0c\u5e76\u7528\u4e8e\u8bb8\u591a\u4e0d\u540c\u7684\u7528\u4f8b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># pip install openai\nimport os\nfrom openai import OpenAI\n\nOPENAI_API_KEY = os.getenv('OPENAI_API_KEY')\n\nclient = OpenAI(\n  api_key=OPENAI_API_KEY\n)\n\nresponse = client.embeddings.create(\n    input=\"What did the author do growing up?\",\n    model=\"text-embedding-3-small\"\n)\n\nprint(response)<\/pre><\/div>\n\n\n\n<p>\u4e0b\u9762\u662f\u8f93\u51fa\u7ed3\u679c\uff0c\u603b\u7ed31536\u4e2a\u6570\u636e\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">CreateEmbeddingResponse(data=[Embedding(embedding=\n[\n  0.010480836033821106, -0.014171650633215904, 0.03369464352726936, 0.051227692514657974, 0.010406885296106339, -0.05329831317067146, 0.0372442789375782, 0.04006785526871681, -0.0686262845993042, 0.0014496025396510959, \n  -0.017586829140782356, -0.007435410283505917, -0.013075835071504116, 0.04649484530091286, 0.0025210478343069553, 0.014292661100625992, 0.035738375037908554, -0.0031950080301612616, -0.034743402153253555, \n  -0.022911282256245613, 0.04254184290766716, 0.008706018328666687, -0.0020807047840207815, 0.007603480480611324, 0.020410403609275818, -0.01528763398528099, 0.011549760587513447, 0.023798691108822823, 0.005946311634033918, \n  0.002961390884593129, -0.08260969817638397, -0.02187597192823887, -0.022010428830981255, -0.012833814136683941, 0.05238401144742966, -0.033264387398958206, 0.03450138121843338, -0.03587283194065094, 0.07459612190723419, \n  0.02861221320927143, -0.01792296953499317, -0.04864614084362984, -0.04939909279346466, 0.03861572965979576, -0.03130133077502251, 3.4375538234598935e-05, 0.01131446287035942, 0.042595624923706055, 0.02471299096941948, \n  -0.009129554964601994, -0.038561947643756866, -0.032968584448099136, 0.015166623517870903, 0.009055604226887226, -0.04041744023561478, 0.008887534029781818, -0.06824980676174164, -0.048753704875707626, \n  -0.0056505086831748486, 0.0007974914624355733, 0.027778586372733116, -0.027993716299533844, -0.04305277392268181, -0.0010504366364330053, 0.002873994642868638, -0.005912697408348322, -0.05351344123482704, \n  -0.012195148505270481, -0.013149785809218884, 0.025143250823020935, 0.01058840099722147, 0.010440499521791935, -0.015906130895018578, -0.016699420288205147, -0.020370066165924072, -0.043483033776283264, \n  -0.019079290330410004, -0.05453530699014664, -0.020638978108763695, 0.011146392673254013, 0.03686780482530594, 0.0033160182647407055, -0.0568479485809803, 0.056901730597019196, 0.025828976184129715, 0.01318340003490448, \n  0.04929152876138687, 0.006645482033491135, -0.02410794049501419, -0.05824628844857216, 0.0006546321092173457, 0.0024118025321513414, 0.025519726797938347, -0.008463998325169086, 0.06744307279586792, \n  -0.037405628710985184, 0.0017311195842921734, 0.028665995225310326, 0.013143062591552734, 0.009297624230384827, 0.005216888152062893, -0.017398592084646225, 0.019832242280244827, -0.0033311445731669664, \n  -0.010373272001743317, 0.03283412754535675, -0.02292472869157791, -0.025022240355610847, -0.01657840982079506, -0.014924603514373302, -0.13789795339107513, -0.00990267563611269, -0.011556483805179596, \n  0.0039462801069021225, -0.062495097517967224, 0.012242208234965801, -0.026837395504117012, -0.035039205104112625, -0.04168132320046425, 0.008827028796076775, -0.03361397236585617, -0.012067415751516819, \n  0.006480773910880089, -0.04816209897398949, 0.05674038454890251, 0.021593615412712097, -0.019052399322390556, -0.019549885764718056, -0.034366924315690994, -0.02117680199444294, 0.025546617805957794, \n  0.013203567825257778, 0.0030622328631579876, -0.011664047837257385, -0.005105962045490742, -0.04843100905418396, 0.018891051411628723, -0.005653869826346636, 0.004716040100902319, 0.012336327694356441, \n  -0.017667504027485847, 0.01544898096472025, 0.013929629698395729, -0.005569835193455219, 0.02529115229845047, 0.02560039982199669, 0.07685498148202896, 0.059268154203891754, 0.024968458339571953, 0.014601909555494785, \n  0.022803718224167824, 0.038669511675834656, -0.021445713937282562, 0.01386240217834711, 0.000705053040292114, 0.015072504989802837, 0.042488060891628265, -0.02295161969959736, -0.01068252045661211, 0.047489818185567856, \n  0.011011936701834202, -0.0017344809602946043, -0.023462552577257156, -0.018581803888082504, -0.07126162201166153, -0.006773215252906084, 0.01949610374867916, -0.02863910421729088, 0.0052975621074438095, \n  -0.014306106604635715, -0.007388351019471884, -0.002801724476739764, 0.04566121846437454, 0.03737873584032059, 0.020262502133846283, -0.020665869116783142, -0.022588588297367096, -0.019549885764718056, -0.03479718416929245, \n  0.01823221892118454, -0.03576526418328285, 0.047301579266786575, -0.07126162201166153, -0.036814022809267044, -0.04383261874318123, 0.010864035226404667, -0.009620318189263344, -0.032000500708818436, 0.030118118971586227, \n  0.004094181582331657, -0.008894257247447968, 0.023381877690553665, -0.012343049980700016, -0.0392342284321785, 0.0391804464161396, -0.042111583054065704, 0.07045488059520721, 0.0034689619205892086, -0.05695551261305809, \n  -0.013741391710937023, 0.06518421322107315, 0.00286895246244967, 0.018689367920160294, -0.01872970536351204, 0.010158142074942589, -0.027859259396791458, 0.018595248460769653, 0.04781251400709152, 0.0037983788643032312, \n  -0.004927808418869972, 0.05031339079141617, -0.02323397621512413, 0.0033529936335980892, 0.02831641025841236, -0.008410215377807617, 0.02194320037961006, 0.03423246741294861, -0.010501004755496979, -0.026541592553257942, \n  0.02028939314186573, 0.020033927634358406, 0.03850816562771797, 0.005502607207745314, 0.033371951431035995, 0.03038703091442585, -0.03762075677514076, -0.02625923417508602, -0.03611484915018082, -0.024686099961400032, \n  -0.04576878249645233, 0.042219147086143494, 0.04625282436609268, -0.02382558211684227, 0.00023697849246673286, -0.018891051411628723, 0.008699296042323112, -0.030118118971586227, 0.03420557826757431, -0.04036365821957588,\n  0.027402110397815704, -0.03463583439588547, 0.014306106604635715, 0.004379900638014078, 0.024928120896220207, -0.02225244976580143, 0.044451117515563965, -0.027213871479034424, -0.04743603616952896, 0.003684091381728649, \n  -0.016430508345365524, -0.016390172764658928, 0.025721410289406776, 0.02137848548591137, 0.02500879392027855, -0.042299821972846985, 0.035550136119127274, 0.009586704894900322, -0.030413921922445297, 0.049856241792440414, \n  -0.02979542501270771, 0.04321411997079849, -0.00595303438603878, 0.010642183013260365, -0.034743402153253555, 0.005606810562312603, 0.034178685396909714, -0.011583374813199043, 0.035442572087049484, -0.0007319442229345441,\n  -0.022104548290371895, -0.053674791008234024, -0.021391931921243668, 0.02136504091322422, 0.004729485604912043, -0.01636328175663948, -0.03928801044821739, 0.020638978108763695, 0.010427054017782211, -0.014776702038943768,\n  0.01636328175663948, -0.007536252494901419, 0.05216888338327408, 0.01569100096821785, 0.029956771060824394, -0.01265229843556881, 0.02156672440469265, 0.020504523068666458, 0.027886150404810905, 0.013808619230985641, \n  -0.029526513069868088, 0.016645638272166252, -0.025560064241290092, 0.03990650549530983, -0.03479718416929245, 0.02488778531551361, -0.013781728222966194, -0.015744784846901894, 0.0066353981383144855, \n  -0.01735825464129448, 0.029096253216266632, 0.06039758399128914, 0.03175847977399826, 0.03420557826757431, 0.05214199423789978, 0.030064336955547333, 0.021539833396673203, 0.033775318413972855, 0.04477380961179733, \n  0.018097762018442154, 0.004453851375728846, 0.003916027490049601, 0.04146619513630867, -0.022386904805898666, -0.010117805562913418, 0.011186730116605759, -0.024470971897244453, -0.024349961429834366, \n  -0.019536439329385757, 0.02410794049501419, -0.05421261489391327, 0.018205326050519943, -0.009129554964601994, -0.01860869489610195, -0.023556672036647797, -0.025546617805957794, -0.009304347448050976, \n  0.014870820567011833, 0.0392342284321785, -0.006648843642324209, -0.0029126505833119154, -0.01337836030870676, -0.05598743259906769, -0.032296303659677505, 0.015193515457212925, 0.011408582329750061, -0.03146267682313919,\n  -0.004638728220015764, 0.0539168119430542, 0.0070723798125982285, -0.01616159826517105, -0.03977205231785774, -0.03498542308807373, -0.0021798659581691027, -0.0010285875760018826, 0.002588275820016861, \n  -0.010050577111542225, 0.011650602333247662, -0.0002556762774474919, -0.025949986651539803, 0.03961070254445076, 0.006420268677175045, 0.023354986682534218, 0.018783487379550934, -0.0016983458772301674, \n  -0.025371825322508812, -0.002534493338316679, -0.00015094774425961077, 0.016941441223025322, 0.02616511471569538, 0.031220655888319016, -0.027993716299533844, -0.025707965716719627, 0.049533549696207047, \n  0.031812261790037155, -0.036706455051898956, 0.023368433117866516, -0.025049131363630295, 0.025076022371649742, -0.01206069253385067, -0.030817288905382156, -0.022817164659500122, -0.0016370003577321768, \n  -0.00707910256460309, 0.014386779628694057, -0.017129680141806602, 0.04065946117043495, -0.0005369831924326718, -0.049748677760362625, -0.0342862494289875, 0.03654510900378227, 0.018097762018442154, 0.08029705286026001,\n  0.0027277737390249968, -0.06432369351387024, 0.03194671869277954, -0.01317667681723833, 0.016403617337346077, -0.006897586863487959, -0.016228824853897095, 0.0259903222322464, -0.023785246536135674, \n  -0.003697536885738373, 0.022575143724679947, 0.0519268624484539, -0.028854232281446457, -0.042004019021987915, 0.04060567915439606, 0.025116359815001488, 0.012558179907500744, 0.004638728220015764, 0.0308979619294405, \n  0.012551456689834595, -0.00897493027150631, 0.025640737265348434, 0.07470369338989258, 0.02147260494530201, 0.018312891945242882, -0.018702814355492592, -0.010312766768038273, 0.007697599474340677, -0.04057878628373146,\n  -0.013875847682356834, 0.0470326691865921, -0.05345965921878815, 0.02117680199444294, -0.03568459302186966, 0.0070185973308980465, -0.010729579254984856, 0.0392342284321785, 0.02607099711894989, -0.09374264627695084, \n  -0.03165091574192047, 0.020464185625314713, -0.03751319274306297, 0.0176406130194664, 0.011153115890920162, 0.004544608760625124, -0.021996982395648956, 0.021647397428750992, 0.03380221128463745, 0.006779938004910946,\n  -0.0010100998915731907, 0.007442133501172066, -0.04342925176024437, 0.005774880293756723, -0.028961798176169395, -0.007549697998911142, 0.0755104273557663, -0.006154718343168497, -0.022696154192090034, \n  0.012813646346330643, 0.024444080889225006, -0.04439733177423477, 0.014964940026402473, -0.030252574011683464, -0.02861221320927143, 0.027092861011624336, 0.003552996786311269, -0.031328219920396805, \n  0.011233788914978504, 0.001621033763512969, -0.07142296433448792, -0.0013975008623674512, -0.008484166115522385, 0.0016201934777200222, 0.032699670642614365, 0.029876098036766052, -0.004282420035451651, \n  -0.022776827216148376, 0.011785058304667473, -0.05136214941740036, 0.021929755806922913, -0.0018504491308704019, -0.04496204853057861, 0.01851457543671131, 0.010232092812657356, 0.023960039019584656, -0.014050640165805817, \n  0.01245061494410038, -0.06045136600732803, -8.970728958956897e-05, 0.023072630167007446, -0.026205452159047127, 0.0259903222322464, -0.042219147086143494, 0.016148151829838753, -0.02853153832256794, -0.027496227994561195, \n  -0.017371701076626778, -0.06039758399128914, 0.0411703921854496, 0.046602409332990646, 0.07803819328546524, -0.030924852937459946, -0.013741391710937023, -0.02088099904358387, -0.07174566388130188, 0.033667754381895065, \n  0.030924852937459946, 0.03861572965979576, -0.05561095476150513, 0.008685850538313389, 0.0066757346503436565, -0.015596882440149784, 0.025049131363630295, 0.00910938624292612, 0.011334631592035294, -0.04792007803916931, \n  -0.011106056161224842, 0.022212112322449684, -0.008322819136083126, 0.04525785148143768, -0.062495097517967224, 0.06830359250307083, -0.025237370282411575, -0.04275697097182274, -0.01999359019100666, 0.012484229169785976,\n  0.018971726298332214, -0.03517365828156471, -0.004178216680884361, -0.033963557332754135, -0.020638978108763695, 0.0067059872671961784, 0.008867365308105946, 0.03576526418328285, 0.009432080201804638, 0.03657200187444687,\n  0.002747942227870226, 0.009983349591493607, 0.005576557945460081, 0.014171650633215904, -0.03401733934879303, 0.03899220749735832, 0.022037319839000702, -0.03076350688934326, -0.014897712506353855, 0.06314048171043396, \n  -0.017909523099660873, -0.00629589706659317, 0.022198665887117386, 0.00940518919378519, 0.009647210128605366, -0.005374873988330364, -0.016309499740600586, 0.03312993049621582, 0.016645638272166252, -0.009210227988660336, 0.0027765140403062105, -0.02753656543791294, -0.010312766768038273, 0.04146619513630867, 0.06098918989300728, 0.03735184669494629, 0.004346286412328482, 0.0156775563955307, -0.020316284149885178, -0.006779938004910946, \n  -0.018891051411628723, -0.022870946675539017, 0.01746581867337227, -0.013916184194386005, 0.008006848394870758, -0.01636328175663948, 0.026151670143008232, 0.0225482527166605, -0.011193452402949333, 0.016793539747595787, \n  0.0006361444247886539, -0.011119501665234566, 0.006500942166894674, -0.025264261290431023, 0.008853919804096222, 0.008437106385827065, 0.014830484054982662, -0.0019832244142889977, -0.006477412302047014, \n  -0.024860892444849014, 0.0019899471662938595, 0.006648843642324209, 0.010763193480670452, 0.01215481199324131, 0.015664109960198402, -0.04138552024960518, -0.03216184675693512, -0.01586579531431198, -0.006373208947479725, \n  0.022427242249250412, -0.030252574011683464, -0.018985170871019363, -0.006161441095173359, 0.023839028552174568, -0.008611899800598621, -0.042703188955783844, -0.03157024085521698, 0.023758355528116226, \n  0.05483110994100571, -0.00573790492489934, 0.012538011185824871, 0.0066118682734668255, -0.023771800100803375, 0.03253832459449768, -0.041116610169410706, 0.03584593906998634, 0.00127480982337147, 0.010897649452090263, \n  -0.005196719896048307, -0.01950954832136631, -0.04235360398888588, -0.02979542501270771, 0.06136566773056984, 0.00799340195953846, -0.01844734698534012, 0.03060215897858143, 0.021432267501950264, 0.003131141420453787, \n  -0.034366924315690994, -0.0333450585603714, 0.00045336844050325453, 0.0022588588763028383, -0.02636680006980896, 0.022763380780816078, -0.01920030079782009, -0.0028134894091635942, 0.0032202184665948153, \n  0.0023630622308701277, 0.025842420756816864, -0.020800326019525528, -0.00940518919378519, 0.018756596371531487, 0.00590597465634346, -0.023986930027604103, -0.034743402153253555, 0.05466976389288902, \n  -0.00045420878450386226, -0.04057878628373146, -0.0011336312163621187, 0.03598039597272873, 0.01715657114982605, 0.04006785526871681, 0.0064404369331896305, 0.02361045405268669, -0.029580295085906982, 0.02804749831557274, \n  0.006732878275215626, -0.015462426468729973, 0.04762427508831024, -0.012363218702375889, -0.032188739627599716, -0.018272554501891136, 0.004500910639762878, -0.0016647318843752146, 0.009297624230384827, \n  0.03684091195464134, -0.011354799382388592, 0.03608796000480652, -0.01872970536351204, -0.0030387029983103275, 0.005835385527461767, -0.00941191241145134, -0.013169953599572182, -0.014964940026402473, 0.005949672777205706,\n  -0.020706206560134888, 0.014830484054982662, -0.01636328175663948, 0.03606106713414192, -0.04203090816736221, 0.013687608763575554, 0.0015563268680125475, 0.008262313902378082, -0.0033429095055907965, -0.0191330723464489,\n  0.0032941692043095827, 0.022427242249250412, 0.010534618981182575, 0.036625783890485764, -0.007260617800056934, 0.002147932769730687, -0.0181380994617939, 0.02547939121723175, 0.029929880052804947, 0.004221914801746607, \n  0.00011659846495604143, 0.01569100096821785, 0.0024084411561489105, 0.0064942194148898125, -6.18707126704976e-05, 0.022091101855039597, -0.01569100096821785, 0.029822316020727158, 0.006463966798037291, \n  -0.003105930984020233, -0.02214488387107849, 0.005949672777205706, 0.01929442025721073, 0.0035866107791662216, -0.010951431468129158, -0.004692510236054659, -0.026003768667578697, -0.04654862731695175, \n  -0.029929880052804947, 0.03791655972599983, -0.012974993325769901, 0.032672781497240067, 0.009264010936021805, -0.03643754497170448, 0.038669511675834656, -0.0066757346503436565, -0.09487207233905792, \n  -0.0022588588763028383, -0.04332168772816658, 0.002744580851867795, 0.012679190374910831, -0.01952299475669861, -0.028961798176169395, 0.017909523099660873, -0.005630340427160263, -0.002721050987020135, \n  -0.01854146644473076, 0.001545402337796986, -0.010958154685795307, 0.01558343693614006, 0.03030635602772236, -0.01087748073041439, -0.0019731400534510612, -0.0026202090084552765, -0.008235422894358635, \n  -0.001558007556013763, 0.029096253216266632, 0.024538198485970497, -0.003082401119172573, -0.00852450355887413, -0.02285750024020672, -0.012127920985221863, -0.007852223701775074, 0.008712741546332836,\n  0.018353227525949478, -0.06539934128522873, 0.05405126512050629, 0.022104548290371895, 0.011711107566952705, -0.003006769809871912, -0.010729579254984856, -0.007287508808076382, 0.01971123367547989, \n  -0.03511987626552582, 0.03657200187444687, 0.031812261790037155, 0.01626916229724884, 0.004527802113443613, -0.00793289765715599, -0.016027141362428665, 0.023973483592271805, 0.037486299872398376, -0.016995223239064217,\n  0.005569835193455219, 0.009425357915461063, -0.028477756306529045, -0.010998491197824478, -0.01890449784696102, 0.011724553070962429, -0.0107900844886899, 0.017801959067583084, -0.009089217521250248, \n  0.0025076023302972317, -0.034850966185331345, 0.005583280697464943, -0.027886150404810905, 0.010736302472651005, 0.027200425043702126, 0.06007488816976547, 0.0003012652159668505, 0.018407011404633522, -0.0016437232261523604, \n  0.019092734903097153, 0.030171900987625122, -0.015327971428632736, -0.0027076054830104113, 0.0069648148491978645, 0.0161884892731905, -0.012470783665776253, 0.027523119002580643, -0.018003642559051514, 0.034071121364831924, \n  0.0026622265577316284, 0.03649132698774338, -0.014601909555494785, 0.018501129001379013, 0.016255715861916542, 0.024040712043642998, -0.01376828271895647, -0.02391970157623291, 0.00011071602057199925, -0.007979956455528736, \n  -0.007529529742896557, 0.019469212740659714, 0.002151294145733118, -0.009640486910939217, -0.027913041412830353, 0.017116233706474304, -0.006971537601202726, 0.011785058304667473, 0.018286000937223434, 0.0074891927652060986,\n  0.016551518812775612, 0.000503369199577719, -0.07513394951820374, 0.0070185973308980465, 0.024820556864142418, -0.019845688715577126, -0.007953065447509289, -0.0015773356426507235, 0.03068283386528492, 0.006467327941209078,\n  0.004252167418599129, -0.0004002163477707654, -0.011576651595532894, -0.022897837683558464, -0.00512276915833354, -0.02273648977279663, -0.029096253216266632, 0.011973296292126179, -0.023462552577257156, \n  -0.036222416907548904, -0.059644632041454315, 0.023691127076745033, -0.023839028552174568, -0.016941441223025322, 0.0041412413120269775, 0.009943013079464436, 0.014964940026402473, -0.001889105187729001, \n  -0.006420268677175045, 0.03579215705394745, -0.023839028552174568, 0.0120001882314682, -0.019832242280244827, -0.018944833427667618, -0.018662476912140846, 0.016645638272166252, -0.025237370282411575, 0.004998397547751665,\n  0.004574861377477646, 0.04888816177845001, -0.0008832070743665099, -0.04439733177423477, -0.014467453584074974, -0.011401859112083912, -0.0013731307117268443, -0.040040962398052216, 0.012470783665776253, \n  -0.041896454989910126, -0.017479265108704567, 0.020800326019525528, 0.002079024212434888, 0.001503384904935956, 0.001756330020725727, -0.003253832459449768, 0.007946343161165714, -0.015206960961222649, \n  0.011146392673254013, -0.022091101855039597, -0.026151670143008232, 0.015206960961222649, 0.020356621593236923, 0.022117992863059044, 0.039556920528411865, -0.0025294513907283545, 0.01474981103092432, 0.01961711421608925,\n  -0.021499495953321457, -0.016336390748620033, 0.047301579266786575, -0.01113294716924429, -0.02098856307566166, -0.0019210384925827384, -0.03165091574192047, -0.009324515238404274, 0.042595624923706055, \n  -0.0005659752641804516, -0.0024773497134447098, -0.00871946383267641, 0.015946468338370323, 0.00698498310521245, -0.01891794241964817, -0.06781955063343048, 0.024551644921302795, 0.025264261290431023, \n  0.026245789602398872, -0.008598454296588898, 0.006171524990350008, -0.0225482527166605, 0.009553090669214725, 0.001241195946931839, -0.02410794049501419, 0.03883086144924164, 0.004944615066051483, -0.009492585435509682,\n  0.022575143724679947, -0.0027748332358896732, -0.00737490551546216, 0.00897493027150631, -0.024739883840084076, 0.026810504496097565, -0.008887534029781818, -0.0156775563955307, -0.054642871022224426, \n  0.0279399324208498, 0.011065719649195671, -0.007105993572622538, 0.01637672632932663, 0.023691127076745033, 0.017412036657333374, -0.0032050921581685543, -0.02187597192823887, 0.0022588588763028383, \n  0.020316284149885178, 0.0038622452411800623, -0.04135863110423088, -0.01279347762465477, -0.009734606370329857, -0.0021798659581691027, -0.018084317445755005, -0.028370192274451256, 0.0392342284321785, \n  0.004591668490320444, -0.011018659919500351, 0.0006231190054677427, -0.0186355859041214, 0.029822316020727158, -0.027671020478010178, -0.047973860055208206, -0.011885900050401688, 0.017008669674396515, \n  0.0007840459002181888, -0.0012277503265067935, 0.05727820843458176, 0.0058824447914958, 0.02264237031340599, -0.006578254047781229, -0.012732972390949726, -0.016242271289229393, 0.0269584059715271, 0.02245413325726986,\n  -0.02527770586311817, 0.0005437060026451945, -0.006796745117753744, 0.06357074528932571, -0.023691127076745033, -0.023677680641412735, -0.04851168394088745, -0.003277362324297428, -0.004672341980040073, \n  0.007341291289776564, 0.011193452402949333, 0.02312641218304634, 0.02401382103562355, 0.015193515457212925, 0.017506156116724014, -0.02128436602652073, 0.03568459302186966, 0.01308255735784769, 0.0042925039306283, \n  0.0015722934622317553, -0.007025320082902908, 0.020356621593236923, -0.0052437796257436275, -0.027993716299533844, 0.015704447403550148, -0.00048320082714781165, 0.01755993813276291, -0.0035294669214636087, \n  -0.005045457277446985, -0.01821877248585224, -0.017277581617236137, -0.032108064740896225, 0.002171462634578347, -0.008847197517752647, -0.004722762852907181, -0.017600275576114655, 0.047973860055208206, \n  0.0006554724532179534, 0.021244030445814133, 0.001500023528933525, 0.005405126605182886, 0.032188739627599716, -0.018111208453774452, 0.01000351831316948, -0.027388663962483406, 0.013102726079523563, \n  0.04340235888957977, -0.001617672387510538, 0.003606779035180807, -0.014884267002344131, -0.030118118971586227, -0.002316002734005451, -0.0006777417147532105, 0.041600652039051056, 0.023462552577257156, \n  -0.016201933845877647, -0.03175847977399826, 0.0022168413270264864, 0.0018403648864477873, -0.017694395035505295, -0.015502763912081718, -0.03173159062862396, -0.024739883840084076, -0.027227316051721573, \n  0.010306043550372124, -0.045822564512491226, -0.009506030939519405, -0.020800326019525528, -0.007516084238886833, 0.004655534867197275, 0.009237118996679783, 0.04716712608933449, -0.01823221892118454, \n  0.03775521367788315, -0.043778836727142334, -0.00043824216118082404, 0.005643785931169987, 0.011422027833759785, -0.025129804387688637, 0.05862276628613472, -0.019146518781781197, -0.04060567915439606, \n  0.0017916247015818954, -0.03194671869277954, 0.02244068682193756, -0.0062690055929124355, -0.03737873584032059, -0.008047184906899929, -0.015233851969242096, -0.020154938101768494, -0.00910938624292612, \n  0.002558023203164339, 0.010406885296106339, 0.014077531173825264, -0.008968207985162735, -0.006931201089173555, 0.02233312278985977, -0.0022185221314430237, -0.016793539747595787, -0.001605907455086708, \n  0.034447599202394485, -0.011630434542894363, 0.032672781497240067, -0.06029001995921135, -0.0014420393854379654, 0.0028336578980088234, -0.0008533747168257833, -0.011778336018323898, 0.010998491197824478, \n  -0.03939557448029518, 0.0037109823897480965, -0.01929442025721073, 0.0036504773888736963, 0.009593427181243896, 0.05227644741535187, 0.00843038409948349, -0.02018182910978794, 0.000994133180938661, \n  -0.007731213234364986, 0.003492491552606225, 0.0032067729625850916, -0.014467453584074974, 0.01815154403448105, 0.009546367451548576, 0.007415242027491331, -0.03038703091442585, 0.0058824447914958, \n  0.013324578292667866, 0.01279347762465477, 0.023744909092783928, 0.014090976677834988, 0.00715977605432272, 0.03186604380607605, -0.01265229843556881, 0.0411703921854496, 0.018985170871019363, -0.02697185054421425, \n  0.010944709181785583, 0.019267529249191284, -0.041331738233566284, -0.01343214325606823, 0.0008533747168257833, -0.025062577798962593, 0.018877606838941574, 0.002211799379438162, 0.026689494028687477, -0.015301079489290714,\n  -0.02753656543791294, -0.0011773293372243643, -0.028773559257388115, -0.0441284216940403, -0.04619904235005379, 0.0362761989235878, -0.009815279394388199, -0.0008441308746114373, 0.0006189172272570431, \n  -0.024538198485970497, -0.011778336018323898, 0.020033927634358406, 0.04049811139702797, 0.0033613971900194883, 0.020033927634358406, 0.02236001379787922, -0.027388663962483406, 0.01792296953499317, 0.015529654920101166,\n  0.011932959780097008, -0.01587923988699913, -0.020060818642377853, -0.024228950962424278, -0.01940198428928852, -0.029660968109965324, -0.02451130747795105, 0.04792007803916931, -0.015462426468729973, 0.004474019631743431,\n  0.003953002858906984, 0.015274188481271267, -0.00039685494266450405, 0.015731338411569595, 0.021512942388653755, -0.009492585435509682, 0.0024454165250062943, 0.021835636347532272, -0.0009126193472184241, \n  0.03076350688934326, -0.013808619230985641, -0.017291026189923286, -0.026044104248285294, -0.00012521204189397395, 0.014171650633215904, -0.018877606838941574, -0.005687484052032232, -0.038077905774116516, \n  -0.01686076819896698, -0.024672655388712883, 0.0006453882670029998, 0.02283060923218727, 0.004776545334607363, 0.04006785526871681, -0.004504272248595953, 0.016349835321307182, -0.04539230838418007, -0.005011843051761389,\n  0.00876652356237173, 0.00698498310521245, 0.005445463582873344, 0.01902550831437111, 0.010151419788599014, 0.008847197517752647, -0.00999679509550333, 0.014601909555494785, 0.07620959728956223, -0.02920381911098957, \n  -0.032592106610536575, -0.03520055115222931, 0.007455579005181789, -0.0016596898203715682, 0.040551893413066864, 0.010433776304125786, -0.0548848919570446, -0.003413498867303133, 0.004783268086612225, \n  -0.01579856686294079, -0.024349961429834366, -0.0161884892731905, 0.013667440973222256, -0.0027344964910298586, -0.004776545334607363, -0.008833752013742924, -0.011845563538372517, 0.02293817326426506, \n  0.024766774848103523, -0.007334568537771702, -0.018380120396614075, -0.03261899948120117, -0.022884391248226166, 0.0026857564225792885, 0.014090976677834988, 0.007879114709794521, 0.012443892657756805,\n  0.013916184194386005, 0.02167428843677044, -0.021338149905204773, 0.009828724898397923, 0.04205780103802681, -0.024336514994502068, 0.028800450265407562, 0.00847744382917881, -0.012464060448110104, \n  -0.020652424544095993, 0.04232671111822128, -0.013586767017841339, 0.003973171580582857, 0.004437044262886047, -0.029580295085906982, -0.015812011435627937, -0.005388319492340088, 0.024659208953380585,\n  0.03958381339907646, -0.0022185221314430237, 0.008403493091464043, -0.034958530217409134, -0.019240636378526688, -0.0030958468560129404, -0.007287508808076382, 0.0024924760218709707, 0.004699232988059521, \n  -0.007105993572622538, 0.006030346266925335, 0.021015454083681107, 0.01445400808006525, 0.03148956969380379, -0.0161884892731905, -0.020733097568154335, -0.018299445509910583, 0.003516021417453885, \n  -0.04286453500390053, 0.012679190374910831, 0.01445400808006525, 0.03479718416929245, -0.02568107470870018, -0.03506609424948692, 0.008840474300086498, 0.019038952887058258, 0.0411972850561142, \n  0.021190248429775238, 0.016927996650338173, 0.0489957258105278, -0.011549760587513447, 0.01587923988699913, -0.025627292692661285, -0.01902550831437111, -0.03934179246425629, -0.012665744870901108, \n  -0.02812817133963108, -0.005153021775186062, -0.046225935220718384, 0.016511183232069016, 0.030521485954523087, 0.016941441223025322, 0.0343400314450264, 0.0034034145064651966, 0.02460542693734169, \n  -0.005963118746876717, 0.014480899088084698, -0.011690938845276833, -0.021338149905204773, -0.005183274392038584, 0.011872454546391964, 0.01833978295326233, 0.018366673961281776, 0.021055791527032852, \n  -0.03027946501970291, 0.003784933127462864, 0.020961672067642212, 0.027119752019643784, -0.05614877864718437, -0.042004019021987915, 0.017869187518954277, 0.013257350772619247, 0.022696154192090034, \n  0.019738124683499336, -0.005959757138043642, -0.01743892766535282, -0.017223799601197243, 0.00017773387662600726, -0.006107658613473177, -0.02686428651213646, 0.002594998572021723, 0.024336514994502068, \n  -0.019563332200050354, 0.003697536885738373, -7.363560871453956e-05, -0.013875847682356834, -0.014279214665293694, 0.016685975715517998, 0.021593615412712097, 0.01695488765835762, 0.015529654920101166, \n  0.02039695717394352, -0.008053907193243504, 0.03087107092142105, 0.015112841501832008, -0.007516084238886833, -0.0124976746737957, 0.00659170001745224, -0.017022114247083664, -0.0005542103317566216, \n  0.007314400281757116, -0.004726124461740255, -0.029176928102970123, -0.012033801525831223, -0.023865919560194016, 0.035146769136190414, 0.008779969066381454, 0.0026807142421603203, -0.0058622765354812145,\n  0.012827091850340366, -0.024094494059681892, -0.014763256534934044, 0.025640737265348434, 0.02331465110182762, -0.0034387093037366867, -0.00182019651401788, -0.012625407427549362, 0.030333247035741806, \n  0.018017088994383812, 0.012383387424051762, 0.016524627804756165, 0.005015204660594463, 0.018312891945242882, 0.01637672632932663, 0.00871946383267641, 0.016094369813799858, -0.017022114247083664, \n  0.007643816992640495, 0.02500879392027855, 0.037701431661844254, -0.01499183103442192, -0.001512628747150302, -0.01696833223104477, -0.016349835321307182, 0.016215380281209946, -0.014427116140723228, \n  0.0026353353168815374, -0.006430352572351694, -0.008020293898880482, 0.0010058981133624911, 0.006460605189204216, 0.029553404077887535, 0.008027016185224056, -0.032780345529317856, -0.01589268632233143, \n  -0.024282732978463173, 0.0038219084963202477, -0.01940198428928852, 0.0022101185750216246, 0.014924603514373302, -0.008921148255467415, 0.011462364345788956, -0.014628800563514233, 0.021647397428750992, \n  -0.02334154210984707, 0.006020262371748686, 0.005620256066322327, -0.004820243455469608, 0.04009474441409111, -0.016322944313287735, -0.03353329747915268, -0.03458205237984657, 0.01259179413318634, \n  0.03302236646413803, -0.01715657114982605, -0.025116359815001488, -0.0011874135816469789, -0.029580295085906982, 0.0035328282974660397, -0.006040430627763271, 0.032108064740896225, -0.025640737265348434, \n  0.03584593906998634, -0.029741641134023666, 0.01445400808006525, 0.006937923841178417, -0.0134724797680974, 0.04991002380847931, -0.03216184675693512, -0.02301884815096855, -0.004830327816307545, \n  -0.001138673280365765, -0.0033378673251718283, 0.02569451928138733, -0.020921336486935616, 0.006487496662884951, -0.010783362202346325, -0.02675672061741352, 0.04108971729874611, 0.00945224892348051, -0.012504396960139275, \n  0.005186636000871658, -0.0026571843773126602, -0.015610327944159508, 0.006420268677175045, -0.016793539747595787, -0.03079039789736271, -0.01147580984979868, 0.02931138314306736, -0.021203693002462387, -0.036410652101039886,\n  -0.016228824853897095, 0.015166623517870903, 0.006400099955499172, 0.01725069060921669, -0.023946592584252357, 0.008813583292067051, -0.020450741052627563, -0.0016554880421608686, -0.005058902781456709, 0.015946468338370323, 0.03506609424948692, 0.006658927537500858, 0.03390977531671524, -0.007025320082902908, -0.02205076441168785, -0.024054158478975296, 0.0017075897194445133, 0.00021124280465301126, -0.015516209416091442,\n  -0.02000703662633896, -0.020746544003486633, 0.016941441223025322, -0.01131446287035942, 0.023852474987506866, 0.03283412754535675, 0.006316065322607756, 0.02812817133963108, 0.012134643271565437, -0.0038487997371703386, \n  -0.01278675440698862, 0.008114412426948547, -0.00421183044090867, -0.029365165159106255, 0.006500942166894674, -0.03079039789736271, 0.0018386841984465718, 0.03904598951339722, -0.004198384936898947, -0.010568232275545597,\n  0.003381565446034074, 0.031005527824163437, 0.023072630167007446, -0.04243427887558937, 0.009492585435509682, 0.035738375037908554, 0.0037277895025908947, 0.01735825464129448, 0.0005037894006818533, -0.03291480243206024,\n  -0.004974867682904005, -0.023664236068725586, 0.0003836194518953562, 0.0030924854800105095, 0.03493163734674454, -0.012430446222424507, 0.01485737506300211, -0.010447222739458084, 0.013001884333789349, \n  -0.006426991429179907, -0.040336765348911285, 4.8950347263598815e-05, 0.004510995000600815, 0.02920381911098957, -0.005845469422638416, -0.018191881477832794, -0.010890927165746689, -0.00922367349267006,\n  0.04264940693974495, -0.0061278268694877625, -0.024336514994502068, -0.036222416907548904, 0.0026252511888742447, 0.006904309615492821, -0.0013302728766575456, 0.03076350688934326, 0.0235432256013155, \n  -0.04638728126883507, -0.009136277250945568, -0.021015454083681107, 0.03315681964159012, 0.0018941472517326474, 0.014306106604635715, -0.03657200187444687, -0.01832633651793003, 0.012578347697854042, \n  -0.037593863904476166, -0.024094494059681892, 0.0018756595673039556, -0.017519602552056313, -0.021633952856063843, -0.008827028796076775, 0.030225683003664017, 0.012820368632674217, 0.010178310796618462,\n  0.04138552024960518, -0.024242395535111427, -0.0027193701826035976, -0.03953003138303757, 0.0038185471203178167, -0.009304347448050976, -0.004574861377477646, 0.010164865292608738, -0.0030050890054553747,\n  0.005912697408348322, -0.009472416713833809, 0.03751319274306297, 0.02675672061741352, 0.011825394816696644, 0.011482533067464828, -0.007206835318356752, -0.003953002858906984, -0.019576776772737503, \n  -0.011805227026343346, -0.008450552821159363, -0.010326212272047997, 0.001211783615872264, 0.012659021653234959, 0.04711334407329559, 0.01317667681723833, -0.02617856115102768, -0.005677399691194296, \n  -0.014736365526914597, -0.000638665456790477, 0.00032185378950089216, 0.005687484052032232, -0.012363218702375889, -0.011401859112083912, 0.007879114709794521, -0.0036168633960187435, -0.007516084238886833,\n  0.03380221128463745, 0.02197009138762951, -0.029634077101945877, -0.01038671750575304, -0.02597687765955925, -0.005317730363458395, -0.057708464562892914, -0.03716360777616501, -0.015596882440149784, \n  0.018164990469813347, 0.01308928057551384, -0.020410403609275818, -0.02187597192823887, 0.006147995591163635, -0.050824325531721115, -0.005566473584622145, 0.022978510707616806, 0.03786277770996094, \n  -0.04012163728475571, 0.007502638269215822, -0.01318340003490448\n], \nindex=0, \nobject='embedding')], \nmodel='text-embedding-3-small', \nobject='list', \nusage=Usage(prompt_tokens=14, total_tokens=14))\n<\/pre><\/div>\n\n\n\n<p>\u5982\u679c\u8981\u76f4\u63a5\u6253\u5370\u5d4c\u5165\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">print(response.data[0].embedding)\n<\/pre><\/div>\n\n\n\n<p>\u6253\u5370\u5d4c\u5165\u6570\u636e\u7684\u957f\u5ea6<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">print(len(response.data[0].embedding))\n\n1536<\/pre><\/div>\n\n\n\n<p>\u4e3a\u5565 Openai \u7684\u5d4c\u5165\u8fd4\u56de\u7684\u662f1536\u4e2a\u6570\u636e?<\/p>\n\n\n\n<p>ChatGPT \u7684\u56de\u7b54\uff1a<\/p>\n\n\n\n<p>OpenAI\u7684\u5d4c\u5165\u6a21\u578b\uff08\u5982GPT-3\u6216\u5176\u5b83\u57fa\u4e8eTransformer\u7684\u6a21\u578b\uff09\u8fd4\u56de1536\u4e2a\u6570\u636e\u70b9\uff08\u6216\u79f0\u4e3a\u7279\u5f81\u3001\u7ef4\u5ea6\uff09\u662f\u7531\u4e8e\u6a21\u578b\u8bbe\u8ba1\u548c\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u7684\u9009\u62e9\u3002\u8fd9\u4e2a\u6570\u5b57\uff0c1536\uff0c\u6307\u7684\u662f\u5d4c\u5165\u5411\u91cf\u7684\u7ef4\u5ea6\uff0c\u662f\u6a21\u578b\u5185\u90e8\u8868\u793a\u4fe1\u606f\u7684\u4e00\u4e2a\u56fa\u5b9a\u5927\u5c0f\u3002<\/p>\n\n\n\n<p>\u5728\u6df1\u5ea6\u5b66\u4e60\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u4e2d\uff0c\u5d4c\u5165\u662f\u4e00\u79cd\u5c06\u6587\u672c\uff08\u5982\u5355\u8bcd\u3001\u53e5\u5b50\u6216\u6574\u4e2a\u6587\u6863\uff09\u8f6c\u6362\u4e3a\u4e00\u7ec4\u6570\u5b57\u7684\u6280\u672f\uff0c\u4f7f\u5f97\u8fd9\u4e9b\u6570\u5b57\u80fd\u591f\u4ee3\u8868\u539f\u59cb\u6587\u672c\u7684\u67d0\u79cd\u610f\u4e49\u3002\u8fd9\u4e9b\u6570\u5b57\u901a\u5e38\u7ec4\u7ec7\u6210\u4e00\u4e2a\u5411\u91cf\u3002\u5411\u91cf\u7684\u6bcf\u4e2a\u7ef4\u5ea6\u90fd\u662f\u4e00\u4e2a\u7279\u5f81\uff0c\u53ef\u4ee5\u6355\u6349\u4e00\u4e9b\u5173\u4e8e\u6587\u672c\u7684\u8bed\u4e49\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u9009\u62e91536\u4e2a\u7ef4\u5ea6\uff08\u6216\u4efb\u4f55\u7279\u5b9a\u7684\u7ef4\u5ea6\u6570\uff09\u662f\u57fa\u4e8e\u591a\u79cd\u56e0\u7d20\u7684\u8003\u8651\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6027\u80fd\u548c\u7cbe\u5ea6<\/strong>\uff1a\u66f4\u9ad8\u7684\u7ef4\u5ea6\u901a\u5e38\u80fd\u591f\u6355\u83b7\u66f4\u4e30\u5bcc\u7684\u8bed\u4e49\u4fe1\u606f\uff0c\u8fd9\u53ef\u80fd\u4f1a\u63d0\u9ad8\u6a21\u578b\u5728\u7279\u5b9a\u4efb\u52a1\u4e0a\u7684\u6027\u80fd\u3002\u7136\u800c\uff0c\u7ef4\u5ea6\u7684\u589e\u52a0\u4e5f\u610f\u5473\u7740\u6a21\u578b\u7684\u8ba1\u7b97\u6210\u672c\u548c\u5b58\u50a8\u9700\u6c42\u4f1a\u589e\u52a0\u30021536\u662f\u7ecf\u8fc7\u7814\u53d1\u548c\u6d4b\u8bd5\u540e\uff0c\u5e73\u8861\u4e86\u6027\u80fd\u3001\u8ba1\u7b97\u590d\u6742\u5ea6\u548c\u8d44\u6e90\u9700\u6c42\u7684\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><strong>\u6a21\u578b\u67b6\u6784<\/strong>\uff1aOpenAI\u7684\u6a21\u578b\u662f\u57fa\u4e8eTransformer\u67b6\u6784\u8bbe\u8ba1\u7684\uff0c\u8fd9\u79cd\u67b6\u6784\u5305\u62ec\u4e86\u591a\u4e2a\u81ea\u6ce8\u610f\u529b\u5c42\u548c\u524d\u9988\u7f51\u7edc\uff0c\u5b83\u4eec\u7684\u5927\u5c0f\uff08\u7ef4\u5ea6\uff09\u4f1a\u5f71\u54cd\u6a21\u578b\u80fd\u591f\u5904\u7406\u7684\u4fe1\u606f\u7c7b\u578b\u548c\u91cf\u30021536\u7ef4\u53ef\u4ee5\u89c6\u4e3a\u8bbe\u8ba1\u51b3\u7b56\u7684\u4e00\u90e8\u5206\uff0c\u4ee5\u4f18\u5316\u6a21\u578b\u5bf9\u4e8e\u5404\u79cd\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u7684\u901a\u7528\u6027\u548c\u6548\u7387\u3002<\/li>\n\n\n\n<li><strong>\u5b9e\u9a8c\u548c\u8bc4\u4f30<\/strong>\uff1a\u5728\u5f00\u53d1\u8fd9\u4e9b\u6a21\u578b\u65f6\uff0c\u4f1a\u8fdb\u884c\u5927\u91cf\u7684\u5b9e\u9a8c\u6765\u8bc4\u4f30\u4e0d\u540c\u914d\u7f6e\u4e0b\u6a21\u578b\u7684\u8868\u73b0\u3002\u5305\u62ec\u5d4c\u5165\u7ef4\u5ea6\u5728\u5185\u7684\u5404\u79cd\u53c2\u6570\u90fd\u4f1a\u88ab\u8c03\u6574\u548c\u4f18\u5316\uff0c\u4ee5\u627e\u5230\u6700\u4f73\u7684\u8bbe\u7f6e\uff0c\u65e2\u80fd\u4fdd\u6301\u9ad8\u8d28\u91cf\u7684\u8f93\u51fa\uff0c\u53c8\u80fd\u5728\u8ba1\u7b97\u548c\u5b58\u50a8\u4e0a\u5c3d\u53ef\u80fd\u9ad8\u6548\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0c1536\u4e2a\u6570\u636e\u70b9\u7684\u9009\u62e9\u53cd\u6620\u4e86\u5728\u6a21\u578b\u8bbe\u8ba1\u548c\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5bf9\u4e8e\u6548\u80fd\u3001\u8d44\u6e90\u548c\u4efb\u52a1\u5e7f\u6cdb\u6027\u7684\u7efc\u5408\u8003\u91cf\u3002\u8fd9\u6837\u7684\u5d4c\u5165\u5411\u91cf\u5927\u5c0f\u65e8\u5728\u4e3a\u591a\u79cd\u5e94\u7528\u548c\u4efb\u52a1\u63d0\u4f9b\u5747\u8861\u4e14\u5f3a\u5927\u7684\u8bed\u4e49\u8868\u793a\u80fd\u529b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u5d4c\u5165\u6a21\u578b<\/strong><\/h2>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>text-embedding-3-small<\/code> \u5d4c\u5165\u5411\u91cf\u7684\u957f\u5ea6\u4e3a 1536 for&nbsp;&nbsp;\u6216 <code>text-embedding-3-large<\/code> \u4e3a 3072\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u4f20\u5165 <a href=\"https:\/\/platform.openai.com\/docs\/api-reference\/embeddings\/create#embeddings-create-dimensions\">dimensions \u53c2\u6570<\/a>\u6765\u51cf\u5c0f\u5d4c\u5165\u7684\u7ef4\u5ea6\uff0c\u800c\u4e0d\u4f1a\u4f7f\u5d4c\u5165\u5931\u53bb\u5176\u6982\u5ff5\u8868\u793a\u5c5e\u6027\u3002\u6211\u4eec\u5c06\u5728<a href=\"https:\/\/platform.openai.com\/docs\/guides\/embeddings\/use-cases\">\u5d4c\u5165\u7528\u4f8b<\/a>\u90e8\u5206\u66f4\u8be6\u7ec6\u5730\u4ecb\u7ecd\u5d4c\u5165\u7ef4\u5ea6\u3002<\/p>\n\n\n\n<p>OpenAI \u63d0\u4f9b\u4e86\u4e24\u4e2a\u5f3a\u5927\u7684\u7b2c\u4e09\u4ee3\u5d4c\u5165\u6a21\u578b\uff08\u5728\u6a21\u578b ID \u4e2d\u7528\u8868\u793a&nbsp;<code>v3<\/code>&nbsp;\uff09\u3002\u6709\u5173\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\uff0c\u53ef\u4ee5\u9605\u8bfb\u5d4c\u5165 v3 <a href=\"https:\/\/openai.com\/blog\/new-embedding-models-and-api-updates\">\u516c\u544a<\/a>\u535a\u5ba2\u6587\u7ae0\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u91cf\u6309\u8f93\u5165\u4ee4\u724c\u5b9a\u4ef7\uff0c\u4ee5\u4e0b\u662f\u6bcf\u7f8e\u5143\u6587\u672c\u9875\u7684\u5b9a\u4ef7\u793a\u4f8b\uff08\u5047\u8bbe\u6bcf\u9875~800\u4e2a\u4ee4\u724c\uff09\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\u6a21\u578b<\/td><td>\u6bcf\u7f8e\u5143\u9875\u6570<\/td><td>EVAL MTEB \u8bc4\u4f30\u6027\u80fd<\/td><td>\u6700\u5927\u8f93\u5165\u957f\u5ea6<\/td><\/tr><tr><td>text-embedding-3-small<\/td><td>62,500<\/td><td>62.3%<\/td><td>8191<\/td><\/tr><tr><td>text-embedding-3-large<\/td><td>9,615<\/td><td>64.6%<\/td><td>8191<\/td><\/tr><tr><td>text-embedding-ada-002<\/td><td>12,500<\/td><td>61.0%<\/td><td>8191<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u4f7f\u7528\u6848\u4f8b<\/strong><\/h2>\n\n\n\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u4e00\u4e9b\u5177\u6709\u4ee3\u8868\u6027\u7684\u7528\u4f8b\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u4e9a\u9a6c\u900a\u7cbe\u54c1\u8bc4\u8bba\u6570\u636e\u96c6\u8fdb\u884c\u4ee5\u4e0b\u793a\u4f8b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u83b7\u53d6\u5d4c\u5165<\/strong><\/h3>\n\n\n\n<p>\u8be5\u6570\u636e\u96c6\u5305\u542b\u622a\u81f3 2012 \u5e74 10 \u6708\u4e9a\u9a6c\u900a\u7528\u6237\u7559\u4e0b\u7684 568\uff0c454 \u6761<a href=\"https:\/\/www.kaggle.com\/snap\/amazon-fine-food-reviews\">\u98df\u54c1\u8bc4\u8bba<\/a>\u3002\u6211\u4eec\u5c06\u4f7f\u7528 1\uff0c000 \u6761\u6700\u65b0\u8bc4\u8bba\u7684\u5b50\u96c6\u8fdb\u884c\u8bf4\u660e\u3002\u8bc4\u8bba\u662f\u82f1\u6587\u7684\uff0c\u5f80\u5f80\u662f\u6b63\u9762\u6216\u8d1f\u9762\u7684\u3002\u6bcf\u6761\u8bc4\u8bba\u90fd\u6709\u4e00\u4e2a\u4ea7\u54c1 ID\u3001\u7528\u6237 ID\u3001\u5206\u6570\u3001\u8bc4\u8bba\u6807\u9898\uff08\u6458\u8981\uff09\u548c\u8bc4\u8bba\u6b63\u6587\uff08\u6587\u672c\uff09\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\u4ea7\u54c1ID<\/td><td>\u7528\u6237ID<\/td><td>\u5f97\u5206(SCORE)<\/td><td>\u603b\u7ed3(SUMMARY)<\/td><td>\u6587\u672c(TEXT)<\/td><\/tr><tr><td>B001E4KFG0<\/td><td>A3SGXH7AUHU8GW<\/td><td>5<\/td><td>Good Quality Dog Food&nbsp;<br>\u4f18\u8d28\u72d7\u7cae<\/td><td>I have bought several of the Vitality canned&#8230;<br>\u6211\u4e70\u4e86\u51e0\u4e2a\u6d3b\u529b\u7f50\u5934&#8230;<\/td><\/tr><tr><td>B00813GRG4<\/td><td>A1D87F6ZCVE5NK<\/td><td>1<\/td><td>Not as Advertised&nbsp;<br>\u4e0d\u50cf\u5e7f\u544a\u4e0a\u6240\u8bf4\u7684\u90a3\u6837<\/td><td>Product arrived labeled as Jumbo Salted Peanut&#8230;<br>\u4ea7\u54c1\u5230\u8fbe\u65f6\u6807\u6709\u5de8\u578b\u76d0\u6e0d\u82b1\u751f&#8230;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6211\u4eec\u5c06\u8bc4\u8bba\u6458\u8981\u548c\u8bc4\u8bba\u6587\u672c\u5408\u5e76\u4e3a\u4e00\u4e2a\u7ec4\u5408\u6587\u672c\u3002\u8be5\u6a21\u578b\u5c06\u5bf9\u6b64\u7ec4\u5408\u6587\u672c\u8fdb\u884c\u7f16\u7801\u5e76\u8f93\u51fa\u5355\u4e2a\u5411\u91cf\u5d4c\u5165\u3002<\/p>\n\n\n\n<p>\u4e0b\u8f7d\u540e\uff0c\u622a\u53d6 1000 \u6761\u8bb0\u5f55\uff0c<strong>\u6587\u4ef6\u4e2d\u4e0d\u8981\u6709\u7a7a\u884c<\/strong>\uff0c\u4fdd\u5b58\u4e3a data\/fine_food_reviews_1k.csv<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4.1.1 \u52a0\u8f7d\u6570\u636e\u96c6<\/strong><\/h4>\n\n\n\n<p>\u60a8\u9700\u8981\u5b89\u88c5\uff1apandas\u3001openai\u3001transformers\u3001plotly\u3001matplotlib\u3001scikit-learn\u3001torch \uff08transformer dep\uff09\u3001torchvision \u548c scipy\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">pip install pandas openai transformers plotly matplotlib scikit-learn\npip install torch torchvision scipy tiktoken<\/pre><\/div>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import pandas as pd\nimport tiktoken\n\nfrom utils.embeddings_utils import get_embedding<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">embedding_model = \"text-embedding-3-small\"\nembedding_encoding = \"cl100k_base\"\nmax_tokens = 8000  # the maximum for text-embedding-3-small is 8191<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># load &amp; inspect dataset\ninput_datapath = \"data\/fine_food_reviews_1k.csv\"  # to save space, we provide a pre-filtered dataset\ndf = pd.read_csv(input_datapath, index_col=0)\ndf = df[[\"Time\", \"ProductId\", \"UserId\", \"Score\", \"Summary\", \"Text\"]]\ndf = df.dropna()\ndf[\"combined\"] = (\n    \"Title: \" + df.Summary.str.strip() + \"; Content: \" + df.Text.str.strip()\n)\ndf.head(2)<\/pre><\/div>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"151\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-11-1024x151.png\" alt=\"\" class=\"wp-image-2976\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-11-1024x151.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-11-300x44.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-11-768x113.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-11-1536x226.png 1536w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-11.png 1648w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># subsample to 1k most recent reviews and remove samples that are too long\ntop_n = 1000\nimport tiktoken\n\ndf = df.sort_values(\"Time\").tail(top_n * 2)  # first cut to first 2k entries, assuming less than half will be filtered out\ndf.drop(\"Time\", axis=1, inplace=True)\n\nencoding = tiktoken.get_encoding(embedding_encoding)\n\n# omit reviews that are too long to embed\ndf[\"n_tokens\"] = df.combined.apply(lambda x: len(encoding.encode(x)))\ndf = df[df.n_tokens &lt;= max_tokens].tail(top_n)\nlen(df)<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"28\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-12-1024x28.png\" alt=\"\" class=\"wp-image-2983\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-12-1024x28.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-12-300x8.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-12-768x21.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-12-1536x42.png 1536w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-12.png 1667w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u83b7\u53d6\u5d4c\u5165\u5e76\u4fdd\u5b58\u5b83\u4eec\u4ee5\u5907\u5c06\u6765\u91cd\u7528<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import os\nfrom openai import OpenAI\n\nOPENAI_API_KEY = os.getenv('OPENAI_API_KEY')\n\nclient = OpenAI(\n  api_key=OPENAI_API_KEY\n)\n\ndef get_embedding(text: str, model=\"text-embedding-3-small\", **kwargs) -&gt; List[float]:\n    # replace newlines, which can negatively affect performance.\n    text = text.replace(\"\\n\", \" \")\n\n    response = client.embeddings.create(input=[text], model=model, **kwargs)\n\n    return response.data[0].embedding\n\n# Ensure you have your API key set in your environment per the README: https:\/\/github.com\/openai\/openai-python#usage\n\n# This may take a few minutes\ndf[\"embedding\"] = df.combined.apply(lambda x: get_embedding(x, model=model='text-embedding-3-small'))\ndf.to_csv(\"data\/fine_food_reviews_with_embeddings_1k.csv\")<\/pre><\/div>\n\n\n\n<p>\u8981\u4ece\u4fdd\u5b58\u7684\u6587\u4ef6\u52a0\u8f7d\u6570\u636e\uff0c\u53ef\u4ee5\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import pandas as pd\nimport numpy as np\n\ndf = pd.read_csv('data\/fine_food_reviews_with_embeddings_1k.csv')\ndf['ada_embedding'] = df.embedding.apply(eval).apply(np.array)<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u51cf\u5c0f\u5d4c\u5165\u5c3a\u5bf8<\/strong><\/h2>\n\n\n\n<p>\u4e0e\u4f7f\u7528\u8f83\u5c0f\u7684\u5d4c\u5165\u76f8\u6bd4\uff0c\u4f7f\u7528\u8f83\u5927\u7684\u5d4c\u5165\uff08\u4f8b\u5982\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u77e2\u91cf\u5b58\u50a8\u4e2d\u4ee5\u4f9b\u68c0\u7d22\uff09\u901a\u5e38\u6210\u672c\u66f4\u9ad8\uff0c\u5e76\u4e14\u6d88\u8017\u66f4\u591a\u7684\u8ba1\u7b97\u3001\u5185\u5b58\u548c\u5b58\u50a8\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u7684\u4e24\u4e2a\u65b0\u5d4c\u5165\u6a21\u578b\u90fd\u4f7f\u7528\u4e00\u79cd\u6280\u672f\u8fdb\u884c\u8bad\u7ec3\uff0c\u8be5\u6280\u672f\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u6743\u8861\u4f7f\u7528\u5d4c\u5165\u7684\u6027\u80fd\u548c\u6210\u672c\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u7f29\u77ed\u5d4c\u5165\uff08\u5373\u4ece\u5e8f\u5217\u672b\u5c3e\u5220\u9664\u4e00\u4e9b\u6570\u5b57\uff09\uff0c\u800c\u4e0d\u4f1a\u901a\u8fc7\u4f20\u5165&nbsp;<code>dimensions<\/code>&nbsp;API \u53c2\u6570\u6765\u4f7f\u5d4c\u5165\u5931\u53bb\u5176\u6982\u5ff5\u8868\u793a\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u5728 MTEB \u57fa\u51c6\u6d4b\u8bd5\u4e2d\uff0c&nbsp;<code>text-embedding-3-large<\/code>&nbsp;\u5d4c\u5165\u53ef\u4ee5\u7f29\u77ed\u5230 256 \u7684\u5927\u5c0f\uff0c\u540c\u65f6\u4ecd\u7136\u4f18\u4e8e\u5927\u5c0f\u4e3a 1536 \u7684\u672a\u7f29\u77ed&nbsp;<code>text-embedding-ada-002<\/code>&nbsp;\u5d4c\u5165\u3002\u60a8\u53ef\u4ee5\u5728\u6211\u4eec\u7684\u5d4c\u5165 v3 \u53d1\u5e03\u535a\u5ba2\u6587\u7ae0\u4e2d\u8be6\u7ec6\u4e86\u89e3\u66f4\u6539\u7ef4\u5ea6\u5982\u4f55\u5f71\u54cd\u6027\u80fd\u3002<\/p>\n\n\n\n<p>\u901a\u5e38\uff0c\u5efa\u8bae\u5728\u521b\u5efa\u5d4c\u5165\u65f6\u4f7f\u7528\u8be5&nbsp;<code>dimensions<\/code>&nbsp;\u53c2\u6570\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5728\u751f\u6210\u5d4c\u5165\u7ef4\u5ea6\u540e\u5bf9\u5176\u8fdb\u884c\u66f4\u6539\u3002\u624b\u52a8\u66f4\u6539\u5c3a\u5bf8\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u89c4\u8303\u5316\u5d4c\u5165\u7684\u5c3a\u5bf8\uff0c\u5982\u4e0b\u6240\u793a\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from openai import OpenAI\nimport numpy as np\n\nclient = OpenAI()\n\ndef normalize_l2(x):\n    x = np.array(x)\n    if x.ndim == 1:\n        norm = np.linalg.norm(x)\n        if norm == 0:\n            return x\n        return x \/ norm\n    else:\n        norm = np.linalg.norm(x, 2, axis=1, keepdims=True)\n        return np.where(norm == 0, x, x \/ norm)\n\n\nresponse = client.embeddings.create(\n    model=\"text-embedding-3-small\", input=\"Testing 123\", encoding_format=\"float\"\n)\n\ncut_dim = response.data[0].embedding[:256]\nnorm_dim = normalize_l2(cut_dim)\n\nprint(norm_dim)<\/pre><\/div>\n\n\n\n<p>\u52a8\u6001\u66f4\u6539\u5c3a\u5bf8\u53ef\u5b9e\u73b0\u975e\u5e38\u7075\u6d3b\u7684\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u4f7f\u7528\u4ec5\u652f\u6301\u6700\u591a 1024 \u4e2a\u7ef4\u5ea6\u7684\u5d4c\u5165\u7684\u5411\u91cf\u6570\u636e\u5b58\u50a8\u65f6\uff0c\u5f00\u53d1\u4eba\u5458\u73b0\u5728\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u6211\u4eec\u7684\u6700\u4f73\u5d4c\u5165\u6a21\u578b&nbsp;<code>text-embedding-3-large<\/code>&nbsp;\uff0c\u5e76\u4e3a&nbsp;<code>dimensions<\/code>&nbsp;API \u53c2\u6570\u6307\u5b9a\u503c 1024\uff0c\u8fd9\u5c06\u7f29\u77ed\u4ece 3072 \u4e2a\u7ef4\u5ea6\u5411\u4e0b\u7684\u5d4c\u5165\u65f6\u95f4\uff0c\u4ece\u800c\u727a\u7272\u4e00\u4e9b\u51c6\u786e\u6027\u4ee5\u6362\u53d6\u8f83\u5c0f\u7684\u5411\u91cf\u5927\u5c0f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u4f7f\u7528\u5d4c\u5165\u7684\u8bed\u4e49\u6587\u672c\u641c\u7d22<\/strong><\/h2>\n\n\n\n<p>\u901a\u8fc7\u5d4c\u5165\u6211\u4eec\u7684\u641c\u7d22\u67e5\u8be2\uff0c\u7136\u540e\u627e\u5230\u6700\u76f8\u4f3c\u7684\u8bc4\u8bba\uff0c\u6211\u4eec\u53ef\u4ee5\u4ee5\u975e\u5e38\u6709\u6548\u7684\u65b9\u5f0f\u4ee5\u975e\u5e38\u4f4e\u7684\u6210\u672c\u5728\u8bed\u4e49\u4e0a\u641c\u7d22\u6211\u4eec\u7684\u6240\u6709\u8bc4\u8bba\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import pandas as pd\nimport numpy as np\nfrom ast import literal_eval\n\ndatafile_path = \"data\/fine_food_reviews_with_embeddings_1k.csv\"\n\ndf = pd.read_csv(datafile_path)\ndf[\"embedding\"] = df.embedding.apply(literal_eval).apply(np.array)\n<\/pre><\/div>\n\n\n\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u6bd4\u8f83\u4e86\u67e5\u8be2\u548c\u6587\u6863\u5d4c\u5165\u7684\u4f59\u5f26\u76f8\u4f3c\u5ea6\uff0c\u5e76\u663e\u793a\u4e86top_n\u6700\u4f73\u5339\u914d\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from utils.embeddings_utils import get_embedding, cosine_similarity\n\n# search through the reviews for a specific product\ndef search_reviews(df, product_description, n=3, pprint=True):\n    product_embedding = get_embedding(\n        product_description,\n        model=\"text-embedding-3-small\"\n    )\n    df[\"similarity\"] = df.embedding.apply(lambda x: cosine_similarity(x, product_embedding))\n\n    results = (\n        df.sort_values(\"similarity\", ascending=False)\n        .head(n)\n        .combined.str.replace(\"Title: \", \"\")\n        .str.replace(\"; Content:\", \": \")\n    )\n    if pprint:\n        for r in results:\n            print(r[:200])\n            print()\n    return results\n\n#\u7f8e\u5473\u7684\u8c46\u5b50\nresults = search_reviews(df, \"delicious beans\", n=3)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Delicious and additive:  Love these chips, and I don't even like black beans.  Very tasty.  Yum!!  Plus, good for you!\n\nBlack Beans Never Tasted So Good!!!:  These are the most fantastic chips I've ever had!  I could eat the whole bag myself!  They're made with lots of whole grain and beans, and make a complete protein\n\nGreat Beans!!!:  I ordered these for my coffee themed wedding. When they arrived I had to fight off friends because they smelled and tasted so good. I literally had to hide the box to the wedding! The\n\n\u7f8e\u5473\u53c8\u4e0a\u763e\uff1a\u7231\u4e0a\u4e86\u8fd9\u4e9b\u85af\u7247\uff0c\u6211\u751a\u81f3\u4e0d\u559c\u6b22\u9ed1\u8c46\u3002\u975e\u5e38\u597d\u5403\u3002\u597d\u68d2\uff01\uff01\u800c\u4e14\uff0c\u5bf9\u4f60\u6709\u597d\u5904\uff01\n\n\u9ed1\u8c46\u4ece\u672a\u5982\u6b64\u7f8e\u5473\uff01\uff01\uff01\uff1a\u8fd9\u662f\u6211\u5403\u8fc7\u7684\u6700\u68d2\u7684\u85af\u7247\uff01\u6211\u53ef\u4ee5\u81ea\u5df1\u5403\u6389\u6574\u888b\uff01\u5b83\u4eec\u7531\u5927\u91cf\u5168\u8c37\u7269\u548c\u8c46\u5b50\u5236\u6210\uff0c\u6784\u6210\u4e86\u5b8c\u6574\u7684\u86cb\u767d\u8d28\n\n\u8d85\u8d5e\u7684\u8c46\u5b50\uff01\uff01\uff01\uff1a\u6211\u4e3a\u6211\u7684\u5496\u5561\u4e3b\u9898\u5a5a\u793c\u8ba2\u8d2d\u4e86\u8fd9\u4e9b\u8c46\u5b50\u3002\u5f53\u5b83\u4eec\u5230\u8fbe\u65f6\uff0c\u6211\u4e0d\u5f97\u4e0d\u62b5\u6321\u4f4f\u670b\u53cb\u4eec\uff0c\u56e0\u4e3a\u5b83\u4eec\u95fb\u8d77\u6765\u548c\u5c1d\u8d77\u6765\u90fd\u975e\u5e38\u597d\u3002\u6211\u771f\u7684\u4e0d\u5f97\u4e0d\u628a\u76d2\u5b50\u85cf\u5230\u5a5a\u793c\u90a3\u5929\uff01<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># \u5168\u9ea6\u610f\u5927\u5229\u9762\nresults = search_reviews(df, \"whole wheat pasta\", n=3)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Annie's Homegrown Organic Whole Wheat Shells &amp; White Cheddar Macaroni &amp; Cheese - 6 oz.:  This product is made by Annie's Inc. in Berkely, CA--made in the USA.  The product maintains excellent organic \n\nPerfect for gluten-free chocolate chip cookies:  We made chocolate chip cookies with BRM Garbanzo Bean Flour and the results were fantastic!  The brown sugar and chocolate mask any bean taste that oth\n\nWOW:  I tried to find israeli couscous in a number of upsacale grocery stores and no luck.  So I decided to try good ole Amazon.com and selected this product.  It is so good..add a few herbs and it's \n\nAnnie's Homegrown\u6709\u673a\u5168\u9ea6\u8d1d\u58f3\u5f62\u610f\u5927\u5229\u9762\u548c\u767d\u8f66\u8fbe\u5976\u916a\u901a\u5fc3\u7c89 - 6\u76ce\u53f8\uff1a\u8fd9\u6b3e\u4ea7\u54c1\u7531\u4f4d\u4e8e\u52a0\u5229\u798f\u5c3c\u4e9a\u5dde\u4f2f\u514b\u5229\u7684Annie's Inc.\u751f\u4ea7--\u7f8e\u56fd\u5236\u9020\u3002\u8be5\u4ea7\u54c1\u4fdd\u6301\u4e86\u5353\u8d8a\u7684\u6709\u673a\u54c1\u8d28\n\n\u5b8c\u7f8e\u9002\u5408\u65e0\u9eb8\u8d28\u5de7\u514b\u529b\u788e\u7247\u997c\u5e72\uff1a\u6211\u4eec\u7528BRM\u9e70\u5634\u8c46\u9762\u7c89\u505a\u4e86\u5de7\u514b\u529b\u788e\u7247\u997c\u5e72\uff0c\u7ed3\u679c\u68d2\u6781\u4e86\uff01\u7ea2\u7cd6\u548c\u5de7\u514b\u529b\u63a9\u76d6\u4e86\u5176\u4ed6\u8c46\u7c7b\u7684\u5473\u9053\n\n\u54c7\uff1a\u6211\u8bd5\u56fe\u5728\u8bb8\u591a\u9ad8\u6863\u6742\u8d27\u5e97\u4e2d\u627e\u5230\u4ee5\u8272\u5217\u5e93\u65af\u5e93\u65af\uff0c\u4f46\u6ca1\u6709\u8fd0\u6c14\u3002\u6240\u4ee5\u6211\u51b3\u5b9a\u5c1d\u8bd5\u597d\u8001\u7684Amazon.com\u5e76\u9009\u62e9\u4e86\u8fd9\u6b3e\u4ea7\u54c1\u3002\u5b83\u975e\u5e38\u597d..\u52a0\u5165\u4e00\u4e9b\u8349\u836f\uff0c\u5b83\u5c31\u53d8\u5f97\u66f4\u597d\u4e86<\/pre><\/div>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u641c\u7d22\u8fd9\u4e9b\u8bc4\u8bba\u3002\u4e3a\u4e86\u52a0\u5feb\u8ba1\u7b97\u901f\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u79cd\u7279\u6b8a\u7684\u7b97\u6cd5\uff0c\u65e8\u5728\u901a\u8fc7\u5d4c\u5165\u66f4\u5feb\u5730\u8fdb\u884c\u641c\u7d22\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># \u7cdf\u7cd5\u7684\u9001\u8d27\u670d\u52a1\nresults = search_reviews(df, \"bad delivery\", n=1)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">disappointing:  not what I was expecting in terms of the company's reputation for excellent home delivery products\n\n\u4ee4\u4eba\u5931\u671b\uff1a\u5c31\u516c\u53f8\u5728\u4f18\u8d28\u5bb6\u5ead\u9001\u8d27\u4ea7\u54c1\u65b9\u9762\u7684\u58f0\u8a89\u800c\u8a00\uff0c\u8fd9\u5e76\u4e0d\u662f\u6211\u6240\u671f\u5f85\u7684<\/pre><\/div>\n\n\n\n<p>\u6b63\u5982\u6211\u4eec\u6240\u770b\u5230\u7684\uff0c\u8fd9\u53ef\u4ee5\u7acb\u5373\u5e26\u6765\u5f88\u591a\u4ef7\u503c\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u80fd\u591f\u5feb\u901f\u627e\u5230\u4ea4\u4ed8\u5931\u8d25\u7684\u793a\u4f8b\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># \u53d8\u8d28\u7684\nresults = search_reviews(df, \"spoilt\", n=1)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">burnt:  These bags had a lot of overcooked brown pieces. also felt very greasy. Had to keep wiping my fingers on a napkin.\n\n\u70e7\u7126\u7684\uff1a\u8fd9\u4e9b\u5305\u88c5\u91cc\u6709\u5f88\u591a\u8fc7\u5ea6\u70f9\u996a\u7684\u68d5\u8272\u788e\u7247\u3002\u4e5f\u611f\u89c9\u975e\u5e38\u6cb9\u817b\u3002\u4e0d\u5f97\u4e0d\u4e00\u76f4\u7528\u9910\u5dfe\u7eb8\u64e6\u624b\u6307\u3002<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># \u5ba0\u7269\u98df\u54c1\nresults = search_reviews(df, \"pet food\", n=2)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Palatable and healthy:  Before I was educated about feline nutrition, I allowed my cats to become addicted to dry cat food. I always offered both canned and dry, but wish I would have fed them premium\n\nMy cats LOVE this \"diet\" food better than their regular food:  One of my boys needed to lose some weight and the other didn't.  I put this food on the floor for the chubby guy, and the protein-rich, n\n\n\u7f8e\u5473\u4e14\u5065\u5eb7\uff1a\u5728\u6211\u4e86\u89e3\u5230\u732b\u54aa\u8425\u517b\u77e5\u8bc6\u4e4b\u524d\uff0c\u6211\u8ba9\u6211\u7684\u732b\u54aa\u6c89\u8ff7\u4e8e\u5e72\u732b\u7cae\u3002\u6211\u603b\u662f\u63d0\u4f9b\u7f50\u88c5\u548c\u5e72\u732b\u7cae\u4e24\u79cd\uff0c\u4f46\u5e0c\u671b\u6211\u5f53\u65f6\u5c31\u7ed9\u5b83\u4eec\u5582\u98df\u4e86\u9ad8\u54c1\u8d28\u7684\n\n\u6211\u7684\u732b\u6bd4\u559c\u6b22\u5b83\u4eec\u5e38\u89c4\u98df\u7269\u66f4\u7231\u8fd9\u79cd\u201c\u51cf\u80a5\u201d\u98df\u7269\uff1a\u6211\u7684\u4e00\u4e2a\u5c0f\u5bb6\u4f19\u9700\u8981\u51cf\u80a5\uff0c\u53e6\u4e00\u4e2a\u5219\u4e0d\u9700\u8981\u3002\u6211\u628a\u8fd9\u79cd\u98df\u7269\u653e\u5728\u5730\u4e0a\u7ed9\u80d6\u4e4e\u4e4e\u7684\u90a3\u4e2a\uff0c\u800c\u8fd9\u79cd\u5bcc\u542b\u86cb\u767d\u8d28\u7684\uff0cnutrition-packed\u7684\u98df\u7269<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u5e26\u5d4c\u5165\u7684\u96f6\u6837\u672c\u5206\u7c7b<\/strong><\/h2>\n\n\n\n<p>\u5728\u6b64\u7b14\u8bb0\u672c\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u5d4c\u5165\u548c\u96f6\u6807\u8bb0\u6570\u636e\u5bf9\u8bc4\u8bba\u7684\u60c5\u7eea\u8fdb\u884c\u5206\u7c7b\uff01<\/p>\n\n\n\n<p><span style=\"color: rgb(55, 65, 81); font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Helvetica Neue&quot;, Arial, &quot;Noto Sans&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Noto Color Emoji&quot;; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;\">\u6211\u4eec\u5c06\u6b63\u9762\u60c5\u7eea\u5b9a\u4e49\u4e3a 4 \u661f\u548c 5 \u661f\u8bc4\u8bba\uff0c\u5c06\u8d1f\u9762\u60c5\u7eea\u5b9a\u4e49\u4e3a 1 \u661f\u548c 2 \u661f\u8bc4\u8bba\u30023 \u661f\u8bc4\u4ef7\u88ab\u89c6\u4e3a\u4e2d\u7acb\uff0c\u6211\u4eec\u4e0d\u4f1a\u5728\u6b64\u793a\u4f8b\u4e2d\u4f7f\u7528\u5b83\u4eec\u3002<\/span><\/p>\n\n\n\n<p>\u6211\u4eec\u5c06\u901a\u8fc7\u5d4c\u5165\u6bcf\u4e2a\u7c7b\u7684\u63cf\u8ff0\uff0c\u7136\u540e\u5c06\u65b0\u6837\u672c\u4e0e\u8fd9\u4e9b\u7c7b\u5d4c\u5165\u8fdb\u884c\u6bd4\u8f83\u6765\u6267\u884c\u96f6\u6837\u672c\u5206\u7c7b\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import pandas as pd\nimport numpy as np\nfrom ast import literal_eval\n\nfrom sklearn.metrics import classification_report\n\nEMBEDDING_MODEL = \"text-embedding-3-small\"\n\ndatafile_path = \"data\/fine_food_reviews_with_embeddings_1k.csv\"\n\ndf = pd.read_csv(datafile_path)\ndf[\"embedding\"] = df.embedding.apply(literal_eval).apply(np.array)\n\n# convert 5-star rating to binary sentiment\ndf = df[df.Score != 3]\ndf[\"sentiment\"] = df.Score.replace({1: \"negative\", 2: \"negative\", 4: \"positive\", 5: \"positive\"})\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.1 \u96f6\u6837\u672c\u5206\u7c7b<\/strong><\/h3>\n\n\n\n<p>\u4e3a\u4e86\u6267\u884c\u96f6\u6837\u672c\u5206\u7c7b\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u6ca1\u6709\u4efb\u4f55\u8bad\u7ec3\u7684\u60c5\u51b5\u4e0b\u9884\u6d4b\u6837\u54c1\u7684\u6807\u7b7e\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u5d4c\u5165\u6bcf\u4e2a\u6807\u7b7e\u7684\u7b80\u77ed\u63cf\u8ff0\uff0c\u4f8b\u5982\u6b63\u548c\u8d1f\uff0c\u7136\u540e\u6bd4\u8f83\u6837\u672c\u5d4c\u5165\u548c\u6807\u7b7e\u63cf\u8ff0\u4e4b\u95f4\u7684\u4f59\u5f26\u8ddd\u79bb\u3002<\/p>\n\n\n\n<p>\u4e0e\u6837\u672c\u8f93\u5165\u76f8\u4f3c\u5ea6\u6700\u9ad8\u7684\u6807\u7b7e\u662f\u9884\u6d4b\u6807\u7b7e\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u9884\u6d4b\u5206\u6570\u5b9a\u4e49\u4e3a\u5230\u6b63\u6807\u7b7e\u548c\u8d1f\u6807\u7b7e\u7684\u4f59\u5f26\u8ddd\u79bb\u4e4b\u95f4\u7684\u5dee\u503c\u3002\u8be5\u5206\u6570\u53ef\u7528\u4e8e\u7ed8\u5236\u7cbe\u786e\u53ec\u56de\u7387\u66f2\u7ebf\uff0c\u8be5\u66f2\u7ebf\u53ef\u7528\u4e8e\u901a\u8fc7\u9009\u62e9\u4e0d\u540c\u7684\u9608\u503c\u5728\u7cbe\u786e\u7387\u548c\u53ec\u56de\u7387\u4e4b\u95f4\u9009\u62e9\u4e0d\u540c\u7684\u6743\u8861\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from embeddings_utils import cosine_similarity, get_embedding\nfrom sklearn.metrics import PrecisionRecallDisplay\n\ndef evaluate_embeddings_approach(\n    labels = ['negative', 'positive'],\n    model = EMBEDDING_MODEL,\n):\n    label_embeddings = [get_embedding(label, model=model) for label in labels]\n\n    def label_score(review_embedding, label_embeddings):\n        return cosine_similarity(review_embedding, label_embeddings[1]) - cosine_similarity(review_embedding, label_embeddings[0])\n\n    probas = df[\"embedding\"].apply(lambda x: label_score(x, label_embeddings))\n    preds = probas.apply(lambda x: 'positive' if x&gt;0 else 'negative')\n\n    report = classification_report(df.sentiment, preds)\n    print(report)\n\n    display = PrecisionRecallDisplay.from_predictions(df.sentiment, probas, pos_label='positive')\n    _ = display.ax_.set_title(\"2-class Precision-Recall curve\")\n\nevaluate_embeddings_approach(labels=['negative', 'positive'], model=EMBEDDING_MODEL)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"880\" height=\"914\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-25.png\" alt=\"\" class=\"wp-image-3150\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-25.png 880w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-25-289x300.png 289w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-25-768x798.png 768w\" sizes=\"auto, (max-width: 880px) 100vw, 880px\" \/><\/figure>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u4e2a\u5206\u7c7b\u5668\u5df2\u7ecf\u8868\u73b0\u5f97\u975e\u5e38\u597d\u4e86\u3002\u6211\u4eec\u4f7f\u7528\u4e86\u76f8\u4f3c\u6027\u5d4c\u5165\u548c\u6700\u7b80\u5355\u7684\u6807\u7b7e\u540d\u79f0\u3002\u8ba9\u6211\u4eec\u5c1d\u8bd5\u901a\u8fc7\u4f7f\u7528\u66f4\u5177\u63cf\u8ff0\u6027\u7684\u6807\u7b7e\u540d\u79f0\u548c\u641c\u7d22\u5d4c\u5165\u6765\u6539\u8fdb\u8fd9\u4e00\u70b9\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">evaluate_embeddings_approach(labels=['An Amazon review with a negative sentiment.', 'An Amazon review with a positive sentiment.'])\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"906\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-26.png\" alt=\"\" class=\"wp-image-3152\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-26.png 900w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-26-298x300.png 298w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-26-150x150.png 150w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-26-768x773.png 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<p><span style=\"color: rgb(55, 65, 81); font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Helvetica Neue&quot;, Arial, &quot;Noto Sans&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Noto Color Emoji&quot;; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;\">\u4f7f\u7528\u641c\u7d22\u5d4c\u5165\u548c\u63cf\u8ff0\u6027\u540d\u79f0\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u6027\u80fd\u3002<\/span><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">evaluate_embeddings_approach(labels=['An Amazon review with a negative sentiment.', 'An Amazon review with a positive sentiment.'])\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"910\" height=\"921\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-27.png\" alt=\"\" class=\"wp-image-3154\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-27.png 910w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-27-296x300.png 296w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-27-768x777.png 768w\" sizes=\"auto, (max-width: 910px) 100vw, 910px\" \/><\/figure>\n\n\n\n<p>\u5982\u4e0a\u6240\u793a\uff0c\u5e26\u6709\u5d4c\u5165\u7684\u96f6\u6837\u672c\u5206\u7c7b\u53ef\u4ee5\u4ea7\u751f\u5f88\u597d\u7684\u7ed3\u679c\uff0c\u5c24\u5176\u662f\u5f53\u6807\u7b7e\u6bd4\u7b80\u5355\u7684\u5355\u8bcd\u66f4\u5177\u63cf\u8ff0\u6027\u65f6\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. \u7528\u6237\u548c\u4ea7\u54c1\u5d4c\u5165<\/strong><\/h2>\n\n\n\n<p>\u6211\u4eec\u6839\u636e\u8bad\u7ec3\u96c6\u8ba1\u7b97\u7528\u6237\u548c\u4ea7\u54c1\u5d4c\u5165\uff0c\u5e76\u5728\u770b\u4e0d\u89c1\u7684\u6d4b\u8bd5\u96c6\u4e0a\u8bc4\u4f30\u7ed3\u679c\u3002\u6211\u4eec\u5c06\u901a\u8fc7\u7ed8\u5236\u7528\u6237\u548c\u4ea7\u54c1\u76f8\u4f3c\u6027\u4e0e\u8bc4\u8bba\u5206\u6570\u6765\u8bc4\u4f30\u7ed3\u679c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.1. \u8ba1\u7b97\u7528\u6237\u548c\u4ea7\u54c1\u5d4c\u5165<\/strong><\/h3>\n\n\n\n<p>\u6211\u4eec\u53ea\u9700\u5bf9\u8bad\u7ec3\u96c6\u4e2d\u5173\u4e8e\u540c\u4e00\u4ea7\u54c1\u6216\u540c\u4e00\u7528\u6237\u64b0\u5199\u7684\u6240\u6709\u8bc4\u8bba\u8fdb\u884c\u5e73\u5747\u6765\u8ba1\u7b97\u8fd9\u4e9b\u5d4c\u5165\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom ast import literal_eval\n\ndf = pd.read_csv('data\/fine_food_reviews_with_embeddings_1k.csv', index_col=0)  # note that you will need to generate this file to run the code below\ndf.head(2)<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"146\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-28-1024x146.png\" alt=\"\" class=\"wp-image-3158\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-28-1024x146.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-28-300x43.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-28-768x110.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-28-1536x219.png 1536w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-28.png 1647w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">df['babbage_similarity'] = df[\"embedding\"].apply(literal_eval).apply(np.array)\nX_train, X_test, y_train, y_test = train_test_split(df, df.Score, test_size = 0.2, random_state=42)\n\nuser_embeddings = X_train.groupby('UserId').babbage_similarity.apply(np.mean)\nprod_embeddings = X_train.groupby('ProductId').babbage_similarity.apply(np.mean)\nlen(user_embeddings), len(prod_embeddings)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">(775, 189)<\/pre><\/div>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5927\u591a\u6570\u7528\u6237\u548c\u4ea7\u54c1\u5728 50k \u793a\u4f8b\u4e2d\u53ea\u51fa\u73b0\u8fc7\u4e00\u6b21\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.2. \u8bc4\u4f30\u5d4c\u5165<\/strong><\/h3>\n\n\n\n<p>\u4e3a\u4e86\u8bc4\u4f30\u8fd9\u4e9b\u5efa\u8bae\uff0c\u6211\u4eec\u67e5\u770b\u4e86\u7528\u6237\u548c\u4ea7\u54c1\u5d4c\u5165\u5728\u770b\u4e0d\u89c1\u7684\u6d4b\u8bd5\u96c6\u4e2d\u7684\u8bc4\u8bba\u4e2d\u7684\u76f8\u4f3c\u6027\u3002\u6211\u4eec\u8ba1\u7b97\u7528\u6237\u548c\u4ea7\u54c1\u5d4c\u5165\u4e4b\u95f4\u7684\u4f59\u5f26\u8ddd\u79bb\uff0c\u8fd9\u7ed9\u4e86\u6211\u4eec\u4e00\u4e2a\u4ecb\u4e8e 0 \u548c 1 \u4e4b\u95f4\u7684\u76f8\u4f3c\u6027\u5206\u6570\u3002\u7136\u540e\uff0c\u6211\u4eec\u901a\u8fc7\u8ba1\u7b97\u6240\u6709\u9884\u6d4b\u5206\u6570\u4e2d\u76f8\u4f3c\u6027\u5206\u6570\u7684\u767e\u5206\u4f4d\u6570\uff0c\u5c06\u5206\u6570\u5f52\u4e00\u5316\u4e3a\u5728 0 \u548c 1 \u4e4b\u95f4\u5e73\u5747\u5206\u914d\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from embeddings_utils import cosine_similarity\n\n# evaluate embeddings as recommendations on X_test\ndef evaluate_single_match(row):\n    user_id = row.UserId\n    product_id = row.ProductId\n    try:\n        user_embedding = user_embeddings[user_id]\n        product_embedding = prod_embeddings[product_id]\n        similarity = cosine_similarity(user_embedding, product_embedding)\n        return similarity\n    except Exception as e:\n        return np.nan\n\nX_test['cosine_similarity'] = X_test.apply(evaluate_single_match, axis=1)\nX_test['percentile_cosine_similarity'] = X_test.cosine_similarity.rank(pct=True)\n<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u901a8.2.1 \u8fc7\u8bc4\u5206\u53ef\u89c6\u5316\u4f59\u5f26(cosine)\u76f8\u4f3c\u5ea6<\/strong><\/h4>\n\n\n\n<p>\u6211\u4eec\u5c06\u4f59\u5f26\u76f8\u4f3c\u5ea6\u5206\u6570\u6309\u8bc4\u8bba\u5206\u6570\u5206\u7ec4\uff0c\u5e76\u7ed8\u5236\u6bcf\u4e2a\u8bc4\u8bba\u5206\u6570\u7684\u4f59\u5f26\u76f8\u4f3c\u6027\u5206\u6570\u5206\u5e03\u56fe\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">!pip install statsmodels<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import matplotlib.pyplot as plt\nimport statsmodels.api as sm\n\n\ncorrelation = X_test[['percentile_cosine_similarity', 'Score']].corr().values[0,1]\nprint('Correlation between user &amp; vector similarity percentile metric and review number of stars (score): %.2f%%' % (100*correlation))\n\n# boxplot of cosine similarity for each score\nX_test.boxplot(column='percentile_cosine_similarity', by='Score')\nplt.title('')\nplt.show()\nplt.close()\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"630\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-29-1024x630.png\" alt=\"\" class=\"wp-image-3166\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-29-1024x630.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-29-300x184.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-29-768x472.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-29.png 1174w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230\u4e00\u4e2a\u75b2\u8f6f\u7684\u8d8b\u52bf\uff0c\u8868\u660e\u7528\u6237\u548c\u4ea7\u54c1\u5d4c\u5165\u4e4b\u95f4\u7684\u76f8\u4f3c\u5ea6\u5f97\u5206\u8d8a\u9ad8\uff0c\u8bc4\u8bba\u5f97\u5206\u5c31\u8d8a\u9ad8\u3002\u56e0\u6b64\uff0c\u7528\u6237\u548c\u4ea7\u54c1\u5d4c\u5165\u53ef\u4ee5\u5f31\u9884\u6d4b\u8bc4\u8bba\u5206\u6570 &#8211; \u751a\u81f3\u5728\u7528\u6237\u6536\u5230\u4ea7\u54c1\u4e4b\u524d\uff01<\/p>\n\n\n\n<p>\u7531\u4e8e\u8be5\u4fe1\u53f7\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u66f4\u5e38\u7528\u7684\u534f\u540c\u6ee4\u6ce2\u4e0d\u540c\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u4f5c\u4e3a\u9644\u52a0\u529f\u80fd\uff0c\u7565\u5fae\u63d0\u9ad8\u73b0\u6709\u95ee\u9898\u7684\u6027\u80fd\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. \u805a\u7c7b(Clustering)<\/strong><\/h2>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684 k \u5747\u503c\u7b97\u6cd5\u6765\u6f14\u793a\u5982\u4f55\u8fdb\u884c\u805a\u7c7b\u3002\u805a\u7c7b\u5206\u6790\u53ef\u4ee5\u5e2e\u52a9\u53d1\u73b0\u6570\u636e\u4e2d\u6709\u4ef7\u503c\u7684\u9690\u85cf\u5206\u7ec4<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># imports\nimport numpy as np\nimport pandas as pd\nfrom ast import literal_eval\n\n# load data\ndatafile_path = \".\/data\/fine_food_reviews_with_embeddings_1k.csv\"\n\ndf = pd.read_csv(datafile_path)\ndf[\"embedding\"] = df.embedding.apply(literal_eval).apply(np.array)  # convert string to numpy array\nmatrix = np.vstack(df.embedding.values)\nmatrix.shape\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">(1000, 1536)<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9.1 \u4f7f\u7528 K-means \u67e5\u627e\u805a\u7c7b<\/strong><\/h3>\n\n\n\n<p>\u6211\u4eec\u5c55\u793a\u4e86 K \u5747\u503c\u7684\u6700\u7b80\u5355\u7528\u6cd5\u3002\u60a8\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u60a8\u7684\u7528\u4f8b\u7684\u96c6\u7fa4\u6570\u91cf\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from sklearn.cluster import KMeans\n\nn_clusters = 4\n\nkmeans = KMeans(n_clusters=n_clusters, init=\"k-means++\", random_state=42)\nkmeans.fit(matrix)\nlabels = kmeans.labels_\ndf[\"Cluster\"] = labels\n\ndf.groupby(\"Cluster\").Score.mean().sort_values()\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"704\" height=\"164\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-30.png\" alt=\"\" class=\"wp-image-3171\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-30.png 704w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-30-300x70.png 300w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from sklearn.manifold import TSNE\nimport matplotlib\nimport matplotlib.pyplot as plt\n\ntsne = TSNE(n_components=2, perplexity=15, random_state=42, init=\"random\", learning_rate=200)\nvis_dims2 = tsne.fit_transform(matrix)\n\nx = [x for x, y in vis_dims2]\ny = [y for x, y in vis_dims2]\n\nfor category, color in enumerate([\"purple\", \"green\", \"red\", \"blue\"]):\n    xs = np.array(x)[df.Cluster == category]\n    ys = np.array(y)[df.Cluster == category]\n    plt.scatter(xs, ys, color=color, alpha=0.3)\n\n    avg_x = xs.mean()\n    avg_y = ys.mean()\n\n    plt.scatter(avg_x, avg_y, marker=\"x\", color=color, s=100)\nplt.title(\"Clusters identified visualized in language 2d using t-SNE\")\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"863\" height=\"698\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-31.png\" alt=\"\" class=\"wp-image-3173\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-31.png 863w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-31-300x243.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-31-768x621.png 768w\" sizes=\"auto, (max-width: 863px) 100vw, 863px\" \/><\/figure>\n\n\n\n<p>\u4e8c\u7ef4\u6295\u5f71\u4e2d\u805a\u7c7b\u7684\u53ef\u89c6\u5316\u3002\u5728\u8fd9\u6b21\u8fd0\u884c\u4e2d\uff0c\u7eff\u8272\u96c6\u7fa4 \uff08#1\uff09 \u4f3c\u4e4e\u4e0e\u5176\u4ed6\u96c6\u7fa4\u5b8c\u5168\u4e0d\u540c\u3002\u8ba9\u6211\u4eec\u770b\u770b\u6bcf\u4e2a\u96c6\u7fa4\u4e2d\u7684\u4e00\u4e9b\u793a\u4f8b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9.2 \u96c6\u7fa4\u4e2d\u7684\u6587\u672c\u6837\u672c\u548c\u96c6\u7fa4\u547d\u540d<\/strong><\/h3>\n\n\n\n<p>\u8ba9\u6211\u4eec\u5c55\u793a\u6bcf\u4e2a\u805a\u7c7b\u7684\u968f\u673a\u6837\u672c\u3002\u6211\u4eec\u5c06\u4f7f\u7528 gpt-4 \u6839\u636e\u8be5\u96c6\u7fa4\u7684 5 \u6761\u8bc4\u8bba\u7684\u968f\u673a\u6837\u672c\u6765\u547d\u540d\u96c6\u7fa4\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from openai import OpenAI\nimport os\n\n#OPENAI_API_KEY\", \"&lt;your OpenAI API key if not set as env var&gt;\")\nclient = OpenAI(api_key=os.environ.get()\n\n# Reading a review which belong to each group.\nrev_per_cluster = 5\n\nfor i in range(n_clusters):\n    print(f\"Cluster {i} Theme:\", end=\" \")\n\n    reviews = \"\\n\".join(\n        df[df.Cluster == i]\n        .combined.str.replace(\"Title: \", \"\")\n        .str.replace(\"\\n\\nContent: \", \":  \")\n        .sample(rev_per_cluster, random_state=42)\n        .values\n    )\n\n    messages = [\n        {\"role\": \"user\", \"content\": f'What do the following customer reviews have in common?\\n\\nCustomer reviews:\\n\"\"\"\\n{reviews}\\n\"\"\"\\n\\nTheme:'}\n    ]\n\n    response = client.chat.completions.create(\n        model=\"gpt-4\",\n        messages=messages,\n        temperature=0,\n        max_tokens=64,\n        top_p=1,\n        frequency_penalty=0,\n        presence_penalty=0)\n    print(response.choices[0].message.content.replace(\"\\n\", \"\"))\n\n    sample_cluster_rows = df[df.Cluster == i].sample(rev_per_cluster, random_state=42)\n    for j in range(rev_per_cluster):\n        print(sample_cluster_rows.Score.values[j], end=\", \")\n        print(sample_cluster_rows.Summary.values[j], end=\":   \")\n        print(sample_cluster_rows.Text.str[:70].values[j])\n\n    print(\"-\" * 100)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"455\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-32-1024x455.png\" alt=\"\" class=\"wp-image-3176\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-32-1024x455.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-32-300x133.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-32-768x341.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-32-1536x682.png 1536w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-32.png 1608w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u8bf7\u52a1\u5fc5\u6ce8\u610f\uff0c\u7fa4\u96c6\u4e0d\u4e00\u5b9a\u4e0e\u60a8\u6253\u7b97\u4f7f\u7528\u5b83\u4eec\u7684\u76ee\u7684\u76f8\u5339\u914d\u3002\u8f83\u5927\u7684\u805a\u7c7b\u5c06\u4e13\u6ce8\u4e8e\u66f4\u5177\u4f53\u7684\u6a21\u5f0f\uff0c\u800c\u5c11\u91cf\u805a\u7c7b\u901a\u5e38\u5c06\u4e13\u6ce8\u4e8e\u6570\u636e\u4e2d\u6700\u5927\u7684\u5dee\u5f02\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>10. \u4f7f\u7528\u5d4c\u5165\u8fdb\u884c\u5206\u7c7b<\/strong><\/h2>\n\n\n\n<p>\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u6587\u672c\u8fdb\u884c\u5206\u7c7b\u3002\u6b64\u7b14\u8bb0\u672c\u5206\u4eab\u4e86\u4e00\u4e2a\u4f7f\u7528\u5d4c\u5165\u8fdb\u884c\u6587\u672c\u5206\u7c7b\u7684\u793a\u4f8b\u3002\u5bf9\u4e8e\u8bb8\u591a\u6587\u672c\u5206\u7c7b\u4efb\u52a1\uff0c\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u5fae\u8c03\u6a21\u578b\u6bd4\u5d4c\u5165\u6a21\u578b\u505a\u5f97\u66f4\u597d\u3002\u8bf7\u53c2\u9605 <a href=\"https:\/\/cookbook.openai.com\/examples\/Fine-tuned_classification.ipynb\">Fine-tuned_classification.ipynb<\/a> \u4e2d\u7528\u4e8e\u5206\u7c7b\u7684\u5fae\u8c03\u6a21\u578b\u793a\u4f8b\u3002\u6211\u4eec\u8fd8\u5efa\u8bae\u4f7f\u7528\u6bd4\u5d4c\u5165\u7ef4\u5ea6\u66f4\u591a\u7684\u793a\u4f8b\uff0c\u6211\u4eec\u5728\u8fd9\u91cc\u6ca1\u6709\u5b8c\u5168\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u6587\u672c\u5206\u7c7b\u4efb\u52a1\u4e2d\uff0c\u6211\u4eec\u6839\u636e\u8bc4\u8bba\u6587\u672c\u7684\u5d4c\u5165\u6765\u9884\u6d4b\u98df\u54c1\u8bc4\u8bba\u7684\u5206\u6570\uff081 \u5230 5\uff09\u3002\u6211\u4eec\u5c06\u6570\u636e\u96c6\u62c6\u5206\u4e3a\u4ee5\u4e0b\u6240\u6709\u4efb\u52a1\u7684\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u771f\u5b9e\u5730\u8bc4\u4f30\u770b\u4e0d\u89c1\u7684\u6570\u636e\u7684\u6027\u80fd\u3002\u6570\u636e\u96c6\u662f\u5728 <a href=\"https:\/\/cookbook.openai.com\/examples\/Get_embeddings_from_dataset.ipynb\">Get_embeddings_from_dataset Notebook<\/a> \u4e2d\u521b\u5efa\u7684\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import pandas as pd\nimport numpy as np\nfrom ast import literal_eval\n\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, accuracy_score\n\ndatafile_path = \"data\/fine_food_reviews_with_embeddings_1k.csv\"\n\ndf = pd.read_csv(datafile_path)\ndf[\"embedding\"] = df.embedding.apply(literal_eval).apply(np.array)  # convert string to array\n\n# split data into train and test\nX_train, X_test, y_train, y_test = train_test_split(\n    list(df.embedding.values), df.Score, test_size=0.2, random_state=42\n)\n\n# train random forest classifier\nclf = RandomForestClassifier(n_estimators=100)\nclf.fit(X_train, y_train)\npreds = clf.predict(X_test)\nprobas = clf.predict_proba(X_test)\n\nreport = classification_report(y_test, preds)\nprint(report)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"615\" height=\"295\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-33.png\" alt=\"\" class=\"wp-image-3180\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-33.png 615w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-33-300x144.png 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/figure>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u8be5\u6a21\u578b\u5df2\u7ecf\u5b66\u4f1a\u4e86\u4f53\u9762\u5730\u533a\u5206\u7c7b\u522b\u30025 \u661f\u8bc4\u8bba\u603b\u4f53\u4e0a\u8868\u73b0\u6700\u4f73\uff0c\u8fd9\u5e76\u4e0d\u5947\u602a\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u6570\u636e\u96c6\u4e2d\u6700\u5e38\u89c1\u7684\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from embeddings_utils import plot_multiclass_precision_recall\n\nplot_multiclass_precision_recall(probas, y_test, [1, 2, 3, 4, 5], clf)<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"968\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-34-1024x968.png\" alt=\"\" class=\"wp-image-3182\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-34-1024x968.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-34-300x284.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-34-768x726.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/04\/\u56fe\u7247-34.png 1179w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u4e0d\u51fa\u6240\u6599\uff0c5 \u661f\u548c 1 \u661f\u8bc4\u8bba\u4f3c\u4e4e\u66f4\u5bb9\u6613\u9884\u6d4b\u3002\u4e5f\u8bb8\u6709\u4e86\u66f4\u591a\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u9884\u6d4b 2-4 \u9897\u661f\u4e4b\u95f4\u7684\u7ec6\u5fae\u5dee\u522b\uff0c\u4f46\u4eba\u4eec\u5982\u4f55\u4f7f\u7528\u4e2d\u95f4\u5206\u6570\u4e5f\u53ef\u80fd\u6709\u66f4\u591a\u7684\u4e3b\u89c2\u6027\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>11. \u4f7f\u7528\u5d4c\u5165\u7684\u56de\u5f52<\/strong><\/h2>\n\n\n\n<p>\u56de\u5f52\u610f\u5473\u7740\u9884\u6d4b\u4e00\u4e2a\u6570\u5b57\uff0c\u800c\u4e0d\u662f\u5176\u4e2d\u4e00\u4e2a\u7c7b\u522b\u3002\u6211\u4eec\u5c06\u6839\u636e\u8bc4\u8bba\u6587\u672c\u7684\u5d4c\u5165\u6765\u9884\u6d4b\u5206\u6570\u3002\u6211\u4eec\u5c06\u6570\u636e\u96c6\u62c6\u5206\u4e3a\u4ee5\u4e0b\u6240\u6709\u4efb\u52a1\u7684\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u771f\u5b9e\u5730\u8bc4\u4f30\u770b\u4e0d\u89c1\u7684\u6570\u636e\u7684\u6027\u80fd\u3002\u6570\u636e\u96c6\u662f\u5728 Get_embeddings_from_dataset Notebook \u4e2d\u521b\u5efa\u7684\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u9884\u6d4b\u7684\u662f\u8bc4\u8bba\u7684\u5206\u6570\uff0c\u5373 1 \u5230 5 \u4e4b\u95f4\u7684\u6570\u5b57\uff081 \u661f\u4e3a\u8d1f\u9762\uff0c5 \u661f\u4e3a\u6b63\u9762\uff09\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >import pandas as pd\nimport numpy as np\nfrom ast import literal_eval\n\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error, mean_absolute_error\n\ndatafile_path = \"data\/fine_food_reviews_with_embeddings_1k.csv\"\n\ndf = pd.read_csv(datafile_path)\ndf[\"embedding\"] = df.embedding.apply(literal_eval).apply(np.array)\n\nX_train, X_test, y_train, y_test = train_test_split(list(df.embedding.values), df.Score, test_size=0.2, random_state=42)\n\nrfr = RandomForestRegressor(n_estimators=100)\nrfr.fit(X_train, y_train)\npreds = rfr.predict(X_test)\n\nmse = mean_squared_error(y_test, preds)\nmae = mean_absolute_error(y_test, preds)\n\nprint(f\"text-embedding-3-small performance on 1k Amazon reviews: mse={mse:.2f}, mae={mae:.2f}\")\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >text-embedding-3-small performance on 1k Amazon reviews: mse=0.53, mae=0.54<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >bmse = mean_squared_error(y_test, np.repeat(y_test.mean(), len(y_test)))\nbmae = mean_absolute_error(y_test, np.repeat(y_test.mean(), len(y_test)))\nprint(\n    f\"Dummy mean prediction performance on Amazon reviews: mse={bmse:.2f}, mae={bmae:.2f}\"\n)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >Dummy mean prediction performance on Amazon reviews: mse=1.60, mae=1.01<\/pre><\/div>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5d4c\u5165\u80fd\u591f\u9884\u6d4b\u5206\u6570\uff0c\u6bcf\u4e2a\u5206\u6570\u9884\u6d4b\u7684\u5e73\u5747\u8bef\u5dee\u4e3a 0.53\u3002\u8fd9\u5927\u81f4\u76f8\u5f53\u4e8e\u5b8c\u7f8e\u5730\u9884\u6d4b\u4e86\u4e00\u534a\u7684\u8bc4\u8bba\uff0c\u5e76\u51cf\u5c11\u4e86\u4e00\u534a\u7684\u4e00\u661f\u3002<\/p>\n\n\n\n<p>\u60a8\u8fd8\u53ef\u4ee5\u8bad\u7ec3\u5206\u7c7b\u5668\u6765\u9884\u6d4b\u6807\u7b7e\uff0c\u6216\u4f7f\u7528\u73b0\u6709 ML \u6a21\u578b\u4e2d\u7684\u5d4c\u5165\u6765\u5bf9\u81ea\u7531\u6587\u672c\u8981\u7d20\u8fdb\u884c\u7f16\u7801\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4ec0\u4e48\u662f\u5d4c\u5165\uff1f OpenAI\u7684\u6587\u672c\u5d4c\u5165\u8861\u91cf\u6587\u672c\u5b57\u7b26\u4e32\u7684\u76f8\u5173\u6027\u3002\u5d4c\u5165\u901a\u5e38\u7528\u4e8e\uff1a \u5d4c\u5165\u662f\u6d6e\u70b9\u6570\u7684\u5411\u91cf\uff08\u5217\u8868\uff09\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[289,442,312,43],"tags":[242,425,314,426],"class_list":["post-2945","post","type-post","status-publish","format-standard","hentry","category-gpt","category-llms","category-openai","category-infoarticle","tag-chatgpt","tag-embeddings","tag-openai-api","tag-426"],"views":1954,"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/2945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2945"}],"version-history":[{"count":85,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/2945\/revisions"}],"predecessor-version":[{"id":3189,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/2945\/revisions\/3189"}],"wp:attachment":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}