คุณจะกำหนดและจัดเตรียมฟังก์ชันที่กำหนดเองให้กับโมเดล Generative AI โดยใช้การเรียกใช้ฟังก์ชันได้ โมเดลจะไม่เรียกใช้ฟังก์ชันเหล่านี้โดยตรง แต่จะสร้างเอาต์พุต Structured Data ที่ระบุชื่อฟังก์ชันและอาร์กิวเมนต์ที่แนะนำแทน เอาต์พุตนี้ทำให้เรียกใช้ API ภายนอกได้ จากนั้นคุณจะรวมเอาต์พุต API ผลลัพธ์กลับเข้าไปในโมเดลได้ ทำให้สามารถตอบกลับการค้นหาได้อย่างครอบคลุมยิ่งขึ้น การเรียกฟังก์ชันช่วยให้ LLM สามารถโต้ตอบกับข้อมูลแบบเรียลไทม์และบริการต่างๆ เช่น ฐานข้อมูล ระบบการจัดการความสัมพันธ์ลูกค้า และที่เก็บเอกสาร เพิ่มความสามารถในการให้คำตอบที่เกี่ยวข้องและมีบริบท
วิธีการทำงานของการเรียกใช้ฟังก์ชัน
ระบบจะอธิบายฟังก์ชันโดยใช้การประกาศฟังก์ชัน หลังจากคุณส่งรายการการประกาศฟังก์ชันในการค้นหาไปยังโมเดลภาษา โมเดลจะแสดงผลออบเจ็กต์ในรูปแบบสคีมาที่เข้ากันได้กับ OpenAPI ที่มีชื่อฟังก์ชันและอาร์กิวเมนต์ และพยายามตอบคำถามของผู้ใช้ด้วยฟังก์ชันที่แสดงผล โมเดลภาษาเข้าใจวัตถุประสงค์ของฟังก์ชันด้วยการวิเคราะห์การประกาศฟังก์ชัน โมเดลไม่ได้เรียกใช้ฟังก์ชันนั้นจริงๆ แต่นักพัฒนาซอฟต์แวร์จะใช้ออบเจ็กต์สคีมาที่ใช้ร่วมกับ OpenAPI ได้ในการตอบสนองเพื่อเรียกใช้ฟังก์ชันที่โมเดลแสดงผลแทน
เมื่อใช้การเรียกฟังก์ชัน คุณจะต้องสร้างการประกาศฟังก์ชันอย่างน้อย 1 รายการ จากนั้นเพิ่มการประกาศฟังก์ชันไปยังออบเจ็กต์ tools
ที่ส่งไปยังโมเดล การประกาศฟังก์ชันแต่ละรายการจะมีข้อมูลเกี่ยวกับ 1 ฟังก์ชันดังต่อไปนี้
- ชื่อฟังก์ชัน
- พารามิเตอร์ของฟังก์ชันในรูปแบบสคีมาที่เข้ากันได้กับ OpenAPI ระบบรองรับชุดย่อยบางชุด เมื่อใช้ Curl สคีมาจะระบุโดยใช้ JSON
- คำอธิบายฟังก์ชัน (ไม่บังคับ) เพื่อผลลัพธ์ที่ดีที่สุด เราขอแนะนำให้คุณใส่คำอธิบายไว้ด้วย
Gemini API ยังรองรับการเรียกฟังก์ชันพร้อมกัน ซึ่งคุณสามารถเรียกใช้หลายฟังก์ชันได้ในครั้งเดียว
เอกสารนี้มีตัวอย่าง Curl ที่เรียกใช้ REST ด้วยคลาส GenerativeModel
และวิธีการของคลาส
รุ่นที่รองรับ
รุ่นต่อไปนี้รองรับการเรียกใช้ฟังก์ชัน
gemini-1.0-pro
gemini-1.0-pro-001
gemini-1.5-flash-latest
gemini-1.5-pro-latest
โหมดการเรียกใช้ฟังก์ชัน
คุณใช้โหมดการเรียกใช้ฟังก์ชันเพื่อกำหนดลักษณะการดำเนินการสำหรับการเรียกใช้ฟังก์ชันได้ โดยมี 3 โหมดดังนี้
AUTO
: ลักษณะการทำงานเริ่มต้นของโมเดล โมเดลนี้จะตัดสินใจคาดการณ์การเรียกใช้ฟังก์ชันหรือการตอบสนองด้วยภาษาธรรมชาติANY
: โมเดลจะถูกจํากัดให้คาดการณ์การเรียกใช้ฟังก์ชันเสมอ หากไม่ระบุallowed_function_names
โมเดลจะเลือกจากการประกาศฟังก์ชันที่มีอยู่ทั้งหมด หากมีการระบุallowed_function_names
โมเดลจะเลือกจากชุดฟังก์ชันที่อนุญาตNONE
: โมเดลจะไม่คาดการณ์การเรียกใช้ฟังก์ชัน ในกรณีนี้ ลักษณะการทำงานของโมเดลจะเหมือนกับว่าคุณไม่ผ่านการประกาศฟังก์ชัน
นอกจากนี้ คุณยังส่งชุด allowed_function_names
ซึ่งจำกัดฟังก์ชันที่โมเดลจะเรียกใช้ได้ หากมี คุณควรรวม allowed_function_names
เฉพาะเมื่ออยู่ในโหมด ANY
ชื่อฟังก์ชันควรตรงกับชื่อการประกาศฟังก์ชัน เมื่อตั้งโหมดเป็น ANY
และตั้งค่า allowed_function_names
โมเดลจะคาดการณ์การเรียกใช้ฟังก์ชันจากชุดชื่อฟังก์ชันที่ให้ไว้
นี่คือส่วนหนึ่งของคำขอตัวอย่างที่ตั้งค่าโหมดเป็น ANY
และระบุรายการฟังก์ชันที่อนุญาต
"tool_config": {
"function_calling_config": {
"mode": "ANY",
"allowed_function_names": ["find_theaters", "get_showtimes"]
},
}
ตัวอย่าง cURL ของฟังก์ชันที่เรียกใช้
เมื่อคุณใช้ cURL ข้อมูลฟังก์ชันและพารามิเตอร์จะรวมอยู่ในองค์ประกอบ tools
การประกาศฟังก์ชันแต่ละรายการในองค์ประกอบ tools
จะมีชื่อฟังก์ชัน พารามิเตอร์ที่ระบุโดยใช้สคีมาที่ใช้ร่วมกับ OpenAPI และคำอธิบายฟังก์ชันได้ ตัวอย่างต่อไปนี้สาธิตวิธีใช้คำสั่ง curl กับการเรียกใช้ฟังก์ชัน
ตัวอย่างเคิร์ลแบบหมุนเดี่ยว
เลี้ยวเดียวคือเมื่อคุณเรียกโมเดลภาษาหนึ่งครั้ง เมื่อใช้การเรียกฟังก์ชัน กรณีการใช้งานแบบเลี้ยวเดียวอาจเป็นกรณีที่คุณให้คำค้นหาเป็นภาษาธรรมชาติและรายการฟังก์ชันแก่โมเดล ในกรณีนี้ โมเดลจะใช้การประกาศฟังก์ชัน ซึ่งรวมถึงชื่อฟังก์ชัน พารามิเตอร์ และคำอธิบาย เพื่อคาดการณ์ฟังก์ชันที่จะเรียกใช้ และอาร์กิวเมนต์ที่จะใช้ในการเรียกใช้
ตัวอย่าง Curl ต่อไปนี้เป็นตัวอย่างของการส่งคำอธิบายของฟังก์ชันซึ่งแสดงข้อมูลว่าภาพยนตร์กำลังเล่นที่ไหน คำขอมีการประกาศฟังก์ชันหลายรายการ เช่น find_movies
และ find_theaters
คำขอตัวอย่างการเรียกใช้ฟังก์ชัน 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" ] } } ] } ] }'
การตอบกลับตัวอย่าง 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 ต่อไปนี้คล้ายกับตัวอย่างแบบเลี้ยวเดียว แต่ตั้งค่า mode เป็น 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 ต่อไปนี้คล้ายกับตัวอย่างแบบเลี้ยวเดียว แต่ตั้งค่า mode เป็น 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" } ] } }
ตัวอย่างเคิร์ลแบบมัลติเทิร์น
คุณติดตั้งใช้งานสถานการณ์การเรียกฟังก์ชันหลายเทิร์นได้โดยทําดังนี้
- รับการตอบสนองต่อการเรียกฟังก์ชันโดยการเรียกโมเดลภาษา นี่เป็นเลี้ยวแรก
- เรียกโมเดลภาษาโดยใช้การตอบสนองการเรียกฟังก์ชันจากการเปิดครั้งแรกและการตอบสนองของฟังก์ชันที่คุณได้รับจากการเรียกฟังก์ชันนั้น นี่เป็นคราวที่ 2
การตอบสนองจากตาที่ 2 จะสรุปผลลัพธ์เพื่อตอบคำค้นหาของคุณในเทิร์นแรก หรือมีการเรียกฟังก์ชันที่ 2 ที่คุณสามารถใช้สำหรับข้อมูลเพิ่มเติมเกี่ยวกับข้อความค้นหาของคุณ
หัวข้อนี้ประกอบด้วยตัวอย่างเคิร์ลหลายตัวแบบมัลติเทิร์น 2 ตัวอย่าง ดังนี้
- ตัวอย่าง Curl ที่ใช้การตอบสนองของฟังก์ชันจากการเลี้ยวก่อนหน้า
- ตัวอย่าง Curl ที่เรียกโมเดลภาษาหลายครั้ง
ตัวอย่างเคิร์ลที่ใช้การตอบสนองจากการเลี้ยวก่อนหน้า
ตัวอย่าง 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 ที่เรียกโมเดลภาษาหลายครั้ง
ตัวอย่าง Curl ต่อไปนี้เรียกโมเดลภาษาหลายครั้งเพื่อเรียกใช้ฟังก์ชัน ทุกครั้งที่โมเดลเรียกใช้ฟังก์ชัน โมเดลจะใช้ฟังก์ชันอื่นเพื่อตอบคำถามของผู้ใช้รายอื่นในคำขอได้
คำขอตัวอย่าง 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 } } ]
แนวทางปฏิบัติแนะนำ
ทำตามแนวทางปฏิบัติแนะนำเหล่านี้เพื่อปรับปรุงความแม่นยำและความน่าเชื่อถือของการเรียกใช้ฟังก์ชัน
ช่องคีย์ฟังก์ชัน
การกำหนดฟังก์ชันให้ถูกต้องเป็นสิ่งสำคัญเมื่อผสานรวมฟังก์ชันไว้ในคำขอ แต่ละฟังก์ชันอาศัยพารามิเตอร์เฉพาะซึ่งกำหนดลักษณะการทำงานและการโต้ตอบกับโมเดล รายละเอียดพารามิเตอร์หลักที่ใช้ภายในอาร์เรย์ functions_declarations
มีดังนี้
function_declarations
(อาร์เรย์):
- มีออบเจ็กต์อย่างน้อย 1 รายการ โดยแต่ละรายการแสดงถึงฟังก์ชันที่ต่างกัน
ภายในออบเจ็กต์ function_declarations
แต่ละรายการ ให้ทำดังนี้
name
(สตริง): ตัวระบุที่ไม่ซ้ำกันสำหรับฟังก์ชันภายในการเรียก API- แนวทางปฏิบัติแนะนำ: ใช้ชื่อที่ชัดเจนและสื่อความหมายโดยไม่เว้นวรรค ใส่จุด (
.
) หรือเครื่องหมายขีดกลาง (-
) แต่ให้ใช้เครื่องหมายขีดล่าง (_
) หรือตัวพิมพ์แบบอูฐแทน
- แนวทางปฏิบัติแนะนำ: ใช้ชื่อที่ชัดเจนและสื่อความหมายโดยไม่เว้นวรรค ใส่จุด (
description
(สตริง): คำอธิบายที่ครอบคลุมเกี่ยวกับวัตถุประสงค์และความสามารถของฟังก์ชัน- แนวทางปฏิบัติแนะนำ: อธิบายฟังก์ชันอย่างละเอียด ชัดเจน และเฉพาะเจาะจงพร้อมยกตัวอย่างหากจำเป็น เช่น แทนที่จะใช้
find theaters
ให้ใช้find theaters based on location and optionally movie title that is currently playing in theaters.
หลีกเลี่ยงคำอธิบายที่กว้างหรือกำกวมมากเกินไป
- แนวทางปฏิบัติแนะนำ: อธิบายฟังก์ชันอย่างละเอียด ชัดเจน และเฉพาะเจาะจงพร้อมยกตัวอย่างหากจำเป็น เช่น แทนที่จะใช้
parameters
(ออบเจ็กต์): กำหนดข้อมูลอินพุตที่ฟังก์ชันต้องใช้type
(สตริง): ระบุประเภทข้อมูลโดยรวม (เช่นobject
).properties
(ออบเจ็กต์):- แสดงรายการพารามิเตอร์แต่ละรายการ โดยแต่ละรายการมีข้อมูลต่อไปนี้
type
(สตริง): ประเภทข้อมูลของพารามิเตอร์ (เช่นstring
,integer
,boolean
)- แนวทางปฏิบัติแนะนำ: ใช้พารามิเตอร์ที่พิมพ์อย่างแรงเพื่อลดอาการภาพหลอนของโมเดล เช่น หากค่าพารามิเตอร์มาจากเซตจำกัด ให้ใช้ช่อง
enum
แทนการระบุค่าไว้ในคำอธิบาย (เช่น"type": "enum", "values": ["now_playing", "upcoming"]
) หากค่าพารามิเตอร์เป็นจำนวนเต็มเสมอ ให้ตั้งค่าประเภทเป็นinteger
แทนnumber
- แนวทางปฏิบัติแนะนำ: ใช้พารามิเตอร์ที่พิมพ์อย่างแรงเพื่อลดอาการภาพหลอนของโมเดล เช่น หากค่าพารามิเตอร์มาจากเซตจำกัด ให้ใช้ช่อง
description
(สตริง): คำอธิบายที่ชัดเจนเกี่ยวกับวัตถุประสงค์ของพารามิเตอร์และรูปแบบที่คาดหวัง- แนวทางปฏิบัติแนะนำ: ให้ตัวอย่างและข้อจำกัดที่เป็นรูปธรรม เช่น ใช้
The city and state, e.g. San Francisco, CA or a zip code e.g. 95616
แทนthe location to search
- แนวทางปฏิบัติแนะนำ: ให้ตัวอย่างและข้อจำกัดที่เป็นรูปธรรม เช่น ใช้
- แสดงรายการพารามิเตอร์แต่ละรายการ โดยแต่ละรายการมีข้อมูลต่อไปนี้
required
(อาร์เรย์):- อาร์เรย์ของสตริงที่แสดงชื่อพารามิเตอร์ที่จำเป็นเพื่อให้ฟังก์ชันทํางาน
ข้อความแจ้งผู้ใช้
เพื่อผลลัพธ์ที่ดีที่สุด ให้เติมรายละเอียดต่อไปนี้ไว้หน้าคำค้นหาของผู้ใช้
- บริบทเพิ่มเติมสำหรับโมเดล ตัวอย่างเช่น
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
หากโมเดลเสนอการเรียกใช้ฟังก์ชันที่จะส่งคำสั่งซื้อ อัปเดตฐานข้อมูล หรือมีผลลัพธ์ที่สำคัญ ให้ตรวจสอบการเรียกใช้ฟังก์ชันกับผู้ใช้ก่อนเรียกใช้