Skip to content

Commit

Permalink
feat: impl function that call the OpenAI completion API
Browse files Browse the repository at this point in the history
  • Loading branch information
samuraikun committed Apr 15, 2023
1 parent 4794504 commit 3e17f00
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY=
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
.env.local
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,24 @@ ref: https://youtu.be/29p8kIqyU_Y

- Supabase
- Deno
- VS Code
- https://github.com/denoland/vscode_deno/

## Run local

Setup your OpenAI API key in `.env.local`

```bash
$ cp .env.example .env.local
```

```bash
$ supabase start

$ supabase functions serve --env-file .env.local openai
Starting supabase/functions/openai
Serving supabase/functions/openai
Watcher Process started.
Check file:///home/deno/functions/openai/index.ts
Listening on http://localhost:8000/
```
48 changes: 37 additions & 11 deletions supabase/functions/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,50 @@
// https://deno.land/manual/getting_started/setup_your_environment
// This enables autocomplete, go to definition, etc.

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

console.log("Hello from Functions!")
const configuration = new Configuration({
apiKey: Deno.env.get("OPENAI_API_KEY"),
});
const openai = new OpenAIApi(configuration);

serve(async (req) => {
const { name } = await req.json()
const data = {
message: `Hello ${name}!`,
}
const { query } = await req.json();
try {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: query,
max_tokens: 256,
temperature: 0,
});

const {
data: {
id,
choices: [{ text }]
},
} = response;

return new Response(
JSON.stringify(data),
{ headers: { "Content-Type": "application/json" } },
)
return new Response(
JSON.stringify({ id, text }),
{ headers: { "Content-Type": "application/json" } },
)
} catch (error) {
const logger = getLogger()
logger.error(error)

return new Response(
JSON.stringify({ error }),
{ headers: { "Content-Type": "application/json" } },
)
}
})

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

0 comments on commit 3e17f00

Please sign in to comment.