Flutter CTO Report 2024
Get insights about Flutter directly from CTOs, CIOs, Tech Leads, and Engineering Managers!

How to Use Gemini API in Europe

Robert Odrowąż-Sypniewski
Flutter Developer at LeanCode

Rating: 4.73 / 5 Based on 11 reviews

Feb 19th, 2024 • 5 min.
Free product design tips
Join our Newsletter for Expert Flutter Tips & Product Design Insights from our seasoned team!
By submitting your email you agree to receive the content requested and to LeanCode's Privacy Policy.
or follow us

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.

GCP project

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.

Vertex AI API

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.

Getting the API key

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

Calling the API

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();
  
  @override
  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.

Summing up

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.

Free product design tips
Join our Newsletter for Expert Flutter Tips & Product Design Insights from our seasoned team!
By submitting your email you agree to receive the content requested and to LeanCode's Privacy Policy.
or follow us
Rate this article
4.73 / 5 Based on 11 reviews
CTOs & Tech Leads share opinions on Flutter
Flutter CTO Report 2024

You may also like

How to Speed Up Flutter App Localization With AI

Many projects still grapple with time-consuming processes, such as utilizing ARB files for translations. Addressing this gap, at LeanCode, we created the Flutter arb_translation package, automating the addition of missing translations with Google's Gemini AI or OpenAI's ChatGPT, promising streamlined localization for Flutter apps. Read how it works.
Flutter AI localization

How We 10x Improved Flutter UI Testing With Patrol 2.0

Discover the potential of Flutter UI testing and the enhancements brought by Patrol 2.0. Find out how our dev team achieved a remarkable 10x improvement in the efficiency and reliability o UI tests, unleashing streamlined testing workflows. Dive into the article and improve your Flutter UI testing process.
Patrol UI Testing Framework by LeanCode

Feature-Based Flutter App Architecture - LeanCode

Read about the LeanCode approach to Flutter architecture. We highlight some of the design decisions that should be made when developing a feature in mobile apps. This includes our approach to dependency injection, state management, widget lifecycle, and data fetching.
Flutter architecture by LeanCode