Hello! Welcome back to our project.
In our last lesson, we took a crucial step in building a secure and professional application. You learned how to handle secret credentials like API keys using environment variables and a .env file, ensuring they are never exposed in your code.
Now that we have a secure way to store our key, it's time to install the tools we need to actually use it. This lesson focuses on our next learning outcome: installing the Google AI Python library to interact with the Gemini API. This library is the bridge that will connect your Python application to Google's powerful AI models, bringing the core feature of your writing assistant to life.
What is a Software Development Kit (SDK)?
Before we install the library, let's understand what it is. The tool we're about to install is a Software Development Kit (SDK). Think of an SDK as a specialized toolkit built for a specific job.
Imagine you want to build a custom piece of furniture. You could start from raw materials—smelting metal for screws, milling wood for planks—but that would be incredibly difficult and time-consuming. Instead, you'd go to a hardware store and buy a kit containing pre-made screws, brackets, and instructions.
An SDK is like that hardware store kit. It provides pre-written Python functions that handle the complex, low-level work of communicating with the Gemini API. Instead of you having to manually format network requests and parse raw data, the SDK gives you simple commands to send a prompt and receive the AI's response. It's a "wrapper" that makes the API much easier to use for Python developers.
A Beginner's Guide to the Google Gen AI SDK for Python
To get a better sense of what this SDK does, let's read a short explanation from 'A Beginner's Guide to the Google Gen AI SDK for Python'.
Read the first two paragraphs of the article. Focus on understanding how the SDK acts as a 'wrapper' around the Gemini API, making it more user-friendly.
Step 1: Check Your Prerequisites
As the article mentions, the Google GenAI SDK requires a modern version of Python. Let's quickly verify your setup.
Open your terminal (the same command-line interface you use to run your Flask app) and run the following command:
python --version
or on some systems:
python3 --version
The SDK requires Python 3.9 or higher. If the command shows a version like "Python 3.9.x", "Python 3.10.x", or higher, you're all set!
Step 2: Install the Google GenAI Library
In the last lesson, you used pip, the Python Package Installer, to install the python-dotenv library. We'll use the exact same tool to install the Google GenAI SDK.
The official documentation provides the precise command we need.
Gemini API quickstart - Google AI for Developers
Let's look at the official 'Gemini API quickstart' guide from Google to get the installation command.
In the guide, find the section named 'Install the Google GenAI SDK' and look at the command provided under the 'Python' tab.
As you saw in the documentation, the command is:
pip install -q -U google-genai
Run this command in your terminal. Let's quickly break down the flags:
install: Thepipcommand to install a package.-U: This stands for--upgrade. It tellspipto install the latest version of the package, or upgrade it if you already have an older version installed. It's a good practice to include this.-q: This stands for--quiet. It reduces the amount of textpipprints during installation, keeping the output clean.google-genai: The official name of the package we want to install.
To see this in action, you can watch this short video clip.
Gemini API with Python - Getting Started Tutorial
The video 'Gemini API with Python - Getting Started Tutorial' by Patrick Loeber shows this exact command being run in a terminal.
Watch from 02:04 to 02:32. Notice how the command is typed into the terminal to start the installation.
After the command finishes, the library is installed and ready to be used in your project.
Test your understanding!
You've now used pip to install two different packages: python-dotenv and google-genai. Where does pip get these packages from? And why is using a tool like pip better than manually downloading and copying the code files yourself?
Show answer
pip downloads packages from the Python Package Index (PyPI), which is a central, public repository for Python software.
Using pip is better because it handles a lot of complexity for you:
- Dependency Management: If
google-genaineeds other packages to work,pipwill automatically find and install them too. - Versioning:
pipmakes it easy to install specific versions of packages or upgrade to the latest version (like we did with the-Uflag). - Simplicity: It's a single, standard command, which is much simpler and less error-prone than finding, downloading, and placing files in the correct directories yourself.
Step 3: Integrate the Library into Your Application
Now that the library is installed, let's update our app.py file to use it. This involves two small changes:
- Import the library: Just like with Flask, we need to
importthe library at the top of our file to make its functions available. The standard convention is to importgoogle.generativeaiand give it a shorter alias,genai, to make our code easier to read. - Configure the library: The library needs to know your API key to make authenticated requests. It has a simple configuration function where we can pass the key we've already loaded from our environment variables.
Open your app.py file and make the following changes:
# app.py
import os
from flask import Flask, render_template, request, jsonify
from dotenv import load_dotenv
import google.generativeai as genai # 1. Import the new library
load_dotenv()
# --- Your Flask app setup ---
app = Flask(__name__)
# --- Configure the Gemini API ---
try:
# 2. Configure the library with the API key
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
print("Gemini API configured successfully.")
except Exception as e:
print(f"Error configuring Gemini API: {e}")
# ... your routes will go here in the future ...
@app.route('/')
def index():
return render_template('index.html')
# Add your existing POST endpoint for text analysis here if you have one
# For now, we'll assume it's just the index route
By adding these two lines, you have successfully connected your application's code to the Google AI library and provided it with the necessary credentials. Our app is now ready to make its first call to the Gemini API.
Conclusion
Excellent work! This was another foundational lesson that builds on our previous steps. You've now installed the official Python SDK for the Gemini API and integrated it into your Flask application.
Key Takeaways:
- An SDK (Software Development Kit) is a set of tools and libraries that simplify development for a specific platform or API.
- You can install Python packages like
google-genaifrom the Python Package Index (PyPI) using thepip installcommand. - It's good practice to use the
-Uflag to ensure you have the latest version of a package. - After installing a library, you must
importit into your Python script to use its functions. - We configured the
genailibrary by passing the API key we securely loaded from our environment variables.
Our setup is now complete. In the next lesson, we'll take the exciting next step: constructing a prompt for a text generation task and making our very first request to the Gemini API.