gemini-api

gemini-api

热门

当用户询问企业级环境下的 Gemini 使用,或明确提到 Vertex AI、Google Cloud 或 Agent Platform 时使用。本 Skill 指导如何在 Agent Platform 上通过 Google Gen AI SDK 使用 Gemini API,涵盖 SDK 的用法(Python、JS/TS、Go、Java、C#),以及多模态输入、工具调用、媒体生成、上下文缓存、批量预测和 Live API 等核心能力。

1.5万Star
1201Fork
更新于 2026/7/29
SKILL.md
只读
名称
gemini-api
描述

当用户询问企业级环境下的 Gemini 使用,或明确提到 Vertex AI、Google Cloud 或 Agent Platform 时使用。本 Skill 指导如何在 Agent Platform 上通过 Google Gen AI SDK 使用 Gemini API,涵盖 SDK 的用法(Python、JS/TS、Go、Java、C#),以及多模态输入、工具调用、媒体生成、上下文缓存、批量预测和 Live API 等核心能力。

IMPORTANT: Agent Platform(全称 Gemini Enterprise Agent Platform)先前名为“Vertex AI”,目前网络上许多资源仍沿用旧版命名。

在 Agent Platform 中使用 Gemini API

借助 Agent Platform 中的 Gemini API,轻松使用 Google 专为企业级场景打造的顶尖 AI 模型。

主要提供以下核心能力:

  • 文本生成 - 对话、补全、文本摘要
  • 多模态理解 - 处理图像、音频、视频及各类文档
  • 函数调用(Function calling) - 允许模型调用自定义函数
  • 结构化输出 - 生成符合指定 Schema 的合法 JSON
  • 上下文缓存(Context caching) - 缓存超长上下文,大幅提升效率
  • 向量嵌入(Embeddings) - 生成文本向量以支持语义搜索
  • Live 实时 API - 支持双向流式传输,实现低延迟语音与视频交互
  • 批量预测(Batch Prediction) - 处理海量异步数据集预测任务

核心原则

  • 统一 SDK:务必使用最新的 Gen AI SDK(Python: google-genai,JS/TS: @google/genai,Go: google.golang.org/genai,Java: com.google.genai:google-genai,C#: Google.GenAI)。
  • 旧版 SDK:严禁使用 google-cloud-aiplatform@google-cloud/vertexaigoogle-generativeai

SDK 安装

  • Python:通过 pip install google-genai 安装
  • JavaScript/TypeScript:通过 npm install @google/genai 安装
  • Go:通过 go get google.golang.org/genai 安装
  • C#/.NET:通过 dotnet add package Google.GenAI 安装
  • Java
    • groupId: com.google.genai, artifactId: google-genai

    • 最新版本可在 Maven Central 查询:https://central.sonatype.com/artifact/com.google.genai/google-genai/versions(此处记为 LAST_VERSION

    • build.gradle 中引入:

      implementation("com.google.genai:google-genai:${LAST_VERSION}")
      
    • pom.xml 中引入 Maven 依赖:

      <dependency>
          <groupId>com.google.genai</groupId>
          <artifactId>google-genai</artifactId>
          <version>${LAST_VERSION}</version>
      </dependency>
      

[!WARNING]
旧版 SDK(如 google-cloud-aiplatform@google-cloud/vertexaigoogle-generativeai)已被弃用。请务必参考 迁移指南 尽快迁移至上方最新 SDK。

身份认证与配置

创建 Client 时,优先使用环境变量,避免硬编码参数。不带参数初始化 Client 即可自动读取环境变量配置。

应用默认凭据 (ADC)

配置以下环境变量以完成标准 Google Cloud 身份认证

export GOOGLE_CLOUD_PROJECT='your-project-id'
export GOOGLE_CLOUD_LOCATION='global'
export GOOGLE_GENAI_USE_ENTERPRISE=true
  • 默认情况下使用 location="global" 来访问全局端点,该端点会自动将请求路由至具备可用容量的区域。
  • 如果用户明确指定了特定区域(如 us-central1europe-west4),请在 GOOGLE_CLOUD_LOCATION 中填入对应区域。如有需要,可查阅 支持的区域文档

Agent Platform 快捷模式(Express Mode)

当使用 API Key 运行 Express Mode 时,配置以下环境变量:

export GOOGLE_API_KEY='your-api-key'
export GOOGLE_GENAI_USE_ENTERPRISE=true

初始化

无需任何参数初始化 Client 即可自动读取环境变量:

from google import genai

client = genai.Client()

或者,你也可以在创建 Client 时显式传入硬编码参数:

from google import genai

client = genai.Client(
    enterprise=True,
    project="your-project-id",
    location="global",
)

模型选择

  • 复杂推理、代码编写、深度研究场景推荐使用 gemini-3.1-pro-preview(替代旧版 gemini-3-pro-preview,支持 1M tokens)
  • 快速响应、高性价比及通用多模态任务推荐使用 gemini-3.6-flash(支持 1M tokens)
  • 高频轻量级任务推荐使用 gemini-3.5-flash-lite(支持 1M tokens)
  • 高品质图像生成与编辑使用 gemini-3-pro-image(即 Nano Banana Pro)
  • 中等画质图像生成与编辑使用 gemini-3.1-flash-image(即 Nano Banana 2)
  • 快速图像生成与编辑使用 gemini-3.1-flash-lite-image(即 Nano Banana 2 Lite)
  • 包含原生音频的 Live 实时 API 交互使用 gemini-live-2.5-flash-native-audio

仅当用户明确要求时,才使用以下模型:

  • gemini-3.5-flash
  • gemini-3.1-flash-lite
  • gemini-2.5-flash-image
  • gemini-2.5-flash
  • gemini-2.5-flash-lite
  • gemini-2.5-pro

[!IMPORTANT]
gemini-2.0-*gemini-1.5-*gemini-1.0-*gemini-pro 等模型均已废弃并下线。请务必使用上述最新模型,不要依赖旧知识库。
生产环境请参阅官方文档选择稳定版模型(例如 gemini-3.6-flash)。

快速上手

Python

from google import genai

client = genai.Client()
response = client.models.generate_content(
    model="gemini-3.6-flash",
    contents="Explain quantum computing",
)
print(response.text)

TypeScript/JavaScript

import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ enterprise: { project: "your-project-id", location: "global" } });
const response = await ai.models.generateContent({
    model: "gemini-3.6-flash",
    contents: "Explain quantum computing"
});
console.log(response.text);

Go

package main

import (
	"context"
	"fmt"
	"log"
	"google.golang.org/genai"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, &genai.ClientConfig{
		Backend:  genai.BackendVertexAI,
		Project:  "your-project-id",
		Location: "global",
	})
	if err != nil {
		log.Fatal(err)
	}

	resp, err := client.Models.GenerateContent(ctx, "gemini-3.6-flash", genai.Text("Explain quantum computing"), nil)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(resp.Text)
}

