ปลั๊กอิน Vertex AI

ปลั๊กอิน Vertex AI ให้อินเทอร์เฟซกับโมเดล Generative AI ของ Google หลายโมเดล ผ่าน Vertex AI API ดังนี้

  • การสร้างข้อความ Gemini 1.0 Pro และ Gemini 1.0 Pro Vision
  • การสร้างรูปภาพ Imagen2
  • การสร้างข้อความตุ๊กแก

และยังให้สิทธิ์เข้าถึงเมตริกการประเมินชุดย่อยผ่าน Rapid Evaluation API ของ Vertex AI ด้วย

การติดตั้ง

npm i --save @genkit-ai/vertexai

หากต้องการเรียกใช้โฟลว์ที่ใช้ปลั๊กอินนี้ในเครื่อง คุณยังต้องใช้ ติดตั้งเครื่องมือ Google Cloud CLI แล้ว

การกำหนดค่า

หากต้องการใช้ปลั๊กอินนี้ ให้ระบุเมื่อเรียกใช้ configureGenkit():

import { vertexAI } from '@genkit-ai/vertexai';

export default configureGenkit({
  plugins: [
    vertexAI({ projectId: 'your-cloud-project', location: 'us-central1' }),
  ],
  // ...
});

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

  • คุณสามารถระบุรหัสโปรเจ็กต์ Google Cloud ได้โดยการตั้งค่า projectId ใน การกำหนดค่า vertexAI() หรือโดยการตั้งค่าสภาพแวดล้อม GCLOUD_PROJECT ตัวแปร หากเรียกใช้โฟลว์จากสภาพแวดล้อม Google Cloud (Cloud) ฟังก์ชัน, Cloud Run และอื่นๆ) จะตั้งค่า GCLOUD_PROJECT เป็น รหัสโปรเจ็กต์ของสภาพแวดล้อม

  • คุณระบุตำแหน่ง API ได้โดยการตั้งค่า location ใน การกำหนดค่า vertexAI() หรือโดยการตั้งค่าสภาพแวดล้อม GCLOUD_LOCATION ตัวแปร

  • หากต้องการระบุข้อมูลเข้าสู่ระบบ API คุณต้องตั้งค่าแอปพลิเคชัน Google Cloud ข้อมูลเข้าสู่ระบบเริ่มต้น

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

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

      • ในสภาพแวดล้อมสำหรับนักพัฒนาแอปภายในของคุณ ให้เรียกใช้คำสั่งต่อไปนี้ด้วยการเรียกใช้

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

การใช้งาน

โมเดล Generative AI

ปลั๊กอินนี้จะส่งออกข้อมูลอ้างอิงแบบคงที่ไปยังโมเดล Generative AI ที่รองรับ ดังนี้

import { gemini15Flash, gemini15Pro, imagen2 } from '@genkit-ai/vertexai';

คุณสามารถใช้การอ้างอิงเหล่านี้เพื่อระบุโมเดลที่ generate() ใช้ ดังนี้

const llmResponse = await generate({
  model: gemini15Flash,
  prompt: 'What should I do when I visit Melbourne?',
});

ปลั๊กอินนี้ยังส่งออกการอ้างอิงไปยังการฝังข้อความ Gecko แบบคงที่อีกด้วย รุ่น:

import { textEmbeddingGecko } from '@genkit-ai/vertexai';

คุณสามารถใช้การอ้างอิงนี้เพื่อระบุตัวฝังที่ตัวจัดทำดัชนีหรือรีทรีฟเวอร์ การใช้งาน เช่น หากคุณใช้ Chroma DB

configureGenkit({
  plugins: [
    chroma([
      {
        embedder: textEmbeddingGecko,
        collectionName: 'my-collection',
      },
    ]),
  ],
});

หรือจะสร้างการฝังโดยตรงก็ได้โดยทำดังนี้

// import { embed, EmbedderArgument } from '@genkit-ai/ai/embedder';
const embedding = await embed({
  embedder: textEmbeddingGecko,
  content: 'How many widgets do you have in stock?',
});

Anthropic Claude 3 ในสวนโมเดล Vertex AI

หากมีสิทธิ์เข้าถึงโมเดล Claude 3 (haiku, sonnet หรือ opus) ใน Vertex AI Model Garden คุณจะใช้โมเดลดังกล่าวกับ Genkit ได้

ต่อไปนี้คือตัวอย่างการกำหนดค่าสำหรับการเปิดใช้โมเดล Vertex AI Model Garden

import {
  vertexAI,
  claude3Haiku,
  claude3Sonnet,
  claude3Opus,
} from '@genkit-ai/vertexai';

export default configureGenkit({
  plugins: [
    vertexAI({
      location: 'us-central1',
      modelGarden: {
        models: [claude3Haiku, claude3Sonnet, claude3Opus],
      },
    }),
  ],
});

จากนั้นใช้เป็นโมเดลปกติ

const llmResponse = await generate({
  model: claude3Sonnet,
  prompt: 'What should I do when I visit Melbourne?',
});

Llama 3.1 405b บน Vertex AI Model Garden

หากมีสิทธิ์เข้าถึง Llama 3.1 405b ใน Vertex AI Model Garden คุณก็ใช้กับ Genkit ได้

ต่อไปนี้คือตัวอย่างการกำหนดค่าสำหรับการเปิดใช้โมเดล Vertex AI Model Garden

import { vertexAI, llama3 } from '@genkit-ai/vertexai';

export default configureGenkit({
  plugins: [
    vertexAI({
      location: 'us-central1',
      modelGarden: {
        models: [llama3],
      },
    }),
  ],
});

จากนั้นใช้เป็นโมเดลปกติ

const llmResponse = await generate({
  model: llama3,
  prompt: 'Write a function that adds two numbers together',
});

ผู้ประเมิน

หากต้องการใช้ผู้ประเมินจาก Vertex AI Rapid Evaluation ให้เพิ่มบล็อก evaluation ลงในการกำหนดค่าปลั๊กอิน vertexAI

import { vertexAI, VertexAIEvaluationMetricType } from '@genkit-ai/vertexai';

export default configureGenkit({
  plugins: [
    vertexAI({
      projectId: 'your-cloud-project',
      location: 'us-central1',
      evaluation: {
        metrics: [
          VertexAIEvaluationMetricType.SAFETY,
          {
            type: VertexAIEvaluationMetricType.ROUGE,
            metricSpec: {
              rougeType: 'rougeLsum',
            },
          },
        ],
      },
    }),
  ],
  // ...
});

การกำหนดค่าข้างต้นเพิ่มผู้ประเมินสำหรับเมตริก Safety และ ROUGE ตัวอย่างนี้แสดง 2 แนวทาง - เมตริก Safety ใช้ข้อกำหนดเริ่มต้น ขณะที่เมตริก ROUGE จะระบุข้อกำหนดเฉพาะซึ่งตั้งค่าประเภท Rouge เป็น rougeLsum

ผู้ประเมินทั้ง 2 ฝ่ายสามารถเรียกใช้ได้โดยใช้คำสั่ง genkit eval:run กับชุดข้อมูลที่เข้ากันได้ ซึ่งก็คือชุดข้อมูลที่มีช่อง output และ reference นอกจากนี้ยังเรียกใช้ตัวประเมิน Safety ได้โดยใช้คำสั่ง genkit eval:flow -e vertexai/safety เนื่องจากต้องใช้เพียง output เท่านั้น