ใช้ Gemini API สำหรับการเรียกใช้ฟังก์ชัน


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

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

คุณสามารถ ดูข้อมูลเพิ่มเติมเกี่ยวกับการเรียกใช้ฟังก์ชัน ในเอกสารประกอบของ Google Cloud

ก่อนเริ่มต้น

หากคุณยังไม่ได้ดำเนินการ โปรดดำเนินการ คู่มือเริ่มต้นใช้งาน Vertex AI สำหรับ Firebase SDK ตรวจสอบว่าคุณได้ดำเนินการทั้งหมดต่อไปนี้แล้ว

  • สร้างโปรเจ็กต์ Firebase ใหม่หรือที่มีอยู่ รวมถึงการใช้ แพ็กเกจราคา Blaze และเปิดใช้ API ที่จำเป็น

  • เชื่อมต่อแอปกับ Firebase รวมถึงการลงทะเบียนแอปและเพิ่ม การกำหนดค่า Firebase ให้กับแอป

  • เพิ่ม SDK และเริ่มต้นบริการ Vertex AI และโมเดล Generative ในแอปของคุณ

หลังจากที่เชื่อมต่อแอปกับ Firebase แล้ว ให้เพิ่ม SDK และเริ่มต้น บริการ Vertex AI และโมเดล Generative คุณก็พร้อมที่จะเรียกใช้ Gemini API แล้ว

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

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

พารามิเตอร์ ประเภท ต้องระบุ คำอธิบาย
currencyFrom สตริง ใช่ สกุลเงินที่ใช้แปลง
currencyTo สตริง ใช่ สกุลเงินที่ต้องการแปลงเป็น

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

{
  "currencyFrom": "USD",
  "currencyTo": "SEK"
}

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

{
  "base": "USD",
  "rates": {"SEK": 10.99}
}

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

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

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

func makeAPIRequest(currencyFrom: String,
                    currencyTo: String) -> JSONObject {
  // This hypothetical API returns a JSON such as:
  // {"base":"USD","rates":{"SEK": 10.99}}
  return [
    "base": .string(currencyFrom),
    "rates": .object([currencyTo: .number(10.99)]),
  ]
}

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

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

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

let getExchangeRate = FunctionDeclaration(
  name: "getExchangeRate",
  description: "Get the exchange rate for currencies between countries",
  parameters: [
    "currencyFrom": Schema(
      type: .string,
      description: "The currency to convert from."
    ),
    "currencyTo": Schema(
      type: .string,
      description: "The currency to convert to."
    ),
  ],
  requiredParameters: ["currencyFrom", "currencyTo"]
)

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

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

import FirebaseVertexAI

// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()

// Initialize the generative model
// Use a model that supports function calling, like a Gemini 1.5 model
let model = vertex.generativeModel(
  modelName: "gemini-1.5-flash",
  // Specify the function declaration.
  tools: [Tool(functionDeclarations: [getExchangeRate])]
)

ดูวิธีเลือกโมเดล Gemini และเลือกสถานที่ ที่เหมาะกับกรณีการใช้งานและแอปของคุณ

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

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

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

let chat = model.startChat()

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

// Send the message to the generative model
let response1 = try await chat.sendMessage(prompt)

// Check if the model responded with a function call
guard let functionCall = response1.functionCalls.first else {
  fatalError("Model did not respond with a function call.")
}
// Print an error if the returned function was not declared
guard functionCall.name == "getExchangeRate" else {
  fatalError("Unexpected function called: \(functionCall.name)")
}
// Verify that the names and types of the parameters match the declaration
guard case let .string(currencyFrom) = functionCall.args["currencyFrom"] else {
  fatalError("Missing argument: currencyFrom")
}
guard case let .string(currencyTo) = functionCall.args["currencyTo"] else {
  fatalError("Missing argument: currencyTo")
}

// Call the hypothetical API
let apiResponse = makeAPIRequest(currencyFrom: currencyFrom, currencyTo: currencyTo)

// Send the API response back to the model so it can generate a text response that can be
// displayed to the user.
let response = try await chat.sendMessage([ModelContent(
  role: "function",
  parts: [.functionResponse(FunctionResponse(
    name: functionCall.name,
    response: apiResponse
  ))]
)])

// Log the text response.
guard let modelResponse = response.text else {
  fatalError("Model did not respond with text.")
}
print(modelResponse)

คุณทำอะไรได้อีกบ้าง

ลองใช้ความสามารถอื่นๆ ของ Gemini API

เรียนรู้วิธีควบคุมการสร้างเนื้อหา

คุณยังสามารถทดสอบข้อความแจ้งและการกำหนดค่าโมเดลโดยใช้ Vertex AI Studio

ดูข้อมูลเพิ่มเติมเกี่ยวกับโมเดล Gemini

ดูข้อมูลเกี่ยวกับ รุ่นที่เหมาะกับกรณีการใช้งานที่หลากหลาย และ โควต้าและการกำหนดราคา


แสดงความคิดเห็น เกี่ยวกับประสบการณ์การใช้งาน Vertex AI สำหรับ Firebase