How to build an AI agent with Gemini 3 in 10 minutes
Using the Gemini 3 model, we will create an automated agent that analyzes CSV files. This agent loads data, calculates statistics, identifies trends, and provides recommendations for business actions.
Using the Gemini 3 model, we will create an automated agent that analyzes CSV files . This agent loads data, calculates statistics, identifies trends, and provides recommendations for business actions. Actual business reports, sales dashboards, and any other tasks requiring insights from spreadsheet data can benefit from this approach.
First, let's configure the Gemini toolkit.
Step 1: Install the Gemini 3 toolkit.
Follow these steps to install the Gemini 3 toolkit on your computer:
1. First, install the Gemini SDK. Open the terminal and run the command:
pip install google-generativeai 2. Next, we need the API key, so go to Google's API key page and select 'Create API key' . Name it as required, generate the key, and copy it.
3. Set your API key as an environment variable. For Windows, open PowerShell and run the following command:
$env:GEMINI_API_KEY='đặt-api-key-của-bạn-ở-đây' Mac/Linux users can run the following command:
export GEMINI_API_KEY='đặt-api-key-của-bạn-ở-đây' 4. Test your setup with a short Python code snippet:
import google.generativeai as genai import os genai.configure(api_key=os.getenv('GEMINI_API_KEY')) model = genai.GenerativeModel('gemini-3-pro-preview') response = model.generate_content('Hello, Gemini!') print(response.text) If you see feedback, you're ready to build your agent.
Step 2: Set up the project directory
1. Create a working directory for your agent project using the following command in the terminal:
mkdir gemini-data-agent cd gemini-data-agent # sử dụng lệnh này để điều hướng vào bên trong thư mục đã tạo
2. Now, let's create a sample dataset. This CSV file contains sales data across different products and regions. Save this file as sales.csv:
date,product,sales,region,quantity 2024-01-15,Widget A,1200,North,45 2024-01-16,Widget B,850,South,30 2024-01-17,Widget A,1100,East,42 2024-01-18,Widget C,2000,West,50 2024-01-19,Widget B,750,North,25 2024-01-20,Widget A,1300,South,48 2024-01-21,Widget C,1900,East,47 2024-01-22,Widget B,900,West,32 2024-01-23,Widget A,1150,North,44 2024-01-24,Widget C,2100,South,52 2024-01-25,Widget B,820,East,28 2024-01-26,Widget A,1250,West,46 2024-01-27,Widget C,1950,North,49 2024-01-28,Widget B,880,South,31 2024-01-29,Widget A,1180,East,43 This dataset contains 15 transactions with information on date, product (Widget A, B, and C), revenue, region, and quantity sold. This information will be used by the agent to calculate performance metrics, identify trends, and generate business insights.
3. Next, create a configuration file that defines how your agent should behave. Save this file as agent_config.json:
{ "role": "Data Analysis Agent", "goal": "Load file CSV, phân tích dữ liệu và tạo ra những hiểu biết có thể hành động được", "tasks": [ "Đọc nội dung file CSV và xác minh chất lượng dữ liệu", "Tính toán số liệu thống kê tóm tắt cho tất cả các cột số", "Xác định xu hướng, mô hình hoặc các giá trị ngoại lệ trong dữ liệu", "Tạo ra những hiểu biết sâu sắc bằng ngôn ngữ tự nhiên thông qua các đề xuất" ], "allowed_tools": ["code_execution", "file_read"], "restrictions": "Chỉ truy cập các file trong thư mục dự án. Chỉ thực thi code Python an toàn.", "output_format": "Báo cáo có cấu trúc với số liệu thống kê, hình ảnh trực quan và phân tích chuyên sâu" } This configuration file tells the agent its role, the tasks it must complete, the tools it can use, and the limitations it should adhere to. Your project structure will now look like this:
gemini-data-agent/ ├── sales.csv ├── agent_config.json └── agent.py (chúng ta sẽ tạo ra nó ở bước tiếp theo) We already have the data and configuration ready. The next step is to define how the agent will operate and think.
Step 3: Identify the agent's goals and behavior.
Now, we need to tell the agent exactly what to do and how to do it. This happens in two parts:
- The configuration file was just created.
- The system instructions control the agent's behavior.
The `agent_config.json` file has defined the high-level targets. Next, create a new file named `agent.py` and start with the configuration loader:
import google.generativeai as genai import json import os class DataAnalysisAgent: def __init__(self, config_path='agent_config.json', api_key=None): """Khởi tạo agent với cấu hình và API key.""" self.config = self.load_config(config_path) self.api_key = api_key or os.getenv('GEMINI_API_KEY') if not self.api_key: raise ValueError("Cần Gemini API key.") self.setup_gemini() def load_config(self, config_path): """Load cấu hình agent từ file JSON""" with open(config_path, 'r') as f: return json.load(f) Now, let's add a method for building system commands. This is where you define exactly how the agent should approach analysis tasks:
def build_system_instruction(self): """Xây dựng hướng dẫn hệ thống cho Gemini agent.""" instruction = f""" Bạn là một {self.config['role']}. Mục tiêu của bạn: {self.config['goal']} Các nhiệm vụ cần hoàn thành: {chr(10).join(f"- {task}" for task in self.config['tasks'])} Các công cụ có sẵn: {', '.join(self.config['allowed_tools'])} Hạn chế: {self.config['restrictions']} Các quy tắc thực thi code: - Thực thi code Python một cách im lặng mà không hiển thị code - Sử dụng pandas để thao tác dữ liệu - Tạo ra những thông tin chi tiết rõ ràng, có thể hành động được - Trình bày kết quả trong các bảng được định dạng gọn gàng Khi phân tích dữ liệu: 1. Load file CSV bằng pandas 2. Tính toán các số liệu thống kê tóm tắt toàn diện 3. Phân tích hiệu suất theo sản phẩm và khu vực 4. Xác định các giá trị ngoại lệ bằng các phương pháp thống kê (IQR) 5. Tạo ra 5-7 thông tin chi tiết quan trọng bằng ngôn ngữ tự nhiên 6. Đưa ra các khuyến nghị có thể hành động được ĐỊNH DẠNG KẾT QUẢ QUAN TRỌNG: - KHÔNG hiển thị các block code Python hoặc chi tiết thực thi code - CHỈ hiển thị kết quả trong các phần được định dạng rõ ràng với tiêu đề - Sau các bảng, cung cấp một phần phân tích toàn diện với những hiểu biết và khuyến nghị chính - Sử dụng tiêu đề phần rõ ràng và định dạng chuyên nghiệp """ return instruction.strip()
Note : All methods here belong to the DataAnalysisAgent class, so they are indented two spaces below the class definition.
These instructions inform the agent:
- What is its role (Data Analysis Agent)?
- What is its main goal (analyzing data and generating insights)?
- Specific tasks to be completed (loading data, calculating statistics, searching for patterns)
- The tools it can use (to execute code and read files)
- Restrictions to follow (only access project files, use safe code)
- How to format the output (clean table, no code displayed, professional structure)
The agent will use these instructions every time it analyzes data. This is like providing the agent with a detailed manual that it will automatically follow.
Step 4: Configure the tool (access files and execute code)
Gemini agents operate by calling tools to complete tasks. For data analysis, we need two main tools:
- Execute code
- Access the file.
Add this method to your DataAnalysisAgent class:
def setup_gemini(self): """Thiết lập mô hình Gemini 3 bằng các công cụ và hướng dẫn hệ thống.""" # Configure the API genai.configure(api_key=self.api_key) # Tạo mô hình bằng công cụ thực thi code self.model = genai.GenerativeModel( 'gemini-3-pro-preview', tools='code_execution', system_instruction=self.build_system_instruction() ) print("Initializing Gemini 3 Agent.") print(f"Role: {self.config['role']}") print(f"Goal: {self.config['goal']}") print(f"Tools: {', '.join(self.config['allowed_tools'])}") Note : This method is part of the DataAnalysisAgent class and is indented two spaces below the class definition.
Here's what happens in this setup:
1. API Configuration : The genai.configure() command authenticates your connection to Gemini using your API key.
2. Creating the Model : We initialize the Gemini model with 3 main components:
- Model name: gemini-3-pro-preview (latest Gemini agent model)
- Tool: code_execution (allows the agent to write and run Python code)
- System instructions: Detailed behavioral instructions that we created in the previous step.
3. Tool safety : Gemini executes code in a sandbox environment . This means:
- The agent can only read files that you explicitly provide.
- The code runs independently without needing to access your system.
- You control the directories and files that the agent can access.
- All operations are logged and transparent.
The code_execution tool makes this agent truly autonomous. Instead of just describing the analysis to be performed, the agent actually writes Python code, executes it, interprets the results, and generates insights.
Step 5: Write the agent definition.
Now, we'll create the main analysis method that links everything together. This method takes a CSV file, sends it to Gemini along with the instructions, and returns the analysis results. Add this code to your DataAnalysisAgent class:
def analyze_data(self, file_path): """Phương pháp chính để phân tích file CSV bằng Gemini 3.""" print(f"Analyzing: {file_path}n") # Đọc nội dung file CSV with open(file_path, 'r') as f: csv_content = f.read() # Tạo prompt để phân tích prompt = f""" Tôi có một file CSV với nội dung như sau: ```csv {csv_content} ``` Phân tích dữ liệu này một cách toàn diện và trình bày kết quả theo định dạng sau: 1. Hiển thị bảng thống kê tóm tắt với tiêu đề "--- Summary Statistics for Sales and Quantity ---" 2. Hiển thị bảng hiệu suất sản phẩm với tiêu đề "--- Performance by Product ---" 3. Hiển thị bảng hiệu suất khu vực với tiêu đề "--- Performance by Region ---" 4. Hiển thị kết quả phát hiện giá trị ngoại lệ với tiêu đề "--- Outlier Detection (IQR Method) ---" 5. Cung cấp phân tích toàn diện với: - Load dữ liệu và kiểm tra chất lượng - Thông tin chi tiết quan trọng (danh sách đánh số) - Các khuyến nghị có thể thực hiện (danh sách đánh số) Thực thi code Python để phân tích nhưng KHÔNG hiển thị mã nguồn - chỉ hiển thị kết quả và thông tin chi tiết đã được định dạng. Sử dụng pandas để phân tích và trình bày tất cả các bảng dưới dạng DataFrame gọn gàng. """ # Gửi yêu cầu tới Gemini response = self.model.generate_content(prompt) # In kết quả print(response.text) return response.text
Note : This method is part of the DataAnalysisAgent class, so it is indented two spaces below the class definition.
Your complete agent definition is now ready. The `analyze_data()` method performs the following:
- Read the contents of a CSV file from your local directory.
- Create a detailed prompt for Gemini that tells them exactly which analysis to perform and how to format the results.
- Send the request to Gemini along with the data.
- Returns the formatted analysis results.
Now the agent has everything it needs: configuration, system instructions, tool access, and analytical methods.
Step 6: Create the main implementation function.
Now, let's create the main function that will run the agent. Add this code to the end of your agent.py file (outside the class, without indentation):
def main(): """Chức năng thực thi chính.""" print("nGEMINI 3 DATA ANALYSIS AGENT (Production)n") # Khởi tạo agent agent = DataAnalysisAgent('agent_config.json') # Chạy phân tích agent.analyze_data('sales.csv') if __name__ == "__main__": main() This function performs three tasks:
- Print the title to show the agent that is starting up.
- Create an agent instance by loading the configuration file we created earlier.
- Call the analyze_data() function with the path to our CSV file.
Step 7: Improve and debug the agent.
What happens when the API key is missing, the CSV file doesn't exist, or the data unexpectedly gets corrupted? Without error handling mechanisms, your agent will crash with confusing messages. Let's make it more robust.
Wrap your main() function with appropriate error handling:
def main(): """Chức năng thực thi chính.""" print("nGEMINI 3 DATA ANALYSIS AGENT (Production)n") # Kiểm tra API key api_key = os.getenv('GEMINI_API_KEY') if not api_key: print("ERROR: GEMINI_API_KEY environment variable not set.") print("nTo use this agent:") print("1. Get your API key from: https://ai.google.dev/") print("2. Set it as environment variable:") print(" export GEMINI_API_KEY='your-api-key-here'") return try: # Khởi tạo agent agent = DataAnalysisAgent('agent_config.json') # Chạy phân tích agent.analyze_data('sales.csv') except FileNotFoundError as e: print(f"Error: File not found - {str(e)}") print("Make sure sales.csv and agent_config.json exist in the current directory") except Exception as e: print(f"Error: {str(e)}") print("nMake sure:") print("1. You have installed: pip install google-generativeai") print("2. Your API key is valid") print("3. All required files exist in the current directory") if __name__ == "__main__": main() And that's it, everything is set up correctly!
Step 8: Check the agent
Now, let's run the agent and see if it provides us with the necessary details. Run the agent from the command line window:
python agent.py If everything is set up correctly, you will see the following result:
GEMINI 3 DATA ANALYSIS AGENT (Production) Khởi tạo Gemini 3 Agent. Role: Data Analysis Agent Goal: Load file CSV, phân tích dữ liệu và tạo ra những thông tin chi tiết hữu ích Tools: code_execution, file_read Phân tích: sales.csv --- Summary Statistics for Sales and Quantity --- sales quantity count 15.000000 15.000000 mean 1288.666667 40.800000 std 468.414753 8.993649 min 750.000000 25.000000 25% 890.000000 31.500000 50% 1180.000000 44.000000 75% 1600.000000 47.500000 max 2100.000000 52.000000 --- Performance by Product --- total_sales average_sales total_quantity average_quantity transaction_count product Widget C 7950 1987.500000 198 49.500000 4 Widget A 7180 1196.666667 268 44.666667 6 Widget B 4200 840.000000 146 29.200000 5 --- Performance by Region --- total_sales average_sales total_quantity average_quantity transaction_count region South 5130 1282.500000 161 40.250000 4 North 5050 1262.500000 163 40.750000 4 East 5000 1250.000000 160 40.000000 4 West 4150 1383.333333 128 42.666667 3 --- Outlier Detection (IQR Method) --- Không phát hiện thấy giá trị ngoại lệ nào đối với 'doanh số' hoặc 'số lượng' bằng phương pháp IQR. ### Load dữ liệu và kiểm tra chất lượng Dữ liệu CSV đã được load thành công vào DataFrame của pandas. Tập dữ liệu chứa 15 mục và 5 cột: date, product, sales, region và quantity. Tất cả các cột đều có kiểu dữ liệu chính xác, với sales và quantity là số nguyên, còn date, product, region là kiểu dữ liệu chuỗi ký tự. Không có giá trị thiếu trong bất kỳ cột nào, cho thấy chất lượng dữ liệu tốt. ### Những hiểu biết chính 1. **Sản phẩm C dẫn đầu về doanh số và số lượng bán ra trên mỗi giao dịch:** Sản phẩm C có tổng doanh số cao nhất (7950) và doanh số trung bình trên mỗi giao dịch cao nhất (1987,50), cũng như số lượng bán ra trung bình trên mỗi giao dịch cao nhất (49,50). Điều này cho thấy đây là sản phẩm có giá trị nhất. 2. **Sản phẩm B là sản phẩm có hiệu suất thấp nhất:** Sản phẩm B luôn có tổng doanh số thấp nhất (4200), doanh số trung bình thấp nhất (840,00) và số lượng bán trung bình thấp nhất (29,20) trong số tất cả các sản phẩm, cho thấy đây là sản phẩm có hiệu suất kém nhất. 3. **Sản phẩm A có số lượng giao dịch nhiều nhất:** Sản phẩm A ghi nhận số lượng giao dịch cao nhất (6), đóng góp đáng kể vào hoạt động chung, mặc dù doanh số và số lượng trung bình của nó ở mức vừa phải. 4. **Phân bổ doanh số khu vực cân bằng:** Tổng doanh số được phân bổ khá đều giữa các khu vực, với các khu vực phía Nam, phía Bắc và phía Đông cho thấy tổng doanh số tương tự nhau (khoảng 5000-5130). Khu vực phía Tây có tổng doanh số thấp hơn một chút nhưng cũng có ít giao dịch hơn. 5. **Khu vực phía Tây cho thấy doanh số trung bình trên mỗi giao dịch cao nhất:** Mặc dù có ít giao dịch nhất (3), khu vực phía Tây lại tự hào có doanh số trung bình trên mỗi giao dịch cao nhất (1383,33) và số lượng trung bình trên mỗi giao dịch cao nhất (42,67), cho thấy các giao dịch có giá trị cao. 6. **Không phát hiện thấy giá trị ngoại lệ:** Phân tích bằng phương pháp IQR không tìm thấy bất kỳ giá trị ngoại lệ đáng kể nào trong 'doanh số' hoặc 'số lượng', cho thấy mô hình bán hàng tương đối nhất quán trong giai đoạn quan sát. ### Các đề xuất khả thi 1. **Tận dụng thành công của Sản phẩm C:** Đầu tư nhiều hơn vào tiếp thị và sản xuất cho Sản phẩm C, tìm kiếm cơ hội mở rộng phạm vi thị trường hoặc giới thiệu các biến thể, dựa trên giá trị bán hàng cao và số lượng mỗi giao dịch lớn. 2. **Cải thiện hiệu suất của Sản phẩm B:** Điều tra các lý do đằng sau hiệu suất thấp hơn của Sản phẩm B. Điều này có thể bao gồm điều chỉnh giá, các chiến dịch tiếp thị, cải tiến sản phẩm hoặc kết hợp nó với các sản phẩm có hiệu suất cao hơn. 3. **Nhắm mục tiêu vào các khu vực có giá trị cao:** Phân tích các yếu tố góp phần vào giá trị giao dịch trung bình cao hơn ở khu vực phía Tây. Nhân rộng các chiến lược thành công từ khu vực phía Tây, nếu có, ở các khu vực khác để tăng doanh số bán hàng trung bình. 4. **Tối ưu hóa chiến lược khu vực dựa trên số lượng giao dịch:** Mặc dù tổng doanh số bán hàng tương tự nhau, nhưng số lượng giao dịch lại khác nhau. Đối với các khu vực có số lượng giao dịch cao (như Bắc, Nam, Đông), hãy tập trung vào việc tăng giá trị giao dịch trung bình. Đối với các khu vực có ít giao dịch hơn nhưng giá trị cao hơn (như Tây), hãy tập trung vào việc tăng tần suất giao dịch mà không làm giảm giá trị. 5. **Theo dõi hiệu suất ổn định của Sản phẩm A:** Sản phẩm A có số lượng giao dịch cao nhất. Đảm bảo nguồn cung ổn định và sự hài lòng của khách hàng để duy trì đóng góp ổn định của sản phẩm này vào doanh thu tổng thể. Cân nhắc bán chéo Sản phẩm C cho khách hàng của Sản phẩm A. 6. **Phân tích khu vực chuyên sâu hơn:** Mặc dù tổng doanh thu tương tự nhau, nhưng vẫn tồn tại những khác biệt nhỏ. Phân tích sâu hơn về nhân khẩu học khách hàng cụ thể, xu hướng thị trường địa phương hoặc bối cảnh cạnh tranh ở mỗi khu vực có thể giúp phát hiện ra các cơ hội hoặc thách thức tăng trưởng cục bộ.
The agent has successfully analyzed your data. Here's what happened:
- The tool has loaded your configuration and understands its role as a data analyst.
- Gemini read the CSV content and automatically planned the analysis steps.
- The tool used Python code to calculate statistics, group data by product and region, and detect outliers.
- Gemini executed the code in a safe sandbox environment and interpreted the results.
- The tool has synthesized the findings into clear insights and business recommendations.
Try it with your own data. Replace sales.csv with any CSV file containing numerical data, and the tool will adjust its analysis to suit your data structure. You can also modify agent_config.json to change the tool's target and get different types of insights.
Discover more
Gemini Gemini 3 AI agentShare by
Lesley MontoyaYou should read it
- How to change the browser User Agent without extenstion
- Overview of Agent Skills
- Applications of AI Agent in business fields
- How to Get a Talent Agent
- How to Easily Identify Songs Using Gemini on Android
- The Quiet Details That Make a Sports Betting Platform Feel Reliable
- Instructions on creating toy set images with ChatGPT AI
- How are AI agents changing the journalism industry?
- 6 tips to help you get the most out of Gemini Deep Research
- 5 ways Gemini helps students learn smarter.
- Instructions for getting started with Gemini Embedding 2