googleapis/python-genai

Google Gen AI Python SDK provides an interface for developers to integrate Google's generative models into their Python applications.

Google Gen AI Python SDK

簡介 – 這是一個官方提供的 Python 用戶端函式庫,讓您能夠呼叫 Google 的 Gemini 生成式 AI 模型(包括公開的 Gemini Developer API 與 Gemini Enterprise Agent Platform)。它將 REST 端點封裝在便捷且具備型別的介面中,並處理身分驗證、HTTP 傳輸與回應解析。

核心功能

  • 使用 API 金鑰(公開版 Gemini)或企業憑證(專案、位置)建立 genai.Client
  • 呼叫 client.models.generate_contentgemini-3.5-flashgemini-2.5-flash 等 Gemini 模型獲取文字、圖像或影片。
  • 提供純字串、字典或更豐富的 google.genai.types Pydantic 模型(例如 Part.from_text, Part.from_uri, Part.from_function_call)作為輸入。
  • 使用 GenerateContentConfig 配置生成參數(temperature、top-p/k、max tokens、安全設定、系統指令、回應模態等)。
  • 使用自動函式呼叫:將 Python 可呼叫物件作為工具傳遞,模型即可呼叫它們;或者停用此功能以接收顯式的函式呼叫物件。
  • 支援串流、非同步呼叫(透過 client.aio)以及快速的 aiohttp 傳輸選項。
  • 內建對代理、自訂基準 URL 與 API 版本選擇(v1, v1alpha)的處理。
  • 支援上下文管理器以實現自動資源清理,並提供顯式的 close()/aclose() 方法。
  • 用於列出模型、上傳檔案與設定安全設定的輔助工具。

典型用法

from google import genai
from google.genai import types

client = genai.Client(api_key="YOUR_GEMINI_API_KEY")
response = client.models.generate_content(
    model="gemini-3.5-flash",
    contents=types.Part.from_text("Why is the sky blue?"),
    config=types.GenerateContentConfig(temperature=0, top_p=0.95),
)
print(response.text)

重要意義 – 它抽象掉了 Google Gemini API 的底層 HTTP 細節,提供了型別安全的請求建構,並整合了函式呼叫與安全控制等進階功能,讓 Python 開發者能更輕鬆地將最先進的生成式 AI 嵌入到應用程式、服務或研究筆記本中。

文件與安裝

相關

  • 專案
  • 專案
  • 專案
  • 專案
  • 專案