Lihat di ai.google.dev | Coba notebook Colab | Lihat notebook di GitHub |
Di notebook ini, Anda akan mempelajari cara mulai menggunakan PaLM API, yang memberi Anda akses ke model bahasa besar (LLM) terbaru Google. Di sini, Anda akan mempelajari cara menggunakan fitur pembuatan embedding PaLM API, dan melihat contoh tindakan yang dapat Anda lakukan dengan embedding ini.
Penyiapan
Pertama, download dan instal library Python PaLM API.
pip install -U google-generativeai
import numpy as np
import google.generativeai as palm
Mengambil Kunci API
Untuk memulai, Anda harus membuat kunci API.
palm.configure(api_key='PALM_KEY')
Apa yang dimaksud dengan embedding?
Embedding adalah teknik yang digunakan untuk merepresentasikan teks (seperti kata, kalimat, atau seluruh paragraf) sebagai daftar angka floating point dalam array. Angka-angka ini tidak acak. Ide utamanya adalah teks dengan makna yang serupa akan memiliki embeddings yang serupa. Anda dapat menggunakan hubungan di antara mereka untuk banyak tugas penting.
Pembuatan embedding
Di bagian ini, Anda akan melihat cara membuat embeddings untuk sebuah teks menggunakan fungsi palm.generate_embeddings
PaLM API. Berikut adalah daftar model yang mendukung fungsi ini.
for model in palm.list_models():
if 'embedText' in model.supported_generation_methods:
print(model.name)
models/embedding-gecko-001
Gunakan fungsi palm.generate_embeddings
dan teruskan nama model serta teks tertentu. Anda akan mendapatkan daftar nilai floating point. Mulai dengan kueri "Apa yang dimakan tupai?" dan melihat bagaimana hubungan
dua {i>string<i} yang berbeda dengannya.
x = 'What do squirrels eat?'
close_to_x = 'nuts and acorns'
different_from_x = 'This morning I woke up in San Francisco, and took a walk to the Bay Bridge. It was a good, sunny morning with no fog.'
model = "models/embedding-gecko-001"
# Create an embedding
embedding_x = palm.generate_embeddings(model=model, text=x)
embedding_close_to_x = palm.generate_embeddings(model=model, text=close_to_x)
embedding_different_from_x = palm.generate_embeddings(model=model, text=different_from_x)
print(embedding_x)
{'embedding': [-0.025894878, -0.02103396, 0.003574992, 0.00822288, 0.03276648, -0.10068223, -0.037702546, 0.01079403, 0.0001406235, -0.029412385, 0.01919925, 0.0048481044, 0.070619866, -0.013349887, 0.028378602, -0.018658886, -0.038629908, 0.056883123, 0.06332366, 0.039849922, -0.085393265, -0.016251814, -0.025535949, 0.0049480307, 0.048581485, -0.11295683, 0.033869933, 0.015498774, -0.07306243, 0.000857902, -0.022031788, -0.005298939, -0.08311722, -0.027091762, 0.042790364, 0.023175264, 0.011238991, -0.02432924, -0.0044626957, 0.05167071, 0.023430848, 0.027325166, -0.01492389, -0.018770715, -0.003783692, 0.040971957, -0.044652887, 0.033220302, -0.05659744, -0.055191413, -0.0023204528, -0.043687623, 0.030044463, -0.015966717, -0.04318426, 0.015735775, -0.038352676, -0.005009736, -0.03289721, 0.016246213, -0.005696393, -0.0010992853, -0.02768714, -0.03534994, -0.045970507, 0.05784305, -0.026696421, -0.013302212, 0.007055761, -0.05885901, 0.03330113, 0.04399591, 0.020755561, 0.0028288597, 0.037333105, 0.0103595415, -0.01942964, 0.033088185, 0.009558319, -0.06524442, -0.07101354, -0.053975347, -0.003952934, -0.11641813, -0.039488368, -0.0033782825, -0.017735159, 0.03198736, 0.014555729, 0.050724585, -0.07849815, -0.0070436746, 0.017992217, -0.003975652, -0.0039650565, 0.08063971, -0.011685766, -0.018323965, 0.007763516, 0.012011537, 0.028457757, -0.099603206, 0.0328822, 0.0063217366, 0.051288057, 0.060445003, -0.007725884, -0.0033487668, -0.02697037, -0.04471915, 0.014793467, 0.0029390613, -0.04365732, -0.036976494, 0.05571355, -0.034228597, 0.05610819, 0.0016565409, 0.06461147, 0.012197695, -0.029221235, 0.015400638, 0.009992722, -0.0126949195, 0.027302667, 0.04309881, 0.013308768, -0.034253325, -0.028620966, 0.0032988666, 0.008901495, 0.0051033413, 0.08693829, -0.035939537, -0.00014025549, -0.0021354076, 0.043875773, -0.057092454, 0.0048032254, 0.04456835, -0.01337361, 0.018620204, -0.0037525205, 0.018113593, -0.0024051766, -0.006519982, 0.043426506, -0.028869089, -0.07003764, -0.027043046, -0.047674373, -0.036566455, -0.029664699, 0.054604772, 0.056459025, 0.016209831, 0.06588335, 0.07294827, -0.07351654, -0.050157, 0.05211485, -0.02302033, 0.022877783, 0.013553745, -0.019406103, -0.0058154585, 0.0373227, 0.0052685454, 0.02164789, -0.019631775, -0.015719362, -0.06862338, 0.021698158, -0.013781832, 0.06955018, -0.023942512, -0.018029014, -0.018007774, -0.0059923544, -0.02771734, -0.0019507131, -0.069619514, 0.054189045, 0.0021985532, -0.01132558, 0.015128105, 0.015424623, -0.038302787, -0.038970694, 0.044268098, 0.015156813, 0.030262465, -0.0010455108, -0.032175235, -0.03357542, -9.529959e-05, 0.062028274, -0.10134925, -0.009874221, 0.051682726, -0.022124732, 0.010147164, -0.012185555, 0.03731382, -0.00059438165, -0.017981028, -0.070909515, 0.02605233, 0.06992509, 0.026033426, -0.023944097, -0.047794044, 0.0204043, 0.025562089, -0.01985736, -0.027300185, 0.029983355, -0.0821883, -0.018791717, -0.004772287, -0.02490102, -0.010111937, 0.050968856, 0.029660473, 3.4716293e-05, -0.017517656, 0.023977743, 0.022549666, 0.04181301, 0.007500569, -0.0019229053, 0.023285722, -0.010899088, -0.004949611, -0.012531907, 0.041027624, -0.004620342, -0.013926477, -0.020054528, 0.026111232, -0.06232942, 0.09978252, -0.044156674, 0.061204664, 0.007044644, -0.0027112814, 0.04620226, 0.006134901, 0.03983195, -0.009853767, 0.0137631735, -0.07085734, 0.009606741, -0.008636412, 0.050337072, 0.045284208, -0.0032710661, -0.016086245, 0.008386805, -0.007903436, 0.0350885, 0.0025110857, 0.04684593, 0.12780859, -0.038998656, -0.029157333, -0.029113598, 0.0074333544, 0.05532698, -0.034412585, -0.00013683736, -0.020530468, 0.06506163, 0.0019480588, 0.0030335467, -0.018495142, -0.054084025, 0.023021378, -0.010500294, -0.007759436, -0.020039978, -0.017755102, 0.0006766737, 0.014525485, -0.026014434, 0.002474586, -0.027173916, 0.0093613025, 0.0058087856, 0.0006998545, 0.04791365, -0.04368597, -0.015235596, 0.0069595333, 0.009612967, -0.0009247106, 0.033619776, -0.00649697, -0.04766721, 0.0391879, -0.010284179, -0.006610166, -0.0020641836, -0.05440346, -0.007050968, -0.015853178, -0.031741284, -0.02172385, 0.03021658, -0.0012069787, 0.050265886, 0.04510601, -0.024716277, -0.05543306, -0.06419837, -0.014273427, -0.023703339, 0.0017521745, -0.056149185, 0.0069642677, 0.0065768356, 0.035255834, 0.039023213, 0.016403731, 0.025051782, 0.00695039, -0.05579997, 0.013183741, 0.08474835, -0.012680079, 0.0041794777, 0.02355896, -0.07197163, 0.024911461, -0.018766653, 0.025204346, 0.0048066434, 0.04904056, 0.016669538, -0.037882168, -0.021643393, 0.0053031743, -0.031009668, -0.016543044, -0.020345997, -0.005761681, -0.0743119, -0.02601627, -0.023271384, -0.07075993, -0.0029876109, 0.0066218525, -0.061091717, 0.032953493, 0.03662513, 0.010290128, 0.05418312, -0.03828874, 0.03312786, -0.014862627, -0.03720938, 0.018570531, -0.020742243, 0.048026983, 0.005438336, 0.020241424, -0.04405181, 0.030792728, 0.033958763, -0.023588262, 0.037658524, 0.010072951, 0.0064869304, 0.019048406, -0.06919818, -0.017083945, -0.016801478, 0.0027415873, 0.008172279, 0.0019755305, -0.057162683, -0.0053946367, 0.0014972482, -0.033361986, -0.0033606717, 0.03242665, 0.072544955, 0.02279949, -0.046871353, -0.06308129, 0.029209439, 0.011341486, 0.032790348, -0.020073028, -0.0044093695, 0.08292041, -0.03140556, 0.009308279, -0.004211382, -0.052444175, 0.0180874, 0.008575959, -0.0013550716, -0.07186043, 0.028372435, 0.024996122, 0.027749002, 0.016944503, -0.014632978, -0.06674174, -0.043031745, -0.044137582, 0.03530514, 0.030504197, 0.060496386, -0.06423886, 0.012235539, -0.05830343, -0.015868725, 0.041861057, 0.027080601, -0.014182999, -0.028095996, 0.0016349283, 0.010679886, 0.048808616, -0.058294244, -0.010633062, -0.056791265, -0.027161647, -0.030019993, -0.010299281, -0.03821823, -0.016588321, -0.0059704296, -0.053497788, 0.05661912, 0.005010262, -0.020186698, -0.03151958, -0.07490499, 0.045715272, -0.03747153, 0.02902543, 0.015007152, -0.01799195, 0.0079564275, -0.028715475, -0.018788284, -0.041037183, 0.012932907, -0.0072463937, -0.0046510296, 0.052094106, 0.047214568, -0.05604256, 0.006124289, -0.06112983, -0.028900363, -0.0033062366, -0.016411366, -0.03985708, -0.005927899, 0.027991273, -0.034023542, 0.0023991684, 0.020010024, 0.014298016, 0.017212953, 0.002652654, -0.08308305, 0.01726592, 0.013845524, 0.0065021385, 0.0364733, 0.020361774, 0.09685079, 0.04039578, 0.016480403, -0.08329836, -0.06590067, 0.00012861127, -0.055775307, 0.0065172235, -0.018937778, -0.021399701, 0.0004559998, -0.0097613875, -0.003239602, 0.0041429265, 0.059930306, -0.01656465, 0.018544743, -0.03232914, 0.006037772, -0.06402926, 0.05761484, -0.02093143, 0.018229362, 0.024098346, 0.025045564, -0.009451666, -0.010259512, 0.006660359, -0.029620942, -0.03495546, -0.06783166, -0.03193859, -0.04261954, 0.027878316, 0.023951625, 0.016354026, -0.0015310713, -0.05785183, -0.04868827, -0.06779814, -0.09212996, 0.04355289, 0.02634198, 0.045933742, -0.012108333, -0.017381534, 0.012251423, 0.035591044, 0.05024221, 0.056855064, 0.0101336455, -0.009532219, -0.054251555, 0.034745548, 0.020292252, 0.033525895, -0.040225316, -0.00015249893, -0.07806101, 0.0075722514, 0.015309747, 0.022623314, 0.06536824, 0.064232446, -0.01557734, -0.04813796, -0.013913105, 0.020742541, 0.060864896, -0.056623433, 0.057601452, -1.6570028e-05, 0.010925783, 0.0036125665, 0.032784764, -0.0801319, -0.048450164, 0.06296668, 0.02989288, -0.011754737, -0.0010066505, -0.05441974, -0.017106231, -0.04285682, -0.005424776, -0.028312048, -0.0022843084, -0.02028908, -0.007416978, 0.016722959, 0.03343588, -0.049168676, 0.003828647, 0.043084797, -0.011436926, -0.017679023, -0.012748326, -0.015104218, 0.008225339, -0.005965197, -0.010827806, -0.015990732, 0.031933613, 0.01862576, -0.013171726, 0.007987761, -0.018449496, 0.041906953, -0.020788714, 0.03404006, -0.00086082605, -0.007771558, 0.023855729, -0.00295711, -0.0085285455, -0.0556957, -0.005321175, -0.018151492, -0.011129989, -0.05183511, 0.0053123147, 0.009127998, -0.011530388, 0.009631709, 0.0041047884, -0.0353711, 0.052883077, -0.01532676, 0.03040235, 0.008731032, -0.00441319, 0.01950203, 0.014064995, 0.03141337, 0.018041868, 0.059427522, 0.048374873, -0.019928444, -0.004559623, 0.021962427, -0.08567552, -0.007796494, 0.033520035, 0.009779213, 0.05753526, 0.010492746, -0.039363436, -0.103733934, -0.024229618, 0.0062162466, -0.017748242, 0.005122951, -0.055344906, -0.010650967, 0.0309389, -0.073542334, -0.014872006, -0.003081951, 0.016437916, -0.0040901243, 0.0018574661, 0.03331834, 0.005815743, 0.022556618, 0.076257, -0.0065593896, -0.026774084, -0.016839791, 0.008689688, -0.015184644, 0.0073800148, -0.018499345, -0.036080927, 0.053406574, 0.015944907, -0.014478417, -0.021485219, -0.018035412, -0.038147416, 0.014293582, -0.021055873, 0.0314314, -0.07782329, 0.015536577, -0.031045694, 0.059434652, -0.020065695, 0.052754566, -0.08380041, 0.06855744, 0.012167185, -0.015827801, 0.04380172, 0.020258602, -0.058169313, -0.04435873, -0.013054301, -0.041333184, -0.02302342, 0.029140746, 0.00812361, 0.033690967, -0.0030892044, 0.052916355, -0.04835076, -0.0101818545, -0.05420185, -0.033779036, 0.02638142, -0.028346056, -0.02331669, -0.005781761, 0.012981267, -0.0055279816, 0.010089179, -0.04489518, -0.024379171, 0.007590703, -0.025511196, -0.06555892, 0.008145539, 0.021736145, -0.033178225, 0.026871512, -0.05637406, -0.030885229, 0.014512168, -0.008024667, 0.026689196, 0.004108927, -0.04103957, 0.0080031715, -0.0030232186, -0.036158007, 0.04256502, -0.0001681743, 0.0117336465, 0.025762333, -0.010921032, -0.0010622365, -0.07185124, 0.029530818, 0.009698986, 0.011916085, 0.0022654524, 0.07175238, 0.029233111, -0.020834876, -0.052442703, 0.011248308, 0.005422925, 0.018166017, 0.0472275, -0.013550265, 0.0350743, -0.010435109, 0.047774173, 0.021216916, -0.0026447468, -0.021085296, 0.013272342, -0.0133805, 0.02943836, -0.032338675, 0.0021435472, -0.016289461, -0.013629232, -0.03840216, 0.06655019, 0.009643849, 0.025085986, -0.018909356, -0.011246176, -0.05254555, -0.06776485, -0.02931862, 0.014850466, 0.029691922, -0.04090594, 0.0544204, 0.01552631, 0.02912549, -0.0020693596, 0.038805272, -0.009980787, 0.031122748, -0.05562063, 0.021108221, 0.0103203785, 0.044171233, 0.009732269, -0.0011330071]}
Setelah Anda membuat embedding, mari kita gunakan produk titik untuk melihat seberapa terkait close_to_x
dan different_from_x
dengan x
. Produk titik mengembalikan nilai antara -1 dan 1, dan mewakili seberapa dekat dua vektor selaras dalam hal arah ke arah mereka. Semakin dekat nilainya ke 0, semakin tidak mirip dengan objek (dalam hal ini, dua string). Semakin dekat nilai dengan 1, semakin mirip keduanya.
similar_measure = np.dot(embedding_x['embedding'], embedding_close_to_x['embedding'])
print(similar_measure)
0.7314063252924405
different_measure = np.dot(embedding_x['embedding'], embedding_different_from_x['embedding'])
print(different_measure)
0.43560702838194704
Seperti yang ditunjukkan di sini, nilai produk titik yang lebih tinggi antara penyematan x
dan close_to_x
menunjukkan keterkaitan yang lebih besar daripada penyematan x
dan different_from_x
.
Apa yang dapat Anda lakukan dengan embedding?
Anda telah membuat set embedding pertama dengan PaLM API. Namun, apa yang dapat Anda lakukan dengan daftar nilai floating point ini? Embeddings dapat digunakan untuk berbagai tugas natural language processing (NLP), termasuk:
- Penelusuran (dokumen, web, dll.)
- Sistem rekomendasi
- Pengelompokan
- Analisis sentimen/klasifikasi teks
Anda dapat menemukan contohnya di sini.