ข้อมูลเบื้องต้นเกี่ยวกับการเรียกฟังก์ชันด้วย Gemini API

สามารถกำหนดและจัดเตรียมฟังก์ชันที่กำหนดเองให้กับโมเดล Gemini โดยใช้ฟังก์ชัน ฟีเจอร์การโทร โมเดลจะเรียกใช้ฟังก์ชันเหล่านี้โดยตรง แต่จะเรียกใช้ฟังก์ชันอื่นแทน สร้างเอาต์พุต Structured Data ที่ระบุชื่อฟังก์ชันและรายการที่แนะนำ อาร์กิวเมนต์ ผลลัพธ์นี้จะช่วยให้คุณสามารถเขียนแอปพลิเคชันที่ใช้ เอาต์พุตและเรียก API ภายนอก แล้วเอาต์พุต API ที่ได้จะสามารถเป็น รวมอยู่ในพรอมต์โมเดลเพิ่มเติมที่ช่วยให้ได้การค้นหาที่ครอบคลุมยิ่งขึ้น คำตอบ การเรียกใช้ฟังก์ชันช่วยให้ผู้ใช้โต้ตอบกับฟังก์ชันแบบเรียลไทม์ ข้อมูลและบริการต่างๆ เช่น ฐานข้อมูล ความสัมพันธ์กับลูกค้า ระบบการจัดการ และที่เก็บเอกสาร เพื่อช่วยเพิ่มความสามารถในการ ให้คำตอบที่เกี่ยวข้องและตามบริบท

วิธีการทำงานของการเรียกใช้ฟังก์ชัน

คุณใช้ฟีเจอร์การเรียกฟังก์ชันโดยการเพิ่มข้อมูลการค้นหาที่มีโครงสร้างซึ่งอธิบาย การเขียนโปรแกรมอินเทอร์เฟซที่เรียกว่าการประกาศฟังก์ชันไปยังพรอมต์ของโมเดล การประกาศฟังก์ชัน ระบุชื่อฟังก์ชัน API อธิบายวัตถุประสงค์ พารามิเตอร์ที่ระบบรองรับ รวมถึงคำอธิบายพารามิเตอร์เหล่านั้น หลังจากคุณผ่านแล้ว รายการการประกาศฟังก์ชันในการค้นหาไปยังโมเดล โมเดลจะวิเคราะห์ฟังก์ชัน และคำค้นหาส่วนที่เหลือเพื่อกำหนดวิธีใช้ API ตามคำขอ

จากนั้นโมเดลจะแสดงออบเจ็กต์ใน สคีมาที่เข้ากันได้กับ OpenAPI ระบุวิธีเรียกใช้ฟังก์ชันที่ประกาศอย่างน้อย 1 รายการเพื่อตอบกลับ สำหรับคำถามของผู้ใช้ จากนั้นคุณสามารถใช้การเรียกฟังก์ชันที่แนะนำ เรียก API จริง รับการตอบกลับ และแสดงการตอบสนองนั้นให้กับ ผู้ใช้หรือดำเนินการเพิ่มเติม โปรดทราบว่าจริงๆ แล้วโมเดลไม่ได้เรียกเมธอด ที่ประกาศ ให้ใช้พารามิเตอร์ออบเจ็กต์สคีมาที่แสดงผลเพื่อ เรียกใช้ฟังก์ชัน Gemini API ยังรองรับการเรียกใช้ฟังก์ชันพร้อมกันด้วย โดย โมเดลนี้แนะนำให้เรียกใช้ฟังก์ชัน API หลายครั้งโดยอิงตามคำขอเดียว

รุ่นที่รองรับ

รุ่นต่อไปนี้รองรับฟีเจอร์การเรียกใช้ฟังก์ชัน

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

การประกาศฟังก์ชัน

การใช้การเรียกฟังก์ชันในพรอมต์จะเป็นการสร้างออบเจ็กต์ tools ซึ่งมี function declarations อย่างน้อย 1 รายการ คุณกำหนดฟังก์ชัน ใช้ JSON โดยเฉพาะที่มี เลือกชุดย่อย ของ สคีมา OpenAPI การประกาศฟังก์ชันเดียวอาจมีพารามิเตอร์ต่อไปนี้

  • name (สตริง): ตัวระบุที่ไม่ซ้ำกันสำหรับฟังก์ชันภายใน API การโทร
  • description (สตริง): คำอธิบายโดยละเอียดเกี่ยวกับฟังก์ชัน และความสามารถ
  • parameters (ออบเจ็กต์): กำหนดข้อมูลอินพุตที่ฟังก์ชันต้องการ
    • type (สตริง): ระบุประเภทข้อมูลโดยรวม เช่น object
    • properties (ออบเจ็กต์): แสดงรายการพารามิเตอร์แต่ละรายการ โดยแต่ละรายการมีสิ่งต่อไปนี้
      • type (สตริง): ประเภทข้อมูลของพารามิเตอร์ เช่น string, integer, boolean
      • description (สตริง): คำอธิบายที่ชัดเจนเกี่ยวกับพารามิเตอร์ วัตถุประสงค์และรูปแบบที่คาดไว้
    • required (อาร์เรย์): อาร์เรย์ของสตริงที่แสดงชื่อพารามิเตอร์ ซึ่งจำเป็นสำหรับการทำงานของฟังก์ชัน

ดูตัวอย่างโค้ดของการประกาศฟังก์ชันโดยใช้คำสั่ง cURL ได้ที่ ตัวอย่างการเรียกใช้ฟังก์ชัน ตัวอย่างเช่น และข้อมูลเกี่ยวกับการสร้างการประกาศฟังก์ชันสำหรับแพลตฟอร์มอื่นๆ โปรดดู คำแนะนำเกี่ยวกับแพลตฟอร์มการเรียกใช้ฟังก์ชัน

แนวทางปฏิบัติแนะนำสำหรับการประกาศฟังก์ชัน

การกำหนดฟังก์ชันให้ถูกต้องเป็นสิ่งจำเป็นในการผสานรวมฟังก์ชันเข้ากับ คำขอ แต่ละฟังก์ชันจะใช้พารามิเตอร์ที่เฉพาะเจาะจงซึ่งเป็นแนวทางในการทำงาน และการโต้ตอบกับโมเดล รายการต่อไปนี้จะให้คำแนะนำเกี่ยวกับ การกำหนดพารามิเตอร์ของแต่ละฟังก์ชันใน functions_declarations อาร์เรย์

  • name: ใช้ชื่อที่สื่อความหมายและชัดเจนโดยไม่ต้องมีช่องว่างและจุด (.) หรืออักขระขีดกลาง (-) โปรดใช้อักขระขีดล่าง (_) แทน หรืออูฐก็ได้

  • description: ระบุฟังก์ชันอย่างละเอียด ชัดเจน และเฉพาะเจาะจง คำอธิบายโดยยกตัวอย่าง ถ้าจำเป็น ตัวอย่างเช่น แทนที่จะแสดง find theaters โปรดใช้ find theaters based on location and optionally movie title that is currently playing in theaters. หลีกเลี่ยงการใช้คำที่กว้างหรือกำกวมเกินไป คำอธิบาย

  • properties type: ใช้พารามิเตอร์ที่พิมพ์อย่างเข้มงวดเพื่อลด ความไม่สมเหตุสมผลของโมเดล เช่น หากค่าพารามิเตอร์มาจากขอบเขตจำกัด ให้ใช้ช่อง enum แทนการแสดงค่าไว้ในคำอธิบาย (เช่น "type": "enum", "values": ["now_playing", "upcoming"]) หาก จะเป็นจำนวนเต็มเสมอ ให้กำหนดประเภทเป็น integer แทนที่จะแสดง number

  • properties description: แสดงตัวอย่างที่เป็นรูปธรรมและข้อจำกัด เช่น ใช้ The city and state, e.g. San Francisco, CA or a zip code e.g. 95616 แทน the location to search

สำหรับแนวทางปฏิบัติที่ดีที่สุดเพิ่มเติมเมื่อใช้การเรียกใช้ฟังก์ชัน โปรดดู แนวทางปฏิบัติแนะนำ

โหมดการเรียกฟังก์ชัน

คุณสามารถใช้พารามิเตอร์การเรียกฟังก์ชัน mode เพื่อแก้ไขการดำเนินการ ลักษณะการทำงานของฟีเจอร์ โดยมี 3 โหมดดังต่อไปนี้

  • AUTO: ลักษณะการทำงานของโมเดลเริ่มต้น โมเดลตัดสินใจที่จะคาดการณ์ หรือการตอบสนองด้วยภาษาธรรมชาติ
  • ANY: โมเดลถูกจำกัดให้คาดการณ์การเรียกใช้ฟังก์ชันเสมอ ถ้า ไม่ได้ให้ allowed_function_names โดยโมเดลจะเลือกจาก การประกาศฟังก์ชันที่ใช้ได้ หากระบุ allowed_function_names ไว้ โมเดลเลือกจากชุดฟังก์ชันที่อนุญาต
  • NONE: โมเดลจะไม่คาดการณ์การเรียกใช้ฟังก์ชัน ในกรณีนี้ โมเดล จะเหมือนกับการไม่ผ่านการตรวจสอบฟังก์ชันใดๆ

คุณยังสามารถส่งผ่านชุด allowed_function_names ซึ่งขีดจำกัดเมื่อมีให้ ฟังก์ชันที่โมเดลจะเรียกใช้ คุณควรใส่เฉพาะ allowed_function_namesเมื่อโหมดคือ ANY ชื่อฟังก์ชันควรตรงกับ ชื่อการประกาศฟังก์ชัน เมื่อตั้งค่าโหมดเป็น ANY และ allowed_function_names โมเดลจะคาดการณ์การเรียกใช้ฟังก์ชันจาก ของชื่อฟังก์ชัน ข้อมูลโค้ดต่อไปนี้จาก คำขอตัวอย่างแสดงวิธี ตั้งค่า mode เป็น ANY และระบุรายการฟังก์ชันที่อนุญาต ดังนี้

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

ตัวอย่างการเรียกใช้ฟังก์ชัน

ส่วนนี้จะแสดงตัวอย่างพรอมต์สำหรับการเรียกใช้ฟังก์ชันโดยใช้คำสั่ง cURL ตัวอย่างเช่น สถานการณ์จำลองที่เลี้ยวเพียงครั้งเดียวและหลายเลี้ยว โหมดการโทรด้วยฟังก์ชันต่างๆ

เมื่อใช้คำสั่ง cURL กับฟีเจอร์นี้ ฟังก์ชันและพารามิเตอร์ จะรวมอยู่ในองค์ประกอบ tools การประกาศฟังก์ชันแต่ละรายการใน เอลิเมนต์ tools มีชื่อฟังก์ชัน และคุณระบุพารามิเตอร์โดยใช้ สคีมาที่เข้ากันได้กับ OpenAPI และคำอธิบายฟังก์ชัน

ตัวอย่างแบบเลี้ยวเดียว

จุดเดียวคือเมื่อเรียกใช้โมเดลภาษา 1 ครั้ง ด้วยการเรียกฟังก์ชัน กรณีการใช้งานแบบผลัดเดียวอาจเป็นเมื่อคุณให้ภาษาธรรมชาติกับโมเดล และรายการฟังก์ชัน ในกรณีนี้ โมเดลจะใช้ฟังก์ชัน ซึ่งมีชื่อฟังก์ชัน พารามิเตอร์ และคำอธิบาย คาดการณ์ฟังก์ชันที่จะเรียกใช้และอาร์กิวเมนต์ที่จะใช้

ตัวอย่าง Curl ต่อไปนี้เป็นตัวอย่างของการส่งผ่านในคำอธิบายของ ที่ส่งคืนข้อมูลเกี่ยวกับตำแหน่งที่ภาพยนตร์กำลังเล่น หลายรายการ การประกาศฟังก์ชันรวมอยู่ในคำขอ เช่น find_movies และ find_theaters

คำขอตัวอย่างการเรียกใช้ฟังก์ชันแบบเลี้ยวเดียว

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

คำตอบสำหรับตัวอย่าง curl นี้อาจคล้ายกับตัวอย่างต่อไปนี้

ตัวอย่างการตอบสนองด้วย 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
  }
}]
    

ตัวอย่างแบบเลี้ยวเดียวโดยใช้โหมดใดก็ได้

ตัวอย่าง curl ต่อไปนี้ดูคล้ายกับ แบบผลัดเดียว แต่กลับ โหมดเป็น ANY:

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

การเรียกฟังก์ชันแบบเลี้ยวเดียวโดยใช้โหมดใดก็ได้ (คำขอ)

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

คำตอบอาจคล้ายกับข้อความต่อไปนี้

การเรียกฟังก์ชันแบบเลี้ยวเดียวโดยใช้โหมดใดก็ได้ (การตอบกลับ)

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

ตัวอย่างแบบเลี้ยวเดียวที่ใช้โหมดใดก็ได้และฟังก์ชันที่อนุญาต

ตัวอย่าง curl ต่อไปนี้ดูคล้ายกับ แบบผลัดเดียว แต่กลับ โหมดเป็น ANY และมีรายการที่อนุญาต ฟังก์ชัน:

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

การเรียกฟังก์ชันแบบเลี้ยวเดียวโดยใช้โหมดใดก็ได้และฟังก์ชันที่อนุญาต (คำขอ)

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

โมเดลนี้คาดการณ์ฟังก์ชัน find_movies ไม่ได้เนื่องจากไม่ได้อยู่ในรายการ ของฟังก์ชันที่อนุญาต โมเดลจึงคาดการณ์ฟังก์ชันอื่นแทน คำตอบ อาจมีลักษณะคล้ายกับข้อความต่อไปนี้

