Files
genesis-2/notebooks/embeddings-openai.ipynb
T
alexrg 7ee9944ab5 feat: add @toon-format/toon dependency and integrate into OpenAI notebooks
- Updated deno.lock and package.json to include @toon-format/toon@^2.1.0.
- Modified background-openai.ipynb to handle error responses and adjust execution counts.
- Enhanced definitions-openai.ipynb to include additional metadata for perishables and origen schemas.
- Refactored embeddings-openai.ipynb to improve error handling and output formatting.
- Updated create-chat-conversation function to append messages and responses to conversations.
- Added SQL functions for appending conversation data to the database.
2026-02-24 15:20:19 -06:00

4.8 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");
Stack trace:
ReferenceError: openai is not defined
    at <anonymous>:5:15
import { encode } from "@toon-format/toon";

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`);
encode(error, {
  indent: 2,
  delimiter: ",",
  keyFolding: "off",
  flattenDepth: Infinity,
});
🔎 Consulta: ¿Cómo son buscados vectores?

"code: PGRST202\n" +
  'details: "Searched for the function public.buscar_documentos with parameters match_count, query_embedding or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache."\n' +
  "hint: Perhaps you meant to call the function public.unaccent\n" +
  'message: "Could not find the function public.buscar_documentos(match_count, query_embedding) in the schema cache"'
error
{
  code: "PGRST202",
  details: "Searched for the function public.buscar_documentos with parameters match_count, query_embedding or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.",
  hint: "Perhaps you meant to call the function public.unaccent",
  message: "Could not find the function public.buscar_documentos(match_count, query_embedding) in the schema cache"
}