Vertex AI Gemini API

כשמפתחים פתרונות של AI גנרטיבי באמצעות Gemini, המפתחים יכולים לבחור בין Gemini Developer API לבין Vertex AI Gemini API.

Gemini Developer API מספק דרך קלה לפתח אפליקציות Gemini, להעביר אותן לסביבת הייצור ולהרחיב אותן. Vertex AI מציע סביבה עסקית מקיפה של תכונות ושירותים שמוכנים לשימוש בארגונים, ליצירה ולפריסה של אפליקציות בינה מלאכותית גנרטיבית שמגובות על ידי פלטפורמת Google Cloud.

הבחירה הטובה ביותר תלויה בצרכים שלכם, אבל לאחרונה הפכנו את המעבר בין השירותים האלה לפשוט יותר. עכשיו אפשר לגשת גם ל-Gemini Developer API וגם ל-Vertex AI Gemini API דרך Google Gen AI SDK המאוחד, שמציע גמישות רבה יותר.

השוואת קוד

בהמשך מופיעות כמה השוואות של קוד זה לצד זה בין Gemini Developer API לבין מדריכי למתחילים של Vertex AI ליצירת טקסט.

Python

אפשר לגשת ל-Gemini Developer API ולשירותי Vertex AI דרך הספרייה google-genai. הוראות להתקנת הספרייה מפורטות בדף libraries.

Gemini Developer API

from google import genai

client = genai.Client(api_key="YOUR_API_KEY")

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 ו-TypeScript

אפשר לגשת ל-Gemini Developer API ולשירותי Vertex AI דרך הספרייה @google/genai. הוראות להתקנת הספרייה מפורטות בדף libraries.

Gemini Developer API

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

const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });

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

אפשר לגשת ל-Gemini Developer API ולשירותי Vertex AI דרך הספרייה google.golang.org/genai. הוראות להתקנת הספרייה מפורטות בדף libraries.

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, &genai.ClientConfig{
    APIKey:  apiKey,
    Backend: genai.BackendGeminiAPI,
  })

  // 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)

}

פלטפורמות ותרחישי שימוש אחרים

במסמכי התיעוד של Gemini Developer API ובמסמכי התיעוד של Vertex AI מפורטים מדריכים ספציפיים לתרחישי שימוש בפלטפורמות אחרות.

שיקולים לגבי העברה

במהלך ההעברה:

אם אתם כבר לא צריכים להשתמש במפתח Gemini API לממשק Gemini Developer API, כדאי למחוק אותו לפי השיטות המומלצות לאבטחה.

כדי למחוק מפתח API:

  1. פותחים את הדף Google Cloud API Credentials.

  2. מאתרים את מפתח ה-API שרוצים למחוק ולוחצים על הסמל Actions.

  3. בוחרים באפשרות Delete API key (מחיקת מפתח ה-API).

  4. בחלון הדו-שיח Delete credential, בוחרים באפשרות Delete.

    המחיקה של מפתח API יכולה להימשך כמה דקות. אחרי שההפצה תושלם, כל תעבורת הנתונים שמשתמשת במפתח ה-API שנמחק תידחה.

השלבים הבאים