Gemini API ile işlev çağrısına giriş

Gemini API işlev çağrısı özelliğini kullanarak, tanımlarını öğreneceksiniz. Model bu işlevleri doğrudan çağırmaz, bunun yerine bir işlev adını ve önerilen bağımsız değişkenleri belirten yapılandırılmış bir çıkış oluşturur. Ardından, harici bir çağrı yapmak için işlev adını ve bağımsız değişkenleri yapabilir ve sonuç olarak elde edilen API çıkışını başka bir sorguya ekleyebilirsiniz. Böylece, Bu şekilde modelin daha kapsamlı bir yanıt vermesini ve ek işlemler.

İşlev çağrısı, kullanıcıların gerçek zamanlı bilgilerle ve veri tabanları, müşteri ilişkileri yönetimi sistemleri ve doküman depoları gibi hizmetlerle etkileşim kurmasını sağlar. Bu özellik, modelin alakalı ve bağlama uygun yanıtlar verme becerisini de artırır. İşlev çağırma, harici sistemlerle etkileşime geçmek için en iyi yöntemdir. Kullanım alanınızda modelin hesaplama yapması gerekiyorsa ancak harici sistemler veya API'ler kullanılmıyorsa bunun yerine kod yürütme özelliğini kullanabilirsiniz.

İşlev çağrısının çalışan bir örneği için "hafif bot" not defteri ekleyin.

İşlev çağrısının işleyiş şekli

İşlev çağrısı özelliğini, işlev bildirimleri adı verilen programlama arayüzlerini model istemine ekleme. İlgili içeriği oluşturmak için kullanılan işlev bildirimleri, API işlevinin adını sağlar, amacını açıklar, URL'nin desteklediği tüm parametreleri ve bu parametrelerin açıklamalarını dahil edin. Sınavı geçtikten sonra işlev bildirimlerinin bir listesidir. Modele gönderilen sorguda, ifadelerinin ve sorgunun geri kalanını inceleyerek, İsteğe yanıt olarak API.

Model, daha sonra OpenAPI uyumlu şema yanıt vermek için bildirilen işlevlerden birinin veya daha fazlasının nasıl çağrılacağını belirterek soru işaretlerine geri dönelim. Ardından, önerilen işlev çağrısını gerçek API'yi çağırın, bir yanıt alın ve bu yanıtı veya başka bir işlem yapmadığı anlamına gelir. Modelin aslında tanımlanmış işlevler. Bunun yerine, döndürülen şema nesnesi parametrelerini kullanarak fonksiyonu çağırın. Gemini API, modelin tek bir isteğe göre birden fazla API işlevi çağrısı önerdiği paralel işlev çağrısını da destekler.

İşlev bildirimleri

Bir istemde işlev çağrısı uyguladığınızda bir veya daha fazla function declarations içeren bir tools nesnesi oluşturursunuz. JSON'u kullanarak, özellikle OpenAPI şeması biçiminin belirli bir alt kümesiyle işlevleri tanımlarsınız. Tek bir işlev bildirimi aşağıdaki parametreleri içerebilir:

  • name (dize): API çağrısındaki işlevin benzersiz tanımlayıcısıdır.
  • description (dize): İşlevin amacı ve özellikleri hakkında kapsamlı bir açıklama.
  • parameters (nesne): İşlevin gerektirdiği giriş verilerini tanımlar.
    • type (dize): Genel veri türünü belirtir (ör. object).
    • properties (nesne): Her biri aşağıdakileri içeren bağımsız parametreleri listeler:
      • type (dize): Parametrenin veri türü. Örneğin: string, integer, boolean.
      • description (dize): Parametrenin amacının ve beklenen biçiminin net bir açıklaması.
    • required (dizi): İşlevin çalışması için zorunlu olan parametre adlarını listeleyen bir dize dizisi.

