tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > Google Gemini API > Gemini Code Generation

Gemini Code Generation

Author: Venkata Sudhakar

Gemini excels at code generation - writing functions, explaining code, converting between languages, and generating SQL queries from natural language. ShopMax India uses Gemini Code Generation to help its data team write BigQuery SQL from plain English business questions, reducing the time to insight from hours to seconds.

The key to reliable code generation is a precise system prompt that specifies the language, coding style, and output format. For SQL generation, including the database schema in the prompt dramatically improves accuracy. Always validate generated code before executing it in production.

The below example shows how to generate a Python data processing function from a natural language description.


It gives the following output,

def sales_by_city(orders: list[dict]) -> dict:
    # Aggregate total sales per city
    city_totals: dict[str, float] = {}
    for order in orders:
        city = order.get("city", "Unknown")
        amount = order.get("amount", 0)
        city_totals[city] = city_totals.get(city, 0) + amount
    # Return sorted by total descending
    return dict(sorted(city_totals.items(), key=lambda x: x[1], reverse=True))

The below example shows natural language to BigQuery SQL generation using the ShopMax orders schema.


It gives the following output,

Generated SQL:
SELECT
    city,
    SUM(amount) AS total_sales
FROM
    shopmax_orders
WHERE
    order_date BETWEEN DATE(2024, 3, 1) AND DATE(2024, 3, 31)
    AND status = 'DELIVERED'
GROUP BY
    city
ORDER BY
    total_sales DESC
LIMIT 5

ShopMax India data analysts use a Gemini-powered SQL assistant daily. Business stakeholders type questions like show me electronics returns above Rs 10,000 in Bangalore last quarter and receive ready-to-run BigQuery SQL. The assistant cut report turnaround from 2 days (waiting for analyst capacity) to under 5 minutes, enabling faster merchandising and pricing decisions.


 
  


  
bl  br