Spaces:
Sleeping
Sleeping
wuyiqunLu
commited on
Commit
•
76503b7
1
Parent(s):
314f2dc
feat: do not use edge for vision agent api (#26)
Browse filescause edge functions are not support native node
https://nextjs.org/docs/messages/node-module-in-edge-runtime
- app/api/vision-agent/route.ts +56 -54
- next.config.js +1 -1
app/api/vision-agent/route.ts
CHANGED
@@ -2,67 +2,69 @@ import { StreamingTextResponse } from 'ai';
|
|
2 |
|
3 |
// import { auth } from '@/auth';
|
4 |
import { MessageBase } from '../../../lib/types';
|
5 |
-
|
6 |
import { CLEANED_SEPARATOR } from '@/lib/constants';
|
7 |
|
8 |
-
export const runtime = 'edge';
|
9 |
export const dynamic = 'force-dynamic';
|
10 |
export const maxDuration = 300; // This function can run for a maximum of 5 minutes
|
11 |
|
12 |
-
export const POST =
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
},
|
59 |
-
|
60 |
-
},
|
61 |
-
);
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
}
|
|
|
|
2 |
|
3 |
// import { auth } from '@/auth';
|
4 |
import { MessageBase } from '../../../lib/types';
|
5 |
+
import { withLogging } from '@/lib/logger';
|
6 |
import { CLEANED_SEPARATOR } from '@/lib/constants';
|
7 |
|
8 |
+
// export const runtime = 'edge';
|
9 |
export const dynamic = 'force-dynamic';
|
10 |
export const maxDuration = 300; // This function can run for a maximum of 5 minutes
|
11 |
|
12 |
+
export const POST = withLogging(
|
13 |
+
async (
|
14 |
+
_session,
|
15 |
+
json: {
|
16 |
+
messages: MessageBase[];
|
17 |
+
id: string;
|
18 |
+
url: string;
|
19 |
+
},
|
20 |
+
) => {
|
21 |
+
const { messages, url } = json;
|
22 |
|
23 |
+
// const session = await auth();
|
24 |
+
// if (!session?.user?.email) {
|
25 |
+
// return new Response('Unauthorized', {
|
26 |
+
// status: 401,
|
27 |
+
// });
|
28 |
+
// }
|
29 |
|
30 |
+
const formData = new FormData();
|
31 |
+
formData.append(
|
32 |
+
'input',
|
33 |
+
JSON.stringify(
|
34 |
+
messages.map(message => {
|
35 |
+
if (message.role !== 'assistant') {
|
36 |
+
return message;
|
37 |
+
} else {
|
38 |
+
const splitedContent = message.content.split(CLEANED_SEPARATOR);
|
39 |
+
return {
|
40 |
+
...message,
|
41 |
+
content:
|
42 |
+
splitedContent.length > 1
|
43 |
+
? splitedContent[1].replace(/!\[answers.*?\.png\)/g, '')
|
44 |
+
: message.content,
|
45 |
+
};
|
46 |
+
}
|
47 |
+
}),
|
48 |
+
),
|
49 |
+
);
|
50 |
+
formData.append('image', url);
|
51 |
|
52 |
+
const fetchResponse = await fetch(
|
53 |
+
'https://api.dev.landing.ai/v1/agent/chat?agent_class=vision_agent&visualize_output=true',
|
54 |
+
// 'http://localhost:5050/v1/agent/chat?agent_class=vision_agent',
|
55 |
+
{
|
56 |
+
method: 'POST',
|
57 |
+
headers: {
|
58 |
+
apikey: 'land_sk_DKeoYtaZZrYqJ9TMMiXe4BIQgJcZ0s3XAoB0JT3jv73FFqnr6k',
|
59 |
+
},
|
60 |
+
body: formData,
|
61 |
},
|
62 |
+
);
|
|
|
|
|
63 |
|
64 |
+
if (fetchResponse.body) {
|
65 |
+
return new StreamingTextResponse(fetchResponse.body);
|
66 |
+
} else {
|
67 |
+
return fetchResponse;
|
68 |
+
}
|
69 |
+
},
|
70 |
+
);
|
next.config.js
CHANGED
@@ -11,5 +11,5 @@ module.exports = {
|
|
11 |
},
|
12 |
experimental: {
|
13 |
serverComponentsExternalPackages: ['pino', 'pino-loki'],
|
14 |
-
}
|
15 |
};
|
|
|
11 |
},
|
12 |
experimental: {
|
13 |
serverComponentsExternalPackages: ['pino', 'pino-loki'],
|
14 |
+
},
|
15 |
};
|