cURL komutlarını kullanan bir işlev bildirimi kod örnekleri için İşlev çağrısı örnekleri. Örnek olarak Gemini API SDK'larını kullanarak işlev bildirimleri oluşturmak için İşlev çağrısı eğiticisi.

İşlev bildirimleri için en iyi uygulamalar

Fonksiyonlarınızı doğru şekilde tanımlamak, fonksiyonlarınızı kabul edersiniz. Her işlev, davranışına ve modelle etkileşimine yön veren belirli parametrelere dayanır. Aşağıdaki listede, functions_declarations dizisindeki tek bir işlevin parametrelerini tanımlama hakkında yol gösterici bilgiler verilmektedir.

  • name: Boşluk, nokta içermeyen, net, açıklayıcı adlar kullanın (.) veya kısa çizgi (-) karakterden oluşmalıdır. Bunun yerine alt çizgi (_) karakterleri kullanın büyük/küçük harf kullanımını değiştirebilirsiniz.

  • description: Ayrıntılı, net ve spesifik bir işlev sağlayın ve gerekirse örnekler vermelidir. Örneğin, find theaters yerine find theaters based on location and optionally movie title that is currently playing in theaters. Aşırı geniş veya belirsiz açıklamalardan kaçının.

  • properties > type: Model halüsinasyonlarını azaltmak için güçlü şekilde yazılmış parametreler kullanın. Örneğin, parametre değerleri sonlu bir kümeden geliyorsa değerleri açıklamada listelemek yerine bir enum alanı kullanın (ör. "type": "enum", "values": ["now_playing", "upcoming"]). Parametre değeri her zaman tam sayıysa türü number yerine integer olarak ayarlayın.

  • properties > description: Somut örnekler ve kısıtlamalar sağlayın. Örneğin, the location to search yerine The city and state, e.g. San Francisco, CA or a zip code e.g. 95616 değerini kullanın.

İşlev çağrısını kullanmayla ilgili diğer en iyi uygulamalar için En İyi Uygulamalar bölümüne bakın.

İşlev çağrısı modu

Yürütmeyi değiştirmek için mode parametresini çağıran işlevi kullanabilirsiniz ve özelliğin davranışı. Üç mod vardır:

  • AUTO: Varsayılan model davranışı. Model, bir işlev çağrısını mı yoksa doğal dil yanıtını mı tahmin edeceğine karar verir.
  • ANY: Model, her zaman bir işlev çağrısını tahmin edecek şekilde sınırlandırılmıştır. allowed_function_names belirtilmemişse model, mevcut tüm işlev beyanlarından seçim yapar. allowed_function_names sağlanırsa model, izin verilen işlevler arasından seçim yapar.
  • NONE: Model, bir işlev çağrısını tahmin etmez. Bu durumda model herhangi bir işlev bildirimi iletmediğinizde ile aynı olur.

ANY modunun ("zorunlu işlev çağrısı") kullanımı: yalnızca Gemini 1.5 Pro ve Gemini 1.5 Flash modelleri için desteklenir.

Ayrıca, sağlandığında sınıra sahip bir allowed_function_names grubu da aktarabilirsiniz modelin çağıracağı işlevler. Yalnızca allowed_function_names, mod ANY olduğunda. İşlev adları eşleşmelidir işlev bildirim adları için geçerlidir. Mod ANY olarak ayarlandığında ve allowed_function_names ayarlanırsa model, verilen işlev adı kümesidir.

Örnek istek'ten alınan aşağıdaki kod snippet'inde, mode değerinin ANY olarak nasıl ayarlanacağı ve izin verilen işlevlerin listesinin nasıl belirtileceği gösterilmektedir:

"tool_config": {
  "function_calling_config": {
    "mode": "ANY",
    "allowed_function_names": ["find_theaters", "get_showtimes"]
  },
}

İşlev çağrısı örnekleri

Bu bölümde, cURL komutları kullanılarak işlev çağrısı için örnek istemler sağlanmaktadır. Örnekler arasında tek dönüş ve çok dönüşlü senaryoları kapsar. farklı işlev çağırma modları vardır.

Bu özellikle birlikte cURL komutları kullanılırken işlev ve parametre bilgileri tools öğesine dahil edilir. tools öğesindeki her işlev beyanı işlev adını içerir. Parametreleri, bir OpenAPI uyumlu şeması ve işlev açıklaması kullanarak belirtirsiniz.

Tek dönüş örneği

Tek dönüş, dil modelini bir kez çağırdığınızda gerçekleşir. İşlev çağırma özelliğinde, modele doğal dil sorgusu ve işlev listesi sağladığınızda tek dönüşlü bir kullanım alanı olabilir. Bu durumda model, işlev adını, parametrelerini ve açıklamasını içeren bildirimi ve hangi fonksiyonla çağrılacağını tahmin etmek için kullanılan bağımsız değişkenleri içerir.

Aşağıdaki curl örneği, bir fonksiyonudur. Birkaç işlev bildirimleri isteğe dahil edilir (ör. find_movies ve find_theaters.

Tek dönüşlü işlev çağrısı örnek isteği

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \
  -H 'Content-Type: application/json' \
  -d '{
    "contents": {
      "role": "user",
      "parts": {
        "text": "Which theaters in Mountain View show Barbie movie?"
    }
  },
  "tools": [
    {
      "function_declarations": [
        {
          "name": "find_movies",
          "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "description": {
                "type": "string",
                "description": "Any kind of description including category or genre, title words, attributes, etc."
              }
            },
            "required": [
              "description"
            ]
          }
        },
        {
          "name": "find_theaters",
          "description": "find theaters based on location and optionally movie title which is currently playing in theaters",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "movie": {
                "type": "string",
                "description": "Any movie title"
              }
            },
            "required": [
              "location"
            ]
          }
        },
        {
          "name": "get_showtimes",
          "description": "Find the start times for movies playing in a specific theater",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "movie": {
                "type": "string",
                "description": "Any movie title"
              },
              "theater": {
                "type": "string",
                "description": "Name of the theater"
              },
              "date": {
                "type": "string",
                "description": "Date for requested showtime"
              }
            },
            "required": [
              "location",
              "movie",
              "theater",
              "date"
            ]
          }
        }
      ]
    }
  ]
}'
    

Bu curl örneğine verilen yanıt aşağıdakine benzer olabilir.

Tek dönüşlü işlev çağrısı curl örnek yanıtı

[{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "functionCall": {
              "name": "find_theaters",
              "args": {
                "movie": "Barbie",
                "location": "Mountain View, CA"
              }
            }
          }
        ]
      },
      "finishReason": "STOP",
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_HARASSMENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HATE_SPEECH",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 9,
    "totalTokenCount": 9
  }
}]
    

HERHANGİ modunu kullanan tek dönüş örneği

Aşağıdaki curl örneği şuna benzerdir: tek dönüşlü örneği, ancak modu ANY olarak ayarlayın:

"tool_config": {
  "function_calling_config": {
    "mode": "ANY"
  },
}

ANY modunu (istek) kullanan tek seferlik işlev çağrısı

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \
  -H 'Content-Type: application/json' \
  -d '{
    "contents": {
      "role": "user",
      "parts": {
        "text": "What movies are showing in North Seattle tonight?"
    }
  },
  "tools": [
    {
      "function_declarations": [
        {
          "name": "find_movies",
          "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "description": {
                "type": "string",
                "description": "Any kind of description including category or genre, title words, attributes, etc."
              }
            },
            "required": [
              "description"
            ]
          }
        },
        {
          "name": "find_theaters",
          "description": "find theaters based on location and optionally movie title which is currently playing in theaters",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "movie": {
                "type": "string",
                "description": "Any movie title"
              }
            },
            "required": [
              "location"
            ]
          }
        },
        {
          "name": "get_showtimes",
          "description": "Find the start times for movies playing in a specific theater",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "movie": {
                "type": "string",
                "description": "Any movie title"
              },
              "theater": {
                "type": "string",
                "description": "Name of the theater"
              },
              "date": {
                "type": "string",
                "description": "Date for requested showtime"
              }
            },
            "required": [
              "location",
              "movie",
              "theater",
              "date"
            ]
          }
        }
      ]
    }
  ],
  "tool_config": {
    "function_calling_config": {
      "mode": "ANY"
    },
  }
}'
    

Yanıt, aşağıdakine benzer olabilir:

HERHANGİ BİR modu kullanarak tek dönüşlü işlev çağrısı (yanıt)

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "functionCall": {
              "name": "find_movies",
              "args": {
                "description": "",
                "location": "North Seattle, WA"
              }
            }
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0,
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HARASSMENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HATE_SPEECH",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "promptFeedback": {
    "safetyRatings": [
      {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_HATE_SPEECH",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_HARASSMENT",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "probability": "NEGLIGIBLE"
      }
    ]
  }
}
    

ANY modunu ve izin verilen işlevleri kullanan tek dönüş örneği

Aşağıdaki curl örneği şuna benzerdir: tek dönüşlü örneği, ancak mod'u ANY olarak ayarlar ve izin verilenlerin listesini içerir. işlevler:

"tool_config": {
  "function_calling_config": {
    "mode": "ANY",
    "allowed_function_names": ["find_theaters", "get_showtimes"]
  },
}

HERHANGİ BİR modu ve izin verilen işlevleri kullanarak tek dönüşlü işlev çağrısı (istek)

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \
  -H 'Content-Type: application/json' \
  -d '{
    "contents": {
      "role": "user",
      "parts": {
        "text": "What movies are showing in North Seattle tonight?"
    }
  },
  "tools": [
    {
      "function_declarations": [
        {
          "name": "find_movies",
          "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "description": {
                "type": "string",
                "description": "Any kind of description including category or genre, title words, attributes, etc."
              }
            },
            "required": [
              "description"
            ]
          }
        },
        {
          "name": "find_theaters",
          "description": "find theaters based on location and optionally movie title which is currently playing in theaters",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "movie": {
                "type": "string",
                "description": "Any movie title"
              }
            },
            "required": [
              "location"
            ]
          }
        },
        {
          "name": "get_showtimes",
          "description": "Find the start times for movies playing in a specific theater",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "movie": {
                "type": "string",
                "description": "Any movie title"
              },
              "theater": {
                "type": "string",
                "description": "Name of the theater"
              },
              "date": {
                "type": "string",
                "description": "Date for requested showtime"
              }
            },
            "required": [
              "location",
              "movie",
              "theater",
              "date"
            ]
          }
        }
      ]
    }
  ],
  "tool_config": {
    "function_calling_config": {
      "mode": "ANY",
      "allowed_function_names": ["find_theaters", "get_showtimes"]
    },
  }
}'
    

Model, izin verilen işlevler listesinde olmadığı için find_movies işlevini tahmin edemez ve bunun yerine farklı bir işlevi tahmin eder. Yanıt aşağıdakine benzer olabilir:

HERHANGİ BİR modu ve izin verilen işlevleri kullanarak tek dönüşlü işlev çağrısı (yanıt)

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "functionCall": {
              "name": "find_theaters",
              "args": {
                "location": "North Seattle, WA",
                "movie": null
              }
            }
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0,
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HARASSMENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HATE_SPEECH",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "promptFeedback": {
    "safetyRatings": [
      {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_HATE_SPEECH",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_HARASSMENT",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "probability": "NEGLIGIBLE"
      }
    ]
  }
}
    

Çok dönüşlü geçiş örnekleri

Aşağıdakileri yaparak çok dönüşlü işlev çağırma senaryosu uygulayabilirsiniz:

  1. Dil modelini çağırarak işlev çağrısı yanıtı alın. Bu ilk tur.
  2. İlk dönüşteki işlev çağrısı yanıtını kullanarak dil modelini çağırın ve bu işlevi çağırarak aldığınız fonksiyon yanıtını ekleyin. Bu ikinci tur.

İkinci dönüşün yanıtı, aklınıza takılanları cevaplamak için sonuçları bir fonksiyon çağrısı içeriyorsa veya ikinci bir fonksiyon çağrısını daha fazla bilgi edinin.

Bu konu, iki çok dönüşlü kıvrılma örneği içerir:

Önceki bir turda verilen yanıtı kullanma

Aşağıdaki curl örneği, önceki tek dönüş örneğini burada bulabilirsiniz. Yöntem ve parametreler tek dönüşlü örneği tarafından döndürülen tüm anahtar/değer çiftleri bu JSON'dadır.

"functionCall": {
  "name": "find_theaters",
  "args": {
    "movie": "Barbie",
    "location": "Mountain View, CA"
  }
}

Çok dönüşlü işlev çağrısı curl örnek isteği

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \
  -H 'Content-Type: application/json' \
  -d '{
    "contents": [{
      "role": "user",
      "parts": [{
        "text": "Which theaters in Mountain View show Barbie movie?"
    }]
  }, {
    "role": "model",
    "parts": [{
      "functionCall": {
        "name": "find_theaters",
        "args": {
          "location": "Mountain View, CA",
          "movie": "Barbie"
        }
      }
    }]
  }, {
    "role": "user",
    "parts": [{
      "functionResponse": {
        "name": "find_theaters",
        "response": {
          "name": "find_theaters",
          "content": {
            "movie": "Barbie",
            "theaters": [{
              "name": "AMC Mountain View 16",
              "address": "2000 W El Camino Real, Mountain View, CA 94040"
            }, {
              "name": "Regal Edwards 14",
              "address": "245 Castro St, Mountain View, CA 94040"
            }]
          }
        }
      }
    }]
  }],
  "tools": [{
    "functionDeclarations": [{
      "name": "find_movies",
      "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "location": {
            "type": "STRING",
            "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
          },
          "description": {
            "type": "STRING",
            "description": "Any kind of description including category or genre, title words, attributes, etc."
          }
        },
        "required": ["description"]
      }
    }, {
      "name": "find_theaters",
      "description": "find theaters based on location and optionally movie title which is currently playing in theaters",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "location": {
            "type": "STRING",
            "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
          },
          "movie": {
            "type": "STRING",
            "description": "Any movie title"
          }
        },
        "required": ["location"]
      }
    }, {
      "name": "get_showtimes",
      "description": "Find the start times for movies playing in a specific theater",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "location": {
            "type": "STRING",
            "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
          },
          "movie": {
            "type": "STRING",
            "description": "Any movie title"
          },
          "theater": {
            "type": "STRING",
            "description": "Name of the theater"
          },
          "date": {
            "type": "STRING",
            "description": "Date for requested showtime"
          }
        },
        "required": ["location", "movie", "theater", "date"]
      }
    }]
  }]
}'
    

Bu curl örneğinin yanıtı, find_theaters yönteminin çağrılmasının sonucunu içerir. Yanıt, aşağıdakine benzer olabilir:

Çok dönüşlü işlev çağrısı curl örnek yanıtı

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": " OK. Barbie is showing in two theaters in Mountain View, CA: AMC Mountain View 16 and Regal Edwards 14."
          }
        ]
      }
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 9,
    "candidatesTokenCount": 27,
    "totalTokenCount": 36
  }
}
    

Modeli birden fazla kez çağırma

Aşağıdaki cURL örneği, bir işlevi çağırmak için üretken yapay zeka modelini birden çok kez çağırır. Model, işlevi her çağrığında istekteki farklı bir kullanıcı sorgusunu yanıtlamak için farklı bir işlev kullanabilir.

