Gemini Enterprise Agent Platform במצב אקספרס מאפשר לכם לנסות תכונות ליבה של AI גנרטיבי שזמינות ב-Agent Platform. במדריך הזה מוסבר איך לבצע את הפעולות הבאות באמצעות Agent Platform API במצב אקספרס:
- מתקינים ומפעילים את ה-SDK של Google Gen AI למצב אקספרס.
שולחים בקשה אל Gemini for Google Cloud API, כולל הפרטים הבאים:
- בקשה לסטרימינג
- בקשה שלא קשורה לסטרימינג
- בקשה להפעלת פונקציה
לפני שמתחילים
לפני שמבצעים את המשימות שמתוארות במאמר הזה, צריך להירשם למצב אקספרס.
התקנה והפעלה של ה-SDK למצב אקספרס
Google Gen AI SDK מאפשר לכם להשתמש במודלים ובתכונות של AI גנרטיבי מבית Google כדי ליצור אפליקציות מבוססות-AI. כשמשתמשים ב-Agent Platform במצב אקספרס, צריך להתקין את חבילת google-genai ולהפעיל אותה כדי לבצע אימות באמצעות מפתח ה-API שנוצר.
התקנה
כדי להתקין את Google Gen AI SDK במצב אקספרס, מריצים את הפקודות הבאות:
# Developer TODO: If you're using Colab, uncomment the following lines:
# from google.colab import auth
# auth.authenticate_user()
!pip install --upgrade google-genai
אם אתם משתמשים ב-Colaboratory, הפעילו מחדש את סביבת זמן הריצה אחרי ההתקנה אם מוצגת בקשה לעשות זאת.
התחלה
מגדירים את מפתח ה-API למצב אקספרס ומפעילים את הלקוח באמצעות enterprise=True. פרטים על קבלת מפתח API מופיעים במאמר סקירה כללית על Agent Platform במצב אקספרס.
from google import genai
from google.genai import types
# Developer TODO: Replace YOUR_API_KEY with your API key.
API_KEY = "YOUR_API_KEY"
client = genai.Client(
enterprise=True, api_key=API_KEY
)
שליחת בקשה אל Gemini for Google Cloud API
אפשר לשלוח בקשות סטרימינג או בקשות לא סטרימינג אל Gemini for Google Cloud API. בקשות סטרימינג מחזירות את התגובה במנות בזמן העיבוד של הבקשה. בקשות שלא כוללות סטרימינג מחזירות את התשובה המלאה אחרי שהעיבוד מסתיים.
בקשה לסטרימינג
כדי לשלוח בקשה להזרמה, קוראים ל-client.models.generate_content_stream() ומדפיסים כל נתח כשהוא מגיע:
from google import genai
from google.genai import types
def generate():
client = genai.Client(enterprise=True, api_key="YOUR_API_KEY")
config = types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
thinking_level="MEDIUM",
),
seed=5,
max_output_tokens=1024,
stop_sequences=["STOP!"],
safety_settings=[
types.SafetySetting(
category="HARM_CATEGORY_HATE_SPEECH",
threshold="BLOCK_ONLY_HIGH",
)
],
)
for chunk in client.models.generate_content_stream(
model="gemini-3.8-flash",
contents="Explain bubble sort to me",
config=config,
):
print(chunk.text, end="")
generate()
בקשה שלא קשורה לסטרימינג
בדוגמת הקוד הבאה מוגדרת פונקציה ששולחת בקשה ללא סטרימינג אל Gemini 3.8 Flash (gemini-3.8-flash). בדוגמה מוצגות ההגדרות של רמת החשיבה, פרמטרים של הפלט והגדרות הבטיחות:
from google import genai
from google.genai import types
def generate():
client = genai.Client(enterprise=True, api_key="YOUR_API_KEY")
config = types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
thinking_level="MEDIUM",
),
seed=5,
max_output_tokens=1024,
stop_sequences=["STOP!"],
safety_settings=[
types.SafetySetting(
category="HARM_CATEGORY_HATE_SPEECH",
threshold="BLOCK_ONLY_HIGH",
)
],
)
response = client.models.generate_content(
model="gemini-3.8-flash",
contents="Explain bubble sort to me",
config=config,
)
print(response.text)
generate()
בקשה להפעלת פונקציה
בדוגמת הקוד הבאה מוצהר על כלי פונקציה, נשלחת הנחיה ראשונית ל-3.8 Flash, מתקבל חלק של בקשה להפעלת פונקציה בתגובה, ואז נשלח בחזרה FunctionResponse תואם למודל כדי ליצור תגובה סופית:
from google import genai
from google.genai import types
client = genai.Client(enterprise=True, api_key="YOUR_API_KEY")
get_weather_declaration = types.FunctionDeclaration(
name="get_current_weather",
description="Gets the current weather in a given city.",
parameters={
"type": "OBJECT",
"properties": {
"location": {
"type": "STRING",
"description": "The city and state, such as Boston, MA.",
},
"unit": {
"type": "STRING",
"enum": ["C", "F"],
},
},
"required": ["location"],
},
)
tools = [types.Tool(function_declarations=[get_weather_declaration])]
prompt = "What is the weather in Boston?"
first_response = client.models.generate_content(
model="gemini-3.8-flash",
contents=prompt,
config=types.GenerateContentConfig(tools=tools),
)
function_call = first_response.function_calls[0]
contents = [
types.Content(role="user", parts=[types.Part(text=prompt)]),
first_response.candidates[0].content,
types.Content(
role="user",
parts=[
types.Part(
function_response=types.FunctionResponse(
id=function_call.id,
name=function_call.name,
response={"weather": "sunny", "temperature": "72F"},
)
)
],
),
]
response = client.models.generate_content(
model="gemini-3.8-flash",
contents=contents,
config=types.GenerateContentConfig(tools=tools),
)
print(response.text)
הסרת המשאבים
במדריך הזה לא נוצרים משאבי Google Cloud , ולכן לא צריך לבצע ניקוי כדי להימנע מחיובים.
המאמרים הבאים
- כדאי לנסות את המדריך ל-Agent Studio ל-Agent Platform במצב אקספרס.
- ראו את מאמרי העזרה של ה-API המלאים של Agent Platform במצב אקספרס.