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

确认 Python 和 Pip 是否已安装

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

pip install -q -U google-generativeai

安装谷歌的生成式人工智能依赖项

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.

获取 Gemini Pro API 密钥

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

复制 API 密钥并保存

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" 模型

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.

将文件命名为 gemini.py

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

使用 Python 执行 gemini.py 文件

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

回答你在 gemini.py 文件中设置的问题

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.

使用 Google Gemini API 密钥访问纯文本 Gemini Pro 模型

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 模型

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

将其命名为 geminiv.py

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.

将 AI 指向保存在我的桌面上的 image.jpg 文件

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

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

Gemini Pro 视觉模型会直接回答问题

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.

使用 Gemini Pro API 密钥进行聊天

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.

命名为 geminichat.py 文件

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

cd Desktop
python geminichat.py

运行 geminichat.py 文件

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 在终端聊天中作出响应

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