Skip to content

Instantly share code, notes, and snippets.

@kallebysantos
Created May 2, 2024 09:07
Show Gist options
  • Save kallebysantos/a1960d788e6b7e1c8c961691d6d71ff6 to your computer and use it in GitHub Desktop.
Save kallebysantos/a1960d788e6b7e1c8c961691d6d71ff6 to your computer and use it in GitHub Desktop.
Supabase edge functions with custom embed model
services:
# others supabase services ...
supabase-functions:
container_name: supabase-edge-functions
# Here you need to specify that you want do docker build a image with your custom model
image: supabase/edge-runtime:v1.41.2-custom-model
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
# The rest of config doesn't matter, you can keep it as default or your custom
FROM supabase/edge-runtime:v1.41.2 AS build
# Change here with the HF model name. It must be ONNX runtime compatible
ENV MODEL_NAME=Xenova/paraphrase-multilingual-MiniLM-L12-v2
# Setting up
RUN apt update && apt install curl -y
# Replacing 'gte-small' by $MODEL_NAME
WORKDIR /etc/sb_ai/models/gte-small
# Downloading the custom model
RUN curl -L -o model.onnx https://huggingface.co/${MODEL_NAME}/resolve/main/onnx/model_quantized.onnx?download=true
RUN curl -L -o tokenizer.json https://huggingface.co/${MODEL_NAME}/resolve/main/tokenizer.json?download=true
FROM supabase/edge-runtime:v1.41.2 AS run
COPY --from=build /etc/sb_ai/models/gte-small /etc/sb_ai/models/gte-small
/// <reference types="https://esm.sh/@supabase/functions-js/src/edge-runtime.d.ts" />
// gte-small has been overriden by paraphrase-multilingual-MiniLM-L12-v2
// Here you need to keep the 'gte-small' for API compatility.
// But since we overwrite the model inside container it will use our custom model
const embedder = new Supabase.ai.Session("gte-small");
Deno.serve(async (req) => {
// ...
// @ts-ignore Embedder generates unknown type
const outputEmbed: number[] = await embedder.run("Olá Mundo!", {
mean_pool: true,
normalize: true,
});
// Rest of your code ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment