מעבר אל Google GenAI SDK

החל מגרסה Gemini 2.0 שהושקה בסוף 2024, הוספנו קבוצה חדשה של ספריות שנקראת Google GenAI SDK. הוא מציע חוויית פיתוח משופרת באמצעות ארכיטקטורת לקוח מעודכנת, ומפשט את המעבר בין תהליכי עבודה של מפתחים לבין תהליכי עבודה של ארגונים.

‫Google GenAI SDK נמצא עכשיו בזמינות כללית (GA) בכל הפלטפורמות הנתמכות. אם אתם משתמשים באחת מספריות מדור קודם, מומלץ מאוד לבצע מיגרציה.

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

התקנה

לפני

Python

pip install -U -q "google-generativeai"

JavaScript

npm install @google/generative-ai

Go

go get github.com/google/generative-ai-go

אחרי

Python

pip install -U -q "google-genai"

JavaScript

npm install @google/genai

Go

go get google.golang.org/genai

גישה באמצעות ממשק API

ב-SDK הישן, הטיפול בלקוח ה-API נעשה באופן מרומז מאחורי הקלעים באמצעות מגוון שיטות אד-הוק. במצב כזה קשה לנהל את הלקוח ואת פרטי הכניסה. עכשיו, אתם יכולים ליצור אינטראקציה באמצעות אובייקט מרכזי של Client. אובייקט Client הזה משמש כנקודת כניסה יחידה לשירותי API שונים (למשל, models, ‏ chats,‏ files, ‏ tunings), ומקדם עקביות ומפשט את ניהול האישורים וההגדרות בקריאות שונות ל-API.

לפני (גישת API פחות מרכזית)

Python

ב-SDK הישן לא נעשה שימוש מפורש באובייקט לקוח ברמה העליונה ברוב הקריאות ל-API. אתם יוצרים ישירות מופעים של אובייקטים של GenerativeModel ומבצעים איתם אינטראקציה.

import google.generativeai as genai

# Directly create and use model objects
model = genai.GenerativeModel('gemini-3.6-flash')
response = model.generate_content(...)
chat = model.start_chat(...)

JavaScript

GoogleGenerativeAI היה נקודה מרכזית למודלים ולצ'אט, אבל כדי להשתמש בפונקציות אחרות כמו ניהול קבצים ומטמון, היה צריך לייבא ולהפעיל מחלקות לקוח נפרדות לגמרי.

import { GoogleGenerativeAI } from "@google/generative-ai";
import { GoogleAIFileManager, GoogleAICacheManager } from "@google/generative-ai/server"; // For files/caching

const genAI = new GoogleGenerativeAI("GEMINI_API_KEY");
const fileManager = new GoogleAIFileManager("GEMINI_API_KEY");
const cacheManager = new GoogleAICacheManager("GEMINI_API_KEY");

// Get a model instance, then call methods on it
const model = genAI.getGenerativeModel({ model: "gemini-3.6-flash" });
const result = await model.generateContent(...);
const chat = model.startChat(...);

// Call methods on separate client objects for other services
const uploadedFile = await fileManager.uploadFile(...);
const cache = await cacheManager.create(...);

Go

הפונקציה genai.NewClient יצרה לקוח, אבל בדרך כלל הפעולות של המודל הגנרטיבי הופעלו במופע GenerativeModel נפרד שהתקבל מהלקוח הזה. יכול להיות שהייתה גישה לשירותים אחרים באמצעות חבילות או דפוסים שונים.

import (
      "github.com/google/generative-ai-go/genai"
      "github.com/google/generative-ai-go/genai/fileman" // For files
      "google.golang.org/api/option"
)

client, err := genai.NewClient(ctx, option.WithAPIKey("GEMINI_API_KEY"))
fileClient, err := fileman.NewClient(ctx, option.WithAPIKey("GEMINI_API_KEY"))

// Get a model instance, then call methods on it
model := client.GenerativeModel("gemini-3.6-flash")
resp, err := model.GenerateContent(...)
cs := model.StartChat()

// Call methods on separate client objects for other services
uploadedFile, err := fileClient.UploadFile(...)

אחרי (אובייקט לקוח מרכזי)

Python

from google import genai

# Create a single client object
client = genai.Client()

# Access API methods through services on the client object
response = client.models.generate_content(...)
chat = client.chats.create(...)
my_file = client.files.upload(...)
tuning_job = client.tunings.tune(...)

JavaScript

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

// Create a single client object
const ai = new GoogleGenAI({apiKey: "GEMINI_API_KEY"});

// Access API methods through services on the client object
const response = await ai.models.generateContent(...);
const chat = ai.chats.create(...);
const uploadedFile = await ai.files.upload(...);
const cache = await ai.caches.create(...);

Go

import "google.golang.org/genai"

// Create a single client object
client, err := genai.NewClient(ctx, nil)

// Access API methods through services on the client object
result, err := client.Models.GenerateContent(...)
chat, err := client.Chats.Create(...)
uploadedFile, err := client.Files.Upload(...)
tuningJob, err := client.Tunings.Tune(...)

אימות

גם בספריות מדור קודם וגם בספריות חדשות, האימות מתבצע באמצעות מפתחות API. אפשר ליצור מפתח API ב-Google AI Studio.

לפני

Python

ב-SDK הישן, אובייקט לקוח ה-API טופל באופן מרומז.

import google.generativeai as genai

genai.configure(api_key=...)

JavaScript

import { GoogleGenerativeAI } from "@google/generative-ai";

const genAI = new GoogleGenerativeAI("GEMINI_API_KEY");

Go

מייבאים את הספריות של Google:

import (
      "github.com/google/generative-ai-go/genai"
      "google.golang.org/api/option"
)

יוצרים את הלקוח:

client, err := genai.NewClient(ctx, option.WithAPIKey("GEMINI_API_KEY"))

אחרי

Python

בעזרת Google GenAI SDK, קודם יוצרים לקוח API, שמשמש לקריאה ל-API. אם לא מעבירים מפתח ללקוח, ה-SDK החדש יקבל את מפתח ה-API ממשתני הסביבה GEMINI_API_KEY.

export GEMINI_API_KEY="YOUR_API_KEY"
from google import genai

client = genai.Client() # Set the API key using the GEMINI_API_KEY env var.
                        # Alternatively, you could set the API key explicitly:
                        # client = genai.Client(api_key="YOUR_API_KEY")

JavaScript

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

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

Go

מייבאים את ספריית ה-GenAI:

import "google.golang.org/genai"

יוצרים את הלקוח:

client, err := genai.NewClient(ctx, &genai.ClientConfig{
        Backend:  genai.BackendGeminiAPI,
})

יצירת תוכן

טקסט

לפני

Python

בעבר לא היו אובייקטים של לקוחות, והגישה לממשקי API הייתה ישירות דרך אובייקטים של GenerativeModel.

import google.generativeai as genai

model = genai.GenerativeModel('gemini-3.6-flash')
response = model.generate_content(
    'Tell me a story in 300 words'
)
print(response.text)

JavaScript

import { GoogleGenerativeAI } from "@google/generative-ai";

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);