babaohuang / GeminiProChat

Minimal web UI for GeminiPro.
https://geminiprochat.com
MIT License
4.26k stars 12.34k forks source link

Vercel Edge Functions: set one or more preferred regions #9

Closed SunsetMkt closed 8 months ago

SunsetMkt commented 9 months ago

Describe the feature

https://vercel.com/docs/functions/configuring-functions/region

For Edge Functions:

Additional context

No response

Participation

babaohuang commented 9 months ago

I have tried it , but i didn't work , maybe the Astro 2 doesn't support this feature ?

SunsetMkt commented 9 months ago

I'm afraid not. Next.js is by Vercel, so there's an official support.

We could have rewritten the request function using the following method:

import type { VercelRequest, VercelResponse } from '@vercel/node';

export const config = {
  runtime: 'edge', // this is a pre-requisite
  // execute this function on iad1 or hnd1, based on the connecting client location
  regions: ['iad1', 'hnd1'],
};

export default function handler(
  request: VercelRequest,
  response: VercelResponse,
) {
  return response.status(200).json({
    text: `I am an Edge Function! (executed on ${process.env.VERCEL_REGION})`,
  });
}

But because it is actually wrapped in @google/generative-ai, we cannot modify it.

babaohuang commented 9 months ago

I'm afraid not. Next.js is by Vercel, so there's an official support.

We could have rewritten the request function using the following method:

import type { VercelRequest, VercelResponse } from '@vercel/node';

export const config = {
  runtime: 'edge', // this is a pre-requisite
  // execute this function on iad1 or hnd1, based on the connecting client location
  regions: ['iad1', 'hnd1'],
};

export default function handler(
  request: VercelRequest,
  response: VercelResponse,
) {
  return response.status(200).json({
    text: `I am an Edge Function! (executed on ${process.env.VERCEL_REGION})`,
  });
}

But because it is actually wrapped in @google/generative-ai, we cannot modify it.

but this project is not base on Next.js framework

SunsetMkt commented 9 months ago

Yes, so we may try the "Other frameworks" example, but we have to refrain from using @google/generative-ai and must rewrite the functionalities it provides on our own.

The docs require using VercelRequest, VercelResponse from @vercel/node, but @google/generative-ai (actually sends the request) won't follow it.

It is also possible that Google will eventually notice this issue and modify @google/generative-ai. Perhaps we should provide feedback to both Google and Vercel first?

I'm not familiar with modern front-end frameworks, so sorry for any confusion.

babaohuang commented 8 months ago

this issue has a perfect solution now , see here

SunsetMkt commented 8 months ago

Thank you for your efforts.

Just a kindly reminder, proxies are not allowed in Vercel Fair Use Policy, though Vercel seldom intervenes in these matters.