### Generate Developer Getting Started Guide Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/use-cases/intro_multimodal_use_cases.ipynb Queries the model to create a getting started guide for new developers based on the cached codebase content. This helps in onboarding. ```python question = """ Provide a getting started guide to onboard new developers to the codebase. """ response = client.models.generate_content( model=MODEL_ID, contents=question, config=GenerateContentConfig( cached_content=cached_content.name, ), ) display(Markdown(response.text)) ``` -------------------------------- ### Backend Setup: Install Dependencies, Authenticate, and Start Server Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/multimodal-live-api/native-audio-websocket-demo-apps/customer-support-demo-app/README.md This Python backend setup involves installing necessary dependencies, authenticating with Google Cloud using application-default login, and starting the WebSocket proxy server. ```bash pip install -r requirements.txt gcloud auth application-default login python server.py ``` -------------------------------- ### Setup and Run Frontend Application Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/quickbot/README.md Commands to install Node.js dependencies and start the frontend development server. ```bash cd frontend npm install npm start ``` -------------------------------- ### Install and Run Gemini Live API Client Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/multimodal-live-api/native-audio-websocket-demo-apps/plain-js-demo-app/README.md Quick start commands to install dependencies, authenticate with Google Cloud, start the server, and access the UI in the browser. ```bash # Install dependencies pip3 install -r requirements.txt # Authenticate with Google Cloud gcloud auth application-default login # Start server (serves UI + WebSocket proxy) python3 server.py # Open browser open http://localhost:8000 ``` -------------------------------- ### Generate Developer Getting Started Guide (Streaming) Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/use-cases/code/analyze_codebase.ipynb Generates a getting started guide for developers using Gemini's streaming API. This method is suitable for longer responses where content can be displayed as it's generated. ```python question = """ Provide a getting started guide to onboard new developers to the codebase. """ # Generate text using streaming method responses = client.models.generate_content_stream( model=MODEL_ID, contents=question, config=GenerateContentConfig( cached_content=cached_content.name, ), ) for response in responses: print(response.text, end="") ``` -------------------------------- ### Setup and Installation for Always-On Memory Agent Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/agents/always-on-memory-agent/README.md Instructions for cloning the repository, installing dependencies, and configuring the required Google Gemini API key for the agent. ```bash git clone https://github.com/Shubhamsaboo/always-on-memory-agent.git cd always-on-memory-agent pip install -r requirements.txt ``` ```bash export GOOGLE_API_KEY="your-gemini-api-key" ``` -------------------------------- ### Install Node Modules and Start React Development Server (Bash) Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/multimodal-live-api/native-audio-websocket-demo-apps/gaming-assistant-demo-app/README.md Installs the Node.js dependencies for the React frontend and starts the development server. This allows you to view and interact with the gaming assistant application locally. ```bash npm install npm run dev ``` -------------------------------- ### Setup Environment and Dependencies Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/image-bash-jam/README.md Commands to clone the repository, install optional dependencies like lolcat, and prepare the environment for Gemini API interactions. ```bash cd git clone https://github.com/GoogleCloudPlatform/generative-ai cd generative-ai/gemini/sample-apps/image-bash-jam/ gem install lolcat ``` -------------------------------- ### Frontend Setup: Install Dependencies Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/quickbot/conversational-app-multi-playbook/README.md Installs the necessary Node.js dependencies for the frontend application using npm. ```bash cd frontend npm install ``` -------------------------------- ### Frontend Setup: Run Frontend Application Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/quickbot/conversational-app-multi-playbook/README.md Starts the frontend development server. The application is typically accessible at http://localhost:4200. ```bash cd frontend npm start # Or, for many Angular projects: # ng serve ``` -------------------------------- ### Project Setup and Activation Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/quickbot/conversational-app-single-playbook/backend/README.md Commands to create and activate a Python virtual environment, and install project dependencies from requirements.txt. ```bash python3 -m venv .venv source .venv/bin/activate pip3 install -r requirements.txt ``` -------------------------------- ### Setup Backend Environment and Dependencies Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/quickbot/README.md Commands to navigate to the backend directory, create a Python virtual environment, and install required dependencies from requirements.txt. ```bash cd backend python3 -m venv .venv source .venv/bin/activate pip3 install -r requirements.txt ``` -------------------------------- ### Frontend: Install Dependencies and Serve Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/quickbot/conversational-app-single-playbook/README.md Installs frontend project dependencies using npm and starts the development server. This is typically for TypeScript/Angular projects. ```bash cd frontend npm install npm start # Or, for many Angular projects: # ng serve ``` -------------------------------- ### Backend Setup: Install Python Dependencies Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/quickbot/conversational-app-multi-playbook/README.md Installs Python dependencies for the backend service using pip. It includes creating and activating a virtual environment. ```bash cd backend # Check if you are already in an environment pip -V # If not, create and activate (for Linux/macOS) python3 -m venv .venv source .venv/bin/activate # Install requirements pip3 install -r requirements.txt ``` -------------------------------- ### Run Backend Application Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/quickbot/README.md Commands to execute the backend setup script and start the Uvicorn server for the API. ```bash python3 setup.py uvicorn main:app --reload --port 8080 ``` -------------------------------- ### Install Dependencies and Setup Environment Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/use-cases/document-processing/summarization_large_documents_langchain.ipynb Installs necessary system libraries and Python packages for document processing and LangChain integration. It also includes a kernel restart command to ensure environment changes take effect. ```bash !sudo apt -y -qq install tesseract-ocr !sudo apt -y -qq install libtesseract-dev !sudo apt-get -y -qq install poppler-utils !sudo apt-get -y -qq install python-dev libxml2-dev libxslt1-dev antiword unrtf poppler-utils pstotext tesseract-ocr flac ffmpeg lame libmad0 libsox-fmt-mp3 sox libjpeg-dev swig %pip install --upgrade --quiet pytesseract pypdf PyPDF2 langchain langchain-core langchain-google-vertexai google-cloud-aiplatform langchain-community ``` ```python import IPython app = IPython.Application.instance() app.kernel.do_shutdown(True) ``` -------------------------------- ### Install Python Dependencies and Run FastAPI Server Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/multimodal-live-api/native-audio-websocket-demo-apps/plain-js-python-sdk-demo-app/README.md Installs necessary Python packages from requirements.txt, authenticates with Google Cloud using application default credentials, and starts the FastAPI development server. ```bash pip install -r requirements.txt gcloud auth application-default login python main.py ``` -------------------------------- ### Backend Setup: Run Backend Application Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/quickbot/conversational-app-multi-playbook/README.md Starts the backend application using Uvicorn, a high-performance ASGI web server. The application will reload automatically on code changes. ```bash # from the backend/ directory uvicorn main:app --reload --port 8080 ``` -------------------------------- ### Install Dependencies and Setup Auto RAG Eval Source: https://github.com/googlecloudplatform/generative-ai/blob/main/search/auto-rag-eval/README.md This snippet outlines the initial steps to set up and run the Auto RAG Eval tool. It includes installing Python dependencies, downloading necessary files from Google Cloud Storage, configuring environment variables, and authenticating with Google Cloud. ```bash pip install -r requirements.txt gcloud storage cp gs://github-repo/search/auto-rag-eval/qa_profiles.json . # Edit .env with your values: # - PROJECT_ID=your-gcp-project-id # - LOCATION=us-central1 # - DATA_STORE_ID=your-vertex-ai-search-datastore-id gcloud auth application-default login ``` -------------------------------- ### Function Calling Setup Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/multimodal-live-api/intro_multimodal_live_api_genai_sdk.ipynb Demonstrates setting up a live API session for function calling. Tool definitions must be declared at the start of the session. Currently, only one tool is supported per API call. ```python def get_current_weather(location: str) -> str: """Example method. Returns the current weather. Args: location: The city and state, e.g. San Francisco, CA """ weather_map: dict[str, str] = { "Boston, MA": "snowing", "San Francisco, CA": "foggy", "Seattle, WA": "raining", "Austin, TX": "hot", "Chicago, IL": "windy", } return weather_map.get(location, "unknown") config = LiveConnectConfig( response_modalities=["audio"], tools=[get_current_weather], ) async with client.aio.live.connect( model=MODEL_ID, config=config, ) as session: text_input = "Get the current weather in Boston." display(Markdown(f"**Input:** {text_input}")) await session.send_client_content( turns=Content(role="user", parts=[Part(text=text_input)]) ) async for message in session.receive(): if message.tool_call: for function_call in message.tool_call.function_calls: display(Markdown(f"**FunctionCall >** {function_call!s}")) ``` -------------------------------- ### Environment Setup for A2A SDK Source: https://github.com/googlecloudplatform/generative-ai/blob/main/agents/gemini_data_analytics/a2a_sdk_sample.ipynb Installs core dependencies, authenticates the user, and sets up environment variables for project ID, location, and agent ID. Ensure you replace '[your-project-id]' and 'your-agent-id' with your actual values. ```python # Copyright 2026 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ``` ```python # @title 1. Environment Setup # Install core dependencies %pip install google-auth requests httpx nest_asyncio import os import time import uuid from google.auth import default from google.auth.transport.requests import Request from google.colab import auth # Authenticate the user auth.authenticate_user() # Get credentials and project ID creds, _ = default() creds.refresh(Request()) access_token = creds.token ENDPOINT = "https://geminidataanalytics.googleapis.com" PROJECT_ID = "[your-project-id]" # @param {type:"string"} LOCATION = "global" # @param {type:"string"} # AGENT_ID can be found from the Cloud URL, e.g. # https://console.cloud.google.com/bigquery/agents_hub/?project= AGENT_ID = "your-agent-id" # @param {type:"string"} if not PROJECT_ID or PROJECT_ID == "[your-project-id]" PROJECT_ID = str(os.environ.get("GOOGLE_CLOUD_PROJECT")) if not LOCATION: LOCATION = os.environ.get("GOOGLE_CLOUD_REGION") TENANT = f"projects/{PROJECT_ID}/locations/{LOCATION}/agents/{AGENT_ID}" print(f"Target Tenant: {TENANT}") ``` -------------------------------- ### Implement Few-Shot Prompting for Specific Output Formats Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/prompts/examples/question_answering.ipynb Shows how to guide the model to provide short, specific answers by providing examples within the prompt, ensuring the model follows the established pattern. ```python prompt = """Q: Who is the current President of France?\n A: Emmanuel Macron \n\n Q: Who invented the telephone? \n A: Alexander Graham Bell \n\n Q: Who wrote the novel \"1984\"?\n A: George Orwell\n\n Q: Who discovered penicillin?\n A:\n """ generation_config = GenerationConfig(temperature=0.1, max_output_tokens=20) response = generation_model.generate_content( contents=prompt, generation_config=generation_config ).text print(response) ``` -------------------------------- ### Create Quickstart Tutorial for a Component Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/use-cases/code/analyze_codebase.ipynb Use the client to stream responses for generating an end-to-end quickstart tutorial for a specific component, including configuration and key capabilities. ```python question = """ Please write an end-to-end quickstart tutorial that introduces AlloyDB, shows how to configure it with the CartService, and highlights key capabilities of AlloyDB in context of the Online Boutique application. """ responses = client.models.generate_content_stream( model=MODEL_ID, contents=question, config=GenerateContentConfig( cached_content=cached_content.name, ), ) for response in responses: print(response.text, end="") ``` -------------------------------- ### Set up tutorial folder Source: https://github.com/googlecloudplatform/generative-ai/blob/main/embeddings/intro_embeddings_tuning.ipynb Creates a directory structure for tutorial content, including data and metadata. Ensures the directory exists before creating it. ```python from pathlib import Path as path root_path = path.cwd() tutorial_path = root_path / "tutorial" data_path = tutorial_path / "data" data_path.mkdir(parents=True, exist_ok=True) ``` -------------------------------- ### Define Custom Memory Bank Topics and Examples Source: https://github.com/googlecloudplatform/generative-ai/blob/main/agents/agent_engine/memory_bank/get_started_with_memory_bank_on_adk.ipynb Configure custom memory topics with specific labels and descriptions. Provide examples of conversations and generated memories to guide the model. This setup is useful for tailoring memory bank behavior to specific domains or user needs. ```python from vertexai.types import MemoryBankCustomizationConfig as CustomizationConfig from vertexai.types import ( MemoryBankCustomizationConfigGenerateMemoriesExample as GenerateMemoriesExample, ) from vertexai.types import ( MemoryBankCustomizationConfigGenerateMemoriesExampleConversationSource as ConversationSource, ) from vertexai.types import ( MemoryBankCustomizationConfigGenerateMemoriesExampleConversationSourceEvent as ConversationSourceEvent, ) from vertexai.types import ( MemoryBankCustomizationConfigGenerateMemoriesExampleGeneratedMemory as ExampleGeneratedMemory, ) from vertexai.types import MemoryBankCustomizationConfigMemoryTopic as MemoryTopic from vertexai.types import ( MemoryBankCustomizationConfigMemoryTopicCustomMemoryTopic as CustomMemoryTopic, ) memory_topic = MemoryTopic( custom_memory_topic=CustomMemoryTopic( label="business_feedback", description='''Specific user feedback about their experience at the coffee shop. This includes opinions on drinks, food, pastries, ambiance, staff friendliness, service speed, cleanliness, and any suggestions for improvement.''' ) ) example = GenerateMemoriesExample( conversation_source=ConversationSource( events=[ ConversationSourceEvent( content=Content( role="model", parts=[ Part( text="Welcome back to The Daily Grind! We'd love to hear your feedback on your visit." ) ], ) ), ConversationSourceEvent( content=Content( role="user", parts=[ Part( text="Hey. The drip coffee was a bit lukewarm today, which was a bummer. Also, the music was way too loud, I could barely hear my friend." ) ], ) ), ] ), generated_memories=[ ExampleGeneratedMemory( fact="The user reported that the drip coffee was lukewarm." ), ExampleGeneratedMemory( fact="The user felt the music in the shop was too loud." ), ], ) noop_example = GenerateMemoriesExample( conversation_source=ConversationSource( events=[ ConversationSourceEvent( content=Content( role="model", parts=[ Part( text="Welcome back to The Daily Grind! We'd love to hear your feedback on your visit." ) ], ) ), ConversationSourceEvent( content=Content( role="user", parts=[Part(text="Thanks for the coffee!")] ) ), ] ), generated_memories=[], ) client.agent_engines.update( name=agent_engine.api_resource.name, config={ "context_spec": { "memory_bank_config": { "customization_configs": [ CustomizationConfig( memory_topics=[memory_topic], generate_memories_examples=[example, noop_example], ) ], } } }, ) ``` -------------------------------- ### Install Google Gen AI SDK Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/function-calling/parallel_function_calling.ipynb Install the necessary Google Gen AI SDK and the Wikipedia library for the examples. This command upgrades the package if it's already installed. ```python %pip install --upgrade --quiet google-genai wikipedia ``` -------------------------------- ### One-Shot Prompting Example Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/prompts/intro_prompt_design.ipynb This example demonstrates one-shot prompting, providing a single example to guide the LLM's response format and style. This helps in achieving more predictable outputs. ```python prompt = """Decide whether a Tweet's sentiment is positive, neutral, or negative. Tweet: I loved the new YouTube video you made! Sentiment: positive Tweet: That was awful. Super boring 😠 Sentiment: """ response = client.models.generate_content(model=MODEL_ID, contents=prompt) display(Markdown(response.text)) ``` -------------------------------- ### Run Computer Use Demo with Custom Screen Resolution Source: https://github.com/googlecloudplatform/generative-ai/blob/main/partner-models/claude/computer-use-demo/README.md Demonstrates how to start the Docker container with specific environment variables for screen resolution (WIDTH and HEIGHT) and required cloud credentials. ```bash docker run \ -e API_PROVIDER=vertex \ -e CLOUD_ML_REGION=$VERTEX_REGION \ -e ANTHROPIC_VERTEX_PROJECT_ID=$VERTEX_PROJECT_ID \ -v $HOME/.config/gcloud/application_default_credentials.json:/home/computeruse/.config/gcloud/application_default_credentials.json \ -p 5900:5900 \ -p 8501:8501 \ -p 6080:6080 \ -p 8080:8080 \ -e WIDTH=1920 \ -e HEIGHT=1080 \ -it computer-use-demo ``` -------------------------------- ### Initialize Project Livewire Locally Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/multimodal-live-api/project-livewire/README.md Commands to clone the repository, configure environment variables, and start the Python backend server and the static frontend server. ```bash git clone https://github.com/heiko-hotz/project-livewire.git cd project-livewire cd server cp .env.example .env pip install -r requirements.txt python server.py cd ../client python -m http.server 8000 ``` -------------------------------- ### Install Vertex AI SDK and BEIR Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/rag-engine/rag_engine_evaluation.ipynb Installs the necessary Python packages for interacting with Vertex AI and the BEIR benchmark dataset. This is a prerequisite for running the subsequent code examples. ```python %pip install --upgrade --user --quiet google-cloud-aiplatform beir ``` -------------------------------- ### Install Google Gen AI SDK for Python Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/getting-started/intro_gemini_2_5_image_gen.ipynb Install the Google Gen AI SDK for Python to use Gemini models. This is a prerequisite for running the code examples in this tutorial. ```python # Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ``` ```python %pip install --upgrade --quiet google-genai ``` -------------------------------- ### Install Dependencies and Run Backend Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/multimodal-live-api/websocket-demo-app/README.md Commands to install Python requirements and launch the WebSocket server. ```bash pip3 install -r backend/requirements.txt python3 backend/main.py ``` -------------------------------- ### Example: Create and Use a Glossary for Translation Source: https://github.com/googlecloudplatform/generative-ai/blob/main/translation/intro_translation.ipynb This example demonstrates creating a glossary for Google Cloud product names and then using it to translate 'Compute Engine' consistently. ```python glossary = create_glossary( input_uri="gs://github-repo/translation/GoogleCloudGlossary.tsv", glossary_id="google_cloud_english_to_spanish", ) print(glossary) ``` ```python response = translate_text("Compute Engine", glossary=glossary.name) ``` -------------------------------- ### Prepare Few-Shot Prompt Examples Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/tuning/sft_gemini_qa.ipynb Selects random examples from the test dataset to create a few-shot prompt. This prompt will be used to guide the model's responses by providing context, questions, and answers. ```python few_shot_examples = test_df.sample(3) # Get the indices of the sampled rows dropped_indices = few_shot_examples.index # Remove the sampled rows from the original DataFrame test_df = test_df.drop(dropped_indices) few_shot_prompt = "" for _, row in few_shot_examples.iterrows(): few_shot_prompt += ( f"Context: {row.context}\nQuestion: {row.question}\nAnswer: {row.answers}\n\n" ) print(few_shot_prompt) ``` -------------------------------- ### Ngrok Installation and Configuration Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/gemini-live-telephony-app/README.md Instructions for downloading, installing, and configuring ngrok to expose a local server to the internet. This involves downloading the binary, adding it to the PATH, and authenticating with an authtoken. ```bash unzip /path/to/ngrok-v3-stable-linux-amd64.zip sudo mv ngrok /usr/local/bin/ ngrok config add-authtoken $YOUR_AUTHTOKEN ngrok --version ``` -------------------------------- ### Initialize Local Development Environment Source: https://github.com/googlecloudplatform/generative-ai/blob/main/search/cloud-function/python/README.md Commands to authenticate with Google Cloud and execute a setup script to prepare the local environment for development. ```bash gcloud auth login bash setup_env.sh ``` -------------------------------- ### Complete Client-Level Retry Configuration Example Source: https://github.com/googlecloudplatform/generative-ai/blob/main/sdk/retries/configure_retries.ipynb Demonstrates the complete setup for initializing a genai.Client with a custom retry policy and making a generate content request. This example shows a retry policy with 10 attempts. ```python client = genai.Client( vertexai=True, project=PROJECT_ID, location=LOCATION, http_options=types.HttpOptions( retry_options=types.HttpRetryOptions( initial_delay=1.0, attempts=10, http_status_codes=[429, 500, 502, 503, 504], ), timeout=120 * 1000, ), ) # You can now use this client to make API calls with the custom retry policy. # For example: response = client.models.generate_content( model="gemini-3-flash-preview", contents="Tell me a story" ) display(Markdown(response.text)) ``` -------------------------------- ### Clone Repository and Set Up Environment Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/agents/genai-experience-concierge/langgraph-demo/backend/README.md Clone the repository and configure Google Application Default Credentials before running the server. ```bash git clone https://github.com/GoogleCloudPlatform/generative-ai.git cd generative-ai/gemini/agents/genai-experience-concierge gcloud auth login gcloud auth application-default login ``` -------------------------------- ### Backend Setup: Run Setup Script Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/quickbot/conversational-app-multi-playbook/README.md Executes a Python setup script for the backend, which may perform initial configurations for agents or playbooks. ```python # from the backend/ directory python3 setup.py ``` -------------------------------- ### Install and Setup Vertex AI SDK Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/evaluation/evaltask_approach/intro_to_gen_ai_evaluation_service_sdk.ipynb Installs the Vertex AI SDK with evaluation capabilities and restarts the runtime. It also includes authentication for Colab environments and initialization of the Vertex AI SDK with project details. ```APIDOC ## Install Vertex AI Python SDK for Gen AI Evaluation Service ```python %pip install --upgrade --user --quiet google-cloud-aiplatform[evaluation] ``` ## Restart runtime To use the newly installed packages in this Jupyter runtime, you must restart the runtime. You can do this by running the cell below, which restarts the current kernel. The restart might take a minute or longer. After it's restarted, continue to the next step. ```python import IPython app = IPython.Application.instance() app.kernel.do_shutdown(True) ```
⚠️ The kernel is going to restart. Wait until it's finished before continuing to the next step. ⚠️
## Authenticate your notebook environment (Colab only) ```python import sys if "google.colab" in sys.modules: from google.colab import auth auth.authenticate_user() ``` ## Set Google Cloud project information and initialize Vertex AI SDK ```python PROJECT_ID = "[your-project-id]" # @param {type:"string"} LOCATION = "us-central1" # @param {type:"string"} EXPERIMENT_NAME = "my-eval-task-experiment" # @param {type:"string"} if not PROJECT_ID or PROJECT_ID == "[your-project-id]": raise ValueError("Please set your PROJECT_ID") import vertexai vertexai.init(project=PROJECT_ID, location=LOCATION) ``` ```python import pandas as pd from vertexai.evaluation import EvalTask, PointwiseMetric, PointwiseMetricPromptTemplate from vertexai.preview.evaluation import notebook_utils ``` ``` -------------------------------- ### Running Setup Script Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/quickbot/conversational-app-single-playbook/backend/README.md Command to execute the Python setup script for the project. ```python python3 setup.py ``` -------------------------------- ### Download Example Audio Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/multimodal-live-api/intro_multimodal_live_api_genai_sdk.ipynb Downloads a sample audio file in PCM format for use with the API. Ensure you have `wget` installed. ```python # Download an example audio file URL = "https://storage.googleapis.com/cloud-samples-data/generative-ai/audio/hello_are_you_there.pcm" !wget -q $URL -O sample.pcm ``` -------------------------------- ### Docker Compose Build with Datastore Setup (Bash) Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/quickbot/document-search-using-agent-builder/README.md Builds the Docker images for the Quickbot application. The `--build-arg IS_FIRST_DEPLOYMENT="True"` option automatically creates a default Vertex AI Search datastore and engine with public data. Omitting this argument or setting it to another value requires manual configuration of datastore/engine via environment variables. ```bash # Build with automatic default datastore setup docker compose build --build-arg IS_FIRST_DEPLOYMENT="True" # Build without automatic setup (for custom or existing datastore) docker compose build ``` -------------------------------- ### Run Cloud Function Locally Source: https://github.com/googlecloudplatform/generative-ai/blob/main/search/cloud-function/python/README.md Commands to install dependencies and start the local development server using the Functions Framework. ```bash pip install -r requirements.txt pip install functions-framework functions-framework --target=vertex_ai_search ``` -------------------------------- ### Execute Local Agent Queries Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/agent-engine/langgraph_human_in_the_loop.ipynb Demonstrates how to set up the agent, perform a synchronous query, and stream state updates or values to monitor the agent's execution process. ```python agent.set_up() inputs = {"messages": [("user", "What is the exchange rate from US dollars to Swedish currency?")]} # Synchronous query response = agent.query(input=inputs, config={"configurable": {"thread_id": "synchronous-thread-id"}}) # Streaming values for state_values in agent.stream_query(input=inputs, stream_mode="values", config={"configurable": {"thread_id": "streaming-thread-values"}}): print(state_values) # Streaming updates for state_updates in agent.stream_query(input=inputs, stream_mode="updates", config={"configurable": {"thread_id": "streaming-thread-updates"}}): print(state_updates) ``` -------------------------------- ### Create and Execute Installation Script Source: https://github.com/googlecloudplatform/generative-ai/blob/main/agents/agent_engine/tutorial_mcp_on_agent_engine.ipynb This Python code snippet first creates a local directory for installation scripts and then writes the bash installation script content to a file. Finally, it makes the script executable and runs it. ```python !mkdir -p installation_scripts ``` ```python install_local_mcp_file = """#!/bin/bash # Exit immediately if a command exits with a non-zero status. set -e echo "Installing MCP Reddit Server" # Install uv (a fast Python package manager) apt-get update apt-get install -y curl echo "Installing uv..." curl -LsSf https://astral.sh/uv/install.sh | sh # Add uv to PATH for current session export PATH=\"$HOME/.local/bin:$PATH\" # Install the mcp-reddit tool using the command from its documentation echo "Installing mcp-reddit using uv..." uv pip install \"git+https://github.com/adhikasp/mcp-reddit.git\" --system echo "MCP Reddit Server installation complete.""" with open("installation_scripts/install_local_mcp.sh", "w") as f: f.write(install_local_mcp_file) f.close() ``` ```python !chmod +x installation_scripts/install_local_mcp.sh && ./installation_scripts/install_local_mcp.sh ``` -------------------------------- ### Example Invocation of Summarization Chain in Python Source: https://github.com/googlecloudplatform/generative-ai/blob/main/search/vertexai-search-options/vertex_ai_search_website_summary.ipynb This Python code snippet provides an example of how to use the `invoke` function to get a summarized answer to a question about Vertex AI benefits. It defines a search query and then calls the `invoke` function with that query. ```python search_query = "What are the benefits of Vertex AI?" invoke(search_query) ``` -------------------------------- ### Few-Shot Prompting for Pattern Matching Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/prompts/examples/question_answering.ipynb Uses a series of context-question-answer examples to guide the model on how to format its output and reason through provided information. ```python prompt = """ Context: The term "artificial intelligence" was first coined by John McCarthy in 1956. Since then, AI has developed into a vast field with numerous applications, ranging from self-driving cars to virtual assistants like Siri and Alexa. Question: What is artificial intelligence? Answer: Artificial intelligence refers to the simulation of human intelligence in machines that are programmed to think and learn like humans. --- Context: The Wright brothers, Orville and Wilbur, were two American aviation pioneers who are credited with inventing and building the world's first successful airplane and making the first controlled, powered and sustained heavier-than-air human flight, on December 17, 1903. Question: Who were the Wright brothers? Answer: The Wright brothers were American aviation pioneers who invented and built the world's first successful airplane and made the first controlled, powered and sustained heavier-than-air human flight, on December 17, 1903. --- Context: The Mona Lisa is a 16th-century portrait painted by Leonardo da Vinci during the Italian Renaissance. It is one of the most famous paintings in the world, known for the enigmatic smile of the woman depicted in the painting. Question: Who painted the Mona Lisa? Answer: """ response = generation_model.generate_content(contents=prompt).text print(response) ``` -------------------------------- ### Initialize Vertex AI SDK and Set Up Project (Python) Source: https://github.com/googlecloudplatform/generative-ai/blob/main/open-models/serving/get_started_with_vllm_lora_serving_on_vertex_ai.ipynb Initializes the Vertex AI SDK with project and location information, and sets up a Google Cloud Storage bucket for model artifacts. It also configures Hugging Face Hub for faster downloads. ```python # Use the environment variable if the user doesn't provide Project ID. import os import vertexai # fmt: off PROJECT_ID = "[your-project-id]" # @param {type: "string", placeholder: "[your-project-id]", isTemplate: true} # fmt: on if not PROJECT_ID or PROJECT_ID == "[your-project-id]": PROJECT_ID = str(os.environ.get("GOOGLE_CLOUD_PROJECT")) LOCATION = os.environ.get("GOOGLE_CLOUD_REGION", "us-central1") # Create GCS bucket BUCKET_NAME = f"{PROJECT_ID}-vllm-peft-serving" BUCKET_URI = f"gs://{BUCKET_NAME}" ! gcloud storage buckets create {BUCKET_URI} --location={LOCATION} --project={PROJECT_ID} # Set fast download from HF os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1" # Initialize Vertex AI SDK vertexai.init(project=PROJECT_ID, location=LOCATION) ``` -------------------------------- ### Install Vertex AI SDK for Agent Engines (Python) Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/agent-engine/agent_engine_express_mode.ipynb Installs the necessary Google Cloud AI Platform SDK, including support for agent engines. This command ensures you have the latest version and forces a reinstallation if needed, ensuring a clean setup. ```python %pip install "google-cloud-aiplatform[agent_engines]" --force-reinstall --quiet ``` -------------------------------- ### Local Configuration Path Example Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/use-cases/entity-extraction/README.md Example of setting the CONFIG_PATH environment variable to point to a local configuration file. ```bash CONFIG_PATH="config.json" ``` -------------------------------- ### Loading an agent in a new environment Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/agent-engine/tutorial_google_maps_agent.ipynb Example of how to instantiate a client and load a remote agent using its resource name. ```python # from vertexai import agent_engines # client = vertexai.Client(project=PROJECT_ID, location=LOCATION) # AGENT_ENGINE_RESOURCE_NAME = "YOUR_AGENT_ENGINE_RESOURCE_NAME" # Replace with the resource name of your deployed Agent Engine # remote_agent = client.agent_engines.get(name=AGENT_ENGINE_RESOURCE_NAME) # response = remote_agent.query(input="What is the lat/long of Tempelhof Airport Field in Berlin, Germany?") ``` -------------------------------- ### Few-Shot Chain of Thought Prompting Example Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/prompts/examples/chain_of_thought_react.ipynb Demonstrates few-shot Chain of Thought prompting by providing an example question with a step-by-step reasoning process (the 'chain of thought') before the actual question. This guides the LLM to produce a similar reasoning process for new questions. ```python question = """ Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now? A: The answer is 11. Q: The cafeteria had 23 apples. If they used 20 to make lunch and bought 6 more, how many apples do they have? A: """ print(llm.invoke(question)) ``` -------------------------------- ### Initialize Vertex AI SDK Source: https://github.com/googlecloudplatform/generative-ai/blob/main/search/gemini-enterprise/gemini_enterprise_prompt_management.ipynb Initializes the Vertex AI SDK with project details and sets up a staging bucket. Includes error handling for missing schema files. ```python import vertexai from google.adk.agents import Agent from google.adk.agents.callback_context import CallbackContext from google.genai import types from vertexai import agent_engines from vertexai.preview import prompts, reasoning_engines from vertexai.preview.prompts import Prompt # TODO for Developer: Update project name, location, bucket and schema path (for testing) PROJECT_ID = "[your-project-id]" PROJECT_NUMBER = "[your-project-number]" LOCATION = "[your-location]" STAGING_BUCKET = "[your-bucket]" SCHEMA = "[your-schema-txt-containing-DDLs]" vertexai.init( project=PROJECT_ID, location=LOCATION, staging_bucket=STAGING_BUCKET, ) try: with open(SCHEMA) as f_handle: schema = f_handle.read() except FileNotFoundError: print(f"Error: The schema file was not found at '{SCHEMA}'.") print("Please update the 'SCHEMA' variable with the correct file path.") schema = "" # Assign a default value to prevent further errors ``` -------------------------------- ### Initialize and Configure Web Agent Environment Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/computer-use/web-agent/README.md Commands to clone the repository, set up a virtual environment, and install necessary Python dependencies for the web agent. ```bash git clone https://github.com/GoogleCloudPlatform/generative-ai.git cd generative-ai/gemini/computer-use/web-agent python -m venv .venv source .venv/bin/activate pip install -r requirements.txt playwright install ``` -------------------------------- ### Configure and Install Backend Python Linting and Formatting Tools Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/sample-apps/quickbot/text-to-image-using-imagen3/README.md Ensures that pylint and black are installed as dependencies for the backend Python project and provides instructions on how to configure pylint using a .pylintrc file. This step is essential for adhering to the Google Python Style Guide. ```bash # from backend/ directory, with .venv activated pip install pylint black # or pip install -r requirements.txt ``` ```bash # from backend/ directory pylint --generate-rcfile > .pylintrc ``` -------------------------------- ### Get Format Instructions for Output Parser Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/orchestration/intro_langchain_gemini.ipynb Retrieve the format instructions from the output parser. These instructions are used to guide the language model on how to structure its response. ```python # See the prompt template you created for formatting format_instructions = output_parser.get_format_instructions() print(format_instructions) ``` -------------------------------- ### Set Tutorial Folder and Workspace (Python) Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/evaluation/evaltask_approach/evaluate_rag_batch_pipeline.ipynb Configures local and cloud storage paths for tutorial artifacts, including data, source code, pipelines, and evaluations. It creates necessary directories if they don't exist, utilizing `pathlib` for local paths and `epath` for cloud URIs. ```python from pathlib import Path ROOT_PATH = Path.cwd() TUTORIAL_PATH = ROOT_PATH / "tutorial" DATA_PATH = TUTORIAL_PATH / "data" SRC_PATH = TUTORIAL_PATH / "src" PIPELINE_PATH = TUTORIAL_PATH / "pipeline" EVAL_PATH = TUTORIAL_PATH / "evaluations" DATA_PATH.mkdir(parents=True, exist_ok=True) SRC_PATH.mkdir(parents=True, exist_ok=True) PIPELINE_PATH.mkdir(parents=True, exist_ok=True) EVAL_PATH.mkdir(parents=True, exist_ok=True) from etils import epath WORKSPACE_BUCKET_URI = epath.Path(BUCKET_URI) / "evaluate_rag" DATA_URI = WORKSPACE_BUCKET_URI / "data" EVALUATIONS_URI = WORKSPACE_BUCKET_URI / "evaluations" SRC_URI = WORKSPACE_BUCKET_URI / "src" PIPELINE_ROOT_URI = WORKSPACE_BUCKET_URI / "pipeline" TMP_URI = WORKSPACE_BUCKET_URI / "tmp" WORKSPACE_BUCKET_URI.mkdir(parents=True, exist_ok=True) DATA_URI.mkdir(parents=True, exist_ok=True) EVALUATIONS_URI.mkdir(parents=True, exist_ok=True) SRC_URI.mkdir(parents=True, exist_ok=True) PIPELINE_ROOT_URI.mkdir(parents=True, exist_ok=True) TMP_URI.mkdir(parents=True, exist_ok=True) ``` -------------------------------- ### Prepare System Instruction with Few-Shot Examples (Python) Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/tuning/sft_gemini_qa.ipynb This snippet shows how to format a system instruction string by incorporating few-shot examples. It prepares the instruction for use with generative AI models, ensuring context and desired output format. ```python systemInstruct = f"""Answer the question with a concise extract from the given context. Do not add any additional information, capital letters (only for names) or a punctuation mark in the end.\n\n Here are some examples: \n\n {few_shot_prompt}""" ``` -------------------------------- ### Setup and Authentication for Gemini API Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/nano-banana/nano_banana_recipes.ipynb Installs the required Google GenAI and Pillow packages, authenticates the environment for Google Colab users, and initializes the Vertex AI client with project credentials. ```python %pip install --upgrade --quiet google-genai pillow import sys import os from google import genai if "google.colab" in sys.modules: from google.colab import auth auth.authenticate_user() PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "[your-project-id]") LOCATION = "global" client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION) ``` -------------------------------- ### Set Up Google Cloud Project and Initialize Gemini Client Source: https://github.com/googlecloudplatform/generative-ai/blob/main/gemini/getting-started/intro_gemini_2_5_image_gen.ipynb Configure your Google Cloud project ID and location, then initialize the Gemini client. Ensure the Agent Platform API is enabled in your project. ```python import os from io import BytesIO import matplotlib.image as img import matplotlib.pyplot as plt import requests from IPython.display import Image, Markdown, display from PIL import Image as PIL_Image from google import genai from google.genai.types import FinishReason, GenerateContentConfig, ImageConfig, Part # fmt: off PROJECT_ID = "[your-project-id]" # @param {type: "string", placeholder: "[your-project-id]", isTemplate: true} # fmt: on if not PROJECT_ID or PROJECT_ID == "[your-project-id]": PROJECT_ID = str(os.environ.get("GOOGLE_CLOUD_PROJECT")) LOCATION = "global" client = genai.Client(enterprise=True, project=PROJECT_ID, location=LOCATION) ```