Running AI Models
How to run AI models in Edge Functions.
Supabase Edge Runtime has a built-in API for running AI models. You can use this API to generate embeddings, build conversational workflows, and do other AI related tasks in your Edge Functions.
Setup
There are no external dependencies or packages to install to enable the API.
You can create a new inference session by doing:
To get type hints and checks for the API we recommend adding this triple-slash directive to the start of the Edge Function code: /// <reference types="https://esm.sh/@supabase/functions-js/src/edge-runtime.d.ts" />
Running a model inference
Once the session is instantiated, you can call it with inputs to perform inferences. Depending on the model you run, you may need to provide different options (discussed below).
How to generate text embeddings
Now let's see how to write an Edge Function using the Supabase.ai
API to generate text embeddings. Currently, Supabase.ai
API only supports the gte-small model.
gte-small
model exclusively caters to English texts, and any lengthy texts will be truncated to a maximum of 512 tokens. While you can provide inputs longer than 512 tokens, truncation may affect the accuracy.
Using Large Language Models
Inference via larger models is supported via Ollama. In the first iteration, you can use it with a self-managed Ollama server. We are progressively rolling out support for the hosted solution. To sign up for early access, fill up this form.
Running locally
-
Install Ollama and pull the Mistral model
-
Run the Ollama server locally
-
Set a function secret called AI_INFERENCE_API_HOST to point to the Ollama server
-
Create a new function with the following code
-
Serve the function
-
Execute the function
Deploying to production
Once the function is working locally, it's time to deploy to production.
-
Deploy a Ollama server and set a function secret called
AI_INFERENCE_API_HOST
to point to the deployed Ollama server -
Deploy the Supabase function
-
Execute the function
As demonstrated in the video above, running Ollama locally is typically slower than running it in on a server with dedicated GPUs. We are collaborating with the Ollama team to improve local performance.
In the future, a hosted Ollama API, will be provided as part of the Supabase platform. Supabase will scale and manage the API and GPUs for you. To sign up for early access, fill up this form.