Gemma Cookbook

repository·main·Indexed 26 days ago

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

A collection of guides, tutorials, and experimental notebooks for building with Google's Gemma family of open models. Includes implementations for running concurrent Gemma 4 instances via concurrent-llm-demo, securing agentic function calls using the Human Delegation Provenance (HDP) protocol, and deploying Gemma models (including Gemma 3 and fine-tuned versions) to Google Cloud Run using Ollama-based Docker images.

Tokens
43.8K
Snippets
126
Records
185
Agent score
83%

What's inside google-gemma-cookbook

  1. Overview of MedGemma models

    main

    MedGemma is a collection of open models built on Gemma 3 designed for medical text and image comprehension. It is intended to accelerate the development of healthcare-based AI applications.

    Available variants:

    • 4B multimodal version: Capable of processing both text and images.
    • 27B text-only version: Optimized for text-based medical tasks.
  2. Overview of Gemma model families and variants

    main

    Gemma is a family of lightweight, generative AI open models. The models are available on the Hugging Face Hub, Kaggle, Google Cloud Vertex AI Model Garden, and ai.nvidia.com.

    Core Gemma Models

    • Gemma: Core models for text generation tasks.
    • Gemma 2: Higher-performing/efficient models (2B, 9B, 27B).
    • Gemma 3: Multimodal (text/image) with longer context (1B, 4B, 12B, 27B).
    • Gemma 3n: Optimized for low-resource devices; handles text, image, video, and audio (E2B, E4B).
    • Gemma 4: Optimized for reasoning, agentic workflows, coding, and multimodal understanding (E2B, E4B, 12B, 26B A4B, 31B).

    Specialized Gemma Variants

    • CodeGemma: Fine-tuned for coding tasks.
    • DataGemma: Uses Data Commons to reduce hallucinations.
    • DiffusionGemma: Experimental text diffusion model for fast generation.
    • FunctionGemma: Fine-tuned for function calling.
    • MedGemma: Medical text and image comprehension (4B multimodal, 27B text-only).
    • PaliGemma / PaliGemma 2: Vision Language Models (VLM) for image analysis.
    • RecurrentGemma: Based on Griffin architecture for text generation.
    • ShieldGemma / ShieldGemma 2: Safety evaluation for text prompts/outputs and image safety classification.
    • T5Gemma: Encoder-decoder models for quality-inference efficiency.
    • TranslateGemma: Designed for translation across 55 languages.
    • TxGemma: Designed for therapeutic development efficiency.
    • VaultGemma: Trained with differential privacy to prevent data memorization.
  3. Overview of TxGemma capabilities

    main

    TxGemma is a set of efficient and agentic LLMs designed for therapeutics. The repository provides resources for three primary workflows:

    1. Inference and serving: Loading, running, and deploying TxGemma models.
    2. Fine-tuning: Adapting TxGemma models for specific tasks and domains.
    3. Agentic: Integrating TxGemma models into agentic workflows.

    For more information, visit the HAI-DEF developer site.

  4. Use TranslateGemma for multi-language translation

    main

    TranslateGemma is a family of lightweight translation models based on the Gemma 3 family. They are designed to handle translation across 55 languages and are optimized for deployment in resource-constrained environments like laptops or desktops.

    Inference can be performed via:

    • Hugging Face: Using the transformers library.
    • Transformers.js: For browser-based or JavaScript environments.
  5. Use T5Gemma encoder-decoder models

    main

    T5Gemma adapts pretrained decoder-only Gemma models into an encoder-decoder architecture. It is available in various scales (Small, Base, Large, XL, and ML) and supports both Hugging Face (PyTorch) and Flax (Kauldron) frameworks.

    Common tasks include:

    • Sampling: Basic text generation.
    • Fine-tuning: Machine translation (e.g., English to French) using datasets like MTNT.
  6. Run concurrent Gemma 4 instances with concurrent-llm-demo

    main
    The concurrent-llm-demo allows you to run N concurrent Gemma 4 instances on a local llama-server and visualize them working in real time using a grid of macOS Terminal windows. It supports various scenarios like SVG generation, text translation, code generation, and ASCII art.
  7. Explore the Gemma Cookbook repository structure

    main

    The Gemma Cookbook is organized into several directories to help you find specific types of content:

    • Tutorials: Tested notebooks for Gemma models and variants.
    • Apps: Full-stack demos and complex end-to-end use cases.
    • Experiments: Research-focused notebooks (e.g., TxGemma, MedGemma).
    • Responsible: Notebooks focused on responsible AI development.
    • Docs: Core documentation, technical guides, and capability overviews.
    • Archive: Historical examples and older notebooks.
  8. Use VaultGemma for privacy-focused fine-tuning and inference

    main

    VaultGemma is a privacy-focused variant of Gemma designed for secure fine-tuning using differential privacy (via Opacus) and LoRA (Low-Rank Adaptation). It supports 4-bit quantization using BitsAndBytes to reduce memory footprint.

    Key features include:

    • 4-bit Quantization: Memory-efficient training.
    • LoRA Fine-tuning: Parameter-efficient adaptation with <2% trainable parameters.
    • Differential Privacy: Configurable ε and δ budgets (default ε=3.0, δ=1e-5).
    • Medical Q&A: Optimized for healthcare applications using medical datasets.
  9. Add a new scenario to concurrent-llm-demo

    main

    To create a custom scenario, edit demo/scenarios.py and define the following components:

    1. make_agents(n): A function returning a list of agent dictionaries containing name, emoji, color, and direct_instruction.
    2. plan: A dictionary with system and user prompt templates using placeholders like {n_agents}, {topic}, and {agent_list}.
    3. system_prompt: The system instruction for the model.
    4. template: A function to build HTML from the results dictionary.
    5. Register the scenario in the SCENARIOS dictionary.

    Example implementation:

    def make_my_agents(n: int = 10) -> list[dict]:
        return [
            {
                "name": f"Agent {i+1}",
                "emoji": "🎯",
                "color": _COLORS[i % len(_COLORS)],
                "direct_instruction": "Process {topic} in style X",
            }
            for i in range(n)
        ]
    
    MY_PLAN = {
        "system": 'Output a JSON array with {n_agents} objects, each with "name" and "instruction".',
        "user": 'Topic: "{topic}". Agents: {agent_list}.',
    }
    
    MY_SYSTEM = "You are a ... Output ONLY ..."
    
    def my_template(topic, results, agents, tasks=None):
        # Build HTML from results dict
        ...
    
    SCENARIOS["my_scenario"] = {
        "make_agents": make_my_agents,
        "plan": MY_PLAN,
        "template": my_template,
        "system_prompt": MY_SYSTEM,
        "default_n": 10,
    }

    Run your new scenario using:

    bash run.sh --scenario my_scenario --topic "My Topic"
  10. Explore Gemma Multimodal Inference Capabilities

    main

    Use the following notebooks to explore Gemma 4's capabilities across different modalities:

    • Text: Basic text generation and prompting, or advanced tool use and function calling.
    • Vision: Image captioning/understanding and video analysis.
    • Audio: Audio processing and understanding.
    • Reasoning: Explore the 'Thinking' capabilities of the model.