6.0 KiB
Getting Started with the Gemini API using Python
This tutorial will guide you through the process of setting up and making your first request to the Gemini API using the Google's Generative AI Python library.
Table of Contents
- Prerequisites
- Configure Your API Key
- Initialize the Model
- Make Your First Request
- Using the Chat Interface
- Exploring Further
- Troubleshooting
- Conclusion
Prerequisites
Before you begin, make sure you have the following:
-
Python 3.11+ installed
You can check your Python version by running:python --version
or:
python3 --version
-
A Google Cloud Project
If you don't have one, create a new project in the Google Cloud Console. -
A Gemini API Key
- Go to Google AI Studio.
- Click on "Get API Key".
- Select your Google Cloud Project where you want to enable the API.
- Click on "Create API Key". Copy this key; you'll need it later.
-
The
google-generativeai
Python library
You can install it using pip:pip install google-generativeai
Configure Your API Key
The google-generativeai
library needs your API key to authenticate your requests to the Gemini API. There are a couple of ways to provide it:
Method 1: Environment Variable (Recommended)
-
Set an environment variable named
GOOGLE_API_KEY
with your API key as the value.-
Linux/macOS:
export GOOGLE_API_KEY="YOUR_API_KEY"
-
Windows:
setx GOOGLE_API_KEY "YOUR_API_KEY"
(You might need to restart your console or IDE for it to take effect.)
-
-
Then in your Python code, configure like this:
import google.generativeai as genai import os genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
Method 2: Directly in Your Code (Less Secure)
You can set your API key directly in your Python code. However, this is less secure, especially if you are sharing or versioning your code.
import google.generativeai as genai
genai.configure(api_key="YOUR_API_KEY") # Replace "YOUR_API_KEY" with your actual key
Initialize the Model
Now, let's initialize the GenerativeModel
with the gemini-pro
model:
import google.generativeai as genai
# ... API key configuration (see "Configure Your API Key") ...
model = genai.GenerativeModel("gemini-pro")
Make Your First Request
Let's send a simple prompt to the Gemini API and get a response:
import google.generativeai as genai
# ... API key configuration and model initialization (see "Configure Your API Key" & "Initialize the Model") ...
prompt = "What is the capital of France?"
response = model.generate_content(prompt)
print(response.text)
Expected Output:
The capital of France is Paris.
(Or a similar, more elaborate response.)
Explanation:
- prompt: This variable holds the text prompt you are sending to the model.
- model.generate_content(prompt): This calls the API, sending the prompt and receiving the generated content.
- response.text: This accesses the text part of the response from the model.
Using the Chat Interface
The Gemini API also supports a chat interface where you can have back-and-forth conversations.
import google.generativeai as genai
# ... API key configuration (see "Configure Your API Key") ...
model = genai.GenerativeModel("gemini-pro")
messages = []
messages.append({
"role": "user",
"parts": ["What is the capital of France?"]
})
response = model.generate_content(messages)
print(response.text)
# Add model's response back into the conversation history
messages.append({
"role": "model",
"parts": [response.text]
})
# User asks another question
messages.append({
"role": "user",
"parts": ["And what is its population?"]
})
response = model.generate_content(messages)
print(response.text)
Explanation
- messages: This list stores the history of your conversation.
- Adding messages: Each item in
messages
is a dictionary with:- role: Either
"user"
or"model"
. - parts: A list of strings (or other content parts) representing the message content.
- role: Either
- model.generate_content(messages): Takes the entire message history to provide context for the model.
- Appending the model's response: To maintain conversation flow, you append the AI's response to
messages
so it “remembers” earlier turns.
Exploring Further
-
More Model Parameters
Check out the Google AI for Developers documentation to learn about additional parameters (e.g.,temperature
,top_k
,top_p
) for controlling the model's generation. -
Safety Settings
You can configure safety settings to control what type of content the model generates. See the Safety Settings documentation. -
Other Models
Explore other available models, such asgemini-pro-vision
for multimodal input (text + images).
Troubleshooting
- API key not found error
Make sure your API key is correctly configured as an environment variable or in your code. - PermissionDenied error
Verify that the API key is associated with a Google Cloud project that has the Gemini API enabled. - Other errors
Refer to the Gemini API documentation for more detailed error messages and troubleshooting steps.
Conclusion
Congratulations! You've now successfully made your first request to the Gemini API and even tried out a simple conversation. This is just the beginning of what you can do with this powerful API. Explore the documentation and experiment with different prompts and model parameters to unlock the full potential of Google's Generative AI models.