Skip to content
AI & Vectors

生成 OpenAI GPT3 的完成内容

Generate GPT text completions using OpenAI and Supabase Edge Functions.

OpenAI 提供了一个 completions API,让你可以在自己的应用中使用他们的生成型 GPT 模型。

🌐 OpenAI provides a completions API that allows you to use their generative GPT models in your own applications.

OpenAI 的 API 主要是用于服务器端。Supabase 提供了 Edge Functions,让和像 OpenAI 这样的第三方 API 交互变得简单。

🌐 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.

设置 Supabase 项目 #

🌐 Setup Supabase project

如果你还没有的话,安装 Supabase CLI 并初始化你的项目:

🌐 If you haven't already, install the Supabase CLI and initialize your project:

1
supabase init

创建边缘函数 #

🌐 Create edge function

通过运行以下命令来搭建一个名为 openai 的新边缘函数:

🌐 Scaffold a new edge function called openai by running:

1
supabase functions new openai

现在在 ./supabase/functions/openai/index.ts 下会有一个新的边缘功能。

🌐 A new edge function will now exist under ./supabase/functions/openai/index.ts.

我们会设计这个函数来接收用户的查询(通过 POST 请求)并将其转发到 OpenAI 的 API。

🌐 We'll design the function to take your user's query (via POST request) and forward it to OpenAI's API.

1
import OpenAI from 'https://deno.land/x/openai@v4.24.0/mod.ts'
2
3
Deno.serve(async (req) => {
4
const { query } = await req.json()
5
const apiKey = Deno.env.get('OPENAI_API_KEY')
6
const openai = new OpenAI({
7
apiKey: apiKey,
8
})
9
10
// Documentation here: https://github.com/openai/openai-node
11
const chatCompletion = await openai.chat.completions.create({
12
messages: [{ role: 'user', content: query }],
13
// Choose model from here: https://platform.openai.com/docs/models
14
model: 'gpt-3.5-turbo',
15
stream: false,
16
})
17
18
const reply = chatCompletion.choices[0].message.content
19
20
return new Response(reply, {
21
headers: { 'Content-Type': 'text/plain' },
22
})
23
})

请注意,我们将 stream 设置为 false,这会在整个响应完成后才返回。如果你希望逐字将 GPT 的回应实时传回给你的客户端,请将 stream 设置为 true

🌐 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.

创建 OpenAI 密钥 #

🌐 Create OpenAI key

你可能注意到我们在向 OpenAI 发送请求时,在 Authorization 头中传递了 OPENAI_API_KEY。要生成这个密钥,请访问 https://platform.openai.com/account/api-keys 并创建一个新的密钥。

🌐 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.

拿到密钥后,把它复制到你 ./supabase 文件夹里的一个新文件 .env.local 中:

🌐 After getting the key, copy it into a new file called .env.local in your ./supabase folder:

1
OPENAI_API_KEY=your-key-here

本地运行 #

🌐 Run locally

通过运行以下命令在本地启动边缘函数:

🌐 Serve the edge function locally by running:

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

注意我们是如何传入 .env.local 文件的。

🌐 Notice how we are passing in the .env.local file.

使用 cURL 或 Postman 向 http://localhost:54321/functions/v1/openai 发送 POST 请求。

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

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

你应该会看到来自 OpenAI 的 GPT 回复!

🌐 You should see a GPT response come back from OpenAI!

部署 #

🌐 Deploy

通过运行以下命令将你的函数部署到云端:

🌐 Deploy your function to the cloud by running:

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

更深入 #

🌐 Go deeper

如果你有兴趣学习如何使用它来构建你自己的 ChatGPT,可以阅读这篇博客文章并观看视频:

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