בקשה להפעלת פונקציה במודלים פתוחים

התכונה "קריאה להפעלת פונקציות" מאפשרת לכם להגדיר פונקציות בהתאמה אישית ולתת למודלים גדולים של שפה (LLM) את היכולת לקרוא להן כדי לאחזר מידע בזמן אמת או לקיים אינטראקציה עם מערכות חיצוניות, כמו מסדי נתונים של SQL או כלים של שירות לקוחות.

מידע נוסף על קריאה להפעלת פונקציות זמין במאמר מבוא לקריאה להפעלת פונקציות.

לפני שמתחילים

כדי להשתמש במודלים פתוחים עם Gemini Enterprise Agent Platform, צריך לבצע את הפעולות הבאות. כדי להשתמש במודלים פתוחים ב-Gemini Enterprise Agent Platform, צריך להפעיל את Agent Platform API (aiplatform.googleapis.com) ואת Cloud Commerce Consumer Procurement API (cloudcommerceconsumerprocurement.googleapis.com). אם כבר יש לכם פרויקט שבו ממשקי ה-API האלה מופעלים, אתם יכולים להשתמש בו במקום ליצור פרויקט חדש.

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Gemini Enterprise Agent Platform and Cloud Commerce Consumer Procurement APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  5. עוברים לכרטיס המודל ב-Model Garden של המודל שבו רוצים להשתמש, ולוחצים על Enable כדי להפעיל את המודל לשימוש בפרויקט.

    כניסה ל-Model Garden

התפקידים הנדרשים

כדי לקבל את ההרשאות שדרושות להפעלה ולשימוש במודלים פתוחים, בקשו מהאדמין להקצות לכם את תפקידי ה-IAM הבאים בפרויקט:

להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.

מידע נוסף על מתן הרשאות והגדרת מדיניות ארגונית למודלים פתוחים זמין במאמר מתן גישה למשתמשים למודלים פתוחים.

שימוש בקריאה להפעלת פונקציות

הדוגמאות הבאות מראות איך להשתמש בקריאה להפעלת פונקציות.

Python

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Python שמפורטות במאמר התחלה מהירה של Agent Platform באמצעות ספריות לקוח.

כדי לבצע אימות ב-Agent Platform, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

לפני שמריצים את הדוגמה הזו, צריך להגדיר את משתנה הסביבה OPENAI_BASE_URL. מידע נוסף זמין במאמר אימות ופרטי כניסה.

from openai import OpenAI
client = OpenAI()

response = client.chat.completions.create(
  model="MODEL",
  messages=[
    {"role": "user", "content": "CONTENT"}
  ],
  tools=[
    {
      "type": "function",
      "function": {
        "name": "FUNCTION_NAME",
        "description": "FUNCTION_DESCRIPTION",
        "parameters": PARAMETERS_OBJECT,
      }
    }
  ],
  tool_choice="auto",
)
  • ‫MODEL: שם המודל שבו רוצים להשתמש, לדוגמה zai-org/glm-5.2-maas.
  • ‫CONTENT: הנחיית המשתמש שתישלח למודל.
  • ‫FUNCTION_NAME: שם הפונקציה שרוצים להפעיל.
  • ‫FUNCTION_DESCRIPTION: תיאור הפונקציה.
  • ‫PARAMETERS_OBJECT: מילון שמגדיר את הפרמטרים של הפונקציה, לדוגמה:
    {"type": "object", "properties": {"location": {"type": "string", "description": "The city and state"}}, "required": ["location"]}

REST

אחרי שמגדירים את הסביבה, אפשר להשתמש ב-REST כדי לבדוק פרומפט טקסטואלי. הדוגמה הבאה שולחת בקשה לנקודת הקצה של המודל של בעל התוכן הדיגיטלי.

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט ב-Google Cloud.
  • ‫MODEL: שם המודל שבו רוצים להשתמש, לדוגמה zai-org/glm-5.2-maas.
  • ‫CONTENT: הנחיית המשתמש שתישלח למודל.
  • ‫FUNCTION_NAME: שם הפונקציה שרוצים להפעיל.
  • ‫FUNCTION_DESCRIPTION: תיאור הפונקציה.
  • ‫PARAMETERS_OBJECT: אובייקט סכימת JSON שמגדיר את הפרמטרים של הפונקציה, לדוגמה:
    {"type": "object", "properties": {"location": {"type": "string", "description": "The city and state"}}, "required": ["location"]}

ה-method של ה-HTTP וכתובת ה-URL:

POST https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/chat/completions

גוף בקשת JSON:

{
  "model": "MODEL",
  "messages": [
    {
      "role": "user",
      "content": "CONTENT"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "FUNCTION_NAME",
        "description": "FUNCTION_DESCRIPTION",
        "parameters": PARAMETERS_OBJECT
      }
    }
  ],
  "tool_choice": "auto"
}

כדי לשלוח את הבקשה עליכם לבחור אחת מהאפשרויות הבאות:

curl

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/chat/completions"

PowerShell

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/chat/completions" | Select-Object -Expand Content

אמורים לקבל קוד סטטוס של הצלחה (2xx) ותגובה ריקה.

דוגמה

הפלט המלא שצפוי להתקבל אחרי שימוש בפונקציה get_current_weather כדי לאחזר מידע מטאורולוגי:

Python

from openai import OpenAI
client = OpenAI()

response = client.chat.completions.create(
  model="zai-org/glm-5.2-maas",
  messages=[
    {
      "role": "user",
      "content": "Which city has a higher temperature, Boston or new Delhi and by how much in F?"
    },
    {
      "role": "assistant",
      "content": "I'll check the current temperatures for Boston and New Delhi in Fahrenheit and compare them. I'll call the weather function for both cities.",
      "tool_calls": [{"function":{"arguments":"{\"location\":\"Boston, MA\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"},{"function":{"arguments":"{\"location\":\"New Delhi, India\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"}]
    },
    {
      "role": "tool",
      "content": "The temperature in Boston is 75 degrees Fahrenheit.",
      "tool_call_id": "get_current_weather"
    },
    {
      "role": "tool",
      "content": "The temperature in New Delhi is 50 degrees Fahrenheit.",
      "tool_call_id": "get_current_weather"
    }
  ],
  tools=[
    {
      "type": "function",
      "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
              "type": "string",
              "enum": ["celsius", "fahrenheit"]
            }
          },
          "required": ["location"]
        }
      }
    }
  ],
  tool_choice="auto"
)

curl

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://aiplatform.googleapis.com/v1/projects/sample-project/locations/global/endpoints/openapi/chat/completions -d \
'{
  "model": "zai-org/glm-5.2-maas",
  "messages": [
    {
      "role": "user",
      "content": "Which city has a higher temperature, Boston or new Delhi and by how much in F?"
    },
    {
      "role": "assistant",
      "content": "I'll check the current temperatures for Boston and New Delhi in Fahrenheit and compare them. I'll call the weather function for both cities.",
      "tool_calls": [{"function":{"arguments":"{\"location\":\"Boston, MA\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"},{"function":{"arguments":"{\"location\":\"New Delhi, India\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"}]
    },
    {
      "role": "tool",
      "content": "The temperature in Boston is 75 degrees Fahrenheit.",
      "tool_call_id": "get_current_weather"
    },
    {
      "role": "tool",
      "content": "The temperature in New Delhi is 50 degrees Fahrenheit.",
      "tool_call_id": "get_current_weather"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
              "type": "string",
              "enum": ["celsius", "fahrenheit"]
            }
          },
          "required": ["location"]
        }
      }
    }
  ],
  "tool_choice": "auto"
}'
אחרי שהמודל מקבל את המידע שאוחזר באמצעות קריאה לפונקציה החיצונית `get_current_weather`, הוא יכול לסנתז את המידע משתי התגובות של `tool` ולענות על השאלה של המשתמש. הנה דוגמה לפלט אפשרי של המודל:
{
 "choices": [
  {
   "finish_reason": "stop",
   "index": 0,
   "logprobs": null,
   "message": {
    "content": "Based on the current weather data:\n\n- **Boston, MA**: 75°F
    \n- **New Delhi, India**: 50°F  \n\n**Comparison**:
    \nBoston is **25°F warmer** than New Delhi.  \n\n**Answer**:
    \nBoston has a higher temperature than New Delhi by 25 degrees Fahrenheit.",
    "role": "assistant"
   }
  }
 ],
 "created": 1750450289,
 "id": "2025-06-20|13:11:29.240295-07|6.230.75.101|-987540014",
 "model": "zai-org/glm-5.2-maas",
 "object": "chat.completion",
 "system_fingerprint": "",
 "usage": {
  "completion_tokens": 66,
  "prompt_tokens": 217,
  "total_tokens": 283
 }
}

הנחיות ספציפיות למודל

בקטעים הבאים מפורטות הנחיות ספציפיות למודל בנוגע לקריאה לפונקציות.

DeepSeek

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

Llama

האפליקציה meta/llama3-405b-instruct-maas לא תומכת ב-tool_choice = 'required'.

OpenAI

כשמשתמשים ב-openai/gpt-oss-120b-maas, כדאי להציב את הגדרות הכלים בהנחיית המערכת כדי לקבל ביצועים אופטימליים. לדוגמה:

{"messages": [
    {"role": "system", "content": "You are a helpful assistant with access to the following functions. Use them if required:\n..."},
    {"role": "user", "content": "What's the weather like in Boston?"},
    ...
]}

המודל הזה לא תומך ב-tool_choice = 'required' או בקריאת API לכלי ספציפי.

המאמרים הבאים