Skip to content

Commit 3e17f00

Browse files
committed
feat: impl function that call the OpenAI completion API
1 parent 4794504 commit 3e17f00

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OPENAI_API_KEY=

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
.env.local

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,24 @@ ref: https://youtu.be/29p8kIqyU_Y
66

77
- Supabase
88
- Deno
9+
- VS Code
10+
- https://github.com/denoland/vscode_deno/
11+
12+
## Run local
13+
14+
Setup your OpenAI API key in `.env.local`
15+
16+
```bash
17+
$ cp .env.example .env.local
18+
```
19+
20+
```bash
21+
$ supabase start
22+
23+
$ supabase functions serve --env-file .env.local openai
24+
Starting supabase/functions/openai
25+
Serving supabase/functions/openai
26+
Watcher Process started.
27+
Check file:///home/deno/functions/openai/index.ts
28+
Listening on http://localhost:8000/
29+
```

supabase/functions/openai/index.ts

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,50 @@
22
// https://deno.land/manual/getting_started/setup_your_environment
33
// This enables autocomplete, go to definition, etc.
44

5-
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
5+
import "https://deno.land/x/xhr@0.3.0/mod.ts"
6+
import { getLogger } from "https://deno.land/std@0.183.0/log/mod.ts"
7+
import { serve } from "https://deno.land/std@0.183.0/http/server.ts"
8+
import { Configuration, OpenAIApi } from "https://esm.sh/openai@3.2.1"
69

7-
console.log("Hello from Functions!")
10+
const configuration = new Configuration({
11+
apiKey: Deno.env.get("OPENAI_API_KEY"),
12+
});
13+
const openai = new OpenAIApi(configuration);
814

915
serve(async (req) => {
10-
const { name } = await req.json()
11-
const data = {
12-
message: `Hello ${name}!`,
13-
}
16+
const { query } = await req.json();
17+
try {
18+
const response = await openai.createCompletion({
19+
model: "text-davinci-003",
20+
prompt: query,
21+
max_tokens: 256,
22+
temperature: 0,
23+
});
24+
25+
const {
26+
data: {
27+
id,
28+
choices: [{ text }]
29+
},
30+
} = response;
1431

15-
return new Response(
16-
JSON.stringify(data),
17-
{ headers: { "Content-Type": "application/json" } },
18-
)
32+
return new Response(
33+
JSON.stringify({ id, text }),
34+
{ headers: { "Content-Type": "application/json" } },
35+
)
36+
} catch (error) {
37+
const logger = getLogger()
38+
logger.error(error)
39+
40+
return new Response(
41+
JSON.stringify({ error }),
42+
{ headers: { "Content-Type": "application/json" } },
43+
)
44+
}
1945
})
2046

2147
// To invoke:
2248
// curl -i --location --request POST 'http://localhost:54321/functions/v1/' \
2349
// --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \
2450
// --header 'Content-Type: application/json' \
25-
// --data '{"name":"Functions"}'
51+
// --data '{"query":"What is Supabase?"}'

0 commit comments

Comments
 (0)