使用 Google Gemini API 进行查询

重要

Google Gemini API 仅兼容于按令牌计费的 Gemini 基础模型和外部模型。 对于可跨不同提供方使用的统一 API,请使用 Open Responses API 或 Chat Completions API。

Google Gemini API 提供与 Azure Databricks 上 Gemini 模型的 Google AI SDK 的本地兼容性。 如果需要 Gemini 特定的功能或迁移现有的 Google AI SDK 代码,请使用此 API。

要求

  • 请参阅 要求。
  • 在 google-genai 计算设备上安装该包。

查询示例

Note

以下示例基于 Unity Gateway 和模型服务。 如果使用模型服务终结点而不是模型服务,请将模型服务名称替换为终结点名称。 请参阅 Databricks 基础模型 API 支持的模型详细列表 ,了解可用的基础模型及其模型服务和端点名称。

以下示例展示了如何使用Google Gemini API查询Foundation Model API按代币付费模型服务。

Python

from google import genai
from google.genai import types
import os

DATABRICKS_TOKEN = os.environ.get('DATABRICKS_TOKEN')

client = genai.Client(
    api_key="databricks",
    http_options=types.HttpOptions(
        base_url="https://example.staging.cloud.databricks.com/ai-gateway/gemini",
        headers={
            "Authorization": f"Bearer {DATABRICKS_TOKEN}",
        },
    ),
)

response = client.models.generate_content(
    model="system.ai.gemini-3-1-pro",
    contents=[
        types.Content(
            role="user",
            parts=[types.Part(text="What is a mixture of experts model?")],
        ),
    ],
    config=types.GenerateContentConfig(
        max_output_tokens=256,
    ),
)

print(response.text)

REST API

curl \
-u token:$DATABRICKS_TOKEN \
-X POST \
-H "Content-Type: application/json" \
-d '{
  "contents": [
    {
      "role": "user",
      "parts": [{"text": "What is a mixture of experts model?"}]
    }
  ],
  "generationConfig": {
    "maxOutputTokens": 256
  }
}' \
https://<workspace_host>.databricks.com/ai-gateway/gemini/v1beta/models/system.ai.gemini-3-1-pro:generateContent

支持的模型

Databricks 托管的基础模型

  • system.ai.gemini-4-carbon
  • system.ai.databricks-gemini-4-argon
  • system.ai.gemini-3-5-flash-lite
  • system.ai.gemini-3-8-flash
  • system.ai.gemini-3-7-flash
  • system.ai.gemini-3-6-flash
  • system.ai.gemini-3-1-pro
  • system.ai.gemini-3-1-flash-lite
  • system.ai.gemini-3-flash

外部模型

  • Google 模型提供程序

支持的输入类型

Azure Databricks上的双子座模型接受文本和图像输入。 Pro版本(system.ai.gemini-3-1-pro)和Flash版本(system.ai.gemini-3-5-flash-lite, system.ai.gemini-3-1-flash-lite, system.ai.gemini-3-flash)也支持视频和音频输入。 有关视频和音频请求示例,请参阅 查询音频和视频模型 ,以及查询图像格式和大小要求的 视觉模型 。 关于每个模型的输入类型,请参见 Databricks Foundation Model API支持的详细模型列表。

其他资源