feat: add OpenAI embeddings functionality and update Supabase migration
This commit is contained in:
@@ -0,0 +1,196 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "902bff53",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import { load } from \"jsr:@std/dotenv\";\n",
|
||||
"import OpenAI from \"jsr:@openai/openai\";\n",
|
||||
"\n",
|
||||
"const env = await load({\n",
|
||||
" export: true,\n",
|
||||
"});\n",
|
||||
"\n",
|
||||
"const openai = new OpenAI();"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "3b0acbc3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import { createClient } from \"@supabase/supabase-js\";\n",
|
||||
"const supabaseUrl = Deno.env.get(\"SUPABASE_URL\") || env.SUPABASE_URL;\n",
|
||||
"const supabaseKey = Deno.env.get(\"SUPABASE_ANON_KEY\") || env.SUPABASE_ANON_KEY;\n",
|
||||
"const supabase = createClient(supabaseUrl, supabaseKey);"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "dae842ad",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{\n",
|
||||
" object: \"list\",\n",
|
||||
" data: [\n",
|
||||
" {\n",
|
||||
" object: \"embedding\",\n",
|
||||
" index: 0,\n",
|
||||
" embedding: [\n",
|
||||
" 0.017360063269734383, 0.022553985938429832, 0.025286488234996796,\n",
|
||||
" -0.009742671623826027, -0.016438385471701622, -0.014725150540471077,\n",
|
||||
" -0.020840751007199287, 0.02215278521180153, -0.02024437114596367,\n",
|
||||
" 0.007834257557988167, 0.0029981620609760284, -0.04109596461057663,\n",
|
||||
" 0.030165957286953926, -0.03402615711092949, 0.0008369643473997712,\n",
|
||||
" 0.00044016868923790753, -0.027129843831062317, -0.04248390346765518,\n",
|
||||
" -0.058683738112449646, 0.034655068069696426, 0.013738414272665977,\n",
|
||||
" 0.02060219831764698, 0.016687780618667603, 0.0059637944214046,\n",
|
||||
" -0.010306521318852901, 0.014898642897605896, -0.008826415985822678,\n",
|
||||
" 0.042136918753385544, 0.009477011859416962, -0.03953453525900841,\n",
|
||||
" -0.012198670767247677, -0.040055014193058014, -0.019409440457820892,\n",
|
||||
" -0.0210576169192791, -0.0392959825694561, -0.005996324121952057,\n",
|
||||
" -0.0022486215457320213, 0.05209103599190712, 0.0009325206046923995,\n",
|
||||
" 0.013163721188902855, 0.0505296029150486, 0.014540815725922585,\n",
|
||||
" 0.0027921402361243963, 0.03190087899565697, 0.00004917589103570208,\n",
|
||||
" 0.03209605813026428, -0.038363464176654816, -0.02424553595483303,\n",
|
||||
" 0.014096241444349289, 0.02559009939432144, -0.06089576333761215,\n",
|
||||
" -0.011016755364835262, -0.026696112006902695, 0.050659723579883575,\n",
|
||||
" 0.019561246037483215, 0.050833214074373245, 0.014529972337186337,\n",
|
||||
" -0.023204581812024117, -0.014399852603673935, -0.009292676113545895,\n",
|
||||
" 0.003149967873468995, 0.02007087878882885, 0.025503354147076607,\n",
|
||||
" 0.0230094026774168, 0.04471761733293533, 0.01479020994156599,\n",
|
||||
" -0.06692461669445038, 0.03591288626194, -0.037127330899238586,\n",
|
||||
" 0.029558734968304634, -0.017576929181814194, 0.01953955926001072,\n",
|
||||
" -0.027888871729373932, -0.0041095963679254055, 0.0004940461367368698,\n",
|
||||
" -0.011266149580478668, -0.04200679808855057, 0.03537072241306305,\n",
|
||||
" -0.0015058581484481692, -0.05551750585436821, -0.05287174880504608,\n",
|
||||
" 0.016373327001929283, 0.005573437083512545, -0.01235047634691,\n",
|
||||
" -0.009005329571664333, -0.034047845751047134, -0.018368486315011978,\n",
|
||||
" -0.00271217105910182, 0.007731246296316385, -0.020981714129447937,\n",
|
||||
" -0.040900785475969315, -0.03727913647890091, -0.059030722826719284,\n",
|
||||
" -0.04536821320652962, 0.02257567271590233, 0.08579189330339432,\n",
|
||||
" 0.014605875127017498, -0.030686434358358383, -0.0019084142986685038,\n",
|
||||
" 0.02216362953186035,\n",
|
||||
" ... 1436 more items\n",
|
||||
" ]\n",
|
||||
" }\n",
|
||||
" ],\n",
|
||||
" model: \"text-embedding-3-small\",\n",
|
||||
" usage: { prompt_tokens: 13, total_tokens: 13 }\n",
|
||||
"}\n",
|
||||
"✅ Insertados textos con embeddings\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"const textos = [\n",
|
||||
" \"Los vectores se usan con el cliente de OPENAI embeddings\",\n",
|
||||
"];\n",
|
||||
"\n",
|
||||
"for (const contenido of textos) {\n",
|
||||
" const emb = await openai.embeddings.create({\n",
|
||||
" model: \"text-embedding-3-small\",\n",
|
||||
" input: contenido,\n",
|
||||
" });\n",
|
||||
"\n",
|
||||
" const embedding = emb.data[0].embedding;\n",
|
||||
" console.log(emb);\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" const { error } = await supabase.from(\"documentos\").insert({\n",
|
||||
" contenido,\n",
|
||||
" embedding,\n",
|
||||
" });\n",
|
||||
"\n",
|
||||
" if (error) throw error;\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"console.log(\"✅ Insertados textos con embeddings\");"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "2d8fa9a9",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"🔎 Consulta: ¿Cómo se usan vectores?\n",
|
||||
"\n",
|
||||
"- Los vectores se usan con el cliente de OPENAI embeddings\n",
|
||||
" Similaridad: 0.6407\n",
|
||||
"\n",
|
||||
"- Supabase permite almacenar vectores usando pgvector.\n",
|
||||
" Similaridad: 0.5367\n",
|
||||
"\n",
|
||||
"- Los embeddings convierten texto en representaciones numéricas.\n",
|
||||
" Similaridad: 0.4706\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"const consulta = \"¿Cómo se usan vectores?\";\n",
|
||||
"\n",
|
||||
"const emb = await openai.embeddings.create({\n",
|
||||
" model: \"text-embedding-3-small\",\n",
|
||||
" input: consulta,\n",
|
||||
"});\n",
|
||||
"\n",
|
||||
"const query_embedding = emb.data[0].embedding;\n",
|
||||
"\n",
|
||||
"const { data, error } = await supabase.rpc(\"buscar_documentos\", {\n",
|
||||
" query_embedding,\n",
|
||||
" match_count: 3,\n",
|
||||
"});\n",
|
||||
"\n",
|
||||
"if (error) throw error;\n",
|
||||
"\n",
|
||||
"console.log(`🔎 Consulta: ${consulta}\\n`);\n",
|
||||
"for (const r of data ?? []) {\n",
|
||||
" console.log(`- ${r.contenido}`);\n",
|
||||
" console.log(` Similaridad: ${Number(r.similarity).toFixed(4)}\\n`);\n",
|
||||
"}\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "791a94d8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Deno",
|
||||
"language": "typescript",
|
||||
"name": "deno"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": "typescript",
|
||||
"file_extension": ".ts",
|
||||
"mimetype": "text/x.typescript",
|
||||
"name": "typescript",
|
||||
"nbconvert_exporter": "script",
|
||||
"pygments_lexer": "typescript",
|
||||
"version": "5.9.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Reference in New Issue
Block a user