Skip to content

Instantly share code, notes, and snippets.

@aiherrera
Last active January 6, 2024 04:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aiherrera/63a0c2ca6081ce205e05abcb315b4fca to your computer and use it in GitHub Desktop.
Save aiherrera/63a0c2ca6081ce205e05abcb315b4fca to your computer and use it in GitHub Desktop.
Supabase EDGE Function - Embeddings generator
// Import the serve function to start a Deno server
import { serve } from "std/server";
// Import pipeline utility from Hugging Face Transformers
import { pipeline } from "transformers";
// Import Supabase client
import { createClient } from "supabase/client";
// Shared CORS headers
import { corsHeaders } from "../_shared/cors.ts";
// Disable caching and allow local models for development
env.useBrowserCache = false;
env.allowLocalModels = false;
// Get Supabase credentials from environment
const SUPABASE_URL = Deno.env.get("SUPABASE_URL")!;
const SUPABASE_ANON_KEY = Deno.env.get("SUPABASE_ANON_KEY")!;
// Create Supabase client instance
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
// Load Hugging Face pipeline and model
const pipe = await pipeline("feature-extraction", "Supabase/gte-small");
// Start server
serve(async (req) => {
try {
// Get input data from request
const { input } = await req.json();
// Run input through pipeline
const output = await pipe(input, {
pooling: "mean",
normalize: true,
});
// Extract embeddings
const getEmbeddings = Array.from(output.data);
// Insert embeddings into Supabase
const { data, error } = await supabase
.from("collections")
.insert({ embedding: getEmbeddings })
.select();
// Handle errors
if (error) {
// Return 500 error
return new Response(JSON.stringify({ error }), {
headers: {...corsHeaders},
status: 500,
});
}
// Return inserted data
return new Response(JSON.stringify({ data }), {
headers: {
...corsHeaders,
"Content-Type": "application/json"
},
});
} catch (err) {
// Catch and return any unhandled errors
return new Response(JSON.stringify({ err }), {
headers: {...corsHeaders},
status: 500,
});
}
});
{
"imports": {
"std/server": "https://deno.land/std@0.168.0/http/server.ts",
"transformers": "https://cdn.jsdelivr.net/npm/@xenova/transformers@2.5.0",
"postgres": "https://deno.land/x/postgres@v0.15.0/mod.ts",
"supabase/client": "https://esm.sh/@supabase/supabase-js@2.38.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment