Gemini Developer API so với Vertex AI

Khi phát triển các giải pháp AI tạo sinh bằng Gemini, Google cung cấp 2 sản phẩm API: Gemini Developer APIVertex AI Gemini API.

Gemini Developer API cung cấp cách nhanh nhất để tạo, sản xuất và mở rộng quy mô các ứng dụng dựa trên Gemini. Hầu hết nhà phát triển nên sử dụng Gemini Developer API, trừ phi cần có các chế độ kiểm soát cụ thể cho doanh nghiệp.

Vertex AI cung cấp một hệ sinh thái toàn diện gồm các tính năng và dịch vụ sẵn sàng cho doanh nghiệp để xây dựng và triển khai các ứng dụng AI tạo sinh dựa trên Nền tảng Google Cloud.

Gần đây, chúng tôi đã đơn giản hoá quy trình di chuyển giữa các dịch vụ này. Giờ đây, bạn có thể truy cập cả Gemini Developer API và Vertex AI Gemini API thông qua Google Gen AI SDK hợp nhất.

So sánh mã

Trang này có các đoạn mã so sánh song song giữa Gemini Developer API và hướng dẫn nhanh của Vertex AI để tạo văn bản.

Python

Bạn có thể truy cập cả Gemini Developer API và các dịch vụ Vertex AI thông qua thư viện google-genai. Hãy xem trang thư viện để biết hướng dẫn về cách cài đặt google-genai.

Gemini Developer API

from google import genai

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.0-flash", contents="Explain how AI works in a few words"
)
print(response.text)

Vertex AI Gemini API

from google import genai

client = genai.Client(
    vertexai=True, project='your-project-id', location='us-central1'
)

response = client.models.generate_content(
    model="gemini-2.0-flash", contents="Explain how AI works in a few words"
)
print(response.text)

JavaScript và TypeScript

Bạn có thể truy cập cả Gemini Developer API và các dịch vụ Vertex AI thông qua thư viện @google/genai. Hãy xem trang thư viện để biết hướng dẫn về cách cài đặt @google/genai.

Gemini Developer API

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.0-flash",
    contents: "Explain how AI works in a few words",
  });
  console.log(response.text);
}

main();

Vertex AI Gemini API

import { GoogleGenAI } from '@google/genai';
const ai = new GoogleGenAI({
  vertexai: true,
  project: 'your_project',
  location: 'your_location',
});

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.0-flash",
    contents: "Explain how AI works in a few words",
  });
  console.log(response.text);
}

main();

Go

Bạn có thể truy cập cả Gemini Developer API và các dịch vụ Vertex AI thông qua thư viện google.golang.org/genai. Hãy xem trang thư viện để biết hướng dẫn về cách cài đặt google.golang.org/genai.

Gemini Developer API

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

// Your Google API key
const apiKey = "your-api-key"

func main() {
  ctx := context.Background()
  client, err := genai.NewClient(ctx, nil)
  if err != nil {
      log.Fatal(err)
  }

  // Call the GenerateContent method.
  result, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", genai.Text("Tell me about New York?"), nil)

}

Vertex AI Gemini API

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

// Your GCP project
const project = "your-project"

// A GCP location like "us-central1"
const location = "some-gcp-location"

func main() {
  ctx := context.Background()
  client, err := genai.NewClient(ctx, &genai.ClientConfig
  {
        Project:  project,
      Location: location,
      Backend:  genai.BackendVertexAI,
  })

  // Call the GenerateContent method.
  result, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", genai.Text("Tell me about New York?"), nil)

}

Các trường hợp sử dụng và nền tảng khác

Tham khảo hướng dẫn cụ thể theo từng trường hợp sử dụng trong Tài liệu về Gemini Developer APITài liệu về Vertex AI cho các nền tảng và trường hợp sử dụng khác.

Những điều cần cân nhắc khi di chuyển

Khi bạn di chuyển:

Nếu không cần dùng khoá Gemini API cho Gemini Developer API nữa, hãy làm theo các phương pháp bảo mật hay nhất và xoá khoá đó.

Cách xoá khoá API:

  1. Mở trang Thông tin đăng nhập API Google Cloud.

  2. Tìm khoá API mà bạn muốn xoá rồi nhấp vào biểu tượng Thao tác.

  3. Chọn Xoá khoá API.

  4. Trong cửa sổ Xoá thông tin đăng nhập, hãy chọn Xoá.

    Quá trình xoá khoá API sẽ mất vài phút để có hiệu lực. Sau khi quá trình truyền dữ liệu hoàn tất, mọi lưu lượng truy cập sử dụng khoá API đã bị xoá đều sẽ bị từ chối.

Các bước tiếp theo