Panggilan fungsi

Fungsi kustom dapat ditentukan dan diberikan ke model AI generatif menggunakan panggilan fungsi. Model tidak langsung memanggil fungsi ini, tetapi menghasilkan output data terstruktur yang menentukan nama fungsi dan argumen yang disarankan. Output ini memungkinkan panggilan API eksternal, dan output API yang dihasilkan kemudian dapat dimasukkan kembali ke dalam model, sehingga memungkinkan respons kueri yang lebih komprehensif. Panggilan fungsi mendukung LLM untuk berinteraksi dengan informasi real-time dan berbagai layanan, seperti database, sistem pengelolaan hubungan pelanggan, dan repositori dokumen, sehingga meningkatkan kemampuan untuk memberikan jawaban yang relevan dan kontekstual.

Cara kerja panggilan fungsi

Fungsi dijelaskan menggunakan deklarasi fungsi. Setelah Anda meneruskan daftar deklarasi fungsi dalam kueri ke model bahasa, model akan menampilkan objek dalam format skema yang kompatibel dengan OpenAPI yang menyertakan nama fungsi dan argumennya serta mencoba menjawab kueri pengguna dengan salah satu fungsi yang ditampilkan. Model bahasa ini memahami tujuan suatu fungsi dengan menganalisis deklarasi fungsinya. Model tersebut tidak benar-benar memanggil fungsi. Sebagai gantinya, developer menggunakan objek skema yang kompatibel dengan OpenAPI sebagai respons untuk memanggil fungsi yang ditampilkan model.

Saat mengimplementasikan panggilan fungsi, Anda harus membuat satu atau beberapa deklarasi fungsi, lalu menambahkan deklarasi fungsi ke objek tools yang diteruskan ke model. Setiap deklarasi fungsi berisi informasi tentang satu fungsi yang mencakup hal berikut:

  • Function name
  • Parameter fungsi dalam format skema yang kompatibel dengan OpenAPI. Subset pilih didukung. Saat menggunakan curl, skema ditetapkan menggunakan JSON.
  • Deskripsi fungsi (opsional). Untuk hasil terbaik, sebaiknya sertakan deskripsi.

Dokumen ini menyertakan contoh curl yang melakukan panggilan REST dengan class GenerativeModel dan metodenya.

Model yang didukung

Model berikut mendukung panggilan fungsi:

  • gemini-1.0-pro
  • gemini-1.0-pro-001
  • gemini-1.5-pro-latest

Mode pemanggilan fungsi

Anda dapat menggunakan fungsi yang memanggil mode untuk menentukan perilaku eksekusi untuk panggilan fungsi. Ada tiga mode yang tersedia:

  • AUTO: Perilaku model default. Model tersebut memutuskan untuk memprediksi panggilan fungsi atau respons natural language.
  • ANY: Model dibatasi untuk selalu memprediksi panggilan fungsi. Jika allowed_function_names tidak disediakan, model akan dipilih dari semua deklarasi fungsi yang tersedia. Jika allowed_function_names ditentukan, model akan memilih dari kumpulan fungsi yang diizinkan.
  • NONE: Model tidak akan memprediksi panggilan fungsi. Dalam hal ini, perilaku model sama seperti jika Anda tidak meneruskan deklarasi fungsi apa pun.

Anda juga dapat meneruskan kumpulan allowed_function_names yang, jika disediakan, akan membatasi fungsi yang akan dipanggil oleh model. Anda hanya boleh menyertakan allowed_function_names saat modenya adalah ANY. Nama fungsi harus cocok dengan nama deklarasi fungsi. Dengan mode yang ditetapkan ke ANY dan allowed_function_names yang ditetapkan, model akan memprediksi panggilan fungsi dari kumpulan nama fungsi yang diberikan.

Berikut adalah bagian dari contoh permintaan yang menetapkan mode ke ANY dan menentukan daftar fungsi yang diizinkan:

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

Fungsi yang memanggil sampel cURL

Saat Anda menggunakan cURL, informasi fungsi dan parameter akan disertakan dalam elemen tools. Setiap deklarasi fungsi dalam elemen tools berisi nama fungsi, parameternya yang ditentukan menggunakan skema yang kompatibel dengan OpenAPI, dan deskripsi fungsi. Contoh berikut menunjukkan cara menggunakan perintah curl dengan panggilan fungsi:

Contoh curl satu giliran

Satu putaran adalah saat Anda memanggil model bahasa satu kali. Dengan panggilan fungsi, kasus penggunaan satu giliran mungkin terjadi saat Anda memberikan kueri natural language dan daftar fungsi kepada model. Dalam hal ini, model menggunakan deklarasi fungsi, yang mencakup nama fungsi, parameter, dan deskripsi, untuk memprediksi fungsi mana yang akan dipanggil dan argumen yang akan dipanggil.

Contoh curl berikut adalah contoh penerusan deskripsi fungsi yang menampilkan informasi tentang tempat film diputar. Beberapa deklarasi fungsi disertakan dalam permintaan, seperti find_movies dan find_theaters.

Fungsi satu giliran memanggil permintaan contoh curl

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"
            ]
          }
        }
      ]
    }
  ]
}'
    

Respons untuk contoh curl ini mungkin mirip dengan berikut ini.

Fungsi satu giliran memanggil respons contoh curl

[{
  "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
  }
}]
    

Contoh satu giliran menggunakan mode APA PUN

Contoh curl berikut mirip dengan contoh satu putaran, tetapi menetapkan mode ke ANY:

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

Panggilan fungsi satu giliran menggunakan mode APA PUN (permintaan)

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"
    },
  }
}'
    

Responsnya mungkin mirip dengan berikut ini:

Pemanggilan fungsi satu giliran menggunakan mode APA PUN (respons)

{
  "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"
      }
    ]
  }
}
    

Contoh satu giliran yang menggunakan mode APA PUN dan fungsi yang diizinkan

Contoh curl berikut mirip dengan contoh satu putaran, tetapi menetapkan mode ke ANY dan menyertakan daftar fungsi yang diizinkan:

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

Panggilan fungsi satu giliran menggunakan mode APA PUN dan fungsi yang diizinkan (permintaan)

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 tidak dapat memprediksi fungsi find_movies, karena tidak ada dalam daftar fungsi yang diizinkan, sehingga memprediksi fungsi yang berbeda. Responsnya mungkin mirip dengan berikut ini:

Pemanggilan fungsi satu giliran menggunakan mode APA PUN dan fungsi yang diizinkan (respons)

{
  "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"
      }
    ]
  }
}
    

Contoh curl multi-giliran

Anda dapat menerapkan skenario panggilan fungsi multi-giliran dengan melakukan hal berikut:

  1. Dapatkan respons panggilan fungsi dengan memanggil model bahasa. Ini adalah putaran pertama.
  2. Panggil model bahasa menggunakan respons panggilan fungsi dari belokan pertama dan respons fungsi yang Anda dapatkan setelah memanggil fungsi tersebut. Ini adalah putaran kedua.

Respons dari belokan kedua merangkum hasil untuk menjawab kueri pada putaran pertama, atau berisi panggilan fungsi kedua yang dapat Anda gunakan untuk mendapatkan informasi lebih lanjut terkait kueri Anda.

Topik ini mencakup dua contoh curl multi-giliran:

Contoh curl yang menggunakan respons dari belokan sebelumnya

Contoh curl berikut memanggil fungsi dan argumen yang ditampilkan oleh contoh satu giliran sebelumnya untuk mendapatkan respons. Metode dan parameter yang ditampilkan oleh contoh putaran tunggal ada dalam JSON ini.

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

Fungsi multi-putar memanggil permintaan contoh curl

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": "function",
    "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"]
      }
    }]
  }]
}'
    

Respons untuk contoh curl ini mencakup hasil pemanggilan metode find_theaters. Responsnya mungkin mirip dengan berikut ini:

Fungsi multi-giliran memanggil respons contoh curl

{
  "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
  }
}
    

