Rating: 4.42 / 5 Based on 12 reviews
Update made in May 2024: Google released the Gemini API in the EU, UK, and Switzerland during Google I/O 2024. There may still be some reasons to use Vertex AI, but in most cases, the standard Gemini API can be used.
Google has recently released its package for communicating with Google AI models such as Gemini Pro. However, Gemini API, which this package uses, is not yet available in all regions, most notably the European Union, because of privacy regulations. Thankfully Gemini LLM model can be accessed from the EU via the Vertex AI service provided by the Google Cloud Platform. Integration with Vertex AI API is more cumbersome than Gemini API but still quite simple, especially if you are already using GCP.
Follow our short guide on how to use Gemini API in Europe so you can create your own solutions with Gemini, such as our last package arb_translate, which automatically adds missing translations to ARB files in Flutter apps.
The first thing we need is a Google Cloud Platform project. You may already have one related to Firebase or some other GCP service. If not, you can follow this instruction to create one: https://developers.google.com/workspace/guides/create-project.
To access the Vertex AI service, we need first to enable it in the GCP console. The instructions on how to do this can be found here: https://cloud.google.com/vertex-ai/docs/featurestore/setup.
Vertex AI uses tokens for authentication. Instead of generating the key in the Google AI Studio, we have to generate the token using the gcloud CLI.
1. Install Google Cloud SDK Google Cloud SDK
2. Log in
gcloud auth login
3. Print token
gcloud auth print-access-token
google_generative_ai package doesn’t support Vertex AI, but its API is so similar to Gemini that with a small hack, we can make it work. That’s how we’ve done in our arb_translate package to make it available for everyone.
There are two most important differences between Gemini and Vertex AI APIs - base URL for requests and authentication. When using Vertex AI, we have to send our requests with the URL matching our GCP, unlike Gemini, where all requests have the same base URL. The authorization with Vertex uses different headers. Instead of x-goog-api-key: {api-key} header, we need to use Authorization: Bearer {token}. google_generative_ai doesn’t support Vertex AI so it doesn’t allow us to provide the URL nor to use different authentication.
class VertexHttpClient extends BaseClient {
VertexHttpClient(this._projectUrl);
final String _projectUrl;
final _client = Client();
Future<StreamedResponse> send(BaseRequest request) {
if (request is! Request ||
request.url.host != 'generativelanguage.googleapis.com') {
return _client.send(request);
}
final vertexRequest = Request(
request.method,
Uri.parse(request.url.toString().replaceAll(
'https://generativelanguage.googleapis.com/v1/models',
_projectUrl)))
..bodyBytes = request.bodyBytes;
for (final header in request.headers.entries) {
if (header.key != 'x-goog-api-key') {
vertexRequest.headers[header.key] = header.value;
}
}
vertexRequest.headers['Authorization'] =
'Bearer ${request.headers['x-goog-api-key']}';
return _client.send(vertexRequest);
}
}
Then, we simply need to pass this client during GenerativeModel creation.
GenerativeModel(
model: 'gemini-pro',
apiKey: apiKey,
httpClient: VertexHttpClient(projectUrl),
);
Of course, this isn’t the perfect solution, but if we want to use the google_generative_ai package in the EU or other regions where Gemini API is unavailable, this is the only way for now.
The good news is that there is a way to use the new generative_ai_google package in the EU via the Vertex AI service provided by the Google Cloud Platform. The current way isn’t perfect, but temporarily resolves the issue until Google either makes Gemini API available everywhere or generative_google_ai officially supports Vertex AI.