File size: 4,359 Bytes
b02e301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Google Gemini AI Calculator

Created by [Vaibhav Manwatkar](https://github.com/learnwithvaibhavm) as a community contribution.

## Overview

This simple Python application demonstrates how to integrate with Google's Gemini AI model using the `google-generativeai` library. The application asks Gemini to solve a basic mathematical problem (2+2) and displays the AI's response, showcasing the fundamental interaction with Google's Generative AI API.

## Features

- **Google Gemini Integration**: Uses Google's latest Gemini 2.0 Flash Experimental model
- **Environment Variable Management**: Secure API key handling using `python-dotenv`
- **Simple Mathematical Query**: Demonstrates AI's ability to perform basic calculations
- **Clean Output**: Displays the AI's response in a readable format

## Prerequisites

- Python 3.7 or higher
- Google API key with access to Gemini API
- Required Python packages (see Installation section)

## Installation

1. **Clone or download this file** to your local machine

2. **Install required dependencies**:
   ```bash

   pip install google-generativeai python-dotenv

   ```
   
   Or if using `uv`:
   ```bash

   uv add google-generativeai python-dotenv

   ```

3. **Set up your Google API key**:
   - Get your API key from [Google AI Studio](https://makersuite.google.com/app/apikey)
   - Create a `.env` file in the same directory as the script
   - Add your API key to the `.env` file:
     ```text

     GOOGLE_API_KEY=your_actual_api_key_here

     ```


## Usage

1. **Run the application**:
   ```bash

   python 1_lab1_google.py

   ```

2. **Expected output**:
   ```

   4

   ```

## Code Structure

```python

from dotenv import load_dotenv

load_dotenv(override=True)



import os

import google.generativeai as genai



# Configure the API key

genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))



# Initialize the model

model = genai.GenerativeModel(model_name="gemini-2.0-flash-exp")



# Generate content

response = model.generate_content(["What is 2+2?"])

print(response.text)

```

## Key Components

### 1. Environment Setup
- `load_dotenv(override=True)`: Loads environment variables from `.env` file
- `os.getenv('GOOGLE_API_KEY')`: Retrieves the API key securely

### 2. Model Configuration
- `genai.configure()`: Sets up the API key for authentication
- `genai.GenerativeModel()`: Initializes the Gemini model with specified version

### 3. Content Generation
- `model.generate_content()`: Sends a prompt to the AI model
- `response.text`: Extracts the text response from the AI

## Model Information

- **Model Used**: `gemini-2.0-flash-exp` (Gemini 2.0 Flash Experimental)
- **Capabilities**: Text generation, reasoning, mathematical calculations
- **Input Format**: List of strings or single string
- **Output Format**: Response object with `.text` attribute

## Error Handling

The application includes a `pyright: ignore[reportMissingImports]` comment to suppress type checker warnings for the `google.generativeai` import, which is a common practice when the package might not be installed in all environments.

## Troubleshooting

### Common Issues

1. **ModuleNotFoundError**: Install the required package:
   ```bash

   pip install google-generativeai

   ```

2. **API Key Error**: Ensure your `.env` file contains a valid `GOOGLE_API_KEY`

3. **Authentication Error**: Verify your API key has access to the Gemini API

## Extending the Application

This basic example can be extended to:
- Ask more complex mathematical questions
- Implement conversation loops
- Add error handling for API failures
- Create a user interface for interactive queries
- Process different types of prompts beyond mathematics

## Dependencies

- `google-generativeai`: Google's official Python client for Generative AI
- `python-dotenv`: Loads environment variables from `.env` files

## License

This project is part of the community contributions for the Agents course and follows the same licensing terms.

## Contributing

Feel free to fork this project and submit improvements or additional features as pull requests.

## Author

**Vaibhav Manwatkar**
- GitHub: [@learnwithvaibhavm](https://github.com/learnwithvaibhavm)
- This is a community contribution to the Agents course