The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
docs(ai): polish openai example
Greg Richardson94d8408c5292
diff --git a/apps/docs/pages/guides/ai/examples/openai.mdx b/apps/docs/pages/guides/ai/examples/openai.mdxindex e18ba4f0829..3d2c3f3f2aa 100644--- a/apps/docs/pages/guides/ai/examples/openai.mdx+++ b/apps/docs/pages/guides/ai/examples/openai.mdx@@ -5,18 +5,32 @@ export const meta = {title: 'Generating OpenAI GPT3 completions',description: 'Generate GPT text completions using OpenAI in Edge Functions.',video: 'https://www.youtube.com/v/29p8kIqyU_Y',+ tocVideo: '29p8kIqyU_Y',}-<div class="video-container">- <iframe- src="https://www.youtube-nocookie.com/embed/29p8kIqyU_Y"- frameBorder="1"- allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"- allowFullScreen- ></iframe>-</div>+OpenAI provides a [completions API](https://platform.openai.com/docs/api-reference/completions) that allows you to use their generative GPT models in your own applications.++OpenAI's API is intended to be used from the server-side. Supabase offers Edge Functions to make it easy to interact with third party APIs like OpenAI.++## Setup Supabase project++If you haven't already, [install the Supabase CLI](/docs/guides/cli) and initialize your project:++```shell+supabase init+```++## Create edge function++Scaffold a new edge function called `openai` by running:-Use the [OpenAI completions API](https://platform.openai.com/docs/api-reference/completions) in Supabase Edge Functions.+```shell+supabase functions new openai+```++A new edge function will now exist under `./supabase/functions/openai/index.ts`.++We'll design the function to take your user's query (via POST request) and forward it to OpenAI's API.```ts index.tsimport 'xhr_polyfill'@@ -31,7 +45,7 @@ serve(async (req) => {prompt: query,max_tokens: 256,temperature: 0,- stream: true,+ stream: false,}return fetch('https://api.openai.com/v1/completions', {@@ -45,12 +59,28 @@ serve(async (req) => {})```+Note that we are setting `stream` to `false` which will wait until the entire response is complete before returning. If you wish to stream GPT's response word-by-word back to your client, set `stream` to `true`.++## Create OpenAI key++You may have noticed we were passing `OPENAI_API_KEY` in the Authorization header to OpenAI. To generate this key, go to https://platform.openai.com/account/api-keys and create a new secret key.++After getting the key, copy it into a new file called `.env.local` in your `./supabase` folder:++```+OPENAI_API_KEY=your-key-here+```+## Run locally+Serve the edge function locally by running:+```bashsupabase functions serve --env-file ./supabase/.env.local --no-verify-jwt```+Notice how we are passing in the `.env.local` file.+Use cURL or Postman to make a POST request to http://localhost:54321/functions/v1/openai.```bash@@ -59,8 +89,12 @@ curl -i --location --request POST http://localhost:54321/functions/v1/openai \--data '{"query":"What is Supabase?"}'```+You should see a GPT response come back from OpenAI!+## Deploy+Deploy your function to the cloud by runnning:+```bashsupabase functions deploy --no-verify-jwt openaisupabase secrets set --env-file ./supabase/.env.local