### Install and Import Gemini API Library
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Counting_Tokens.zh.ipynb
Installs the required google-generativeai library and imports the module. This is the initial setup step required before using any Gemini API functionality. The -q flag ensures quiet installation without output.
```python
!pip install -U -q google-generativeai
import google.generativeai as genai
```
--------------------------------
### Install Gemini Python SDK
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Gemini_Flash_Introduction.zh.ipynb
Installs the Google Generative AI SDK via pip in a Jupyter/Colab environment. Requires internet access and pip installed. Outputs installation confirmation if successful; assumes -q flag for quiet mode to reduce output.
```python
!pip install -q -U google-generativeai # Install the Python SDK
```
--------------------------------
### Install Google Gen AI SDK
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/live_api_starter.ipynb
Installs the Google Generative AI SDK for Python. This SDK provides programmatic access to Gemini models. Ensure you have an active internet connection for the installation.
```python
#@param interval
!pip install -U -q google-genai
```
--------------------------------
### Install Google Generative AI Library
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/System_instructions.zh.ipynb
Installs the necessary Google Generative AI library with a specific version. This is a prerequisite for using the Gemini API in Python environments like Google Colab.
```bash
# @title Install Google Generative AI Library
!pip install -qU 'google-generativeai>0.4.1'
```
--------------------------------
### Install Poppler Utilities for PDF Processing
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/PDF_Files.zh.ipynb
Installs poppler-utils package which provides tools for PDF processing including pdftoppm and pdftotext commands.
```Shell
!apt install poppler-utils
```
--------------------------------
### Install Google Generative AI Library
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/examples/Story_Writing_with_Prompt_Chaining.zh.ipynb
Installs the Google Generative AI library, a dependency for interacting with the Gemini API. This is a prerequisite for running the subsequent code examples.
```shell
# @title Install Google Generative AI Library
! pip install -q -U google-generativeai
```
--------------------------------
### Install Google GenerativeAI Python Package
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Video.zh.ipynb
Installs the latest version of the google-generativeai package required for interacting with the Gemini API.
```python
!pip install -U google-generativeai
```
--------------------------------
### Start a Chat with a Pirate Persona
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/System_instructions.zh.ipynb
Initiates a chat session with the Gemini API using a pirate persona. It sends an initial message and prints the response, demonstrating conversational interaction.
```python
# @title Start a Chat with a Pirate Persona
chat = model.start_chat()
response = chat.send_message("Good day fine chatbot")
print(response.text)
```
--------------------------------
### Initialize and Conduct Chat with Gemini
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Gemini_Flash_Introduction.zh.ipynb
Starts a chat session, sends a message, and manages conversation history. Takes model version and initial prompt. Outputs chat response and allows history token counting; retains history in chat object.
```python
version = 'models/gemini-1.5-flash-latest' # @param ["models/gemini-1.5-flash-latest", "models/gemini-1.5-pro-latest", "models/gemini-1.0-pro-latest"]
model = genai.GenerativeModel(version)
chat = model.start_chat(history=[])
response = chat.send_message("How can I start learning artificial intelligence?")
print(response.text)
model.count_tokens(chat.history)
```
--------------------------------
### Start a Chat Session with Gemini API
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Prompting.zh.ipynb
Initializes a chat session using the Gemini API. The `ChatSession` class stores conversation history for multi-turn interactions. This code sets up the model and starts an empty chat.
```python
import google.generativeai as genai
# Assuming genai is already configured with an API key
# genai.configure(api_key='YOUR_API_KEY')
model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat(history=[])
```
--------------------------------
### Install google-generativeai Library
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/examples/Apollo_11.zh.ipynb
Installs the necessary library for interacting with the Google Generative AI API. This is a prerequisite for using the API in Python.
```python
#@title 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 -U -q google-generativeai
```
--------------------------------
### Python: Enable Code Execution with Gemini API
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/websockets/live_api_starter.ipynb
This code demonstrates how to enable code execution capabilities for the Gemini API. By including the 'code_execution' tool, the API can interpret and run code snippets provided in the input. This example initializes an AudioLoop with this capability.
```python
tools = [
{'code_execution': {}}
]
await AudioLoop(tools=tools).run()
```
--------------------------------
### Install Google GenerativeAI Python Package
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/PDF_Files.zh.ipynb
Installs the Google GenerativeAI Python package required for interacting with the Gemini API. This is a prerequisite for using the API.
```Python
!pip install -Uq google-generativeai
```
--------------------------------
### File Upload and Handling with Gemini API in Python
Source: https://context7.com/doggy8088/gemini-api-cookbook/llms.txt
This snippet provides the setup for uploading files (images, videos, audio, PDFs) to be used within Gemini API prompts. It includes configuring the API key by loading it from environment variables using dotenv and then initializing the genai library. Dependencies: google.generativeai, os, python-dotenv.
```python
import google.generativeai as genai
import os
from dotenv import load_dotenv
# 載入環境變數
load_dotenv()
api_key = os.environ["GOOGLE_API_KEY"]
genai.configure(api_key=api_key)
```
--------------------------------
### Install and Configure Google GenerativeAI
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Embeddings.zh.ipynb
Installs the google-generativeai package and configures API authentication using Colab userdata for the GOOGLE_API_KEY credential.
```python
!pip install -q -U google-generativeai
import google.generativeai as genai
from google.colab import userdata
GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
```
--------------------------------
### Install jq for JSON processing
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/rest/Safety_REST.zh.ipynb
Installs the `jq` command-line JSON processor, which is used later in the script to parse and extract information from JSON responses. This is a prerequisite for handling API output effectively.
```bash
#!/bin/bash
apt install jq
```
--------------------------------
### Python: Use Google Search with Gemini API
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/websockets/live_api_starter.ipynb
This example demonstrates how to integrate Google Search functionality into the Gemini API. It initializes an AudioLoop with the google_search tool enabled. The output indicates the tool is active and ready for interaction, followed by placeholders for audio objects.
```python
tools = [
{'google_search': {}},
]
await AudioLoop(tools=tools).run()
```
--------------------------------
### Python: Story Writing Prompt Chaining Example
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/examples/Story_Writing_with_Prompt_Chaining.zh.ipynb
This Python code provides an example of prompt chains for story writing using a language model. The code utilizes f-strings for including personas and writing guidelines, as well as placeholders to incorporate previous prompt outputs. It demonstrates a three-step approach: premise generation, outline creation, and story start.
```python
persona = '''\nYou are an award-winning science fiction author with a penchant for expansive,\nintricately woven stories. Your ultimate goal is to write the next award winning\nsci-fi novel.'''
guidelines = '''\nWriting Guidelines\n\nDelve deeper. Lose yourself in the world you're building. Unleash vivid\ndescriptions to paint the scenes in your reader's mind. Develop your\ncharacters—let their motivations, fears, and complexities unfold naturally. Weave in the\nthreads of your outline, but don't feel constrained by it. Allow\nyour story to surprise you as you write. Use rich imagery, sensory details, and\nevocative language to bring the setting, characters, and events to life.\nIntroduce elements subtly that can blossom into complex subplots, relationships,\nor worldbuilding details later in the story. Keep things intriguing but not\nfully resolved. Avoid boxing the story into a corner too early. Plant the seeds\nof subplots or potential character arc shifts that can be expanded later.\n\nRemember, your main goal is to write as much as you can. If you get through\nthe story too fast, that is bad. Expand, never summarize.\n'''
premise_prompt = f'''\n{persona}\n\nWrite a single sentence premise for a sci-fi story featuring cats.'''
outline_prompt = f'''\n{persona}\n\nYou have a gripping premise in mind:\n\n{{premise}}\n\nWrite an outline for the plot of your story.'''
starting_prompt = f'''\n{persona}\n\nYou have a gripping premise in mind:\n\n{{premise}}\n\nYour imagination has crafted a rich narrative outline:\n\n{{outline}}\n\nFirst, silently review the outline and the premise. Consider how to start the\nstory.\n\nStart to write the very beginning of the story. You are not expected to finish\nthe whole story now. Your writing should be detailed enough that you are only\nscratching the surface of the first bullet of your outline. Try to write AT\nMINIMUM 1000 WORDS.\n\n{guidelines}'''
```
--------------------------------
### Install Dependencies with Pip
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/plotting_and_mapping.ipynb
Installs the necessary Python packages for the project, specifically the 'websockets' library (version 14.0 or compatible) and 'altair' for charting.
```python
%pip install -q 'websockets~=14.0' altair
```
--------------------------------
### Python: Install Websockets Library
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/websockets/live_api_tool_use.ipynb
Installs the 'websockets' library, which is required for establishing WebSocket connections to the Gemini API.
```python
!pip install -q websockets
```
--------------------------------
### Import Required Libraries
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/websockets/live_api_starter.ipynb
Imports necessary modules for handling asynchronous operations, base64 encoding, audio processing, and WebSocket connections in Python.
```python
import asyncio
import base64
import contextlib
import datetime
import os
import json
import wave
import itertools
from websockets.asyncio.client import connect
from IPython.display import display, Audio
```
--------------------------------
### Gemini API Text-to-Text Interaction (Python)
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/live_api_starter.ipynb
This snippet demonstrates a simple text-to-text interaction with the Gemini API. It sends a message and receives a text response, printing both to the console. It relies on the `client` object from a previous setup, indicating a connection to the Gemini API is already established.
```python
config={
"generation_config": {"response_modalities": ["TEXT"]}}
async with client.aio.live.connect(model=MODEL, config=config) as session:
message = "Hello? Gemini are you there?"
print("> ", message, "\n")
await session.send(message, end_of_turn=True)
# For text responses, When the model's turn is complete it breaks out of the loop.
turn = session.receive()
async for chunk in turn:
if chunk.text is not None:
print(f'- {chunk.text}')
```
--------------------------------
### Install ChromaDB and Gemini API Libraries
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/examples/vectordb_with_chroma.zh.ipynb
Installs the necessary Python libraries for using ChromaDB and the Google Generative AI SDK. These commands are typically run in a notebook environment.
```python
# Install ChromaDB and Gemini API libraries
!pip install -U -q google.generativeai
!pip install -q chromadb==0.4.24
```
--------------------------------
### Python: Generate Starting Draft using Gemini API
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/examples/Story_Writing_with_Prompt_Chaining.zh.ipynb
This Python snippet generates the initial draft of a story using the Gemini API. It formats a starting prompt with the generated premise and outline, then uses `generate_with_retry` to create the draft and prints it using `pprint` for better readability.
```python
starting_draft = generate_with_retry(model, starting_prompt.format(premise=premise, outline=outline)).text
pprint(starting_draft)
```
--------------------------------
### Coffee Shop Ordering Bot - Application Example
Source: https://context7.com/doggy8088/gemini-api-cookbook/llms.txt
Builds an interactive AI agent for a coffee shop by combining function calling with system instructions, managing orders, and confirming them.
```APIDOC
## Coffee Shop Ordering Bot
### Description
This example demonstrates how to create an interactive AI agent using Gemini's function calling capabilities. The bot takes coffee orders, manages them, and confirms with the customer.
### Function Definitions
- **add_to_order(drink: str, modifiers: list = [])**
- Description: Adds the specified drink to the customer's order.
- Parameters:
- **drink** (string) - Required - The name of the drink.
- **modifiers** (list of strings) - Optional - List of modifiers for the drink.
- **get_order() -> list**
- Description: Returns the customer's current order.
- **clear_order()**
- Description: Removes all items from the customer's order.
- **confirm_order() -> str**
- Description: Asks the customer if the order is correct and returns their confirmation.
- **place_order() -> int**
- Description: Submits the order to the kitchen and returns an order ID.
### System Prompt
```python
COFFEE_BOT_PROMPT = """You are a coffee order taking system...
MENU:
Coffee Drinks: Espresso, Americano, Latte, Cappuccino, Mocha
Modifiers: Milk options (Whole, Oat, Almond), Espresso shots, Hot-Iced
..."""
```
### Request Example (Conceptual Interaction)
*User:* "I'd like a latte with oat milk."
*Bot:* (Calls `add_to_order` with `drink='Latte'`, `modifiers=['Oat milk']`)
*Bot:* "Your order is: Latte with Oat milk. Is this correct?"
*User:* "Yes."
*Bot:* (Calls `confirm_order()`)
*Bot:* "Okay, I'll place your order. Your order number is [returns result of `place_order()`]."
```
--------------------------------
### Install Google Gen AI SDK
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/spatial_understanding_3d.ipynb
Installs the Google Gen AI SDK, which provides programmatic access to Gemini models. This SDK is essential for interacting with the Gemini API and can be used for both developer API prototyping and migration to Vertex AI.
```shell
#@title Install Google Gen AI SDK
!pip install -U -q google-genai
```
--------------------------------
### Install Google Generative AI Library in Python
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/examples/Upload_files_to_Colab.zh.ipynb
This command installs the 'google-generativeai' library, which is required for interacting with the Gemini API. The '-U' flag ensures that the library is upgraded to the latest version if already installed. The '-q' flag suppresses verbose output during installation.
```python
!pip install -U -q google-generativeai
```
--------------------------------
### Build Model and Chat Session with Function Calling (Python)
Source: https://context7.com/doggy8088/gemini-api-cookbook/llms.txt
This snippet demonstrates how to initialize a Gemini AI model with specific tools (functions) and start a chat session. It enables automatic function calling, allowing the model to interact with predefined functions based on user input. The code then enters a loop to send user messages to the model and print its responses, simulating a conversational agent.
```python
import google.generativeai as genai
# Assuming add_to_order, get_order, clear_order, confirm_order, place_order are defined elsewhere
ordering_system = [add_to_order, get_order, clear_order, confirm_order, place_order]
# Assuming COFFEE_BOT_PROMPT is defined elsewhere
model = genai.GenerativeModel(
'gemini-1.5-pro-latest',
tools=ordering_system,
system_instruction=COFFEE_BOT_PROMPT
)
convo = model.start_chat(enable_automatic_function_calling=True)
placed_order = False
while not placed_order:
user_input = input('> ')
response = convo.send_message(user_input)
print(response.text)
# Logic to check if order is placed and update placed_order flag
print(f'Your order: {placed_order}')
print('- Thanks for using Barista Bot!')
```
--------------------------------
### Install Google Gen AI SDK
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/search_tool.ipynb
Installs the `google-genai` Python SDK, which provides programmatic access to Gemini models. This is a prerequisite for interacting with the Gemini API.
```python
!pip install -U -q google-genai
```
--------------------------------
### Install Google Generative AI Library
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/examples/Market_a_Jet_Backpack.zh.ipynb
Installs the google-generativeai library, which is necessary for interacting with the Gemini API. This command is typically run in a Colab environment.
```bash
!pip install -q -U google-generativeai
```
--------------------------------
### Configure Logging
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/websockets/live_api_starter.ipynb
Initializes a logger for monitoring WebSocket communication. Logging level can be adjusted for debugging purposes.
```python
import logging
logger = logging.getLogger('Bidi')
#logger.setLevel('DEBUG')
```
--------------------------------
### Get Gemini Model Information and Token Limits
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Counting_Tokens.zh.ipynb
Retrieves model information including input and output token limits for the specified Gemini model. Used to understand model capabilities and context window size. The example shows gemini-1.0-pro-latest with 30720 input and 2048 output tokens.
```python
model_info = genai.get_model('models/gemini-1.0-pro-latest')
(model_info.input_token_limit, model_info.output_token_limit)
```
--------------------------------
### Live Audio Streaming with Gemini 2.0 (Python)
Source: https://context7.com/doggy8088/gemini-api-cookbook/llms.txt
This example demonstrates live audio streaming with Gemini 2.0 using WebSockets. It handles sending and receiving audio data, and printing model responses. Requires `asyncio` and `pyaudio` libraries.
```python
import asyncio
import pyaudio
from google import genai
# 音訊配置
FORMAT = pyaudio.paInt16
CHANNELS = 1
SEND_SAMPLE_RATE = 16000
RECEIVE_SAMPLE_RATE = 24000
CHUNK_SIZE = 512
MODEL = "models/gemini-2.0-flash-exp"
client = genai.Client(http_options={'api_version': 'v1alpha'})
CONFIG = {
"generation_config": {
"response_modalities": ["AUDIO"]
}
}
class AudioLoop:
def __init__(self):
self.audio_in_queue = asyncio.Queue()
self.audio_out_queue = asyncio.Queue()
self.session = None
async def send_audio(self):
while True:
chunk = await self.audio_out_queue.get()
await self.session.send({"data": chunk, "mime_type": "audio/pcm"})
async def receive_audio(self):
while True:
async for response in self.session.receive():
server_content = response.server_content
if server_content is not None:
model_turn = server_content.model_turn
if model_turn is not None:
parts = model_turn.parts
for part in parts:
if part.text is not None:
print(part.text, end="")
elif part.inline_data is not None:
self.audio_in_queue.put_nowait(part.inline_data.data)
async def run(self):
async with client.aio.live.connect(model=MODEL, config=CONFIG) as session:
self.session = session
async with asyncio.TaskGroup() as tg:
tg.create_task(self.send_audio())
tg.create_task(self.receive_audio())
# 其他任務:listen_audio, play_audio, send_frames 等
if __name__ == "__main__":
main = AudioLoop()
asyncio.run(main.run())
```
--------------------------------
### Generate Content from Video
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Video.zh.ipynb
Creates a prompt and uses the Gemini 1.5 Pro model to generate content based on the uploaded video.
```python
# Create the prompt.
prompt = "Describe this video."
# Set the model to Gemini 1.5 Pro.
model = genai.GenerativeModel(model_name="models/gemini-1.5-pro-latest")
```
--------------------------------
### Download Video File
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Video.zh.ipynb
Downloads the Big Buck Bunny video file from a specified URL for processing with the Gemini API.
```python
!wget https://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4
```
--------------------------------
### Initialize and Query Text Model Info
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Gemini_Flash_Introduction.zh.ipynb
Initializes a GenerativeModel instance and retrieves model information including token limits. Takes model version string as input (e.g., 'models/gemini-1.5-flash-latest'). Outputs printable model details; relies on API connectivity.
```python
version = 'models/gemini-1.5-flash-latest' # @param ["models/gemini-1.5-flash-latest", "models/gemini-1.5-pro-latest", "models/gemini-1.0-pro-latest"]
model = genai.GenerativeModel(version)
model_info = genai.get_model(version)
print(f'{version} - input limit: {model_info.input_token_limit}, output limit: {model_info.output_token_limit}')
```
--------------------------------
### Set Up API Key
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/live_api_starter.ipynb
Sets up the Google API key by retrieving it from Colab secrets and storing it in an environment variable. This is crucial for authenticating with the Gemini API. Ensure the 'GOOGLE_API_KEY' secret is correctly configured in your Colab environment.
```python
from google.colab import userdata
import os
os.environ['GOOGLE_API_KEY'] = userdata.get('GOOGLE_API_KEY')
```
--------------------------------
### Multimodal Generation with Gemini
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Gemini_Flash_Introduction.zh.ipynb
Loads an image, counts its tokens, and generates a described response combining text and image. Requires PIL for image handling and internet for downloading. Outputs image display, token info, and multimodal response; supports vision-enabled models.
```python
import PIL
from IPython.display import display, Image
!curl -s -o image.jpg "https://storage.googleapis.com/generativeai-downloads/images/croissant.jpg"
img = PIL.Image.open('image.jpg')
display(Image('image.jpg', width=300))
print(img.size)
print(model.count_tokens(img))
prompt = """
Describe this image, including which country is famous for having this food and what is the best pairing for it.
"""
response = model.generate_content([prompt, img])
print(response.text)
```
--------------------------------
### Download Image for Prompting
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Prompting.zh.ipynb
Downloads an image file from a specified URL to the local environment. This image will be used as input for a multimodal prompt. The code uses `curl` to download the image, and the output is the download progress and confirmation.
```bash
!curl -o image.jpg "https://storage.googleapis.com/generativeai-downloads/images/jetpack.jpg"
```
--------------------------------
### Upload File to Gemini File API (Python)
Source: https://context7.com/doggy8088/gemini-api-cookbook/llms.txt
This example demonstrates how to upload a file to the Gemini File API using the Python client library. It covers file upload, retrieval, and deletion. The example requires the `genai` library to be installed.
```python
file_path = "sample_data/gemini_logo.png"
display_name = "Gemini Logo"
file_response = genai.upload_file(path=file_path, display_name=display_name)
print(f"Uploaded file {file_response.display_name} as: {file_response.uri}")
# 驗證檔案已上傳
get_file = genai.get_file(name=file_response.name)
print(f"Retrieved file {get_file.display_name} as: {get_file.uri}")
# 使用上傳的檔案進行內容生成
prompt = "Describe the image with a creative description"
model_name = "models/gemini-1.5-pro-latest"
model = genai.GenerativeModel(model_name=model_name)
response = model.generate_content([prompt, file_response])
print(response.text)
# 刪除檔案
genai.delete_file(name=file_response.name)
print(f'Deleted file {file_response.display_name}')
```
--------------------------------
### Python: Interaction with Gemini API using Tools
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/websockets/live_api_tool_use.ipynb
Demonstrates using the Gemini API with tool capabilities. Sends a 'Turn on the lights' prompt and provides the model with tool definitions for light control.
```python
prompt = "Turn on the lights"
tools = [
{'function_declarations': [turn_on_the_lights_schema, turn_off_the_lights_schema]}
]
await run(prompt, tools=tools)
```
--------------------------------
### Few-shot prompting example in Python
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/examples/Search_Wikipedia_using_ReAct.ipynb
This Python code snippet demonstrates a few-shot prompt structure for the Gemini API. It includes example question-thought-action-observation sequences to guide the model's reasoning process for complex queries.
```python
examples = """
Here are some examples.
Question
What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?
Thought 1
I need to search Colorado orogeny, find the area that the eastern sector of the Colorado orogeny extends into, then find the elevation range of the area.
Action 1
Colorado orogeny
Observation 1
The Colorado orogeny was an episode of mountain building (an orogeny) in Colorado and surrounding areas.
Thought 2
It does not mention the eastern sector. So I need to look up eastern sector.
Action 2
eastern sector
Observation 2
The eastern sector extends into the High Plains and is called the Central Plains orogeny.
Thought 3
The eastern sector of Colorado orogeny extends into the High Plains. So I need to search High Plains and find its elevation range.
Action 3
High Plains
Observation 3
High Plains refers to one of two distinct land regions
Thought 4
I need to instead search High Plains (United States).
Action 4
High Plains (United States)
Observation 4
The High Plains are a subregion of the Great Plains. From east to west, the High Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130m).
Thought 5
High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.
Action 5
1,800 to 7,000 ft
Question
Musician and satirist Allie Goertz wrote a song about the "The Simpsons" character Milhouse, who Matt Groening named after who?
Thought 1
The question simplifies to "The Simpsons" character Milhouse is named after who. I only need to search Milhouse and find who it is named after.
Action 1
Milhouse
Observation 1
Milhouse Mussolini Van Houten is a recurring character in the Fox animated television series The Simpsons voiced by Pamela Hayden and created by Matt Groening.
Thought 2
The paragraph does not tell who Milhouse is named after, maybe I can look up "named after".
Action 2
named after
Observation 2
Milhouse was named after U.S. president Richard Nixon, whose middle name was Milhous.
Thought 3
Milhouse was named after U.S. president Richard Nixon, so the answer is Richard Nixon.
Action 3
Richard Nixon
Question
Which documentary is about Finnish rock groups, Adam Clayton Powell or The Saimaa Gesture?
Thought 1
I need to search Adam Clayton Powell and The Saimaa Gesture, and find which documentary is about Finnish rock groups.
Action 1
Adam Clayton Powell
Observation 1
Could not find [Adam Clayton Powell]. Similar: [’Adam Clayton Powell III’, ’Seventh Avenue (Manhattan)’, ’Adam Clayton Powell Jr. State Office Building’, ’Isabel Washington Powell’, ’Adam Powell’, ’Adam Clayton Powell (film)’, ’Giancarlo Esposito’].
Thought 2
To find the documentary, I can search Adam Clayton Powell (film).
Action 2
Adam Clayton Powell (film)
Observation 2
Adam Clayton Powell is a 1989 American documentary film directed by Richard Kilberg. The film is about the rise and fall of influential African-American politician Adam Clayton Powell Jr.[3][4] It was later aired as part of the PBS series The American Experience.
Thought 3
Adam Clayton Powell (film) is a documentary about an African-American politician, not Finnish rock groups. So the documentary about Finnish rock groups must instead be The Saimaa Gesture.
Action 3
The Saimaa Gesture
Question
What profession does Nicholas Ray and Elia Kazan have in common?
Thought 1
I need to search Nicholas Ray and Elia Kazan, find their professions, then find the profession they have in common.
Action 1
Nicholas Ray
Observation 1
Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16, 1979) was an American film director, screenwriter, and actor best known for the 1955 film Rebel Without a Cause.
Thought 2
Professions of Nicholas Ray are director, screenwriter, and actor. I need to search Elia Kazan next and find his professions.
Action 2
Elia Kazan
Observation 2
Elia Kazan was an American film and theatre director, producer, screenwriter and actor.
Thought 3
Professions of Elia Kazan are director, producer, screenwriter, and actor. So profession Nicholas Ray and Elia Kazan have in common is director, screenwriter, and actor.
Action 3
director, screenwriter, actor
Question
Which magazine was started first Arthur’s Magazine or First for Women?
Thought 1
```
--------------------------------
### Generate Batch Text Embeddings in Python
Source: https://context7.com/doggy8088/gemini-api-cookbook/llms.txt
This example demonstrates how to efficiently generate embedding vectors for multiple text inputs simultaneously. It uses the `embed_content` function with a list of strings to get batch embeddings. The output shows the first 50 characters of each embedding vector. Dependencies: google.generativeai.
```python
import google.generativeai as genai
# 批次生成嵌入向量
result = genai.embed_content(
model="models/text-embedding-004",
content=[
'What is the meaning of life?',
'How much wood would a woodchuck chuck?',
'How does the brain work?'
])
for embedding in result['embedding']:
print(str(embedding)[:50], '... TRIMMED]')
# 輸出:
# [-0.010632277, 0.019375855, 0.0209652, 0.000770642 ... TRIMMED]
# [0.018467998, 0.0054281196, -0.017658804, 0.013859 ... TRIMMED]
# [0.05808907, 0.020941721, -0.108728774, -0.0403925 ... TRIMMED]
```
--------------------------------
### Import Necessary Libraries
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/live_api_starter.ipynb
Imports essential Python libraries for the Gemini API cookbook, including modules for asynchronous operations, base64 encoding, date/time, file operations, JSON handling, audio processing (wave), and display utilities for Colab. These libraries support the multimodal functionalities.
```python
import asyncio
import base64
import contextlib
import datetime
import os
import json
import wave
import itertools
from IPython.display import display, Audio
from google import genai
from google.genai import types
```
--------------------------------
### Gemini File API - Upload, Get, Generate, and Delete
Source: https://context7.com/doggy8088/gemini-api-cookbook/llms.txt
Demonstrates how to upload a file to the Gemini File API, retrieve it, use it for content generation, and then delete it.
```APIDOC
## File Operations with Gemini File API
### Description
This section covers the lifecycle of a file within the Gemini File API: uploading, retrieving, using for content generation, and finally deleting.
### Upload File
```python
file_path = "sample_data/gemini_logo.png"
display_name = "Gemini Logo"
file_response = genai.upload_file(path=file_path, display_name=display_name)
print(f"Uploaded file {file_response.display_name} as: {file_response.uri}")
```
### Get File
```python
get_file = genai.get_file(name=file_response.name)
print(f"Retrieved file {get_file.display_name} as: {get_file.uri}")
```
### Generate Content with File
```python
prompt = "Describe the image with a creative description"
model_name = "models/gemini-1.5-pro-latest"
model = genai.GenerativeModel(model_name=model_name)
response = model.generate_content([prompt, file_response])
print(response.text)
```
### Delete File
```python
genai.delete_file(name=file_response.name)
print(f'Deleted file {file_response.display_name}')
```
```
--------------------------------
### Initialize SDK Client
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/live_api_starter.ipynb
Initializes the Google Gen AI client, specifying the API version as 'v1alpha' for live API access. This client object is used to interact with the Gemini API services. The API key is automatically fetched from the environment variables.
```python
from google import genai
client = genai.Client(http_options= {'api_version': 'v1alpha'})
```
--------------------------------
### Get File Metadata (Python)
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/File_API.zh.ipynb
This snippet retrieves the metadata of the uploaded file using `genai.get_file`, verifying that the file was successfully received by the File API. It prints the display name and URI of the retrieved file.
```python
file = genai.get_file(name=sample_file.name)
print(f"Retrieved file '{file.display_name}' as: {sample_file.uri}")
```
--------------------------------
### Single Round Query with Search and Code Tools
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/plotting_and_mapping.ipynb
This snippet demonstrates a basic asynchronous connection to the Gemini API using quick_connect context manager with google_search and code_execution tools. It sends a prompt to retrieve movie information and handles the response in AUDIO modality. Dependencies include logging setup; inputs are tool configurations and prompt strings; outputs audio playback; limited to single turn without custom function calls.
```python
tools = [
{'google_search': {}},
{'code_execution': {}},
]
async def go():
async with quick_connect(tools=tools, modality="AUDIO") as ws:
await run(ws, "Please find the last 5 Denis Villeneuve movies and look up their runtimes and the year published.")
logger.setLevel('INFO')
await go()
```
--------------------------------
### Send a Message and Get Response in Gemini Chat
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Prompting.zh.ipynb
Sends a message to the active chat session and retrieves the model's text response. The `response.text` attribute contains the generated content. This is a core part of multi-turn conversation.
```python
response = chat.send_message("In one sentence, explain how a computer works to a young child.")
print(response.text)
```
--------------------------------
### Download Sample Audio and PDF Files from Cloud Storage
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/examples/Voice_memos.zh.ipynb
Uses wget to download sample audio and PDF files from Google Cloud Storage for processing with Gemini API. Downloads multiple file types including M4A audio and PDF documents. Input: None (hardcoded URLs). Output: Downloaded files in current directory with progress output.
```shell
!wget https://storage.googleapis.com/generativeai-downloads/data/Walking_thoughts_3.m4a
!wget https://storage.googleapis.com/generativeai-downloads/data/A_Possible_Future_for_Online_Content.pdf
!wget https://storage.googleapis.com/generativeai-downloads/data/Unanswered_Questions_and_Endless_Possibilities.pdf
```
--------------------------------
### Download Sample Images
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/spatial_understanding_3d.ipynb
Downloads sample images for demonstrating spatial understanding capabilities. These images are used in subsequent code examples to test Gemini's ability to identify objects and understand spatial relationships.
```shell
#@title Load sample images
!wget https://storage.googleapis.com/generativeai-downloads/images/kitchen.jpg -O kitchen.jpg -q
!wget https://storage.googleapis.com/generativeai-downloads/images/room-clock.jpg -O room.jpg -q
!wget https://storage.googleapis.com/generativeai-downloads/images/spill.jpg -O spill.jpg -q
!wget https://storage.googleapis.com/generativeai-downloads/images/tool.png -O tool.png -q
```
--------------------------------
### List Available Gemini Models with OAuth
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Authentication_with_OAuth.zh.ipynb
Lists all available base models from Gemini API using OAuth default credentials. Automatically discovers and uses application default credentials without requiring manual API key configuration. Requires properly set up OAuth credentials. Outputs model names as strings.
```python
import google.generativeai as genai
print('Available base models:', [m.name for m in genai.list_models()])
```
--------------------------------
### Get Detailed Metadata for Specific Gemini Models
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Models.zh.ipynb
Fetch comprehensive model information including token limits and capabilities. Demonstrates two approaches: iterating through model list to find a specific model, and directly retrieving model details using get_model().
```python
for m in genai.list_models():
if m.name == "models/gemini-1.5-pro-latest":
print(m)
```
```python
model_info = genai.get_model("models/aqa")
print(model_info)
```
--------------------------------
### List Gemini Models with Content Generation Support
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Models.zh.ipynb
Enumerates available Gemini models that support the generateContent method for text and multimodal generation. Filters models based on supported generation methods.
```python
for m in genai.list_models():
if "generateContent" in m.supported_generation_methods:
print(m.name)
```
--------------------------------
### Generate Content with Stop Sequences in Gemini API
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Prompting.zh.ipynb
Generates content using the `generate_content` method and specifies `stop_sequences` to control when the model should stop generating text. This example limits the output by stopping after a newline followed by '6', effectively limiting the list to 5 items.
```python
response = model.generate_content(
'Give me a numbered list of cat facts.',
# Limit to 5 facts.
generation_config = genai.GenerationConfig(stop_sequences=['\n6'])
)
print(response.text)
```
--------------------------------
### Stream Responses with Gemini API Python SDK
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Streaming.zh.ipynb
Demonstrates how to implement streaming responses using the Gemini API Python SDK. Includes SDK installation, configuration, and streaming response handling. Note that streaming may not work properly in Google Colab due to implementation details.
```Python
# @title 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.
```
```Bash
!pip install -U -q google-generativeai
```
```Python
import google.generativeai as genai
```
```Python
import os
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
```
```Python
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Write a cute story about cats.", stream=True)
for chunk in response:
print(chunk.text)
print("_"*80)
```
--------------------------------
### Python: Combine Google Search and Custom Functions with Gemini API
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/websockets/live_api_starter.ipynb
This example integrates both Google Search and custom function capabilities within the Gemini API. It configures an AudioLoop to utilize both 'google_search' and a set of function declarations. This allows the API to perform web searches and execute user-defined functions.
```python
tools = [
{'google_search': {}},
{'function_declarations': [{'name': 'turn_on_the_lights', 'description': None}, {'name': 'turn_off_the_lights', 'description': None}]}
]
await AudioLoop(tools=tools).run()
```
--------------------------------
### Generate Single Text Embedding in Python
Source: https://context7.com/doggy8088/gemini-api-cookbook/llms.txt
This code snippet shows how to generate an embedding vector for a single piece of text using the Gemini API. It configures the API key, specifies the embedding model, and sends the text content to get its vector representation. The output includes the embedding vector and its dimensionality. Dependencies: google.generativeai.
```python
import google.generativeai as genai
genai.configure(api_key=GOOGLE_API_KEY)
# 生成單個文本的嵌入向量
text = "Hello world"
result = genai.embed_content(model="models/text-embedding-004", content=text)
print(str(result['embedding'])[:50], '... TRIMMED]')
# 輸出: [0.013168523, -0.008711934, -0.046782676, 0.000699 ... TRIMMED]
print(len(result['embedding'])) # 嵌入向量有 768 維
# 輸出: 768
```
--------------------------------
### Text Generation with Gemini Model
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Gemini_Flash_Introduction.zh.ipynb
Counts tokens for a prompt and generates text response using the Gemini API. Inputs a string prompt (e.g., 'What is artificial intelligence?'). Outputs token count and generated text; limited by model token limits and API quotas.
```python
prompt = "What is artificial intelligence?"
model.count_tokens(prompt)
response = model.generate_content(prompt)
print(response.text)
```
--------------------------------
### Setup ChromaDB Database Instance in Python
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/examples/vectordb_with_chroma.zh.ipynb
Instantiates a ChromaDB collection with documents related to Google cars, using the create function. This step initializes the database for later querying. Requires pre-defined documents list and collection name; outputs a ChromaDB collection ready for queries.
```python
# Set up the DB
db = create_chroma_db(documents, "googlecarsdatabase")
```
--------------------------------
### Initializing GenAI Client in Python
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/spatial_understanding.ipynb
Imports necessary modules and creates a GenAI client instance using the API key. Depends on the google-genai SDK; uses the new SDK for Gemini access. The client enables content generation calls; limitations include API key validity and rate limits.
```python
from google import genai
from google.genai import types
client = genai.Client(api_key=GOOGLE_API_KEY)
```
--------------------------------
### Call Gemini API with Previous Function Call (Bash/cURL)
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/rest/Function_calling_REST.zh.ipynb
This example uses cURL to send a request to the Gemini API, incorporating a function call previously generated by the model. It reconstructs a conversation history including user input, the model's function call, and a simulated function response to guide the model's next action. The JSON payload includes tool definitions for available functions.
```bash
%%bash
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"contents": [{"role": "user", "parts": [{"text": "Which theaters in Mountain View show Barbie movie?"}]}, { "role": "model", "parts": [{ "functionCall": { "name": "find_theaters", "args": { "location": "Mountain View, CA", "movie": "Barbie" } } }] }, { "role": "function", "parts": [{ "functionResponse": { "name": "find_theaters", "response": { "name": "find_theaters", "content": { "movie": "Barbie", "theaters": [{ "name": "AMC Mountain View 16", "address": "2000 W El Camino Real, Mountain View, CA 94040" }, { "name": "Regal Edwards 14", "address": "245 Castro St, Mountain View, CA 94040" }] } } } }] }],
"tools": [{"functionDeclarations": [{ "name": "find_movies", "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "description": { "type": "STRING", "description": "Any kind of description including category or genre, title words, attributes, etc." } }, "required": ["description"] } }, { "name": "find_theaters", "description": "find theaters based on location and optionally movie title which are is currently playing in theaters", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "STRING", "description": "Any movie title" } }, "required": ["location"] } }, { "name": "get_showtimes", "description": "Find the start times for movies playing in a specific theater", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "STRING", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" }, "movie": { "type": "STRING", "description": "Any movie title" }, "theater": { "type": "STRING", "description": "Name of the theater" }, "date": { "type": "STRING", "description": "Date for requested showtime" } }, "required": ["location", "movie", "theater", "date"] } } }]}]
}' 2> /dev/null
```
--------------------------------
### Install Google Generative AI Library (Python)
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/examples/Search_Wikipedia_using_ReAct.zh.ipynb
This snippet demonstrates how to install the Google Generative AI library using pip. This library is a prerequisite for interacting with the Gemini API. Ensure you have pip installed prior to running this code.
```Python
!pip install -q google-generativeai
```
--------------------------------
### Generate HTML with Inline CSS
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/System_instructions.zh.ipynb
Generates HTML code with inline CSS based on a text prompt. The system instruction defines the model's role as a coding expert specializing in front-end interfaces, and the prompt describes the desired layout.
```python
# @title Generate HTML with Inline CSS
instruction = (
"You are a coding expert that specializes in front end interfaces. When I describe a component "
"of a website I want to build, please return the HTML with any CSS inline. Do not give an "
"explanation for this code."
)
model = genai.GenerativeModel(
"models/gemini-1.5-pro-latest", system_instruction=instruction
)
prompt = (
"A flexbox with a large text logo aligned left and a list of links aligned right."
)
response = model.generate_content(prompt)
print(response.text)
```
--------------------------------
### Set Up API Key from Colab Secrets
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/websockets/live_api_starter.ipynb
Retrieves the Google API key stored in Colab secrets and sets it as an environment variable for authentication with the Gemini Live API.
```python
from google.colab import userdata
os.environ['GOOGLE_API_KEY'] = userdata.get('GOOGLE_API_KEY')
```
--------------------------------
### Select Gemini Model
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/gemini-2/live_api_starter.ipynb
Defines the model to be used for the Gemini 2.0 multimodal live API. Currently, 'gemini-2.0-flash-exp' is specified, which is part of the Gemini 2.0 family. This model is required for utilizing the new multimodal features.
```python
MODEL = "gemini-2.0-flash-exp"
```
--------------------------------
### Install Wikipedia Library
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/examples/Search_reranking_using_embeddings.zh.ipynb
Installs the wikipedia library, which is used to fetch content from Wikipedia. Note that this library is not recommended for production or large-scale usage.
```python
!pip install -q wikipedia
```
--------------------------------
### Load Image using PIL
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/quickstarts/Prompting.zh.ipynb
Loads the downloaded image file ('image.jpg') into a PIL (Pillow) Image object. This object can then be used as input for multimodal models. The output is a representation of the loaded image object.
```python
import PIL.Image
img = PIL.Image.open('image.jpg')
img
```
--------------------------------
### Install Google GenerativeAI Package
Source: https://github.com/doggy8088/gemini-api-cookbook/blob/zh-tw/examples/Agents_Function_Calling_Barista_Bot.zh.ipynb
Installs the Google GenerativeAI package required for interacting with the Gemini API. This is a prerequisite for running the notebook.
```python
!pip install -qU google-generativeai
```