The release of Gemini 3.0 and 1.5 Pro didn't just bring better chat capabilities; it introduced a powerful SDK for developers. With a massive context window (up to 2 million tokens), you can now feed entire documents, codebases, or datasets into a Python script and get instant analysis.
In this tutorial, we will build a simple AI Data Analyst that can read CSV data and find profitable trends without you needing to write complex Pandas filters.
Step 1: Installation & Setup
First, you need to install the official Google Generative AI library. Open your terminal or command prompt and run:
pip install -q -U google-generativeai
Note: You will need an API Key. You can get one for free at Google AI Studio.
Step 2: Test Your Connection
Before building the agent, let's verify everything works with a simple "Hello World" test. This script connects to Gemini and asks a basic question.
import google.generativeai as genai
import os
# Configure the API key
# Replace 'YOUR_ACTUAL_API_KEY' with your real key
os.environ["GEMINI_API_KEY"] = "YOUR_ACTUAL_API_KEY"
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
# Initialize the Model
model = genai.GenerativeModel('gemini-1.5-flash')
# Send a Test Prompt
print("Sending prompt to Gemini...")
response = model.generate_content("Hello Gemini! Are you ready to code?")
# Print the Result
print(f"Response: {response.text}")
Expected Output:
Sending prompt to Gemini...
Response: Hello! I am ready to help you with your coding tasks. What would you like to work on today?
Step 3: The Data Analysis Agent
Now that we are connected, let's build something useful. We will simulate a CSV file containing sales data, but you could easily load a real file using pd.read_csv().
import google.generativeai as genai
import os
# --- CONFIGURATION ---
api_key = os.environ.get("GEMINI_API_KEY", "YOUR_API_KEY")
genai.configure(api_key=api_key)
model = genai.GenerativeModel('gemini-1.5-flash')
# --- MOCK DATA ---
# Imagine this is loaded from 'sales_data.csv'
csv_data = """
Date,Product,Region,Sales,Profit
2024-01-01,Widget A,North,1000,200
2024-01-02,Widget B,North,1500,400
2024-01-03,Widget A,South,800,150
2024-01-04,Widget C,East,2000,1000
2024-01-05,Widget A,North,1200,240
"""
def analyze_data(data):
print("🤖 Analyzing data with Gemini...")
# The Prompt: We give the model a persona and the raw data
prompt = f"""
You are a Senior Data Analyst.
Analyze the following sales data and tell me:
1. Which product is the most profitable?
2. Which region should we focus on expanding?
3. Are there any anomalies?
Data:
{data}
"""
response = model.generate_content(prompt)
return response.text
# Run the analysis
result = analyze_data(csv_data)
print("\n--- GEMINI INSIGHTS ---")
print(result)
Step 4: Why This Matters
Traditionally, to answer "Which product is most profitable?", you would need to write Pandas code:
# The Old Way (Pandas)
df.groupby('Product')['Profit'].sum().sort_values(ascending=False).head(1)
With Gemini, you just ask. This is revolutionary for:
- Messy Data: LLMs can handle typos and inconsistent formatting better than strict code.
- Complex Logic: You can ask subjective questions like "Which region looks promising?" that hard code can't easily answer.
- Speed: You spend less time writing boilerplate code and more time acting on insights.
Conclusion
Integrating Gemini into your Python workflows allows you to build "Agentic" applications—software that can reason about data, not just process it. As Gemini 3.0 rolls out with even stronger reasoning capabilities ("Deep Think"), these agents will only become smarter.
Comments
Post a Comment