Files
genesis-2/notebooks/embeddings-openai.ipynb
T
alexrg fed348e214
Deploy Function / deploy (push) Successful in 20s
Deploy Migrations to Production / deploy (push) Successful in 9s
fix: update query text and similarity values in embeddings example
2026-02-17 11:28:29 -06:00

6.9 KiB

import { load } from "jsr:@std/dotenv";
import OpenAI from "jsr:@openai/openai";

const env = await load({
    export: true,
});

const openai = new OpenAI();
import { createClient } from "@supabase/supabase-js";
const supabaseUrl = Deno.env.get("SUPABASE_URL") || env.SUPABASE_URL;
const supabaseKey = Deno.env.get("SUPABASE_ANON_KEY") || env.SUPABASE_ANON_KEY;
const supabase = createClient(supabaseUrl, supabaseKey);
const textos = [
  "Los vectores se usan con el cliente de OPENAI embeddings",
];

for (const contenido of textos) {
  const emb = await openai.embeddings.create({
    model: "text-embedding-3-small",
    input: contenido,
  });

  const embedding = emb.data[0].embedding;
  console.log(emb);
  

  const { error } = await supabase.from("documentos").insert({
    contenido,
    embedding,
  });

  if (error) throw error;
}

console.log("✅ Insertados textos con embeddings");
{
  object: "list",
  data: [
    {
      object: "embedding",
      index: 0,
      embedding: [
          0.017360063269734383,   0.022553985938429832,   0.025286488234996796,
         -0.009742671623826027,  -0.016438385471701622,  -0.014725150540471077,
         -0.020840751007199287,    0.02215278521180153,   -0.02024437114596367,
          0.007834257557988167,  0.0029981620609760284,   -0.04109596461057663,
          0.030165957286953926,   -0.03402615711092949,  0.0008369643473997712,
        0.00044016868923790753,  -0.027129843831062317,   -0.04248390346765518,
         -0.058683738112449646,   0.034655068069696426,   0.013738414272665977,
           0.02060219831764698,   0.016687780618667603,     0.0059637944214046,
         -0.010306521318852901,   0.014898642897605896,  -0.008826415985822678,
          0.042136918753385544,   0.009477011859416962,   -0.03953453525900841,
         -0.012198670767247677,  -0.040055014193058014,  -0.019409440457820892,
           -0.0210576169192791,    -0.0392959825694561,  -0.005996324121952057,
        -0.0022486215457320213,    0.05209103599190712,  0.0009325206046923995,
          0.013163721188902855,     0.0505296029150486,   0.014540815725922585,
         0.0027921402361243963,    0.03190087899565697, 0.00004917589103570208,
           0.03209605813026428,  -0.038363464176654816,   -0.02424553595483303,
          0.014096241444349289,    0.02559009939432144,   -0.06089576333761215,
         -0.011016755364835262,  -0.026696112006902695,   0.050659723579883575,
          0.019561246037483215,   0.050833214074373245,   0.014529972337186337,
         -0.023204581812024117,  -0.014399852603673935,  -0.009292676113545895,
          0.003149967873468995,    0.02007087878882885,   0.025503354147076607,
            0.0230094026774168,    0.04471761733293533,    0.01479020994156599,
          -0.06692461669445038,       0.03591288626194,  -0.037127330899238586,
          0.029558734968304634,  -0.017576929181814194,    0.01953955926001072,
         -0.027888871729373932, -0.0041095963679254055,  0.0004940461367368698,
         -0.011266149580478668,   -0.04200679808855057,    0.03537072241306305,
        -0.0015058581484481692,   -0.05551750585436821,   -0.05287174880504608,
          0.016373327001929283,   0.005573437083512545,      -0.01235047634691,
         -0.009005329571664333,  -0.034047845751047134,  -0.018368486315011978,
          -0.00271217105910182,   0.007731246296316385,  -0.020981714129447937,
         -0.040900785475969315,   -0.03727913647890091,  -0.059030722826719284,
          -0.04536821320652962,    0.02257567271590233,    0.08579189330339432,
          0.014605875127017498,  -0.030686434358358383, -0.0019084142986685038,
           0.02216362953186035,
        ... 1436 more items
      ]
    }
  ],
  model: "text-embedding-3-small",
  usage: { prompt_tokens: 13, total_tokens: 13 }
}
✅ Insertados textos con embeddings
const consulta = "¿Cómo son buscados vectores?";

const emb = await openai.embeddings.create({
  model: "text-embedding-3-small",
  input: consulta,
});

const query_embedding = emb.data[0].embedding;

const { data, error } = await supabase.rpc("buscar_documentos", {
  query_embedding,
  match_count: 3,
});

if (error) throw error;

console.log(`🔎 Consulta: ${consulta}\n`);
for (const r of data ?? []) {
  console.log(`- ${r.contenido}`);
  console.log(`  Similaridad: ${Number(r.similarity).toFixed(4)}\n`);
}
🔎 Consulta: ¿Cómo son buscados vectores?

- Los vectores se usan con el cliente de OPENAI embeddings
  Similaridad: 0.5639

- Supabase permite almacenar vectores usando pgvector.
  Similaridad: 0.4758

- Los embeddings convierten texto en representaciones numéricas.
  Similaridad: 0.3645