### Installing Google GenAI and MCP Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-mcp-example.ipynb This snippet demonstrates how to install the `google-genai` and `mcp` Python packages using pip. These packages are required for interacting with the Gemini API and MCP server, respectively. It uses the `%pip` magic command, common in environments like Jupyter notebooks, to install the packages. ```python # install Google GenAI and MCP %pip install google-genai mcp ``` -------------------------------- ### Installing scikit-learn Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-langchain.ipynb This command installs the scikit-learn package, which might be needed for further embedding tasks or similarity calculations. ```python %pip install scikit-learn ``` -------------------------------- ### Installing langchain-google-genai Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-langchain.ipynb This command installs the langchain-google-genai package, which is necessary to use Google's Gemini models with LangChain. ```python %pip install langchain-google-genai ``` -------------------------------- ### Installing Gemini and Jinja2 Packages with pip Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-analyze-transcribe-youtube.ipynb This snippet uses the pip package manager to install the google-genai and jinja2 libraries. These libraries are necessary for interacting with the Gemini API and using Jinja2 templates for prompt generation, respectively. ```python %pip install google-genai jinja2 ``` -------------------------------- ### Installing Google GenAI Python Library Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemma-function-calling.ipynb This snippet provides the command to install the `google-genai` Python library, which is required to interact with the Google GenAI API and Gemma models. It uses the `%pip` magic command, common in Jupyter environments. ```Python %pip install google-genai ``` -------------------------------- ### Installing CrewAI with Tools (Python) Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-crewai.ipynb This snippet installs the `crewai` library along with its `tools` extra, which provides additional functionalities for agents to interact with external systems. It's a common setup step for CrewAI projects, ensuring all necessary dependencies are available. ```Python !uv pip install "crewai[tools]" ``` -------------------------------- ### Cloning the Repository - Git Bash Source: https://github.com/philschmid/gemini-samples/blob/main/README.md This command clones the `gemini-samples` GitHub repository to your local machine, providing access to all the included examples, guides, and scripts. ```bash git clone https://github.com/philschmid/gemini-samples.git ``` -------------------------------- ### Installing google-genai Package Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-google-search.ipynb This command installs the google-genai library, which is required to interact with Google's Gemini models. ```python %pip install google-genai ``` -------------------------------- ### Installing Gemini and GitIngest Libraries (Python) Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-context-caching.ipynb This command installs the necessary Python libraries: `google-genai` for interacting with the Gemini API and `gitingest` for ingesting content from Git repositories. These are prerequisites for using the context caching features demonstrated in the notebook. ```Python %pip install google-genai gitingest ``` -------------------------------- ### Installing Dependencies for Gemini API in Python Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-transcribe-with-timestamps.ipynb This code snippet installs the necessary Python packages for interacting with the Google Gemini API. It uses pip to install the google-genai and jinja2 libraries. ```python %pip install google-genai jinja2 ``` -------------------------------- ### Initializing Gemini Client Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-analyze-transcribe-youtube.ipynb This snippet initializes the Gemini API client using an API key retrieved from the environment variables. It imports the necessary modules from the google-genai library and creates a client instance. ```python import os from google import genai from google.genai import types # create client api_key = os.getenv("GEMINI_API_KEY","xxx") client = genai.Client(api_key=api_key) ``` -------------------------------- ### Setting up Google API Key Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-langchain.ipynb This code snippet retrieves the Google AI API key from the environment variables or prompts the user to enter it if not found, ensuring secure access to the Gemini API. ```python import getpass import os if "GOOGLE_API_KEY" not in os.environ: os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter your Google AI API key: ") ``` -------------------------------- ### Installing LangChain and Gemini Integration Source: https://github.com/philschmid/gemini-samples/blob/main/guides/function-calling.ipynb This code installs the necessary Python packages for using LangChain with Google Gemini. It installs the `langchain` and `langchain-google-genai` packages, which provide the framework for building LLM-powered applications and the integration with Google Gemini models, respectively. ```python %pip install langchain langchain-google-genai ``` -------------------------------- ### Installing OpenAI library using pip Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-with-openai-sdk.ipynb This code snippet installs the OpenAI library using the pip package manager. It is necessary to execute this command before using the OpenAI library in Python. ```python # install openai %pip install openai ``` -------------------------------- ### Initializing Gemma 3 Chat Session and Sending First Message Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemma-function-calling.ipynb This Python snippet demonstrates how to initiate a chat session with the Gemma 3 model using the `google-genai` SDK. It sends an initial "hello" message, formatted with the predefined instruction prompt, to start the conversation and observe the model's direct response without function invocation. ```Python chat = client.chats.create(model=model_id) response = chat.send_message(instruction_prompt_with_function_calling.format(user_message="hello")) print(response.text) ``` -------------------------------- ### Installing google-genai library Source: https://github.com/philschmid/gemini-samples/blob/main/guides/function-calling.ipynb This snippet installs the `google-genai` library along with `geopy` and `requests` which are used for geocoding and making HTTP requests to the weather API. This is a prerequisite for interacting with the Gemini API. ```python %pip install "google-genai>=1.0.0" geopy requests ``` -------------------------------- ### Full Agentic Example with Gemini and MCP (Python) Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-mcp-example.ipynb This snippet demonstrates a complete agentic loop using Gemini and an Airbnb MCP server. It initializes the Gemini client and MCP server connection. The `agent_loop` function takes a user prompt, retrieves tools from the MCP server, converts them to Gemini tools, and then iterates, calling tools based on the model's responses. It handles tool execution, manages conversation history, and returns the final response from the model. ```python from typing import List from google import genai from google.genai import types from mcp import ClientSession, StdioServerParameters from mcp.client.stdio import stdio_client import os client = genai.Client(api_key=os.getenv("GEMINI_API_KEY")) model = "gemini-2.0-flash" # Create server parameters for stdio connection server_params = StdioServerParameters( command="npx", # Executable args=[ "-y", "@openbnb/mcp-server-airbnb", "--ignore-robots-txt", ], # Optional command line arguments env=None, # Optional environment variables ) async def agent_loop(prompt: str, client: genai.Client, session: ClientSession): contents = [types.Content(role="user", parts=[types.Part(text=prompt)])] # Initialize the connection await session.initialize() # --- 1. Get Tools from Session and convert to Gemini Tool objects --- mcp_tools = await session.list_tools() tools = types.Tool(function_declarations=[ { "name": tool.name, "description": tool.description, "parameters": tool.inputSchema, } for tool in mcp_tools.tools ]) # --- 2. Initial Request with user prompt and function declarations --- response = await client.aio.models.generate_content( model=model, # Or your preferred model supporting function calling contents=contents, config=types.GenerateContentConfig( temperature=0, tools=[tools], ), # Example other config ) # --- 3. Append initial response to contents --- contents.append(response.candidates[0].content) # --- 4. Tool Calling Loop --- turn_count = 0 max_tool_turns = 5 while response.function_calls and turn_count < max_tool_turns: turn_count += 1 tool_response_parts: List[types.Part] = [] # --- 4.1 Process all function calls in order and return in this turn --- for fc_part in response.function_calls: tool_name = fc_part.name args = fc_part.args or {} # Ensure args is a dict print(f"Attempting to call MCP tool: '{tool_name}' with args: {args}") tool_response: dict try: # Call the session's tool executor tool_result = await session.call_tool(tool_name, args) print(f"MCP tool '{tool_name}' executed successfully.") if tool_result.isError: tool_response = {"error": tool_result.content[0].text} else: tool_response = {"result": tool_result.content[0].text} except Exception as e: tool_response = {"error": f"Tool execution failed: {type(e).__name__}: {e}"} # Prepare FunctionResponse Part tool_response_parts.append( types.Part.from_function_response( name=tool_name, response=tool_response ) ) # --- 4.2 Add the tool response(s) to history --- contents.append(types.Content(role="user", parts=tool_response_parts)) print(f"Added {len(tool_response_parts)} tool response parts to history.") # --- 4.3 Make the next call to the model with updated history --- print("Making subsequent API call with tool responses...") response = await client.aio.models.generate_content( model=model, contents=contents, # Send updated history config=types.GenerateContentConfig( temperature=1.0, tools=[tools], ), # Keep sending same config ) contents.append(response.candidates[0].content) if turn_count >= max_tool_turns and response.function_calls: print(f"Maximum tool turns ({max_tool_turns}) reached. Exiting loop.") print("MCP tool calling loop finished. Returning final response.") # --- 5. Return Final Response --- return response async def run(): async with stdio_client(server_params) as (read, write): async with ClientSession( read, write, ) as session: # Test prompt prompt = "I want to book an apartment in Paris for 2 nights. 03/28 - 03/30" print(f"Running agent loop with prompt: {prompt}") # Run agent loop res = await agent_loop(prompt, client, session) return res res = await run() print(res.text) ``` -------------------------------- ### Simple MCP and Gemini Tool Calling Example (Python) Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-mcp-example.ipynb This snippet demonstrates how to use MCP with Google DeepMind Gemini by converting MCP tools into Gemini-compatible tools. It initializes a Gemini client, sets up a connection to the MCP server via `stdio_client`, lists the tools exposed by the MCP server, and converts them to Gemini's `types.Tool` format. Finally, it calls the Gemini model with the prompt and the tool declarations, processing the function call, if any, in the response. ```python from google import genai from google.genai import types from mcp import ClientSession, StdioServerParameters from mcp.client.stdio import stdio_client import os client = genai.Client( api_key=os.getenv("GEMINI_API_KEY") ) # Replace with your actual API key setup # Create server parameters for stdio connection server_params = StdioServerParameters( command="npx", # Executable args=[ "-y", "@openbnb/mcp-server-airbnb", "--ignore-robots-txt", ], # Optional command line arguments env=None, # Optional environment variables ) async def run(): async with stdio_client(server_params) as (read, write): async with ClientSession( read, write, ) as session: prompt = "I want to book an apartment in Paris for 2 nights. 03/28 - 03/30" # Initialize the connection await session.initialize() # Get tools from MCP session and convert to Gemini Tool objects mcp_tools = await session.list_tools() tools = types.Tool(function_declarations=[ { "name": tool.name, "description": tool.description, "parameters": tool.inputSchema, } for tool in mcp_tools.tools ]) # Send request with function declarations response = client.models.generate_content( model="gemini-2.0-flash", # Or your preferred model supporting function calling contents=prompt, config=types.GenerateContentConfig( temperature=0.7, tools=[tools], ), # Example other config ) # Check for a function call if response.candidates[0].content.parts[0].function_call: function_call = response.candidates[0].content.parts[0].function_call print(f"Function to call: {function_call.name}") print(f"Arguments: {function_call.args}") # In a real app, you would call your function here: # result = await session.call_tool(function_call.args, arguments=function_call.args) # sent new request with function call else: print("No function call found in the response.") print(response.text) await run() ``` -------------------------------- ### Installing Google Gemini and Pydantic in Python Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-structured-outputs.ipynb Installs the `google-genai` and `pydantic` packages using pip. These packages are necessary for interacting with the Gemini API and defining data schemas, respectively. ```python %pip install google-genai pydantic ``` -------------------------------- ### Installing OpenAI Package Source: https://github.com/philschmid/gemini-samples/blob/main/guides/function-calling.ipynb This snippet installs the OpenAI Python package. It's a prerequisite for using the OpenAI-compatible API with Google Gemini. ```python %pip install openai ``` -------------------------------- ### Installing Google GenAI Library - Python Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemma-with-genai-sdk.ipynb This snippet demonstrates how to install the `google-genai` Python library using pip. This library is required to interact with Google's Generative AI API, including models like Gemma 3. ```Python %pip install google-genai ``` -------------------------------- ### Tool Calling/Function Calling with Gemini Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-langchain.ipynb This code demonstrates how to use tool calling/function calling with the Gemini model. It defines a custom tool (get_weather), binds it to the model, and invokes the model with a query that triggers the tool. It then passes the tool results back to the model to get a final response. ```python from langchain_google_genai import ChatGoogleGenerativeAI from langchain_core.tools import tool from langchain_core.messages import ToolMessage # Define a tool @tool(description="Get the current weather in a given location") def get_weather(location: str) -> str: return "It's sunny." # Initialize model and bind the tool llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash") llm_with_tools = llm.bind_tools([get_weather]) # Invoke with a query that should trigger the tool query = "What's the weather in San Francisco?" ai_msg = llm_with_tools.invoke(query) # Access tool calls in the response print(ai_msg.tool_calls) # Pass tool results back to the model tool_message = ToolMessage( content=get_weather(*ai_msg.tool_calls[0]['args']), tool_call_id=ai_msg.tool_calls[0]['id'] ) final_response = llm_with_tools.invoke([ai_msg, tool_message]) print(final_response.content) ``` -------------------------------- ### Installing LangGraph and Dependencies (Python) Source: https://github.com/philschmid/gemini-samples/blob/main/guides/langgraph-react-agent.ipynb This code snippet installs the necessary Python packages, including langgraph, langchain-google-genai, geopy, and requests, using pip. ```python %pip install langgraph langchain-google-genai geopy requests ``` -------------------------------- ### Installing Dependencies using pip Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-code-executor-data-analysis.ipynb This code snippet installs the necessary Python libraries: google-genai and pandas using pip. These libraries are required for interacting with the Gemini API and for data manipulation using pandas. ```python %pip install google-genai pandas ``` -------------------------------- ### Defining Meta-Prompt for JSON Schema Generation (Python) Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-meta-prompt-structured-outputs.ipynb This snippet defines a multi-line string `meta_prompt` which acts as a system instruction for the Gemini model. It guides the model to act as a JSON Schema expert, providing strict rules, restrictions, and guidelines for generating valid JSON schemas based on user input. The prompt includes examples of desired input-output pairs to demonstrate the expected schema structure. ```python # Used to generate valid JSON Schema that can be used to generate structured output with Gemini meta_prompt = """You are a JSON Schema expert. Your task is to create JSON schema baed on the user input. The schema will be used for extra data. You must also make sure: - All fields in an object are set as required - All objects must have properties defined - Order matters! If the values are dependent or would require additional information, make sure to include the additional information in the description. Same counts for "reasoning" or "thinking" should come before the conclusion. - $defs must be defined under the schema param - Return only the schema JSON not more, use ```json to start and ``` to end the JSON schema Restrictions: - You cannot use examples, if you think examples are helpful include them in the description. - You cannot use default values, If you think default are helpful include them in the description. - Never include a $schema - The "type" needs to be a single value, no arrays Guidelines: - If the user prompt is short define a single object schema and fields based on your knowledge. - If the user prompt is in detail about the data only use the data in the schema. Don't add more fields than the user asked for. Examples: Input: Cookie Recipes Output: ```json {{ "title":"Cookie Recipe", "description":"Schema for a cookie recipe, including ingredients and quantities. The 'ingredients' array lists each ingredient along with its corresponding quantity and unit of measurement. The 'instructions' array provides a step-by-step guide to preparing the cookies. The order of instructions is important.", "type":"object", "properties":{{ "name":{{ "type":"string", "description":"The name of the cookie recipe." }}, "description":{{ "type":"string", "description":"A short description of the cookie, including taste and textures." }}, "ingredients":{{ "type":"array", "description":"A list of ingredients required for the recipe.", "items":{{ "type":"object", "description":"An ingredient with its quantity and unit.", "properties":{{ "name":{{ "type":"string", "description":"The name of the ingredient (e.g., flour, sugar, butter)." }}, "quantity":{{ "type":"number", "description":"The amount of the ingredient needed." }}, "unit":{{ "type":"string", "description":"The unit of measurement for the ingredient (e.g., cups, grams, teaspoons). Use abbreviations like 'tsp' for teaspoon and 'tbsp' for tablespoon." }} }}, "required":[ "name", "quantity", "unit" ] }} }}, "instructions":{{ "type":"array", "description":"A sequence of steps to prepare the cookie recipe. The order of instructions matters.", "items":{{ "type":"string", "description":"A single instruction step." }} }} }}, "required":[ "name", "description", "ingredients", "instructions" ], "$defs":{{ "ingredient":{{ "type":"object", "description":"An ingredient with its quantity and unit.", "properties":{{ "name":{{ "type":"string", "description":"The name of the ingredient (e.g., flour, sugar, butter)." }}, "quantity":{{ "type":"number", "description":"The amount of the ingredient needed." }}, "unit":{{ "type":"string", "description":"The unit of measurement for the ingredient (e.g., cups, grams, teaspoons). Use abbreviations like 'tsp' for teaspoon and 'tbsp' for tablespoon." }} }}, "required":[ "name", "quantity", "unit" ] }} }} }} ``` Input: Book with title, author, and publication year. Output: ```json {{ "type": "object", "properties": {{ "title": {{ "type": "string", "description": "The title of the book." }}, "author": {{ "type": "string", "description": "The author of the book." }}, "publicationYear": {{ "type": "integer", "description": "The year the book was published." }} }}, "required": [ "title", "author", "publicationYear" ], }} ``` Input: {user_input}""" ``` -------------------------------- ### Installing Google GenAI Library Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-file-editing.ipynb Installs the `google-genai` Python library, which is required to interact with Google Gemini models. This is a prerequisite step for running the subsequent code. ```python !pip install google-genai ``` -------------------------------- ### Defining Function Calling Instructions and Tools for Gemma 3 Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemma-function-calling.ipynb This snippet provides the initial prompt structure for Gemma 3, instructing the model on how to use function calling. It defines the `convert` Python function, its purpose, and arguments, making it available for the model to invoke. The prompt also specifies the `tool_code` and `tool_output` wrapping conventions. ```Prompt Template user At each turn, if you decide to invoke any of the function(s), it should be wrapped with ```tool_code```. The python methods described below are imported and available, you can only use defined methods. The generated code should be readable and efficient. The response to a method will be wrapped in ```tool_output``` use it to call more tools or generate a helpful, friendly response. When using a ```tool_call``` think step by step why and how it should be used. The following Python methods are available: ```python def convert(amount: float, currency: str, new_currency: str) -> float: """Convert the currency with the latest exchange rate Args: amount: The amount of currency to convert currency: The currency to convert from new_currency: The currency to convert to """ ``` User: {{user_message}} model ``` -------------------------------- ### Chain Calls with Prompt Template Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-langchain.ipynb This example demonstrates how to chain a prompt template with the Gemini chat model to create a more complex interaction. It initializes the model and defines a prompt that takes input and output languages as parameters, allowing for dynamic translation tasks. ```python from langchain_google_genai import ChatGoogleGenerativeAI from langchain_core.prompts import ChatPromptTemplate # Initialize model llm = ChatGoogleGenerativeAI( model="gemini-2.0-flash", temperature=0, ) prompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful assistant that translates {input_language} to {output_language}."), ("human", "{input}"), ]) chain = prompt | llm result = chain.invoke({ "input_language": "English", "output_language": "German", "input": "I love programming.", }) print(result.content) # Output: Ich liebe Programmieren. ``` -------------------------------- ### Defining Demo Functions and Instruction Prompt for Gemma 3 Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemma-function-calling.ipynb This snippet defines two simple Python functions, `convert` and `get_exchange_rate`, which serve as mock implementations for demonstration purposes. It also constructs the `instruction_prompt_with_function_calling` string, which includes the detailed instructions for Gemma 3 on how to perform function calling and the Python signatures and docstrings for the available functions. ```Python def convert(amount: float, currency: str, new_currency: str) -> float: # demo implementation return amount * 0.9 def get_exchange_rate(currency: str, new_currency: str) -> float: # demo implementation return 1.2 instruction_prompt_with_function_calling = '''At each turn, if you decide to invoke any of the function(s), it should be wrapped with ```tool_code```. The python methods described below are imported and available, you can only use defined methods. The generated code should be readable and efficient. The response to a method will be wrapped in ```tool_output``` use it to call more tools or generate a helpful, friendly response. When using a ```tool_call``` think step by step why and how it should be used. The following Python methods are available: ```python def convert(amount: float, currency: str, new_currency: str) -> float: """Convert the currency with the latest exchange rate Args: amount: The amount of currency to convert currency: The currency to convert from new_currency: The currency to convert to """ def get_exchange_rate(currency: str, new_currency: str) -> float: """Get the latest exchange rate for the currency pair Args: currency: The currency to convert from new_currency: The currency to convert to """ ``` User: {user_message} ''' ``` -------------------------------- ### Built-in Tools: Google Search, Code Execution Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-langchain.ipynb This example utilizes built-in tools like Google Search and Code Execution with the Gemini model. It invokes the model with queries designed to trigger these tools and prints the results. ```python from langchain_google_genai import ChatGoogleGenerativeAI from google.ai.generativelanguage_v1beta.types import Tool as GenAITool # Initialize model llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash") # Google Search search_resp = llm.invoke( "When is the next total solar eclipse in US?", tools=[GenAITool(google_search={})], ) print(search_resp.content) # Code Execution code_resp = llm.invoke( "What is 2*2, use python", tools=[GenAITool(code_execution={})], ) for c in code_resp.content: if isinstance(c, dict): if c["type"] == 'code_execution_result': print(f"Code execution result: {c['code_execution_result']}") elif c["type"] == 'executable_code': print(f"Executable code: {c['executable_code']}") else: print(c) ``` -------------------------------- ### Setting Up Gemini API Key - Shell/Environment Source: https://github.com/philschmid/gemini-samples/blob/main/README.md This line demonstrates how to set the `GEMINI_API_KEY` environment variable, which is essential for authenticating with the Google DeepMind Gemini API. Users should replace `your_api_key_here` with their actual API key. ```shell GEMINI_API_KEY=your_api_key_here ``` -------------------------------- ### Installing Pydantic-AI Package in Python Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-pydanticai-agent.ipynb This command installs the `pydantic-ai` package using pip, which is required for building the conversational agent. The package facilitates integration with AI models and allows for defining agents and tools. ```python %pip install pydantic-ai ``` -------------------------------- ### Gemini Embeddings with Vector Store Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-langchain.ipynb This example demonstrates how to use Gemini embeddings with a vector store. It initializes the embeddings, creates an in-memory vector store, and then retrieves similar documents based on a query. ```python from langchain_google_genai import GoogleGenerativeAIEmbeddings from langchain_core.vectorstores import InMemoryVectorStore # Initialize embeddings embeddings = GoogleGenerativeAIEmbeddings(model="models/gemini-embedding-exp-03-07") text = "LangChain is the framework for building context-aware reasoning applications" # Create vector store and retriever vectorstore = InMemoryVectorStore.from_texts([text], embedding=embeddings) retriever = vectorstore.as_retriever() # Retrieve similar documents retrieved_documents = retriever.invoke("What is LangChain?") print(retrieved_documents[0].page_content) ``` -------------------------------- ### Chat with Gemini Chat Model Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-langchain.ipynb This example initializes the Gemini chat model using `ChatGoogleGenerativeAI` from `langchain-google-genai`, sets basic configurations, and invokes it with a simple translation task. It demonstrates a basic interaction with the model, translating English to French. ```python from langchain_google_genai import ChatGoogleGenerativeAI # Initialize model llm = ChatGoogleGenerativeAI( model="gemini-2.0-flash", temperature=0, max_tokens=None, timeout=None, max_retries=2, ) # Simple invocation messages = [ ("system", "You are a helpful assistant that translates English to French."), ("human", "I love programming."), ] response = llm.invoke(messages) print(response.content) # Output: J'adore la programmation. ``` -------------------------------- ### Installing Google ADK Python Package Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-adk-mcp.ipynb This snippet demonstrates how to install or upgrade the `google-adk` Python package using pip. This package is essential for developing AI agents with Google's Agent Development Kit, providing necessary classes and functions for agent creation and management. ```python %pip install google-adk --upgrade ``` -------------------------------- ### Simulated Tool Output for Gemma 3 Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemma-function-calling.ipynb This snippet represents the output generated by executing the `tool_code` from the previous step. It's formatted within a `tool_output` block, which is then fed back to the Gemma model to continue the conversation or generate a final user-friendly response. ```Text/Tool Output ```tool_output 180000.0 ``` ``` -------------------------------- ### Implementing a Gemini Agent with ADK and MCP Tooling in Python Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-adk-mcp.ipynb This comprehensive Python example demonstrates how to build and run an AI agent using Google's ADK, integrating with an MCP server for tool-calling capabilities. It covers connecting to an MCP server to retrieve tools, defining a Gemini-powered agent, setting up session management, executing a prompt, and processing the agent's responses, including function calls and their results. The `GEMINI_API_KEY` environment variable is required for authentication. ```python import os from google.genai import types from google.adk import Agent, Runner from google.adk.sessions import InMemorySessionService from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset, StdioServerParameters os.environ["GOOGLE_API_KEY"] = os.getenv("GEMINI_API_KEY") async def mcp_agent(): # --- Step 1: Connect to MCP Server and Get Tools --- tools, exit_stack = await MCPToolset.from_server( connection_params=StdioServerParameters( command="npx", args=["-y", "@philschmid/weather-mcp"], # connection_params=SseServerParams(url="http://remote-server:port/path", headers={...}) ) ) # --- Step 2: Define the Agent --- root_agent = Agent( model="gemini-2.0-flash", name='weather_assistant', instruction='Can read the weather in a specific city', tools=tools, ) # --- Step 3: Setup Session, Runner, and Execute --- # Session saves the conversation history and state session_service = InMemorySessionService() session = session_service.create_session( state={}, app_name='weather_app', user_id='user_fs' ) # Runner is responsible for executing the agent runner = Runner( app_name='weather_app', agent=root_agent, session_service=session_service, ) # --- Step 4: Run the agent --- prompt = "What is the weather in Berlin tomorrow, 2025/04/15?" user_content = types.Content(role='user', parts=[types.Part(text=prompt)]) events_async = runner.run_async( session_id=session.id, user_id=session.user_id, new_message=user_content ) async for event in events_async: if event.content.parts[0].function_call: print(f"Function call: {event.content.parts[0].function_call.args}") elif event.content.parts[0].function_response: print(f"Function response: {event.content.parts[0].function_response.response}") else: print(event.content.parts[0].text) # --- 5: Cleanup MCP server connection --- if exit_stack: await exit_stack.aclose() await mcp_agent() ``` -------------------------------- ### Multi-Turn Conversation Prompt with Tool Output for Gemma 3 Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemma-function-calling.ipynb This comprehensive snippet illustrates the full multi-turn conversation flow, including the initial instructions, the user's query, Gemma's `tool_code` response, and the subsequent `tool_output` from the executed function. This complete prompt is then sent back to the model to generate the final user-facing answer. ```Prompt Template user At each turn, if you decide to invoke any of the function(s), it should be wrapped with ```tool_code```. The python methods described below are imported and available, you can only use defined methods. The generated code should be readable and efficient. The response to a method will be wrapped in ```tool_output``` use it to call more tools or generate a helpful, friendly response. When using a ```tool_call``` think step by step why and how it should be used. The following Python methods are available: ```python def convert(amount: float, currency: str, new_currency: str) -> float: """Convert the currency with the latest exchange rate Args: amount: The amount of currency to convert currency: The currency to convert from new_currency: The currency to convert to """ ``` User: {{user_message}} model Okay, I need to convert $200,000 to EUR. I will use the `convert` function for this. ```tool_code convert(amount=200000.0, currency="USD", new_currency="EUR") ``` user ```tool_output 180000.0 ``` model ``` -------------------------------- ### Generate Image with Gemini Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-langchain.ipynb This code generates an image using the Gemini model with image generation capabilities. It initializes the model, provides a text prompt, and displays the generated image using IPython.display.Image. ```python from langchain_google_genai import ChatGoogleGenerativeAI import base64 from IPython.display import Image, display # Initialize model for image generation llm = ChatGoogleGenerativeAI(model="models/gemini-2.0-flash-exp-image-generation") message = { "role": "user", "content": "Generate an image of a cat wearing a hat.", } response = llm.invoke( [message], generation_config=dict(response_modalities=["TEXT", "IMAGE"]), ) # Display the generated image image_base64 = response.content[0].get("image_url").get("url").split(",")[-1] image_data = base64.b64decode(image_base64) display(Image(data=image_data, width=300)) ``` -------------------------------- ### Podcast Transcription with Gemini 2.5 Pro Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-analyze-transcribe-youtube.ipynb This snippet transcribes a podcast episode using Gemini 2.5 Pro. It uploads an audio file, generates a structured prompt using a Jinja2 template, and then calls the Gemini API to generate a transcript with speaker identification, timestamps, and audio event detection. The transcript is then printed to the console. ```python from jinja2 import Template # path to the file to upload file_path = "../assets/porsche.mp3" # Repalce with your own file path # Upload the file to the File API file = client.files.upload(file=file_path) # Generate a structured response using the Gemini API prompt_template = Template("""Generate a transcript of the episode. Include timestamps and identify speakers. Speakers are: {% for speaker in speakers %}- {{ speaker }}{% if not loop.last %}\n{% endif %}{% endfor %} eg: [00:00] Brady: Hello there. [00:02] Tim: Hi Brady. It is important to include the correct speaker names. Use the names you identified earlier. If you really don't know the speaker's name, identify them with a letter of the alphabet, eg there may be an unknown speaker 'A' and another unknown speaker 'B'. If there is music or a short jingle playing, signify like so: [01:02] [MUSIC] or [01:02] [JINGLE] If you can identify the name of the music or jingle playing then use that instead, eg: [01:02] [Firework by Katy Perry] or [01:02] [The Sofa Shop jingle] If there is some other sound playing try to identify the sound, eg: [01:02] [Bell ringing] Each individual caption should be quite short, a few short sentences at most. Signify the end of the episode with [END]. Don't use any markdown formatting, like bolding or italics. Only use characters from the English alphabet, unless you genuinely believe foreign characters are correct. It is important that you use the correct words and spell everything correctly. Use the context of the podcast to help. If the hosts discuss something like a movie, book or celebrity, make sure the movie, book, or celebrity name is spelled correctly.""") # Define the speakers and render the prompt speakers = ["John"] prompt = prompt_template.render(speakers=speakers) response = client.models.generate_content( model="gemini-2.5-pro-exp-03-25", contents=[prompt, file], ) print(response.text) ``` -------------------------------- ### Token Usage Tracking with Gemini Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-langchain.ipynb This code snippet demonstrates how to track the token usage metadata for the Gemini model. It invokes the model and prints both the response content and the usage metadata. ```python from langchain_google_genai import ChatGoogleGenerativeAI # Initialize model llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash") result = llm.invoke("Explain the concept of prompt engineering in one sentence.") print(result.content) print("\nUsage Metadata:") print(result.usage_metadata) ``` -------------------------------- ### YouTube Video Analysis with Gemini 2.5 Pro Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-analyze-transcribe-youtube.ipynb This snippet analyzes a YouTube video using Gemini 2.5 Pro. It takes a YouTube URL as input, constructs a prompt to summarize the video, and then calls the Gemini API to analyze the video content. The response, which includes a summary of the video's main points, key topics, call to action, and a general overview, is then printed to the console. ```python from google.genai import types youtube_url = "https://www.youtube.com/watch?v=RDOMKIw1aF4" # Repalce with the youtube url you want to analyze prompt = """Analyze the following YouTube video content. Provide a concise summary covering: 1. **Main Thesis/Claim:** What is the central point the creator is making? 2. **Key Topics:** List the main subjects discussed, referencing specific examples or technologies mentioned (e.g., AI models, programming languages, projects). 3. **Call to Action:** Identify any explicit requests made to the viewer. 4. **Summary:** Provide a concise summary of the video content. Use the provided title, chapter timestamps/descriptions, and description text for your analysis.""" # Analyze the video response = client.models.generate_content( model="gemini-2.5-pro-exp-03-25", contents=types.Content( parts=[ types.Part(text=prompt), types.Part( file_data=types.FileData(file_uri=youtube_url) ) ] ) ) print(response.text) ``` -------------------------------- ### Audio Input to Gemini Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-langchain.ipynb This code demonstrates how to provide audio input to the Gemini model. It reads an audio file, encodes it to base64, and sends it as a media type message, instructing the model to transcribe the audio. ```python from langchain_google_genai import ChatGoogleGenerativeAI from langchain_core.messages import HumanMessage import base64 # Initialize model llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash") audio_file_path = "../assets/porsche.mp3" audio_mime_type = "audio/mpeg" with open(audio_file_path, "rb") as audio_file: encoded_audio = base64.b64encode(audio_file.read()).decode('utf-8') message = HumanMessage( content=[ {"type": "text", "text": "Transcribe this audio."}, {"type": "media", "data": encoded_audio, "mime_type": audio_mime_type} ] ) response = llm.invoke([message]) print(response.content) ``` -------------------------------- ### Implementing the Multi-Agent Pattern with Handoff Logic in Python Source: https://github.com/philschmid/gemini-samples/blob/main/guides/agentic-pattern.ipynb This Python example demonstrates a simplified multi-agent system where distinct agents (Hotel Agent, Restaurant Agent) collaborate using handoff logic. It defines structured output schemas for responses and an agent function to interact with the Gemini model, showcasing how agents can pass control based on the user's request. ```Python from google import genai from pydantic import BaseModel, Field # Configure the client (ensure GEMINI_API_KEY is set in your environment) client = genai.Client(api_key=os.environ["GEMINI_API_KEY"]) # Define Structured Output Schemas class Response(BaseModel): handoff: str = Field(default="", description="The name/role of the agent to hand off to. Available agents: 'Restaurant Agent', 'Hotel Agent'") message: str = Field(description="The response message to the user or context for the next agent") # Agent Function def run_agent(agent_name: str, system_prompt: str, prompt: str) -> Response: response = client.models.generate_content( model='gemini-2.0-flash', contents=prompt, config = {'system_instruction': f'You are {agent_name}. {system_prompt}', 'response_mime_type': 'application/json', 'response_schema': Response} ) return response.parsed # Define System Prompts for the agents hotel_system_prompt = "You are a Hotel Booking Agent. You ONLY handle hotel bookings. If the user asks about restaurants, flights, or anything else, respond with a short handoff message containing the original request and set the 'handoff' field to 'Restaurant Agent'. Otherwise, handle the hotel request and leave 'handoff' empty." restaurant_system_prompt = "You are a Restaurant Booking Agent. You handle restaurant recommendations and bookings based on the user's request provided in the prompt." # Prompt to be about a restaurant initial_prompt = "Can you book me a table at an Italian restaurant for 2 people tonight?" print(f"Initial User Request: {initial_prompt}") # Run the first agent (Hotel Agent) to force handoff logic output = run_agent("Hotel Agent", hotel_system_prompt, initial_prompt) # simulate a user interaction to change the prompt and handoff if output.handoff == "Restaurant Agent": print("Handoff Triggered: Hotel to Restaurant") output = run_agent("Restaurant Agent", restaurant_system_prompt, initial_prompt) elif output.handoff == "Hotel Agent": print("Handoff Triggered: Restaurant to Hotel") output = run_agent("Hotel Agent", hotel_system_prompt, initial_prompt) print(output.message) ``` -------------------------------- ### Video Input to Gemini Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemini-langchain.ipynb This code demonstrates how to provide video input to the Gemini model. It reads a video file, encodes it to base64, and sends it as a media type message, requesting the model to describe the video content. ```python from langchain_google_genai import ChatGoogleGenerativeAI from langchain_core.messages import HumanMessage import base64 # Initialize model llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash") video_file_path = "../assets/screen.mp4" video_mime_type = "video/mp4" with open(video_file_path, "rb") as video_file: encoded_video = base64.b64encode(video_file.read()).decode('utf-8') message = HumanMessage( content=[ {"type": "text", "text": "Describe what's happening in this video."}, {"type": "media", "data": encoded_video, "mime_type": video_mime_type} ] ) response = llm.invoke([message]) print(response.content) ``` -------------------------------- ### Initializing GenAI Client and Extracting Tool Calls in Python Source: https://github.com/philschmid/gemini-samples/blob/main/examples/gemma-function-calling.ipynb This Python snippet initializes the `google-genai` client using an API key and defines the `model_id` for Gemma 3 27B It. It also includes the `extract_tool_call` helper function, which uses regex to parse `tool_code` blocks from model responses, executes the extracted Python code using `eval`, and formats the result into a `tool_output` string. ```Python import os from google import genai import re # create client api_key = os.getenv("GEMINI_API_KEY","xxx") client = genai.Client(api_key=api_key) # speicfy the model id model_id = "gemma-3-27b-it" # extract the tool call from the response def extract_tool_call(text): import io from contextlib import redirect_stdout pattern = r"```tool_code\s*(.*?)\s*```" match = re.search(pattern, text, re.DOTALL) if match: code = match.group(1).strip() # Capture stdout in a string buffer f = io.StringIO() with redirect_stdout(f): result = eval(code) output = f.getvalue() r = result if output == '' else output return f'```tool_output\n{str(r).strip()}\n```''' return None ```