pgai を軽く試してみた
前回 ↓ の記事 で取り上げた pgvectorscale と同時に Timescale 社が OSS としてリリースした PostgreSQL 拡張機能(extension)、pgai を少しだけ試してみました。
pgai とは
コンセプトとしては Amazon Aurora ML の PostgreSQL での Bedrock 統合と同じで、DB から直接生成 AI(LLM)の API 呼び出しを可能にするものです。
サポート対象の API
2024/8/12 現在の最新バージョンは 0.3.0 で、このバージョンでは
- OpenAI
- Ollama
- Anthropic
- Cohere
の API をサポートしているようです(サポートしている機能はそれぞれ異なります)。
具体的な対応モデル・機能および使い方について網羅的に示すドキュメントは残念ながら現時点では存在しないようですが、テストコードが参考になります。
また、pgai の機能(関数)本体はストアドファンクションとしてplpython3u
を使って実装されているので、ストアドファンクション定義の SQL 文を見ると実装内容がわかります。
軽く試してみた
前回の pgvectorscale の記事のときと同様、pgvector + pgvectorscale + pgai をあらかじめ内包する TimescaleDB-HA コンテナを Docker で起動して、Cohere の Embed(多言語版バージョン 3.0)を呼び出してみます。
Dokcer コンテナ起動
前回同様公式ドキュメントどおりの手順で起動しました。
docker pull timescale/timescaledb-ha:pg16
docker run -d --name timescaledb -p 5432:5432 -e POSTGRES_PASSWORD=password timescale/timescaledb-ha:pg16
pgai 有効化
CASCADE
を付けて有効化すると、あわせて pgvector および plpython3u が有効化されます。
postgres=# CREATE EXTENSION IF NOT EXISTS ai CASCADE;
NOTICE: installing required extension "vector"
NOTICE: installing required extension "plpython3u"
CREATE EXTENSION
Cohere の API キーを設定
設定方法は
-
postgresql.conf
への追記 -
SET
コマンド- 管理者権限の場合は
ALTER SYSTEM SET
も可
- 管理者権限の場合は
SELECT set_config()
のいずれかを使います。
以下を参考にしてください。
ここでは、SELECT set_config()
を使って設定します。
postgres=# SELECT set_config('ai.cohere_api_key', '【CohereのAPIキー】', false) is not null as set_config;
set_config
------------
t
(1 row)
Cohere Embed を試す
トリガーではなく直接SELECT
から試してみます。
postgres=# SELECT cohere_embed('embed-multilingual-v3.0', 'Cohere Embedで埋め込みベクトル取得を試します', _input_type=>'search_document');
cohere_embed
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[0.050689697,0.07763672,-0.027038574,-0.0004081726,0.011169434,0.02381897,-0.020355225,-0.070617676,-0.009254456,-0.039276123,-0.012374878,-0.046569824,-0.000426054,-0.009521484,-0.066711426,-0.0026836395,0.04284668,-0.017913818,0.038848877,0.0004529953,-0.028839111,0.020980835,0.03463745,-0.0690918,0.05050659,-0.008102417,0.042907715,0.006778717,0.044036865,-0.00038909912,-0.020187378,-0.028259277,0.056732178,0.036834717,0.0049972534,0.0006380081,0.014373779,-0.008018494,0.017181396,0.0069847107,0.038635254,-0.0058898926,-0.033721924,0.020385742,-0.04324341,0.00062704086,0.039001465,0.052459717,0.06518555,0.044433594,0.05380249,-0.031799316,-0.013893127,0.03970337,-0.030471802,0.0007762909,-0.010070801,0.042175293,0.044555664,0.01977539,0.024551392,0.02986145,-0.010787964,0.017730713,-0.030426025,-0.018127441,-0.014343262,0.009422302,0.011520386,0.02659607,-0.013404846,0.023132324,0.022262573,0.0037593842,-0.042785645,0.02607727,0.028427124,0.042022705,0.01727295,0.033233643,0.0019483566,0.0178833,0.059509277,0.008644104,0.02746582,0.01713562,0.019424438,0.015357971,0.005367279,-0.040252686,0.048217773,0.010604858,-0.018234253,0.0016565323,-0.02394104,-0.026123047,0.009170532,-0.058044434,-0.029449463,-0.06512451,-0.013366699,-0.023620605,-0.005821228,0.026168823,0.012969971,0.029968262,-0.0021629333,-0.009239197,-0.031066895,-0.0010156631,0.010910034,0.015686035,-0.011871338,-0.031463623,-0.03881836,-0.0345459,0.048736572,0.0035591125,0.028625488,-0.019195557,-0.029724121,-0.014907837,-0.010131836,0.0206604,-0.0044441223,0.00579834,-0.008506775,-0.054626465,-0.046325684,0.020126343,0.0069122314,-0.0048561096,0.0047302246,-0.0021781921,0.0231781,-0.041259766,-0.03164673,0.016601562,0.059417725,0.008422852,0.027008057,-0.0076026917,0.015716553,0.037628174,0.03390503,-0.02432251,0.0126571655,0.04800415,-0.017623901,0.00086402893,-0.021530151,-0.006324768,0.03894043,-0.039520264,0.0020599365,0.037261963,0.045013428,-0.028335571,0.01473999,-0.043670654,0.009185791,-0.093322754,-0.031555176,-0.03778076,-0.010894775,-0.03262329,0.03640747,-0.03756714,0.015220642,-0.019104004,0.01222229,0.012512207,-0.014282227,0.0132369995,0.017105103,0.019546509,-0.025115967,-0.033935547,0.006225586,0.014076233,0.010055542,-0.039245605,-0.055908203,-0.018753052,0.030578613,0.008544922,0.003572464,0.041748047,0.02305603,-0.022903442,0.0052261353,-0.074401855,-0.055389404,0.00033855438,-0.042755127,0.047943115,0.020050049,-0.016906738,0.026550293,0.04498291,-0.08630371,-0.008087158,-0.018066406,0.041015625,-0.035888672,0.049346924,-0.054992676,0.0085372925,-0.089782715,0.044647217,-0.04067993,-0.029205322,-0.032958984,0.03781128,0.02029419,-0.002368927,-0.048980713,0.008079529,0.008506775,0.028411865,-0.04711914,-0.005207062,-0.0032691956,-0.022659302,0.0045280457,0.010177612,-0.012321472,-0.017089844,0.023544312,0.011444092,0.018814087,-0.006565094,-0.0018119812,0.04812622,0.022460938,0.044891357,-0.14782715,-0.09429932,-0.09387207,-0.0074043274,0.028549194,0.06439209,-0.017288208,-0.0035552979,0.05847168,-0.005996704,-0.008003235,-0.029937744,0.0042686462,0.02142334,-0.02192688,0.022659302,0.0116119385,-0.031677246,0.013671875,-0.018310547,0.028533936,0.016174316,0.0446167,0.010269165,-0.063964844,-0.013946533,0.049041748,-0.029632568,-0.028884888,-0.04019165,0.01348114,-0.032348633,0.06719971,0.011772156,-0.02458191,-0.02079773,-0.0034675598,0.0010004044,-0.0149383545,-0.03274536,-0.014625549,-0.0077667236,-0.018341064,0.02520752,0.04852295,-0.0029087067,-0.0039901733,-0.019897461,-0.0836792,0.039978027,-0.02243042,-0.00064611435,-0.018981934,0.01675415,-0.007843018,-0.017669678,0.005634308,0.030380249,0.0015764236,-8.21352e-05,0.033111572,-0.03302002,-0.0018405914,-0.005001068,-0.07318115,0.05279541,0.043823242,-0.03286743,0.043029785,-0.009971619,-0.011749268,0.0001937151,0.02558899,-0.007221222,0.006893158,0.01878357,-0.0019016266,0.011558533,-0.06878662,0.032348633,-0.016189575,-0.07373047,-0.015541077,0.012535095,-0.013397217,-0.052368164,-0.053253174,-0.019973755,-0.035186768,0.097839355,-0.00248909,0.0061531067,-0.006729126,0.007827759,0.06518555,-0.01626587,0.032043457,-0.02545166,0.041870117,0.034484863,0.021972656,-0.0146484375,0.012901306,0.01071167,-0.028945923,-0.016616821,-0.010910034,0.0018758774,0.08050537,-0.028656006,0.0028305054,-0.11657715,-0.05456543,-0.08074951,0.025375366,0.0073928833,-0.06329346,-0.054870605,-0.06378174,0.01826477,0.014053345,0.059509277,-0.027069092,-0.009376526,0.01928711,-0.000259161,-0.04977417,-0.015350342,-0.027114868,-0.004966736,0.00065374374,-0.00045108795,0.015991211,0.01965332,-0.0011262894,0.014678955,0.025680542,0.026184082,-0.02268982,-0.020431519,-0.029174805,-0.0069122314,0.020095825,-0.05218506,-0.010971069,-0.023895264,-0.0011196136,-0.013809204,0.04385376,0.07598877,-0.039855957,-0.018295288,0.04031372,-0.029571533,-0.02041626,0.035583496,0.0064048767,-0.0075263977,0.027038574,-0.01234436,-0.024978638,-0.00793457,0.009010315,-0.02279663,0.025238037,-0.0062408447,0.0073547363,0.01158905,0.005455017,0.016326904,0.01889038,-0.008766174,0.022750854,-0.009933472,-0.013343811,0.026687622,0.01474762,-0.019302368,-0.012626648,-0.0030822754,-0.048828125,-0.026489258,-0.03857422,-0.019866943,-0.01940918,-0.031433105,-0.03262329,-0.013664246,0.0013151169,0.0034751892,0.023544312,-0.019317627,0.077941895,0.015975952,0.017059326,-0.008659363,0.026168823,-0.0039367676,0.01586914,0.0016307831,-0.013015747,-0.008613586,-0.018630981,-0.029388428,0.024734497,-0.054748535,0.029663086,-0.0057754517,0.08135986,0.0259552,-0.0132751465,0.011787415,-0.076416016,-0.0040893555,-0.02758789,-0.012489319,-0.011642456,-0.01713562,0.005130768,-0.026275635,-0.04260254,0.042022705,-0.0036392212,0.0041618347,-0.052581787,-0.0048332214,0.038391113,-0.062042236,0.018218994,-0.02319336,0.05886841,0.027542114,0.0036506653,0.010917664,-0.03189087,0.056518555,-0.07885742,-0.021774292,0.016662598,0.05886841,-0.026016235,0.009048462,0.012687683,-0.03137207,-0.04034424,0.026916504,0.017578125,-0.015464783,0.026824951,0.009178162,0.035980225,-0.008041382,0.071777344,-0.05340576,-0.019424438,0.04135132,-0.024475098,0.004055023,0.025421143,-0.05340576,0.021148682,-0.0047836304,0.019882202,-0.022613525,0.08129883,-0.014549255,0.013191223,0.010948181,0.024429321,-0.03173828,0.003578186,0.04046631,-0.064941406,0.033721924,-0.030975342,-0.03878784,0.009643555,0.030899048,0.04159546,-0.00054359436,0.04156494,-0.0018148422,0.02319336,-0.02645874,0.033172607,0.009559631,0.020324707,0.04333496,0.015960693,-0.02381897,0.010910034,-0.011001587,-0.04626465,-0.0012683868,-0.017807007,-0.02243042,-0.0023479462,0.013725281,-0.057556152,-0.06781006,0.005180359,0.0041275024,0.010055542,-0.012176514,0.0020866394,0.036865234,-0.0074806213,0.08892822,-0.006275177,-0.013145447,-0.013618469,-0.020050049,-0.014297485,0.02494812,0.0035152435,0.017669678,-0.060333252,-0.04348755,-0.020004272,0.00086688995,-0.020492554,0.020477295,0.030517578,-0.09436035,0.0335083,-0.03930664,-0.0042037964,0.044769287,0.0023384094,-0.038757324,0.02961731,0.025360107,0.011024475,-0.016723633,0.0036087036,-0.006088257,-0.010322571,-0.011871338,-0.0029506683,-0.047546387,0.0035953522,0.0093688965,-0.013046265,-0.007671356,-0.011703491,0.01914978,0.0033454895,-0.0026512146,0.0048561096,-0.01007843,-0.007858276,-0.032470703,0.0079422,0.014099121,0.007320404,-0.018218994,0.039001465,0.023605347,-0.0008072853,-0.012413025,-0.019088745,0.022903442,0.01928711,0.016235352,0.005428314,0.05441284,0.0039138794,0.012168884,-0.032409668,0.05758667,0.0418396,0.023330688,0.00447464,-0.022201538,0.035339355,0.008094788,-0.0015878677,0.038330078,0.0030345917,-0.018341064,0.008087158,0.031311035,-0.00011950731,0.014694214,-0.022598267,0.005332947,0.062683105,-0.0033283234,-0.04260254,0.023864746,0.070129395,-0.009086609,-0.037017822,0.011299133,-0.025283813,-0.045074463,-0.020690918,0.0072364807,-0.011077881,-0.055847168,0.0056114197,-0.025817871,0.009391785,0.024505615,0.0049209595,-0.03503418,0.047576904,-0.023345947,-0.01852417,-0.016937256,0.013320923,-0.01687622,-0.008911133,0.0012750626,-0.014732361,0.02758789,0.01638794,-0.030685425,-0.002910614,0.015129089,-0.008842468,0.03366089,0.0041046143,0.037963867,-0.034576416,-0.007762909,0.021484375,0.017593384,0.017181396,0.02746582,0.018096924,-0.016983032,-0.006099701,-0.0036182404,-0.018234253,0.04663086,0.05279541,0.03741455,0.06072998,-0.02923584,-0.039764404,-0.00541687,0.018600464,0.006336212,0.031433105,0.03262329,-0.026977539,0.02571106,0.017837524,0.0009174347,-0.013137817,-0.039154053,0.0151901245,-0.004917145,0.001581192,0.0758667,-0.008811951,0.026809692,-0.008872986,0.037963867,0.04486084,0.046569824,-0.046691895,-0.0049858093,-0.0072364807,0.050964355,-0.04171753,0.007785797,-0.009269714,-0.004119873,0.0062713623,-0.022857666,0.05697632,0.04550171,-0.008735657,0.008056641,-0.02017212,0.01322937,-0.026367188,0.024490356,0.019973755,-0.001329422,-0.012840271,0.045013428,0.03262329,-0.051239014,-0.03503418,-0.05508423,0.021484375,-0.0076179504,0.042755127,0.0033721924,0.018325806,0.020217896,0.016052246,0.029724121,-0.023590088,-0.003982544,0.0035686493,-0.021850586,-0.010368347,-0.045043945,0.101379395,0.0020389557,0.03262329,-0.028152466,-0.039916992,-0.057403564,0.0075187683,0.0046577454,-0.008430481,0.02406311,-0.016479492,-0.021591187,-0.007041931,0.024215698,0.019714355,0.004497528,-0.016021729,-0.02053833,0.003320694,-0.028579712,-0.054626465,0.007144928,-0.007259369,-0.0017290115,0.004322052,-0.033843994,-0.003545761,0.013656616,0.00907135,0.024246216,-0.010154724,0.011024475,0.03668213,-0.035339355,0.020004272,0.062164307,-0.041107178,0.003276825,-0.025100708,-0.025222778,-0.0056266785,-0.01133728,-0.057800293,-0.05230713,0.076171875,-0.006286621,-0.018951416,0.059661865,0.048187256,-0.0027599335,0.043395996,0.01423645,-0.056427002,-0.022766113,0.011146545,0.020462036,0.0340271,-0.004497528,-0.023880005,-0.021438599,-0.0005993843,-0.032073975,-0.009246826,-0.028869629,-0.004798889,-0.0039749146,0.025878906,-0.03390503,-0.023864746,0.042999268,-0.016113281,0.018432617,0.015602112,0.021011353,-0.0262146,0.044799805,-0.00819397,-0.006801605,0.04257202,0.04800415,0.0027618408,0.06695557,-0.0044441223,0.0021781921,-0.02418518,0.042755127,0.008903503,0.043701172,-0.024993896,-0.03692627,-0.017471313,0.02281189,-0.064331055,-0.022720337,-0.004852295,-0.030227661,0.044952393,0.0051574707,0.0013656616,0.03375244,0.013046265,-0.018157959,0.020553589,0.0146865845,0.003572464,0.02470398,0.041046143,0.018112183,0.010498047,0.055389404,-0.027023315,0.022521973,0.003944397,-0.002796173,0.06210327,-0.013336182,-0.0121154785,-0.003211975,-0.00017428398,-0.021133423,0.00674057,-0.016174316,-0.046020508,0.019073486,-0.03665161,0.020324707,0.034210205,0.007713318,-0.0059661865,-0.005847931,0.006511688,-0.02468872,0.019927979,-0.015991211,0.019424438,-0.00856781,0.010772705,-0.0093307495,0.018493652,-0.018356323,-0.0056877136,-0.004219055,0.048217773,-0.013549805,-0.060668945,-0.018997192,0.0037937164,-0.0040664673,-0.026809692,0.039764404,-0.01701355,-0.045318604,0.047332764,0.042663574,0.0067977905,0.026275635,-0.0074272156,0.009140015,0.05633545,-0.0073928833,-0.06311035,-0.014785767,-0.018615723,-0.013298035,0.030731201,0.0023155212,-0.030044556,-0.047698975,0.0012989044,-0.0066566467,0.004802704,-0.05126953,0.07128906,0.049072266,-0.039398193,0.008888245,-0.0031967163,0.011024475,0.038085938,-0.019729614,-0.012825012,0.023376465,0.0101623535,0.039733887,-0.042266846,0.03189087,0.00592041,0.0020046234,0.0004963875,0.035949707,0.008255005,0.02432251,-0.027313232,-0.011741638,0.014152527,-0.030395508,0.026260376,-0.016326904,0.0014896393,-0.0335083,-0.0014476776,-0.02633667,0.00554657,-0.015258789,-0.06427002,-0.034698486,-0.00242424,0.03878784,0.0072021484,-0.036590576,0.018997192,0.03277588,0.014976501,0.038208008,0.025558472,-0.0070495605,-0.020339966,0.03756714,-0.013885498,-0.025878906,-0.008644104,0.021057129,-0.021728516,0.020111084,-0.00042200089,0.042816162,-0.02067566,0.0011339188,0.018859863,-0.03237915,-0.028274536,0.029785156,0.034179688,-0.051361084,-0.013450623,0.011833191,-0.024124146,0.011245728,0.011627197,0.010688782,0.032440186,0.008865356,0.0014047623,0.0035514832,0.022201538,0.016540527,0.0006737709,0.019577026,0.052520752,-0.01828003,0.0023021698,0.009666443,-0.034729004,-0.01966858,-0.08654785,-0.052520752,0.017410278,0.0423584,-0.014854431,-0.026397705,0.037017822,0.038024902,0.0044670105,-0.016525269,0.0017633438,0.054138184,-0.04046631,-0.026794434,0.015853882,-0.039245605,0.024887085,-0.043182373,0.026824951,-0.06121826,-0.017654419,0.013206482,-0.0067481995,0.007167816,0.023483276,-0.0013475418,-0.022354126,0.04321289,-0.020736694,0.061065674,-0.034820557,0.01838684]
(1 row)
結果が返ってきました。
Discussion