บทแนะนำ: การเรียกฟังก์ชันด้วย Gemini API


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

คุณระบุโมเดล Gemini พร้อมคำอธิบายฟังก์ชันได้ ฟังก์ชันเหล่านี้เป็นฟังก์ชันที่คุณเขียนในภาษาของแอป (ซึ่งไม่ใช่ Google Cloud Functions) โมเดลอาจขอให้คุณเรียกฟังก์ชันและส่งผลลัพธ์กลับมาเพื่อช่วยให้โมเดลจัดการกับคำค้นหาของคุณ

ดูข้อมูลเพิ่มเติมได้ในข้อมูลเบื้องต้นเกี่ยวกับการเรียกใช้ฟังก์ชัน หากยังไม่ได้อ่าน

ตั้งค่าโปรเจ็กต์

ก่อนที่จะเรียกใช้ Gemini API คุณต้องตั้งค่าโปรเจ็กต์ ซึ่งรวมถึงการตั้งค่าคีย์ API การติดตั้งแพ็กเกจ SDK และการเริ่มต้นใช้งานโมเดล

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

สำหรับบทแนะนำนี้ คุณจะให้โมเดลโต้ตอบกับ API การแลกเปลี่ยนสกุลเงินสมมติที่รองรับพารามิเตอร์ต่อไปนี้

พารามิเตอร์ ประเภท จำเป็น คำอธิบาย
currencyDate string ใช่ วันที่ที่จะดึงข้อมูลอัตราแลกเปลี่ยน
(ซึ่งต้องอยู่ในรูปแบบ YYYY-MM-DD เสมอ หรือค่า latest หากไม่ได้ระบุระยะเวลา)
currencyFrom string ใช่ สกุลเงินที่จะแปลง
currencyTo string ไม่ สกุลเงินที่จะแปลง

ตัวอย่างคำขอ API

{
  "currencyDate": "2024-04-17",
  "currencyFrom": "USD",
  "currencyTo": "SEK"
}

ตัวอย่างการตอบกลับจาก API

{
  "base": "USD",
  "date": "2024-04-17",
  "rates": {"SEK": 0.091}
}

ขั้นตอนที่ 1: สร้างฟังก์ชันที่ส่งคำขอ API

หากยังไม่ได้สร้าง ให้เริ่มด้วยการสร้างฟังก์ชันที่ส่งคำขอ API

เพื่อจุดประสงค์ในการสาธิตในบทแนะนำนี้ คุณจะส่งคืนค่าแบบฮาร์ดโค้ดในรูปแบบเดียวกับที่ API จริงจะแสดง แทนการส่งคำขอ API จริง

func exchangeRate(currencyDate string,
    currencyFrom string, currencyTo string) map[string]any {
    // This hypothetical API returns a JSON such as:
    // {"base":"USD","date":"2024-04-17","rates":{"SEK": 0.091}}
    return map[string]any{
        "base":  currencyFrom,
        "date":  currencyDate,
        "rates": map[string]any{currencyTo: 0.091}}
}

ขั้นตอนที่ 2: สร้างการประกาศฟังก์ชัน

สร้างการประกาศฟังก์ชันที่คุณจะส่งไปยังโมเดล Generative (ขั้นตอนถัดไปของบทแนะนำนี้)

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

currencyExchangeTool := &genai.Tool{
    FunctionDeclarations: []*genai.FunctionDeclaration{{
        Name:        "exchangeRate",
        Description: "Lookup currency exchange rates by date",
        Parameters: &genai.Schema{
            Type: genai.TypeObject,
            Properties: map[string]*genai.Schema{
                "currencyDate": {
                    Type:        genai.TypeString,
                    Description: "A date that must always be in YYYY-MM-DD format" +
                        " or the value 'latest' if a time period is not specified",
                },
                "currencyFrom": {
                    Type:        genai.TypeString,
                    Description: "Currency to convert from",
                },
                "currencyTo": {
                    Type:        genai.TypeString,
                    Description: "Currency to convert to",
                },
            },
            Required: []string{"currencyDate", "currencyFrom"},
        },
    }},
}

ขั้นตอนที่ 3: ระบุการประกาศฟังก์ชันระหว่างการเริ่มต้นโมเดล

ระบุการประกาศฟังก์ชันเมื่อเริ่มต้นโมเดล Generative โดยส่งไปยังพารามิเตอร์ Tools ของโมเดล ดังนี้

// ...

currencyExchangeTool := &genai.Tool{
  // ...
}

// Use a model that supports function calling, like Gemini 1.0 Pro.
// See "Supported models" in the "Introduction to function calling" page.
model := client.GenerativeModel("gemini-1.0-pro")

// Specify the function declaration.
model.Tools = []*genai.Tool{currencyExchangeTool}

ขั้นตอนที่ 4: สร้างการเรียกใช้ฟังก์ชัน

จากนั้นคุณสามารถเรียกพรอมต์โมเดลด้วยฟังก์ชันที่กำหนดไว้ได้

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

// Start new chat session.
session := model.StartChat()

prompt := "How much is 50 US dollars worth in Swedish krona?"

// Send the message to the generative model.
resp, err := session.SendMessage(ctx, genai.Text(prompt))
if err != nil {
    log.Fatalf("Error sending message: %v\n", err)
}

// Check that you got the expected function call back.
part := resp.Candidates[0].Content.Parts[0]
funcall, ok := part.(genai.FunctionCall)
if !ok {
    log.Fatalf("Expected type FunctionCall, got %T", part)
}
if g, e := funcall.Name, currencyExchangeTool.FunctionDeclarations[0].Name; g != e {
    log.Fatalf("Expected FunctionCall.Name %q, got %q", e, g)
}
fmt.Printf("Received function call response:\n%q\n\n", part)

apiResult := map[string]any{
    "base":  "USD",
    "date":  "2024-04-17",
    "rates": map[string]any{"SEK": 0.091}}

// Send the hypothetical API result back to the generative model.
fmt.Printf("Sending API result:\n%q\n\n", apiResult)
resp, err = session.SendMessage(ctx, genai.FunctionResponse{
    Name:     currencyExchangeTool.FunctionDeclarations[0].Name,
    Response: apiResult,
})
if err != nil {
    log.Fatalf("Error sending message: %v\n", err)
}

// Show the model's response, which is expected to be text.
for _, part := range resp.Candidates[0].Content.Parts {
    fmt.Printf("%v\n", part)
}