เมื่อพัฒนาโซลูชัน Generative AI ด้วย Gemini ทาง Google มีผลิตภัณฑ์ API 2 รายการ ได้แก่ Gemini Developer API และ Vertex AI Gemini API
Gemini Developer API เป็นเส้นทางที่เร็วที่สุดในการสร้าง นำไปใช้งานจริง และ ปรับขนาดแอปพลิเคชันที่ทำงานด้วย Gemini นักพัฒนาแอปส่วนใหญ่ควรใช้ Gemini Developer API เว้นแต่จะมีความจำเป็นต้องใช้การควบคุมเฉพาะสำหรับองค์กร
Vertex AI มีระบบนิเวศที่ครอบคลุมของฟีเจอร์และบริการที่พร้อมใช้งานในองค์กร สำหรับการสร้างและปรับใช้แอปพลิเคชัน Generative AI ที่ขับเคลื่อนโดย Google Cloud Platform
เมื่อเร็วๆ นี้เราได้ปรับปรุงการย้ายข้อมูลระหว่างบริการเหล่านี้ให้ง่ายขึ้น ตอนนี้คุณเข้าถึงทั้ง Gemini Developer API และ Vertex AI Gemini API ได้ผ่าน Google Gen AI SDK แบบรวม
การเปรียบเทียบโค้ด
หน้านี้มีการเปรียบเทียบโค้ดแบบเคียงข้างกันระหว่าง Gemini Developer API กับ การเริ่มต้นใช้งาน Vertex AI อย่างรวดเร็วสำหรับการสร้างข้อความ
Python
คุณเข้าถึงทั้ง Gemini Developer API และบริการ Vertex AI ได้ผ่านgoogle-genai
ไลบรารี ดูวิธีการติดตั้ง google-genai
ได้ที่หน้าไลบรารี
Gemini Developer API
from google import genai
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.0-flash", contents="Explain how AI works in a few words"
)
print(response.text)
Vertex AI Gemini API
from google import genai
client = genai.Client(
vertexai=True, project='your-project-id', location='us-central1'
)
response = client.models.generate_content(
model="gemini-2.0-flash", contents="Explain how AI works in a few words"
)
print(response.text)
JavaScript และ TypeScript
คุณเข้าถึงทั้ง Gemini Developer API และบริการ Vertex AI ได้ผ่าน@google/genai
ไลบรารี ดูวิธีการติดตั้ง @google/genai
ได้ที่หน้าไลบรารี
Gemini Developer API
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: "Explain how AI works in a few words",
});
console.log(response.text);
}
main();
Vertex AI Gemini API
import { GoogleGenAI } from '@google/genai';
const ai = new GoogleGenAI({
vertexai: true,
project: 'your_project',
location: 'your_location',
});
async function main() {
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: "Explain how AI works in a few words",
});
console.log(response.text);
}
main();
Go
คุณเข้าถึงทั้ง Gemini Developer API และบริการ Vertex AI ได้ผ่านgoogle.golang.org/genai
ไลบรารี ดูวิธีการติดตั้ง google.golang.org/genai
ได้ที่หน้าไลบรารี
Gemini Developer API
import (
"context"
"encoding/json"
"fmt"
"log"
"google.golang.org/genai"
)
// Your Google API key
const apiKey = "your-api-key"
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
// Call the GenerateContent method.
result, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", genai.Text("Tell me about New York?"), nil)
}
Vertex AI Gemini API
import (
"context"
"encoding/json"
"fmt"
"log"
"google.golang.org/genai"
)
// Your GCP project
const project = "your-project"
// A GCP location like "us-central1"
const location = "some-gcp-location"
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, &genai.ClientConfig
{
Project: project,
Location: location,
Backend: genai.BackendVertexAI,
})
// Call the GenerateContent method.
result, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", genai.Text("Tell me about New York?"), nil)
}
กรณีการใช้งานและแพลตฟอร์มอื่นๆ
ดูคำแนะนำเฉพาะสำหรับกรณีการใช้งานในเอกสารประกอบเกี่ยวกับ Gemini Developer API และเอกสารประกอบเกี่ยวกับ Vertex AI สำหรับแพลตฟอร์มและกรณีการใช้งานอื่นๆ
ข้อควรพิจารณาในการย้ายข้อมูล
สิ่งที่จะเกิดขึ้นเมื่อคุณย้ายข้อมูล
คุณจะต้องใช้บัญชีบริการ Google Cloud เพื่อตรวจสอบสิทธิ์ ดูข้อมูลเพิ่มเติมได้ที่เอกสารประกอบของ Vertex AI
คุณสามารถใช้โปรเจ็กต์ Google Cloud ที่มีอยู่ (โปรเจ็กต์เดียวกับที่ใช้สร้างคีย์ API) หรือจะสร้างโปรเจ็กต์ Google Cloud ใหม่ก็ได้
ภูมิภาคที่รองรับอาจแตกต่างกันระหว่าง Gemini Developer API กับ Vertex AI Gemini API ดูรายการภูมิภาคที่รองรับ Generative AI ใน Google Cloud
คุณต้องฝึกโมเดลที่สร้างใน Google AI Studio อีกครั้งใน Vertex AI
หากไม่จำเป็นต้องใช้คีย์ Gemini API สำหรับ Gemini Developer API อีกต่อไป โปรดทำตามแนวทางปฏิบัติแนะนำด้านความปลอดภัยและลบคีย์ดังกล่าว
วิธีลบคีย์ API
ค้นหาคีย์ API ที่ต้องการลบ แล้วคลิกไอคอนการดำเนินการ
เลือกลบคีย์ API
ในโมดัลลบข้อมูลเข้าสู่ระบบ ให้เลือกลบ
การลบคีย์ API จะใช้เวลาสักครู่จึงจะมีผล หลังจาก การเผยแพร่เสร็จสมบูรณ์แล้ว ระบบจะปฏิเสธการเข้าชมใดก็ตามที่ใช้คีย์ API ที่ลบไปแล้ว
ขั้นตอนถัดไป
- ดูข้อมูลเพิ่มเติมเกี่ยวกับโซลูชัน Generative AI ใน Vertex AI ได้ที่ ภาพรวมของ Generative AI ใน Vertex AI