การเรียกฟังก์ชันแบบเลี้ยวเดียวโดยใช้โหมดใดก็ได้และฟังก์ชันที่อนุญาต (การตอบกลับ)

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

ตัวอย่างหลายเลี้ยว

คุณสามารถใช้สถานการณ์การเรียกใช้ฟังก์ชันแบบหลายเลี้ยวได้โดยดำเนินการดังนี้

  1. รับการตอบกลับการเรียกใช้ฟังก์ชันโดยการเรียกใช้โมเดลภาษา นี่คือรายการแรก เปิดหรือปิด
  2. เรียกใช้โมเดลภาษาโดยใช้การตอบกลับการเรียกใช้ฟังก์ชันตั้งแต่เลี้ยวแรก และการตอบสนองของฟังก์ชันที่คุณได้รับจากการเรียกใช้ฟังก์ชันนั้น นี่คือ รอบที่ 2

การตอบสนองจากรอบที่ 2 จะสรุปผลลัพธ์เพื่อตอบ ในจุดแรก หรือมีการเรียกฟังก์ชันที่สองที่คุณสามารถใช้เพื่อดู ข้อมูลเพิ่มเติมสำหรับคำถามของคุณ

หัวข้อนี้มีตัวอย่าง Curl แบบมัลติเทิร์น 2 รายการ:

ใช้คำตอบจากการเลี้ยวก่อนหน้า

ตัวอย่าง curl ต่อไปนี้เรียกใช้ฟังก์ชันและอาร์กิวเมนต์ที่แสดงผลโดย ตัวอย่างการเปลี่ยนครั้งเดียวก่อนหน้านี้ เพื่อรับคำตอบ เมธอดและพารามิเตอร์ ซึ่งแสดงผลโดยตัวอย่างแบบเลี้ยวครั้งเดียวที่อยู่ใน JSON นี้

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

ตัวอย่างคำขอ 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"]
      }
    }]
  }]
}'
    

การตอบสนองต่อตัวอย่าง curl นี้ประกอบด้วยผลลัพธ์ของการเรียกใช้ฟิลด์ find_theaters วิธี คำตอบอาจคล้ายกับข้อความต่อไปนี้

ตัวอย่างการตอบสนองด้วย 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
  }
}
    

เรียกใช้โมเดลหลายครั้ง

ตัวอย่าง cURL ต่อไปนี้เรียกใช้โมเดล Generative AI หลายครั้ง ฟังก์ชัน แต่ละครั้งที่โมเดลเรียกใช้ฟังก์ชัน โมเดลจะใช้ฟังก์ชัน เพื่อตอบคำถามของผู้ใช้รายอื่นๆ ในคำขอ

ตัวอย่างคำขอ 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"]
      }
    }]
  }]
}'
    

ตัวอย่างการตอบสนองด้วย 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
  }
}
]
    

แนวทางปฏิบัติแนะนำ

ทำตามแนวทางปฏิบัติแนะนำเหล่านี้เพื่อปรับปรุงความแม่นยำและ ความเสถียรของการเรียกฟังก์ชัน

ข้อความแจ้งผู้ใช้

เพื่อผลลัพธ์ที่ดีที่สุด ให้เพิ่มรายละเอียดต่อไปนี้ไว้หน้าคำค้นหาของผู้ใช้

  • บริบทเพิ่มเติมสำหรับโมเดล ตัวอย่างเช่น You are a movie API assistant to help users find movies and showtimes based on their preferences.
  • รายละเอียดหรือวิธีการและเวลาในการใช้ฟังก์ชัน ตัวอย่างเช่น Don't make assumptions on showtimes. Always use a future date for showtimes.
  • วิธีการถามคำถามเพื่อความชัดเจนหากคำถามของผู้ใช้ไม่ชัดเจน สำหรับ ตัวอย่างเช่น Ask clarifying questions if not enough information is available to complete the request.

พารามิเตอร์การสุ่มตัวอย่าง

สำหรับพารามิเตอร์อุณหภูมิ ให้ใช้ 0 หรือค่าอื่นระดับต่ำ วิธีนี้จะส่งผล โมเดลเพื่อสร้างผลลัพธ์ที่มั่นใจมากขึ้นและลดความไม่สมเหตุสมผล

การเรียกใช้ API

หากโมเดลเสนอการเรียกใช้ฟังก์ชันที่จะส่งคำสั่งซื้อ อัปเดตฐานข้อมูล หรือมีผลที่ตามมาที่สำคัญ ให้ตรวจสอบ กับผู้ใช้ก่อนที่จะเรียกใช้