Gemini API Cookbook

repository·main·Indexed 30 days ago

https://github.com/google-gemini/cookbook

Practical guides and code examples for Google's Gemini models. Includes quickstarts for JavaScript/TypeScript, Python, and Node.js covering multimodal prompting, File API, Live API (WebSockets), reasoning models, media generation (Omni Flash, Veo, Lyria), grounding, and agentic workflows.

Tokens
107.3K
Snippets
288
Records
401
Agent score
97%

What's inside google-gemini-cookbook

  1. Explore Gemini Multimodal Live API via WebSockets

    main

    The Gemini Multimodal Live API is served over secure WebSockets. While Python developers are encouraged to use the official Google GenAI SDK to access these capabilities, the cookbook provides low-level WebSocket examples to help you understand the underlying protocol.

    Available resources include:

    • Local Python Script: A script that uses websockets to support streaming audio in and audio + video out from your local machine.
    • Bash Example: A shell script using websocat to interact with the Live API.
    • Google Colab Notebooks: Interactive notebooks for exploring the Live API and implementing tool use via WebSockets.
  2. Explore Gemini API features and quickstarts

    main

    The Gemini API Cookbook provides several specialized quickstart guides for different models and capabilities:

    Multimodal & Reasoning

    • Gemini 3.6 Flash: Optimized for complex agentic and multimodal tasks. Use the Get Started or Thinking guides.
    • Gemini 3.5 Flash-Lite: High-throughput, low-cost model for fast execution.
    • Gemini Robotics-ER 1.5: Specialized for spatial understanding and reasoning in robotics.
    • Live API: Low-latency voice/video streaming via the multimodal Live API.
    • Live Translation API: Real-time audio stream translation via the Live Translation guide.

    Media Generation & Editing

    Agents & Grounding

    • Agents API: Create custom agents (e.g., the Antigravity agent) in a controlled environment.
    • File Search: Ground generations in your own data using a hosted RAG system via the File Search quickstart.
    • Grounding: Use tools like Google Search, YouTube, URLs, or Google Maps to ground answers in factual data.

    API Operations

    • Webhooks: Receive real-time notifications for async operations like batch jobs or video generation via the Webhooks quickstart.
    • Inference Tiers: Balance speed, cost, and reliability using Priority and Flex tiers.
    • Batch API: Send large volumes of non-time-sensitive requests at a significant discount (up to 90%) via the Batch API guide.
  3. Explore Multimodal Live API and Tool Use in Google Colab

    main

    For an interactive environment without local setup, use the following Google Colab notebooks:

    • Live API starter (Get_started_LiveAPI.ipynb): Provides an overview of the Multimodal Live API using WebSockets.
    • Live API tool use (Get_started_LiveAPI_tools.ipynb): Demonstrates how to implement tool use within the Live API via WebSockets.
  4. Install official Gemini API SDKs

    main

    While the Gemini API is a REST API that can be called via curl or Postman, Google provides official SDKs for several languages:

    • Python: googleapis/python-genai
    • Go: googleapis/go-genai
    • Node.js: googleapis/js-genai
    • Java: googleapis/java-genai
    • C#: googleapis/dotnet-genai
  5. Call the Gemini API with cURL

    main
    You can interact with the Gemini API directly from your terminal using curl commands. This is useful for testing endpoints, verifying authentication, or integrating with environments where installing a full SDK is not feasible. Ensure you have your GOOGLE_API_KEY available to authenticate your requests.
  6. Use Gemini 3.6 Flash and Thinking models

    main
    Gemini 3.6 Flash offers stronger performance on complex agentic and multimodal tasks with reduced token usage. For tasks requiring deeper reasoning, use the 'Thinking' models which are capable of more extensive chains of thought to solve complex problems.
  7. Implement Grounding and File Search

    main

    Enhance model accuracy using these techniques:

    • Grounding: Use external sources like Google Search, Google Maps, YouTube, or URL context to ground answers.
    • File Search: Use the hosted RAG (Retrieval-Augmented Generation) capability of the File Search tool to upload and search your own data.
  8. Set up the Gemini File API Python sample

    main

    To run the Python sample client for the File API, follow these steps to prepare your environment and run the code:

    1. Prepare a virtual environment:
      python3 -m venv venv
      source venv/bin/activate
    2. Configure your API key: Create a .env file and add your GOOGLE_API_KEY.
      touch .env
      echo "GOOGLE_API_KEY='YOUR_API_KEY'" >> .env
    3. Install dependencies:
      pip3 install -U -r requirements.txt
    4. Run the sample:
      python3 sample.py
    python3 -m venv venv
    source venv/bin/activate
    touch .env
    echo "GOOGLE_API_KEY='YOUR_API_KEY'" >> .env
    pip3 install -U -r requirements.txt
    python3 sample.py
  9. Set up the Gemini File API Node.js sample

    main

    To run the Node.js sample client for the File API, ensure npm is installed and follow these steps:

    1. Configure your API key: Create a .env file and add your GOOGLE_API_KEY.
      touch .env
      echo "GOOGLE_API_KEY='YOUR_API_KEY'" >> .env
    2. Install dependencies:
      npm install
    3. Run the sample:
      npm start
    touch .env
    echo "GOOGLE_API_KEY='YOUR_API_KEY'" >> .env
    npm install
    npm start
  10. Explore Gemini API Quick Starts

    main

    The cookbook provides several Quick Start tutorials (often available as Google Colab notebooks) to help you learn individual Gemini API features. Key tutorials include:

    • Live API: Comprehensive overview of Live API capabilities.
    • Live Translation: Real-time audio stream translation.
    • Omni Flash: Conversational video generation and editing using the Interactions API.
    • Veo: Video generation capabilities.
    • Imagen & Native Image Generation: Image generation capabilities.
    • Grounding: Using Google Search for grounded responses.
    • Code Execution: Generating and running Python code to solve complex tasks and output graphs.