Skip to main content

User account menu

  • Log in
Home

Main navigation

  • Home
  • Blog
  • Case studies
  • Events
  • News
  • People
  • Projects

Breadcrumb

  1. Home
  2. Blog
  3. Building with Gemini API: Implementation Best Practices

Building with Gemini API: Implementation Best Practices

30 October 2024 by Sushil | 18 views
Image
gemini

​

 Gemini is a new model developed by Google, and Bard is becoming usable again. With Gemini, it is now possible to get almost perfect answers to your queries by providing them with images, audio, and text.
In this tutorial, we will learn about the Gemini API and how to set it up on your machine. We will also explore various Python API functions, including text generation and image understanding.

Introducing Gemini AI Models
 
Gemini is a new AI model developed through collaboration between teams at Google, including Google Research and Google DeepMind. It was built specifically to be multimodal, meaning it can understand and work with different types of data like text, code, audio, images, and video.
Gemini is the most advanced and largest AI model developed by Google to date. It has been designed to be highly flexible so that it can operate efficiently on a wide range of systems, from data centers to mobile devices. This means that it has the potential to revolutionize the way in which businesses and developers can build and scale AI applications.
Here are three versions of the Gemini model designed for different use cases:
Gemini Ultra: Largest and most advanced AI capable of performing complex tasks.
Gemini Pro: A balanced model that has good performance and scalability.
Gemini Nano: Most efficient for mobile devices.

To use the API, we have to first get an API key that you can can from here:

https://ai.google.dev/tutorials/setup

Image
a

 

​

After that click on “Get an API key” button and then click on “Create API key in new project”.
 

Image
key

​

Copy the API key and set it as an environment variable. We are using Deepnote and it is quite easy for us to set the key with the name “GEMINI_API_KEY”. Just go to the integration, scroll down and select environment variables.

Image
l

​

In the next step, we will instal the Python API using PIP:

pip install -q -U google-generativeai

 

After that, we will set the API key to Google’s GenAI and initiate the instance.

import google.generativeai as genai
import os

gemini_api_key = os.environ["GEMINI_API_KEY"]
genai.configure(api_key = gemini_api_key)

Gemini can generate multiple responses, called candidates, for a single prompt. You can select the most suitable one. In our case, we had only one respons.

response.candidates

Configuring the Response
 

You can customize your response using the `generation_config` argument. We are limiting candidate count to 1, adding the stop word "space," and setting max tokens and temperature.

response = model.generate_content(
    'Write a short story about aliens.',
    generation_config=genai.types.GenerationConfig(
        candidate_count=1,
        stop_sequences=['space'],
        max_output_tokens=200,
        temperature=0.7)
)

Markdown(response.text)
 


 

​

Tags
Gemini
Drupal
Popular blog
No

Quick Links

  • Blog
  • FAQ
  • Terms of Use

Support

  • Contact Us
  • Privacy Policy
  • Support

Follow Us

© 2025 MyChapter. Designed and Developed by Mychapter.

Clear keys input element