ปลั๊กอิน Google Generative AI

ปลั๊กอิน Generative AI ของ Google มีอินเทอร์เฟซสำหรับโมเดล Gemini ของ Google ผ่าน Gemini API หรือ Gemini API in Vertex AI

การกำหนดค่า

การกําหนดค่าจะขึ้นอยู่กับผู้ให้บริการที่คุณเลือก

AI ของ Google

หากต้องการใช้ปลั๊กอินนี้ ให้นําเข้าแพ็กเกจ googlegenai และส่ง googlegenai.GoogleAI ไปยัง WithPlugins() ในตัวเริ่มต้น Genkit ดังนี้

import "github.com/firebase/genkit/go/plugins/googlegenai"
g, err := genkit.Init(context.Background(), ai.WithPlugins(&googlegenai.GoogleAI{}))

ปลั๊กอินต้องใช้คีย์ API สําหรับ Gemini API ซึ่งคุณขอได้จาก Google AI Studio

กำหนดค่าปลั๊กอินให้ใช้คีย์ API โดยทำอย่างใดอย่างหนึ่งต่อไปนี้

  • ตั้งค่าตัวแปรสภาพแวดล้อม GEMINI_API_KEY เป็นคีย์ API

  • ระบุคีย์ API เมื่อเริ่มต้นใช้งานปลั๊กอิน

    ai.WithPlugins(&googlegenai.GoogleAI{APIKey: "YOUR_API_KEY"})
    

    แต่อย่าฝังคีย์ API ในโค้ดโดยตรง ใช้ฟีเจอร์นี้ร่วมกับบริการอย่าง Secret Manager ของ Cloud หรือบริการที่คล้ายกันเท่านั้น

Vertex AI

หากต้องการใช้ปลั๊กอินนี้ ให้นําเข้าแพ็กเกจ googlegenai และส่ง googlegenai.VertexAI ไปยัง WithPlugins() ในตัวเริ่มต้น Genkit ดังนี้

import "github.com/firebase/genkit/go/plugins/googlegenai"
g, err := genkit.Init(context.Background(), genkit.WithPlugins(&googlegenai.VertexAI{}))

ปลั๊กอินกำหนดให้คุณระบุรหัสโปรเจ็กต์ Google Cloud, ภูมิภาคที่คุณต้องการส่งคำขอ Vertex API และข้อมูลเข้าสู่ระบบโปรเจ็กต์ Google Cloud

  • โดยค่าเริ่มต้น googlegenai.VertexAI จะรับรหัสโปรเจ็กต์ Google Cloud จากตัวแปรสภาพแวดล้อม GOOGLE_CLOUD_PROJECT

    คุณยังส่งค่านี้โดยตรงได้ด้วย

    genkit.WithPlugins(&googlegenai.VertexAI{ProjectID: "my-project-id"})
    
  • โดยค่าเริ่มต้น googlegenai.VertexAI จะรับตำแหน่ง Vertex AI API จากตัวแปรสภาพแวดล้อม GOOGLE_CLOUD_LOCATION

    คุณยังส่งค่านี้โดยตรงได้ด้วย

    genkit.WithPlugins(&googlegenai.VertexAI{Location: "us-central1"})
    
  • หากต้องการระบุข้อมูลเข้าสู่ระบบ API คุณต้องตั้งค่าข้อมูลเข้าสู่ระบบเริ่มต้นของแอปพลิเคชัน Google Cloud

    1. วิธีระบุข้อมูลเข้าสู่ระบบ

      • หากคุณเรียกใช้โฟลว์จากสภาพแวดล้อม Google Cloud (Cloud Function, Cloud Run และอื่นๆ) ระบบจะตั้งค่านี้ให้โดยอัตโนมัติ

      • ในสภาพแวดล้อมการทํางานแบบสํารวจในเครื่อง ให้ทําดังนี้

      gcloud auth application-default login
    2. นอกจากนี้ โปรดตรวจสอบว่าบัญชีได้รับบทบาท IAM ผู้ใช้ Vertex AI (roles/aiplatform.user) โปรดดูเอกสารการควบคุมการเข้าถึงของ Vertex AI

การใช้งาน

โมเดล Generative

หากต้องการดูข้อมูลอ้างอิงสำหรับรุ่นที่รองรับ ให้ระบุตัวระบุเป็น googlegenai.GoogleAIModel หรือ googlgenai.VertexAIModel

model := googlegenai.GoogleAIModel(g, "gemini-2.0-flash")

หรือจะสร้าง ModelRef ที่จับคู่ชื่อโมเดลกับการกำหนดค่าก็ได้ โดยทำดังนี้

modelRef := googlegenai.GoogleAIModelRef("gemini-2.0-flash", &googlegenai.GeminiConfig{
    Temperature: 0.5,
    MaxOutputTokens: 500,
    // Other configuration...
})

ระบบรองรับรุ่น gemini-1.5-pro, gemini-1.5-flash, gemini-2.0-pro, gemini-2.0-flash และรุ่นอื่นๆ ที่เป็นเวอร์ชันทดลอง

ข้อมูลอ้างอิงรูปแบบมีเมธอด Generate() ที่เรียก Google API ดังนี้

resp, err := genkit.Generate(ctx, g, ai.WithModel(modelRef), ai.WithPrompt("Tell me a joke."))
if err != nil {
      return err
}

log.Println(resp.Text())

ดูข้อมูลเพิ่มเติมได้ที่การสร้างเนื้อหาด้วยโมเดล AI

การฝังโมเดล

หากต้องการดูข้อมูลอ้างอิงสำหรับรูปแบบการฝังที่รองรับ ให้ระบุตัวระบุเป็น googlegenai.GoogleAIEmbedder หรือ googlgenai.VertexAIEmbedder

embeddingModel := googlegenai.GoogleAIEmbedder(g, "text-embedding-004")

ระบบรองรับรุ่นต่อไปนี้

  • AI ของ Google

    text-embedding-004 และ embedding-001

  • Vertex AI

    textembedding-gecko@003, textembedding-gecko@002, textembedding-gecko@001, text-embedding-004, textembedding-gecko-multilingual@001, text-multilingual-embedding-002 และ multimodalembedding

ข้อมูลอ้างอิงของผู้ฝังมีเมธอด Embed() ที่เรียก Google AI API ดังนี้

resp, err := ai.Embed(ctx, embeddingModel, ai.WithDocs(userInput))
if err != nil {
      return err
}

นอกจากนี้ คุณยังส่งตัวฝังไปยังIndex()เมธอดของผู้จัดทําดัชนีและ Retrieve()เมธอดของผู้ดึงข้อมูลได้ด้วย โดยทำดังนี้

if err := ai.Index(ctx, myIndexer, ai.WithDocs(docsToIndex...)); err != nil {
      return err
}
resp, err := ai.Retrieve(ctx, myRetriever, ai.WithDocs(userInput))
if err != nil {
      return err
}

ดูข้อมูลเพิ่มเติมได้ที่การสร้างที่เพิ่มการดึงข้อมูล (RAG)