Çok dönüşlü işlev çağrısı curl örnek isteği

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \
  -H 'Content-Type: application/json' \
  -d '{
    "contents": [{
      "role": "user",
      "parts": [{
        "text": "Which theaters in Mountain View show Barbie movie?"
    }]
  }, {
    "role": "model",
    "parts": [{
      "functionCall": {
        "name": "find_theaters",
        "args": {
          "location": "Mountain View, CA",
          "movie": "Barbie"
        }
      }
    }]
  }, {
    "role": "user",
    "parts": [{
      "functionResponse": {
        "name": "find_theaters",
        "response": {
          "name": "find_theaters",
          "content": {
            "movie": "Barbie",
            "theaters": [{
              "name": "AMC Mountain View 16",
              "address": "2000 W El Camino Real, Mountain View, CA 94040"
            }, {
              "name": "Regal Edwards 14",
              "address": "245 Castro St, Mountain View, CA 94040"
            }]
          }
        }
      }
    }]
  },
  {
    "role": "model",
    "parts": [{
      "text": " OK. Barbie is showing in two theaters in Mountain View, CA: AMC Mountain View 16 and Regal Edwards 14."
    }]
  },{
    "role": "user",
    "parts": [{
      "text": "Can we recommend some comedy movies on show in Mountain View?"
    }]
  }],
  "tools": [{
    "functionDeclarations": [{
      "name": "find_movies",
      "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "location": {
            "type": "STRING",
            "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
          },
          "description": {
            "type": "STRING",
            "description": "Any kind of description including category or genre, title words, attributes, etc."
          }
        },
        "required": ["description"]
      }
    }, {
      "name": "find_theaters",
      "description": "find theaters based on location and optionally movie title which is currently playing in theaters",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "location": {
            "type": "STRING",
            "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
          },
          "movie": {
            "type": "STRING",
            "description": "Any movie title"
          }
        },
        "required": ["location"]
      }
    }, {
      "name": "get_showtimes",
      "description": "Find the start times for movies playing in a specific theater",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "location": {
            "type": "STRING",
            "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
          },
          "movie": {
            "type": "STRING",
            "description": "Any movie title"
          },
          "theater": {
            "type": "STRING",
            "description": "Name of the theater"
          },
          "date": {
            "type": "STRING",
            "description": "Date for requested showtime"
          }
        },
        "required": ["location", "movie", "theater", "date"]
      }
    }]
  }]
}'
    

Çok turlu işlev çağırma curl örnek yanıtı

[{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "functionCall": {
              "name": "find_movies",
              "args": {
                "description": "comedy",
                "location": "Mountain View, CA"
              }
            }
          }
        ]
      },
      "finishReason": "STOP",
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_HARASSMENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HATE_SPEECH",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 48,
    "totalTokenCount": 48
  }
}
]
    

En iyi uygulamalar

İşlev çağrılarınızın doğruluğunu ve güvenilirliğini artırmak için aşağıdaki en iyi uygulamalardan yararlanın.

Kullanıcı istemi

En iyi sonuçlar için kullanıcı sorgusuna aşağıdaki ayrıntıları ekleyin:

  • Model için ek bağlam. Örneğin, You are a movie API assistant to help users find movies and showtimes based on their preferences.
  • İşlevlerin nasıl ve ne zaman kullanılacağına ilişkin ayrıntılar veya talimatlar. Örneğin, Don't make assumptions on showtimes. Always use a future date for showtimes.
  • Kullanıcı sorguları belirsizse netleştirici sorular sorma talimatları. Örneğin, Ask clarifying questions if not enough information is available to complete the request.

Örnekleme parametreleri

Sıcaklık parametresi için 0 veya başka bir düşük değer kullanın. Bu komut ve halüsinasyonları azaltan bir model kullanır.

API çağrısı

Model, bir sipariş gönderecek, veritabanını güncelleyecek veya başka önemli sonuçlara yol açacak bir işlevin çağrılmasını öneriyorsa işlev çağrısını yürütmeden önce kullanıcıyla doğrulayın.