{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "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": 3,
   "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": 1,
   "id": "dae842ad",
   "metadata": {},
   "outputs": [
    {
     "ename": "ReferenceError",
     "evalue": "openai is not defined",
     "output_type": "error",
     "traceback": [
      "Stack trace:",
      "ReferenceError: openai is not defined",
      "    at <anonymous>:5:15"
     ]
    }
   ],
   "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": 12,
   "id": "2d8fa9a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "🔎 Consulta: ¿Cómo son buscados vectores?\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\u001b[32m\"code: PGRST202\\n\"\u001b[39m +\n",
       "  \u001b[32m'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'\u001b[39m +\n",
       "  \u001b[32m\"hint: Perhaps you meant to call the function public.unaccent\\n\"\u001b[39m +\n",
       "  \u001b[32m'message: \"Could not find the function public.buscar_documentos(match_count, query_embedding) in the schema cache\"'\u001b[39m"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import { encode } from \"@toon-format/toon\";\n",
    "\n",
    "const consulta = \"¿Cómo son buscados 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",
    "encode(error, {\n",
    "  indent: 2,\n",
    "  delimiter: \",\",\n",
    "  keyFolding: \"off\",\n",
    "  flattenDepth: Infinity,\n",
    "});\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "791a94d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "  code: \u001b[32m\"PGRST202\"\u001b[39m,\n",
       "  details: \u001b[32m\"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.\"\u001b[39m,\n",
       "  hint: \u001b[32m\"Perhaps you meant to call the function public.unaccent\"\u001b[39m,\n",
       "  message: \u001b[32m\"Could not find the function public.buscar_documentos(match_count, query_embedding) in the schema cache\"\u001b[39m\n",
       "}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "error"
   ]
  }
 ],
 "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
}
