Agent Skills by ALSEL
Google GeminiLLM・AI開発⭐ リポ 60品質スコア 82/100

gemini-api

Google Gemini APIのPythonおよびTypeScriptでの実装パターンです。テキスト生成、ストリーミング、ツール使用(関数呼び出し)、ビジョン機能、システムインストラクション、コンテキストキャッシング、バッチリクエスト、エージェントワークフローに対応しています。Gemini APIまたはGoogle Generative AI SDKを使用したアプリケーション開発時にご利用ください。

description の原文を見る

Google Gemini API patterns for Python and TypeScript. Covers content generation, streaming, tool use (function calling), vision, system instructions, context caching, batch requests, and agent workflows. Use when building applications with the Gemini API or Google Generative AI SDKs.

SKILL.md 本文

Gemini API

Google Gemini APIとSDKを使用してアプリケーションを構築します。

使用する場合

  • Gemini APIを呼び出すアプリケーションを構築する場合
  • コードが google.generativeai(Python)または @google/generative-ai(TypeScript)をインポートする場合
  • ユーザーがGemini APIのパターン、ファンクション呼び出し、ストリーミング、またはビジョンについて質問する場合
  • Gemini APIを使用してエージェントワークフローを実装する場合
  • APIのコスト、トークン使用量、またはレイテンシを最適化する場合

モデル選択

モデルID最適な用途
Pro 2.5gemini-2.5-pro複雑な推論、アーキテクチャ、リサーチ
Flash 2.5gemini-2.5-flashバランスの取れたコーディング、ほとんどの開発タスク
Flash Lite 2.5gemini-2.5-flash-lite高速レスポンス、大量処理、コスト重視

タスクが深い推論を必要としない限り(Pro)、または速度/コスト最適化が必要でない限り(Flash Lite)、Flash 2.5をデフォルトにします。本番環境では、エイリアスではなく、ピン留めされたスナップショットIDを優先します。

Python SDK

インストール

pip install google-generativeai

基本的なメッセージ

import os
import google.generativeai as genai

genai.configure(api_key=os.environ["GEMINI_API_KEY"])

model = genai.GenerativeModel("gemini-2.5-flash")
response = model.generate_content("Explain async/await in Python")
print(response.text)

ストリーミング

model = genai.GenerativeModel("gemini-2.5-flash")
response = model.generate_content(
    "Write a haiku about coding",
    stream=True,
)
for chunk in response:
    print(chunk.text, end="", flush=True)

システムインストラクション

model = genai.GenerativeModel(
    "gemini-2.5-flash",
    system_instruction="You are a senior Python developer. Be concise.",
)
response = model.generate_content("Review this function")

TypeScript SDK

インストール

npm install @google/generative-ai

基本的なメッセージ

import { GoogleGenerativeAI } from "@google/generative-ai";

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });

const result = await model.generateContent("Explain async/await in TypeScript");
console.log(result.response.text());

ストリーミング

const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });

const result = await model.generateContentStream("Write a haiku");

for await (const chunk of result.stream) {
  const text = chunk.text();
  process.stdout.write(text);
}

ファンクション呼び出し(ツール使用)

ツールを定義して、Geminiに呼び出させます:

def get_weather(location: str, unit: str = "celsius") -> dict:
    """Get current weather for a location."""
    # Your implementation here
    return {"temp": 18, "unit": unit, "location": location}

model = genai.GenerativeModel(
    "gemini-2.5-flash",
    tools=[get_weather],
)

chat = model.start_chat(enable_automatic_function_calling=True)
response = chat.send_message("What's the weather in SF?")
print(response.text)

手動でのファンクション呼び出し(より多くの制御)の場合:

import google.generativeai as genai
from google.generativeai.types import FunctionDeclaration, Tool

weather_func = FunctionDeclaration(
    name="get_weather",
    description="Get current weather for a location",
    parameters={
        "type": "object",
        "properties": {
            "location": {"type": "string", "description": "City name"},
            "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
        },
        "required": ["location"],
    },
)

tool = Tool(function_declarations=[weather_func])
model = genai.GenerativeModel("gemini-2.5-flash", tools=[tool])

chat = model.start_chat()
response = chat.send_message("What's the weather in SF?")

# Check for function call in response
for part in response.parts:
    if fn := part.function_call:
        # Execute the function with fn.args
        result = get_weather(**dict(fn.args))
        # Send result back
        response = chat.send_message(
            genai.protos.Content(
                parts=[genai.protos.Part(
                    function_response=genai.protos.FunctionResponse(
                        name=fn.name,
                        response={"result": result},
                    )
                )]
            )
        )

ビジョン

分析用に画像を送信します:

import PIL.Image

image = PIL.Image.open("diagram.png")

model = genai.GenerativeModel("gemini-2.5-flash")
response = model.generate_content(
    ["Describe this diagram", image]
)
print(response.text)

