How to use Google Gemini API key? AI example tutorial, teaching and training included

Google Gemini API keys, no more worries! Get it done in one minute and say goodbye to worries! ✌✌✌

Detailed tutorials will teach you step by step, and even a novice can become a master in seconds!

Say goodbye to cumbersome steps and have it easilyAIArtifact! Join me to unlock the new world of AI!

How to use Google Gemini API key? AI example tutorial, teaching and training included

After the advent of Google's Gemini AI, Google released API access to its Gemini model. Now, Google provides API access to Gemini Pro, including text-only models and text-plus-visual models. This is a notable launch because to date, Google hasn't added visual capabilities to Bard, which runs a text-only model. With this API key, you can finally test Gemini's multimodal capabilities on your local computer. Let's learn how to access and use the Gemini API in this guide.

note:The Google Gemini API key is currently free for both text and visual models. It will be free until it becomes generally available early next year. Therefore, you can send up to 60 requests per minute without setting up Google Cloud billing or incurring any charges.

Configure Python and Pip on your computer

Head to our guide on PC or MacInstall Python and Pip. You need Python 3.9 or higher installed.

If you are using Linux system, you can follow our tutorialInstall Python and Pip on Ubuntu or other distributions.

You can run the following command in the terminal toConfirm Python and Pip is installed on your computer. It returns the version number.

python -V
pip -V

Confirm whether Python and Pip are installed. Picture 2

After successful installation, run the command below to install Google’s Generative AI dependencies.

pip install -q -U google-generativeai

Installing Google’s Generative AI Dependencies Part 3

How to obtain Gemini Pro API key?

Next, visit makersuite.google.com/app/apikey and log in with your Google account.

Under API Keys, clickCreate API key in new project" button.

Get Gemini Pro API Key 4th

Copy the API key and keep it in a safe place. Never make or share API keys public.

Copy the API key and save the 5th one

How to use Gemini Pro API Key (plain text mode)?

Similar to OpenAI, Google also uses Gemini API keys directly for development and testing purposes. I wrote the code fairly simple so that it can be easily tested and used by regular users. In this example, I'll show how to use API keys with Gemini Pro text models.

First, launch your favorite code editor. If you are new, just install Notepad + +. For advanced users, Visual Studio Code is a great tool.

Then, copy and paste the code below into a code editor.

import google.generativeai as genai
genai.configure(api_key='PASTE YOUR API KEY HERE')
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("What is the meaning of life?")
print(response.text)

In the code editor, paste your Gemini API key. As you can see, we defined the "gemini-pro" model, which is a plain text model. Additionally, we've added a query where you can ask questions.

"gemini-pro" model picture 6

Now, save the code and give the file a name. Make sure to add at the end .py. I named the file gemini.py, and save it on the desktop.

Name the file gemini.py Picture 7

Next, open a terminal and run the following command to move to the desktop.

cd Desktop

Once in the desktop terminal, simply run the following command to execute using Python gemini.py file.

python gemini.py

Use Python to execute gemini.py file Picture 8

Now it will answer your question gemini.py Issues set in the file.

Answer the question you set in the gemini.py file Picture 9

You can modify the question in the code editor, save it and run it again gemini.py file to get new replies in the terminal. This is how you use a Google Gemini API key to access a text-only Gemini Pro model.

Use Google Gemini API key to access plain text Gemini Pro model No. 10

How to use Gemini Pro API keys (text and visual models)

In this example, I'll demonstrate how to interact with a Gemini Pro multimodal model. It's not live on Google Bard yet, but through the API, you can access it immediately. Thankfully, the process is also very easy and seamless.

Open a new file in the code editor and paste the code below.

import google.generativeai as genai
import PIL.Image
img = PIL.Image.open('image.jpg')
genai.configure(api_key='PASTE YOUR API KEY HERE')
model = genai.GenerativeModel('gemini-pro-vision')
response = model.generate_content(["what is the total calorie count?", img])
print(response.text)

Make sure to paste your Gemini API key. Here we are using gemini-pro-vision model, which is a textual and visual model.

gemini-pro-vision model picture 11

Now, save the file on your desktop and add after the file name .py. I'll name it here geminiv.py .

Name it geminiv.py Picture 12

In the third line of code, as you can see, I point the AI ​​to the image.jpg files, the file names are exactly the same. Whatever image you are working with, make sure it is saved with geminiv.py The files are in the same location and have the same file name with the correct extension. You can pass in local JPG and PNG files up to 4MB.

Point the AI ​​to the image.jpg file saved on my desktop. Image 13

In the sixth line of code, you can ask questions related to the image. Since I was entering a food-related image, I asked Gemini Pro to calculate the total calories.

Now it's time to run the code in the terminal. Just move to the desktop (in my case) and run the commands below one by one. If you make any changes, be sure to save the file.

cd Desktop
python geminiv.py
geminiv.py No. 14

Gemini Pro visual models answer questions directly. You can ask further questions and ask the AI ​​to explain why.

Gemini Pro visual model will directly answer question 15

You can also enter a different image, but make sure it matches the image file name, change the question in the code, and run again geminiv.py file to get a new response.

How to use Gemini Pro API key in chat format?

Thanks to unconv's concise code, you can chat with a Gemini Pro model using a Gemini AI API key in a terminal window. This way, you don't have to change the problem in your code or rerun the Python file to get new output. You can continue chatting in the terminal window.

Best of all, Google implements chat history natively, so you don’t need to manually add replies or manage chat history in arrays or lists. With a simple function, Google can store all conversation history in a chat session. The specific operations are as follows:

Open the code editor and paste the code below.

import google.generativeai as genai
genai.configure(api_key='PASTE YOUR API KEY HERE')
model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat()
while True:
message = input("You: ")
response = chat.send_message(message)
print("Gemini: " + response.text)

As usual, copy and paste the key similar to the API above.

Chatting with Gemini Pro API Key Picture 16

At this point, save the file to your desktop or other location. Be sure to add at the end .py. I named it geminichat.py file.

File named geminichat.py No. 17

Now, open the terminal and move to the desktop. Next, run geminichat.py file.

cd Desktop
python geminichat.py

Run geminichat.py file picture 18

Now you can easily continue the conversation and it will remember your chat history. So there's another great way to use Google Gemini API keys.

gemini pro api responds in terminal chat picture 19

These are just some examples of what you can do with Google Gemini through the API. I'm glad Google is making its vision model available to enthusiasts and developers, and pairing it with OpenAI's DALL-E 3 and ChatGPT Compare. Although the Gemini Pro visual model is not as good as the GPT-4V model, it is still quite good. We are looking forward to the launch of Gemini Ultra, which will be comparable to the GPT-4 model.

Beyond that, the Gemini Pro API responds differently to Google Bard, which is also powered by a tweaked version of Gemini Pro. Bard's responses seemed a bit bland, but Gemini Pro's API responses were more lively and distinctive.

We'll be keeping a close eye on all changes in this area, so stay tuned for more on Gemini AI. In the meantime, please also check out the Google Gemini API yourself.

Comment

Your email address will not be published. Required fields * Callout

Scroll to Top