1
1
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" ;
4
3
5
4
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
+ ) ;
14
46
} ) ;
15
47
16
48
/* To invoke locally:
@@ -21,6 +53,6 @@ Deno.serve(async (req) => {
21
53
curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/hello-anilog' \
22
54
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \
23
55
--header 'Content-Type: application/json' \
24
- --data '{"name":"Functions "}'
56
+ --data '{"imageUrl": "https:example.com "}'
25
57
26
58
*/
0 commit comments