The way to Entry and Use Gemini API for Free


How to Access and Use Gemini API for Free
Picture by Writer
 

Gemini is a brand new mannequin developed by Google, and Bard is changing into usable once more. With Gemini, it’s now potential to get virtually good solutions to your queries by offering them with pictures, audio, and textual content.

On this tutorial, we are going to study in regards to the Gemini API and easy methods to set it up in your machine. We can even discover varied Python API capabilities, together with textual content era and picture understanding.

 

 

Gemini is a brand new AI mannequin developed by means of collaboration between groups at Google, together with Google Analysis and Google DeepMind. It was constructed particularly to be multimodal, which means it will possibly perceive and work with several types of information like textual content, code, audio, pictures, and video.

Gemini is probably the most superior and largest AI mannequin developed by Google to this point. It has been designed to be extremely versatile in order that it will possibly function effectively on a variety of programs, from information facilities to cellular units. Which means that it has the potential to revolutionize the way in which by which companies and builders can construct and scale AI purposes.

Listed below are three variations of the Gemini mannequin designed for various use instances:

  • Gemini Extremely: Largest and most superior AI able to performing advanced duties.
  • Gemini Professional: A balanced mannequin that has good efficiency and scalability.
  • Gemini Nano: Most effective for cellular units.

 

How to Access and Use Gemini API for Free
Picture from Introducing Gemini

 

Gemini Extremely has state-of-the-art efficiency, exceeding the efficiency of GPT-4 on a number of metrics. It’s the first mannequin to outperform human consultants on the Large Multitask Language Understanding benchmark, which exams world information and drawback fixing throughout 57 numerous topics. This showcases its superior understanding and problem-solving capabilities.

 

 

To make use of the API, we have now to first get an API key you can can from right here: https://ai.google.dev/tutorials/setup

 
How to Access and Use Gemini API for Free
 

After that click on on “Get an API key” button after which click on on “Create API key in new mission”.

 
How to Access and Use Gemini API for Free
 

Copy the API key and set it as an surroundings variable. We’re utilizing Deepnote and it’s fairly straightforward for us to set the important thing with the identify “GEMINI_API_KEY”. Simply go to the combination, scroll down and choose surroundings variables.

 
How to Access and Use Gemini API for Free
 

Within the subsequent step, we are going to instal the Python API utilizing PIP:

pip set up -q -U google-generativeai

 

After that, we are going to set the API key to Google’s GenAI and provoke the occasion.

import google.generativeai as genai
import os

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

 

 

After organising the API key, utilizing the Gemini Professional mannequin to generate content material is easy. Present a immediate to the `generate_content` perform and show the output as Markdown.

from IPython.show import Markdown

mannequin = genai.GenerativeModel('gemini-pro')
response = mannequin.generate_content("Who's the GOAT within the NBA?")

Markdown(response.textual content)

 

That is superb, however I do not agree with the record. Nevertheless, I perceive that it is all about private desire.

 
How to Access and Use Gemini API for Free
 

Gemini can generate a number of responses, referred to as candidates, for a single immediate. You may choose probably the most appropriate one. In our case, we had just one respons.

 

How to Access and Use Gemini API for Free
 

Let’s ask it to put in writing a easy recreation in Python.

response = mannequin.generate_content("Construct a easy recreation in Python")

Markdown(response.textual content)

 

The result’s easy and to the purpose. Most LLMs begin to clarify the Python code as a substitute of writing it.

 

How to Access and Use Gemini API for Free

 

 

You may customise your response utilizing the `generation_config` argument. We’re limiting candidate depend to 1, including the cease phrase “area,” and setting max tokens and temperature.

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

Markdown(response.textual content)

 

As you’ll be able to see, the response stopped earlier than the phrase “area”. Wonderful.

 
How to Access and Use Gemini API for Free

 

 

You can too use the `stream` argument to stream the response. It’s just like the Anthropic and OpenAI APIs however quicker.

mannequin = genai.GenerativeModel('gemini-pro')
response = mannequin.generate_content("Write a Julia perform for cleansing the info.", stream=True)

for chunk in response:
    print(chunk.textual content)

 

How to Access and Use Gemini API for Free

 

 

On this part, we are going to load Masood Aslami’s photograph and use it to check the multimodality of Gemini Professional Imaginative and prescient.

Load the photographs to the `PIL` and show it.

import PIL.Picture

img = PIL.Picture.open('pictures/photo-1.jpg')

img

 

We now have a top quality photograph of Rua Augusta Arch.

 
How to Access and Use Gemini API for Free
 

Let’s load the Gemini Professional Imaginative and prescient mannequin and supply it with the picture.

mannequin = genai.GenerativeModel('gemini-pro-vision')

response = mannequin.generate_content(img)

Markdown(response.textual content)

 

