本页还没有译文
本页 24 段还没译成简体中文,下面显示的是英文原文。Generate Embeddings
还没翻译译自英文(supabase.com)本页 24 段都还没译
This guide will walk you through how to generate high quality text embeddings in Edge Functions using its built-in AI inference API, so no external API is required.
Build the Edge Function
Build an Edge Function that accepts an input string and generates an embedding for it. Edge Functions are server-side TypeScript HTTP endpoints that run on-demand closest to your users.
-
Set up Supabase locally
Make sure you have the latest version of the Supabase CLI installed.
Initialize Supabase in the root directory of your app and start your local stack.
supabase init supabase start -
Create Edge Function
Create an Edge Function that we will use to generate embeddings. We'll call this
embed(you can name this anything you like).This will create a new TypeScript file called
index.tsunder./supabase/functions/embed.supabase functions new embed -
Setup Inference Session
Create a new inference session to use for the lifetime of this function. Multiple requests can use the same inference session.
Currently, only the
gte-small(https://huggingface.co/Supabase/gte-small) text embedding model is supported in Supabase's Edge Runtime.const session = new Supabase.ai.Session('gte-small'); -
Implement request handler
Modify our request handler to accept an
inputstring from the POST request JSON body.Then generate the embedding by calling
session.run(input).Deno.serve(async (req) => { // Extract input string from JSON body const { input } = await req.json(); // Generate the embedding from the user input const embedding = await session.run(input, { mean_pool: true, normalize: true, }); // Return the embedding return new Response( JSON.stringify({ embedding }), { headers: { 'Content-Type': 'application/json' } } ); });Note the two options we pass to
session.run():mean_pool: The first option setspoolingtomean. Pooling refers to how token-level embedding representations are compressed into a single sentence embedding that reflects the meaning of the entire sentence. Average pooling is the most common type of pooling for sentence embeddings.normalize: The second option normalizes the embedding vector so that it can be used with distance measures like dot product. A normalized vector means its length (magnitude) is 1 - also referred to as a unit vector. A vector is normalized by dividing each element by the vector's length (magnitude), which maintains its direction but changes its length to 1.
-
Test it!
To test the Edge Function, first start a local functions server.
supabase functions serveThen in a new shell, create an HTTP request using cURL and pass in your input in the JSON body.
curl --request POST 'http://localhost:54321/functions/v1/embed' \ --header 'Content-Type: application/json' \ --header 'apikey: SUPABASE_PUBLISHABLE_KEY' \ --data '{ "input": "hello world" }'Be sure to replace
SUPABASE_PUBLISHABLE_KEYwith your project's publishable key. You can get this key by runningsupabase status.
Next steps
- Learn more about embedding concepts
- Store your embeddings in a database