Home

Generating OpenAI GPT3 completions

Use the OpenAI completions API in Supabase Edge Functions.

import 'xhr_polyfill'
import { serve } from 'std/server'
import { CreateCompletionRequest } from 'openai'

serve(async (req) => {
  const { query } = await req.json()

  const completionConfig: CreateCompletionRequest = {
    model: 'text-davinci-003',
    prompt: query,
    max_tokens: 256,
    temperature: 0,
    stream: true,
  }

  return fetch('https://api.openai.com/v1/completions', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${Deno.env.get('OPENAI_API_KEY')}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(completionConfig),
  })
})

Run locally#

1supabase functions serve --env-file ./supabase/.env.local --no-verify-jwt

Use cURL or Postman to make a POST request to http://localhost:54321/functions/v1/openai.

1curl -i --location --request POST http://localhost:54321/functions/v1/openai \
2  --header 'Content-Type: application/json' \
3  --data '{"query":"What is Supabase?"}'

Deploy#

1supabase functions deploy --no-verify-jwt openai
2supabase secrets set --env-file ./supabase/.env.local

Go deeper#

If you're interesting in learning how to use this to build your own ChatGPT, read the blog post and check out the video: