Gemini Batch API utility
Project description
Gemini Batch API Utility
gemini-batch-api は、GoogleのGemini Batch APIを簡単に利用するためのユーティリティライブラリです。
大量のリクエストを効率的に一括処理できます。
特徴
- 簡単なリクエスト定義: テキスト、ローカルファイル、アップロードファイルの指定が容易です。
- 構造化出力のサポート: Pydanticモデルを使用して、JSON形式の結果を直接取得できます。
- 一括モニタリング: バッチジョブの完了を待機し、結果をリストとして取得します。
使い方の例
from pathlib import Path
from gemini_batch_api import Request, create_batch, get_inlined_responses
import os
# 1. クライアントを初期化
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
# 2. リクエストのリストを作成
requests = [
Request(query="富士山について教えてください。"),
Request(query="画像の内容を説明してください。", file=Path("sample.png")),
]
# 3. バッチジョブを作成
batch_name = create_batch(
client=client,
model="gemini-flash-lite-latest",
requests=requests,
display_name="my-first-batch"
)
print(f"Batch job created: {batch_name}")
# 4. 結果の取得(完了するまでポーリングし、結果をリストで返します)
responses = get_inlined_responses(client, batch_name)
if responses:
for res in responses:
if res.response:
print(res.response.text)
上記をmain.pyに保存して、以下のコマンドで実行できます(結果取得まで時間がかかることがあります)。
GEMINI_API_KEY="AI..." uv run --with gemini-batch-api main.py
構造化出力 (Structured Output)
Pydanticモデルを使用して、出力を構造化できます。
from pydantic import BaseModel, Field
class ItemInfo(BaseModel):
name: str = Field(description="商品名")
price: int = Field(description="価格")
requests = [
Request(
query="このレシートの内容を抽出して",
file=Path("receipt.jpg"),
schema=ItemInfo # Pydanticモデルを指定
),
]
# あとは同様に create_batch / get_inlined_responses を実行します
ファイルアップロード
ファイルをアップロードする場合は、Request.uploadにTrueを指定してください。
アップロードするファイル名がASCIIでない場合は、Request.upload_file_nameにASCIIのファイル名を指定してください。
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gemini_batch_api-0.1.3-py3-none-any.whl.
File metadata
- Download URL: gemini_batch_api-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4b78596c34f5834fc93e52d11fb6f3cc00fd90b4ed6e53a2244d2f678f51575
|
|
| MD5 |
4b26b3954127b199a13899ff183434be
|
|
| BLAKE2b-256 |
8a5430722188bb282e8eab8687f58f71f2bb8691e03d8163ad24909bbeaa524d
|