Contoh curl yang memanggil model bahasa beberapa kali

Contoh curl berikut memanggil model bahasa beberapa kali untuk memanggil fungsi. Setiap kali fungsi memanggil fungsi, model dapat menggunakan fungsi yang berbeda untuk menjawab kueri pengguna yang berbeda dalam permintaan.

Fungsi multi-putar memanggil permintaan contoh curl

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": "function",
    "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"]
      }
    }]
  }]
}'
    

Fungsi multi-giliran memanggil respons contoh curl

[{
  "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
  }
}
]
    

Praktik terbaik

Ikuti praktik terbaik berikut untuk meningkatkan akurasi dan keandalan panggilan fungsi Anda.

Kolom tombol fungsi

Menentukan fungsi secara akurat sangat penting ketika mengintegrasikannya ke dalam permintaan. Setiap fungsi bergantung pada parameter tertentu yang memandu perilaku dan interaksinya dengan model. Berikut adalah perincian parameter utama yang digunakan dalam array functions_declarations.

function_declarations (array):

  • Berisi satu atau beberapa objek, masing-masing mewakili fungsi yang berbeda.

Dalam setiap objek function_declarations:

  • name (string): ID unik untuk fungsi dalam panggilan API.
    • Praktik terbaik: Gunakan nama yang jelas dan deskriptif tanpa karakter spasi, titik (.), atau tanda hubung (-). Sebagai gantinya, gunakan karakter garis bawah (_) atau camel case.
  • description (string): Penjelasan komprehensif tentang tujuan dan kemampuan fungsi.
    • Praktik terbaik: Berikan contoh yang mendetail, jelas, dan spesifik dalam deskripsi fungsi, jika perlu. Misalnya, gunakan find theaters based on location and optionally movie title that is currently playing in theaters., bukan find theaters, Hindari deskripsi yang terlalu luas atau ambigu.
  • parameters (objek): Menentukan data input yang diperlukan oleh fungsi.
    • type (string): Menentukan jenis data secara keseluruhan (mis., object).
    • properties (objek):
      • Menampilkan daftar parameter individual, masing-masing dengan:
        • type (string): Jenis data parameter (mis., string, integer, boolean).
          • Praktik terbaik: Gunakan parameter yang diketik dengan kuat untuk mengurangi halusinasi model. Misalnya, jika nilai parameter berasal dari set terbatas, gunakan kolom enum, bukan mencantumkan nilai dalam deskripsi (mis., "type": "enum", "values": ["now_playing", "upcoming"]). Jika nilai parameter selalu berupa bilangan bulat, setel jenisnya ke integer, bukan number.
        • description (string): Penjelasan yang jelas tentang tujuan parameter dan format yang diharapkan.
          • Praktik terbaik: Berikan contoh dan batasan konkret. Misalnya, gunakan The city and state, e.g. San Francisco, CA or a zip code e.g. 95616, bukan the location to search.
    • required (array):
      • Array string yang mencantumkan nama parameter yang wajib agar fungsi dapat beroperasi.

Perintah pengguna

Untuk hasil terbaik, tambahkan detail berikut ke kueri pengguna:

  • Konteks tambahan untuk model. Contoh, You are a movie API assistant to help users find movies and showtimes based on their preferences.
  • Detail atau petunjuk tentang cara dan waktu untuk menggunakan fungsi. Contoh, Don't make assumptions on showtimes. Always use a future date for showtimes.
  • Petunjuk untuk mengajukan pertanyaan klarifikasi jika kueri pengguna ambigu. Contoh, Ask clarifying questions if not enough information is available to complete the request.

Parameter pengambilan sampel

Untuk parameter suhu, gunakan 0 atau nilai rendah lainnya. Hal ini memerintahkan model untuk memberikan hasil yang lebih meyakinkan dan mengurangi halusinasi.

Pemanggilan API

Jika model mengusulkan pemanggilan fungsi yang akan mengirim pesanan, memperbarui database, atau memiliki konsekuensi yang signifikan, validasi panggilan fungsi tersebut dengan pengguna sebelum menjalankannya.