Java

import com.google.genai.Client;
import com.google.genai.types.GenerateContentResponse;

public class GenerateTextFromTextInput {
  public static void main(String[] args) {
    Client client = Client.builder().enterprise(true).project("your-project-id").location("global").build();
    GenerateContentResponse response =
        client.models.generateContent(
            "gemini-3.6-flash",
            "Explain quantum computing",
            null);

    System.out.println(response.text());
  }
}

C#/.NET

using Google.GenAI;

var client = new Client(
    project: "your-project-id",
    location: "global",
    enterprise: true
);

var response = await client.Models.GenerateContent(
    "gemini-3.6-flash",
    "Explain quantum computing"
);

Console.WriteLine(response.Text);

API 规范与官方文档(权威来源)

在 Agent Platform 上实现或调试 API 对接时,请参阅官方 Agent Platform 文档:

Agent Platform 上的 Gen AI SDK 使用 v1beta1v1 REST API 端点(例如:https://{LOCATION}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT}/locations/{LOCATION}/publishers/google/models/{MODEL}:generateContent)。

[!TIP]
使用 Developer Knowledge MCP Server:如果当前环境提供了 search_documentsget_document 工具,优先使用它们直接检索 Google Cloud 及 Agent Platform 的最新官方文档。这是获取最新 API 细节和代码范例的首选方式。

工作流与代码示例

参阅 Python Docs Samples 代码库 获取更多代码示例与具体场景实现。

根据用户的具体需求,可查阅以下参考文档了解详细的代码示例与用法规范(基于 Python 范例):