The mannequin precisely recognized the palace and offered extra details about its historical past and structure.

 
How to Access and Use Gemini API for Free
 

Let’s present the identical picture to the GPT-4 and ask it in regards to the picture. Each fashions have offered virtually comparable solutions. However I just like the GPT-4 response extra.

 
How to Access and Use Gemini API for Free
 

We’ll now present textual content and the picture to the API. We now have requested the imaginative and prescient mannequin to put in writing a journey weblog utilizing the picture as reference.

response = mannequin.generate_content(["Write a travel blog post using the image as reference.", img])

Markdown(response.textual content)

 

It has offered me with a brief weblog. I used to be anticipating longer format.

 
How to Access and Use Gemini API for Free
 

In comparison with GPT-4, the Gemini Professional Imaginative and prescient mannequin has struggled to generate a long-format weblog.

 
How to Access and Use Gemini API for Free

 

 

We are able to arrange the mannequin to have a back-and-forth chat session. This manner, the mannequin remembers the context and response utilizing the earlier conversations.

In our case, we have now began the chat session and requested the mannequin to assist me get began with the Dota 2 recreation.

mannequin = genai.GenerativeModel('gemini-pro')

chat = mannequin.start_chat(historical past=[])

chat.send_message("Are you able to please information me on easy methods to begin enjoying Dota 2?")

chat.historical past

 

As you’ll be able to see, the `chat` object is saving the historical past of the consumer and mode chat.

 
How to Access and Use Gemini API for Free
 
We are able to additionally show them in a Markdown type.

for message in chat.historical past:
    show(Markdown(f'**{message.function}**: {message.components[0].textual content}'))

 

How to Access and Use Gemini API for Free
 

Let’s ask the observe up query.

chat.send_message("Which Dota 2 heroes ought to I begin with?")

for message in chat.historical past:
    show(Markdown(f'**{message.function}**: {message.components[0].textual content}'))

 

We are able to scroll down and see the complete session with the mannequin.

 
How to Access and Use Gemini API for Free

 

 

Embedding fashions have gotten more and more widespread for context-aware purposes. The Gemini embedding-001 mannequin permits phrases, sentences, or complete paperwork to be represented as dense vectors that encode semantic which means. This vector illustration makes it potential to simply evaluate the similarity between completely different items of textual content by evaluating their corresponding embedding vectors.

We are able to present the content material to `embed_content` and convert the textual content into embeddings. It’s that straightforward.

output = genai.embed_content(
    mannequin="fashions/embedding-001",
    content material="Are you able to please information me on easy methods to begin enjoying Dota 2?",
    task_type="retrieval_document",
    title="Embedding of Dota 2 query")

print(output['embedding'][0:10])

 

[0.060604308, -0.023885584, -0.007826327, -0.070592545, 0.021225851, 0.043229062, 0.06876691, 0.049298503, 0.039964676, 0.08291664]

 

We are able to convert a number of chunks of textual content into embeddings by passing an inventory of strings to the ‘content material’ argument.

output = genai.embed_content(
    mannequin="fashions/embedding-001",
    content material=[
        "Can you please guide me on how to start playing Dota 2?",
        "Which Dota 2 heroes should I start with?",
    ],
    task_type="retrieval_document",
    title="Embedding of Dota 2 query")

for emb in output['embedding']:
    print(emb[:10])

 

[0.060604308, -0.023885584, -0.007826327, -0.070592545, 0.021225851, 0.043229062, 0.06876691, 0.049298503, 0.039964676, 0.08291664]

[0.04775657, -0.044990525, -0.014886052, -0.08473655, 0.04060122, 0.035374347, 0.031866882, 0.071754575, 0.042207796, 0.04577447]

 

If you happen to’re having bother reproducing the identical end result, try my Deepnote workspace.

 

 

There are such a lot of superior capabilities that we did not cowl on this introductory tutorial. You may study extra in regards to the Gemini API by going to the Gemini API: Quickstart with Python.

On this tutorial, we have now realized about Gemini and easy methods to entry the Python API to generate responses. Particularly, we have now realized about textual content era, visible understanding, streaming, dialog historical past, customized output, and embeddings. Nevertheless, this simply scratches the floor of what Gemini can do.

Be at liberty to share with me what you might have constructed utilizing the free Gemini API. The probabilities are limitless.

 
 

Abid Ali Awan (@1abidaliawan) is an authorized information scientist skilled who loves constructing machine studying fashions. Presently, he’s specializing in content material creation and writing technical blogs on machine studying and information science applied sciences. Abid holds a Grasp’s diploma in Expertise Administration and a bachelor’s diploma in Telecommunication Engineering. His imaginative and prescient is to construct an AI product utilizing a graph neural community for college kids scuffling with psychological sickness.

Leave a Reply

Your email address will not be published. Required fields are marked *