mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
Closes DOCS-1553 ## Problem Some guides set a subtitle that says almost exactly what the first sentence of the page says, so the reader reads the same line twice. ## Solution - Add a **Write good frontmatter** section to the style guide, with **Title**, **Subtitle**, and **Description** subsections. A title is 35 characters or fewer, in sentence case, and follows the Google developer style guide's title rules. A subtitle is optional and decorative. If it matches the first sentence, leave it out. A description is 150–160 characters, with relevant keywords. - Remove the subtitle on 12 guides where it repeated the first sentence. On the 6 of those with no description, move the subtitle to `description` so the meta description doesn't change. - Change the Vault subtitle to `Manage secrets in Postgres` to match the rule's example. - In `CONTRIBUTING.md`, mark `description` as required and link to the new section. ## Manual testing 1. Open [Creating a client](https://docs-git-docs-guide-subtitles-supabase.vercel.app/docs/guides/auth/server-side/creating-a-client). See no subtitle under the title. 2. Open [Vault](https://docs-git-docs-guide-subtitles-supabase.vercel.app/docs/guides/database/vault). See `Manage secrets in Postgres` under the title. 3. Read [Write good frontmatter](https://github.com/supabase/supabase/blob/docs/guide-subtitles/apps/docs/style-guide/03-page-structure.md#write-good-frontmatter).
158 lines
4.9 KiB
Plaintext
158 lines
4.9 KiB
Plaintext
---
|
|
id: 'ai-generating-embeddings'
|
|
title: 'Generate Embeddings'
|
|
description: 'Generate text embeddings using Edge Functions.'
|
|
breadcrumb: 'AI Quickstarts'
|
|
---
|
|
|
|
This guide will walk you through how to generate high quality text embeddings in [Edge Functions](/docs/guides/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.
|
|
|
|
<StepHikeCompact>
|
|
|
|
<StepHikeCompact.Step step={1}>
|
|
|
|
<StepHikeCompact.Details title="Set up Supabase locally">
|
|
|
|
Make sure you have the latest version of the [Supabase CLI installed](/docs/guides/local-development/cli/getting-started).
|
|
|
|
Initialize Supabase in the root directory of your app and start your local stack.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```shell
|
|
supabase init
|
|
supabase start
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={2}>
|
|
|
|
<StepHikeCompact.Details title="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.ts` under `./supabase/functions/embed`.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```shell
|
|
supabase functions new embed
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={3}>
|
|
|
|
<StepHikeCompact.Details title="Setup Inference Session" fullWidth>
|
|
|
|
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.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
```ts ./supabase/functions/embed/index.ts
|
|
const session = new Supabase.ai.Session('gte-small');
|
|
```
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={4}>
|
|
|
|
<StepHikeCompact.Details title="Implement request handler">
|
|
|
|
Modify our request handler to accept an `input` string from the POST request JSON body.
|
|
|
|
Then generate the embedding by calling `session.run(input)`.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```ts ./supabase/functions/embed/index.ts
|
|
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' } }
|
|
);
|
|
});
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
<StepHikeCompact.Details fullWidth>
|
|
|
|
Note the two options we pass to `session.run()`:
|
|
|
|
- `mean_pool`: The first option sets `pooling` to `mean`. 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.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={5}>
|
|
|
|
<StepHikeCompact.Details title="Test it!">
|
|
|
|
To test the Edge Function, first start a local functions server.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```shell
|
|
supabase functions serve
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
<StepHikeCompact.Details fullWidth>
|
|
|
|
Then in a new shell, create an HTTP request using cURL and pass in your input in the JSON body.
|
|
|
|
```shell
|
|
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_KEY` with your project's publishable key. You can get this key by running `supabase status`.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
</StepHikeCompact>
|
|
|
|
## Next steps
|
|
|
|
- Learn more about [embedding concepts](/docs/guides/ai/concepts)
|
|
- [Store your embeddings](/docs/guides/ai/vector-columns) in a database
|