または生バイナリデータから:

with open("diagram.png", "rb") as f:
    image_data = f.read()

response = model.generate_content([
    "Describe this diagram",
    {"mime_type": "image/png", "data": image_data},
])

シンキング(拡張推論)

複雑な推論タスクの場合、生成設定でシンキングを有効にします:

model = genai.GenerativeModel("gemini-2.5-flash")
response = model.generate_content(
    "Solve this math problem step by step...",
    generation_config=genai.GenerationConfig(
        thinking_config=genai.types.ThinkingConfig(
            thinking_budget=10000,
        ),
    ),
)

for part in response.candidates[0].content.parts:
    if part.thought:
        print(f"Thinking: {part.text}")
    else:
        print(f"Answer: {part.text}")

コンテキストキャッシング

大規模なコンテキストをキャッシュして、繰り返されるリクエストのコストを削減します:

from google.generativeai import caching
import datetime

cache = caching.CachedContent.create(
    model="gemini-2.5-flash",
    display_name="my-cached-context",
    system_instruction="You are an expert analyst.",
    contents=[large_context_text],
    ttl=datetime.timedelta(minutes=30),
)

# Use the cached content with a model
model = genai.GenerativeModel.from_cached_content(cache)
response = model.generate_content("Question about the cached context")

バッチリクエスト

複数のリクエストを効率的に処理します:

model = genai.GenerativeModel("gemini-2.5-flash")

# Use asyncio for concurrent requests
import asyncio

async def process_batch(prompts: list[str]) -> list[str]:
    """Process multiple prompts concurrently."""
    async_model = genai.GenerativeModel("gemini-2.5-flash")
    tasks = [
        async_model.generate_content_async(prompt)
        for prompt in prompts
    ]
    responses = await asyncio.gather(*tasks)
    return [r.text for r in responses]

# Run the batch
results = asyncio.run(process_batch(prompts))

エージェントループ

ファンクション呼び出しを使用した複数ステップエージェントを構築します:

import google.generativeai as genai

# Define tools as function declarations
tools = [
    genai.protos.Tool(function_declarations=[
        genai.protos.FunctionDeclaration(
            name="search_codebase",
            description="Search the codebase for relevant code",
            parameters={
                "type": "object",
                "properties": {"query": {"type": "string"}},
                "required": ["query"],
            },
        )
    ])
]

model = genai.GenerativeModel("gemini-2.5-flash", tools=tools)
chat = model.start_chat()

response = chat.send_message("Review the auth module for security issues")

# Agentic loop: keep processing function calls until the model stops
while response.candidates[0].finish_reason.name == "STOP" and any(
    part.function_call for part in response.parts
):
    for part in response.parts:
        if fn := part.function_call:
            result = execute_tool(fn.name, dict(fn.args))
            response = chat.send_message(
                genai.protos.Content(
                    parts=[genai.protos.Part(
                        function_response=genai.protos.FunctionResponse(
                            name=fn.name,
                            response={"result": result},
                        )
                    )]
                )
            )

print(response.text)

コスト最適化

戦略削減効果使用する場合
コンテキストキャッシングキャッシュされたトークンで最大75%繰り返されるシステムプロンプトまたはコンテキスト
非同期バッチ処理変動的時間に縛られない大量処理
Flash Lite(Flash の代わり)約75%シンプルなタスク、分類、抽出
より短い max_output_tokens変動的出力が短くなることがわかっている場合
ストリーミングなし(同じコスト)より良いUX、同じ価格

エラーハンドリング

import time

from google.api_core.exceptions import (
    ResourceExhausted,
    ServiceUnavailable,
    GoogleAPIError,
)

try:
    response = model.generate_content(...)
except ResourceExhausted:
    # Rate limited, back off and retry
    time.sleep(60)
except ServiceUnavailable:
    # Service issue, retry with backoff
    pass
except GoogleAPIError as e:
    print(f"API error: {e.message}")

環境設定

# 必須
export GEMINI_API_KEY="your-api-key-here"

# オプション: デフォルトモデルを設定
export GEMINI_MODEL="gemini-2.5-flash"

APIキーをハードコードしないでください。常に環境変数を使用します。

ライセンス: MIT(寛容ライセンスのため全文を引用しています) · 原本リポジトリ

詳細情報

作者
Jamkris
リポジトリ
Jamkris/everything-gemini-code
ライセンス
MIT
最終更新
2026/5/12

Source: https://github.com/Jamkris/everything-gemini-code / ライセンス: MIT

本サイトは GitHub 上で公開されているオープンソースの SKILL.md ファイルをクロール・インデックス化したものです。 各スキルの著作権は原作者に帰属します。掲載に問題がある場合は info@alsel.co.jp または /takedown フォームよりご連絡ください。
原作者: Jamkris · Jamkris/everything-gemini-code · ライセンス: MIT