Skip to content

Repository files navigation

Gemini Python - Building Tools with the API

Python Gemini google-generativeai uv

A study project demonstrating how to build small tools with the Google Gemini API using Python. The scripts cover text generation, classification, sentiment analysis, model selection by token limit, and cost estimation.

Table of Contents

Technologies used

  • Python 3.12+: base of the project.
  • google-generativeai: official SDK for accessing Gemini models.
  • python-dotenv: loads environment variables from a .env file.
  • Gemini 2.5 Flash / Pro: models used in the examples.

Project structure

.
├── main.py                 # Generates a product list with brief descriptions
├── categorizer.py          # Classifies a product into predefined categories
├── sentiment_analyzer.py   # Summarizes reviews and extracts sentiment/strengths/weaknesses
├── model_selector.py       # Picks Flash or Pro based on the prompt's token count
├── token_counter.py        # Shows token limits and estimates cost per response
└── data/
    ├── purchase_profile.csv           # Sample customer purchase history
    ├── reviews-<product>.txt          # Sample product reviews (input)
    └── response-<product>             # Generated sentiment analysis (output)

What each script does

Script Description
main.py Generates a list of products with a brief description using a system prompt.
categorizer.py Classifies a product into predefined categories (input via terminal).
sentiment_analyzer.py Summarizes reviews and reports overall sentiment, strengths and weaknesses. Reads data/reviews-<product>.txt files and saves the result to data/response-<product>.
model_selector.py Picks a model based on the token limit and analyzes purchase profiles in data/purchase_profile.csv.
token_counter.py Shows token limits and estimates the cost per response.

How to run

1) Requirements

  • Python 3.12+ installed
  • A Gemini API key (GEMINI_API_KEY environment variable)

2) Install dependencies

Option A (pip):

python -m venv .venv
. .venv/bin/activate
pip install -U pip
pip install .

Option B (uv):

uv sync

3) Set up the API key

Create a .env file in the project root:

GEMINI_API_KEY=your_key_here

4) Run the examples

python main.py
python categorizer.py
python sentiment_analyzer.py
python model_selector.py
python token_counter.py

Real-world applications

  • E-commerce and retail: automatic product categorization and catalog enrichment.
  • Customer service and support: sentiment analysis on reviews and tickets to spot recurring issues.
  • Marketing: fast generation of descriptions and segmented campaigns.
  • Finance and planning: cost-per-query estimation and routing to the most cost-effective model.
  • BI and CRM: customer segmentation based on purchase history.

Notes

  • Adjust the prompts to fit your domain and business rules.
  • For production use, consider error handling, structured logging, and input validation.

About

A study project demonstrating how to build small tools with the Google Gemini API using Python. The scripts cover text generation, classification, sentiment analysis, model selection by token limit, and cost estimation.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages