Chiamata di funzione

Le funzioni personalizzate possono essere definite e fornite a un modello di IA generativa utilizzando le chiamate di funzione. Il modello non richiama direttamente queste funzioni, ma genera invece un output di dati strutturati che specifica il nome della funzione e gli argomenti suggeriti. Questo output consente la chiamata di API esterne e l'output API risultante può essere incorporato nuovamente nel modello, consentendo risposte più complete alle query. La chiamata di funzione consente agli LLM di interagire con informazioni in tempo reale e vari servizi, come database, sistemi di gestione dei rapporti con i clienti e repository di documenti, migliorando la loro capacità di fornire risposte pertinenti e contestuali.

Come funzionano le chiamate di funzione

Le funzioni sono descritte mediante le dichiarazioni delle funzioni. Dopo aver trasmesso a un modello linguistico un elenco di dichiarazioni di funzioni in una query, il modello restituisce un oggetto in un formato schema compatibile con OpenAPI che include i nomi delle funzioni e i relativi argomenti e tenta di rispondere alla query dell'utente con una delle funzioni restituite. Il modello linguistico comprende lo scopo di una funzione analizzandone la dichiarazione. Il modello in realtà non chiama la funzione. Uno sviluppatore utilizza invece l'oggetto Schema compatibile con OpenAPI nella risposta per chiamare la funzione restituita dal modello.

Quando implementi le chiamate di funzione, crei una o più dichiarazioni di funzione e poi aggiungi le dichiarazioni di funzione a un oggetto tools che viene passato al modello. Ogni dichiarazione di una funzione contiene informazioni su una funzione che include quanto segue:

  • Nome funzione
  • Parametri delle funzioni in formato schema compatibile con OpenAPI. È supportato un sottoinsieme selezionato. Quando utilizzi curl, lo schema viene specificato utilizzando JSON.
  • Descrizione della funzione (facoltativa). Per ottenere risultati ottimali, consigliamo di includere una descrizione.

Questo documento include esempi di curl che effettuano chiamate REST con la classe GenerativeModel e i suoi metodi.

Modelli supportati

I seguenti modelli supportano le chiamate delle funzioni:

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

Modalità di chiamata delle funzioni

Puoi utilizzare la chiamata di funzione mode per definire il comportamento di esecuzione per le chiamate di funzione. Sono disponibili tre modalità:

  • AUTO: il comportamento predefinito del modello. Il modello decide di prevedere una chiamata di funzione o una risposta in linguaggio naturale.
  • ANY: il modello è vincolato a prevedere sempre una chiamata di funzione. Se allowed_function_names non viene fornito, il modello sceglie tra tutte le dichiarazioni di funzione disponibili. Se allowed_function_names viene fornito, il modello sceglie dall'insieme di funzioni consentite.
  • NONE: il modello non prevede una chiamata di funzione. In questo caso, il comportamento del modello è lo stesso che se non passi alcuna dichiarazione di funzione.

Puoi anche passare un insieme di allowed_function_names che, se fornito, limita le funzioni che il modello chiamerà. Devi includere allowed_function_names solo quando la modalità è ANY. I nomi delle funzioni devono corrispondere a quelli delle dichiarazioni delle funzioni. Con la modalità impostata su ANY e allowed_function_names impostata, il modello prevede una chiamata di funzione dall'insieme di nomi di funzione fornito.

Ecco parte di una richiesta di esempio che imposta la modalità su ANY e specifica un elenco di funzioni consentite:

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

Funzione che chiama esempi cURL

Quando utilizzi cURL, le informazioni relative a funzioni e parametri sono incluse nell'elemento tools. Ogni dichiarazione di funzione nell'elemento tools contiene il nome della funzione, i parametri specificati utilizzando lo schema compatibile OpenAPI e una descrizione della funzione. I seguenti esempi mostrano come utilizzare i comandi curl con le chiamate di funzione:

Esempio di curl a giro singolo

A turno singolo quando chiami il modello linguistico una volta. Con le chiamate di funzione, un caso d'uso a turno singolo potrebbe essere quando fornisci al modello una query in linguaggio naturale e un elenco di funzioni. In questo caso, il modello utilizza la dichiarazione della funzione, che include il nome, i parametri e la descrizione della funzione, per prevedere la funzione da chiamare e gli argomenti con cui chiamarla.

Il seguente esempio di curl è un esempio di trasferimento di una descrizione di una funzione che restituisce informazioni sul luogo in cui è in riproduzione un film. Nella richiesta sono incluse diverse dichiarazioni di funzioni, ad esempio find_movies e find_theaters.

Funzione a turno singolo che chiama una richiesta di esempio 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"
            ]
          }
        }
      ]
    }
  ]
}'
    

La risposta a questo esempio di curl potrebbe essere simile alla seguente.

Funzione a turno singolo che chiama curl di esempio di risposta

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

Esempio a turno singolo con la modalità QUALSIASI

Il seguente esempio di curl è simile all'esempio a turno singolo, ma imposta la modalità su ANY:

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

Chiamata di funzione a turno singolo utilizzando QUALSIASI modalità (richiesta)

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

La risposta potrebbe essere simile alla seguente:

Chiamata di funzione a turno singolo utilizzando QUALSIASI modalità (risposta)

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

Esempio a turno singolo utilizzando la modalità QUALSIASI e le funzioni consentite

Il seguente esempio di curl è simile all'esempio a turno singolo, ma imposta la modalità su ANY e include un elenco di funzioni consentite:

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

Chiamata di funzione a turno singolo utilizzando QUALSIASI modalità e funzioni consentite (richiesta)

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

Il modello non può prevedere la funzione find_movies poiché non è nell'elenco delle funzioni consentite, quindi prevede una funzione diversa. La risposta potrebbe essere simile alla seguente:

Chiamata di funzione a turno singolo utilizzando la modalità QUALSIASI e le funzioni consentite (risposta)

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

Esempi di arricciature in più passaggi

Puoi implementare uno scenario di chiamata di funzione in più passaggi procedendo nel seguente modo:

  1. Per ottenere una risposta alla chiamata di funzione, chiama il modello linguistico. Questo è il primo turno.
  2. Richiama il modello linguistico utilizzando la risposta della chiamata di funzione del primo turno e la risposta della funzione che ottieni dalla chiamata di quella funzione. Questo è il secondo turno.

La risposta del secondo turno riassume i risultati per rispondere alla tua query nel primo turno oppure contiene una seconda chiamata di funzione che puoi utilizzare per ottenere ulteriori informazioni sulla query.

Questo argomento include due esempi di curl in più passaggi:

Esempio di curl che utilizza una risposta di una svolta precedente

Il seguente esempio di curl chiama la funzione e gli argomenti restituiti dall'esempio precedente a turno singolo per ottenere una risposta. Il metodo e i parametri restituiti dall'esempio a turno singolo sono in questo JSON.

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

Esempio di richiesta curl che chiama la funzione multi-turno

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

La risposta a questo esempio di curl include il risultato della chiamata al metodo find_theaters. La risposta potrebbe essere simile alla seguente:

Esempio di risposta curl che chiama la funzione in più passaggi

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

Esempio di Curl che chiama un modello linguistico più volte

Il seguente esempio di curl chiama il modello linguistico più volte per chiamare una funzione. Ogni volta che il modello chiama la funzione, può utilizzare una funzione diversa per rispondere a una diversa query utente nella richiesta.

Esempio di richiesta curl che chiama la funzione multi-turno

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

Esempio di risposta curl che chiama la funzione in più passaggi

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

Best practice

Segui queste best practice per migliorare la precisione e l'affidabilità delle chiamate alle funzioni.

Campi chiave funzione

Definire in modo accurato le funzioni è fondamentale per integrarle nelle richieste. Ogni funzione si basa su parametri specifici che ne guidano il comportamento e l'interazione con il modello. Ecco un'analisi dei parametri chiave utilizzati nell'array functions_declarations.

function_declarations (array):

  • Contiene uno o più oggetti, ognuno dei quali rappresenta una funzione distinta.

All'interno di ogni oggetto function_declarations:

  • name (stringa): l'identificatore univoco della funzione nell'ambito della chiamata API.
    • Best practice: utilizza nomi chiari e descrittivi senza caratteri di spazio, punto (.) o trattino (-). Utilizza invece il trattino basso (_) o la custodia a cammello.
  • description (stringa): una spiegazione completa dello scopo e delle capacità della funzione.
    • Best practice: fornisci esempi dettagliati, chiari e specifici nelle descrizioni delle funzioni, se necessario. Ad esempio, anziché find theaters, utilizza find theaters based on location and optionally movie title that is currently playing in theaters. Evita descrizioni troppo ampie o ambigue.
  • parameters (oggetto): definisce i dati di input richiesti dalla funzione.
    • type (stringa): specifica il tipo di dati complessivo (ad es. object).
    • properties (oggetto):
      • Elenca i singoli parametri, ciascuno con:
        • type (stringa): il tipo di dati del parametro (ad es. string, integer e boolean).
          • Best practice: utilizza parametri altamente digitati per ridurre le allucinazioni nei modelli. Ad esempio, se i valori dei parametri provengono da un insieme finito, utilizza un campo enum anziché elencare i valori nella descrizione (ad es. "type": "enum", "values": ["now_playing", "upcoming"]). Se il valore del parametro è sempre un numero intero, imposta il tipo su integer anziché su number.
        • description (stringa): una spiegazione chiara dello scopo e del formato previsto del parametro.
          • Best practice: fornisci esempi e vincoli concreti. Ad esempio, utilizza The city and state, e.g. San Francisco, CA or a zip code e.g. 95616 invece di the location to search.
    • required (array):
      • Un array di stringhe che elencano i nomi dei parametri obbligatori per il funzionamento della funzione.

Richiesta dell'utente

Per ottenere risultati ottimali, anteponi i seguenti dettagli alla query dell'utente:

  • Contesto aggiuntivo per il modello. Ad esempio, You are a movie API assistant to help users find movies and showtimes based on their preferences.
  • Dettagli o istruzioni su come e quando utilizzare le funzioni. Ad esempio, Don't make assumptions on showtimes. Always use a future date for showtimes.
  • Istruzioni per porre domande di chiarimento in caso di query degli utenti ambigue. Ad esempio, Ask clarifying questions if not enough information is available to complete the request.

Parametri di campionamento

Per il parametro della temperatura, utilizza 0 o un altro valore basso. Questo indica al modello di generare risultati più affidabili e riduce le allucinazioni.

Chiamata API

Se il modello propone di chiamare una funzione che invierebbe un ordine, aggiornerà un database o avrebbe comunque conseguenze significative, convalida la chiamata della funzione con l'utente prima di eseguirla.