Skip to content

Commit 420e4a3

Browse files
committed
feat: add openai plant recognizer edge function
1 parent 15e82d1 commit 420e4a3

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"deno.enable": true
3+
}
Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
11
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
2-
3-
console.log("Hello from Functions!");
2+
import OpenAI from "https://deno.land/x/openai@v4.69.0/mod.ts";
43

54
Deno.serve(async (req) => {
6-
const { name } = await req.json();
7-
const data = {
8-
message: `Hello ${name}!`,
9-
};
10-
11-
return new Response(JSON.stringify(data), {
12-
headers: { "Content-Type": "application/json" },
13-
});
5+
try {
6+
const { imageUrl } = await req.json();
7+
8+
const openai = new OpenAI({
9+
apiKey: Deno.env.get("OPENAI_KEY"),
10+
});
11+
12+
let assistantId = "asst_86rYwd4L2Ux25rWec1mRSEoY";
13+
14+
const thread = await openai.beta.threads.create({
15+
messages: [
16+
{
17+
role: "user",
18+
content: imageUrl,
19+
},
20+
],
21+
});
22+
23+
const run = await openai.beta.threads.runs.createAndPoll(thread.id, {
24+
assistant_id: assistantId,
25+
});
26+
27+
if (run.status == "completed") {
28+
const messages = await openai.beta.threads.messages.list(thread.id);
29+
const assistantResponse = messages.data[0]?.content[0].text.value;
30+
31+
return new Response(assistantResponse, {
32+
headers: { "Content-Type": "application/json" },
33+
});
34+
}
35+
} catch (error) {}
36+
37+
return new Response(
38+
JSON.stringify({
39+
error: "Unknown error",
40+
}),
41+
{
42+
headers: { "Content-Type": "application/json" },
43+
status: 500,
44+
}
45+
);
1446
});
1547

1648
/* To invoke locally:
@@ -21,6 +53,6 @@ Deno.serve(async (req) => {
2153
curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/hello-anilog' \
2254
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \
2355
--header 'Content-Type: application/json' \
24-
--data '{"name":"Functions"}'
56+
--data '{"imageUrl": "https:example.com"}'
2557
2658
*/

code/supabase/readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
# Supabase Edge Functions
22

3+
Copy the .env.example and fill in the OPENAI_KEY
4+
`cp .env.example .env`
5+
36
To start locally, Docker Desktop must be installed. The cloud functions can be started by running `npx supabase start` and are accessible at `http://127.0.0.1:54321/functions/v1/[function-name]`
7+
8+
## Update env varibales in production
9+
10+
Login to your supabase account
11+
`npx supabase login`
12+
13+
Sync the .env file to production
14+
`npx supabase secrets set --env-file .env`
15+
16+
Check the synced env keys
17+
`npx supabase secrets list`

0 commit comments

Comments
 (0)