การเริ่มต้นใช้งาน Gemini API อย่างรวดเร็ว

การเริ่มต้นใช้งานอย่างรวดเร็วนี้แสดงวิธีติดตั้งไลบรารีและส่งคําขอ Gemini API รายการแรก

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

คุณต้องมีคีย์ Gemini API หากยังไม่มี คุณสามารถรับได้ฟรีใน Google AI Studio

ติดตั้ง Google GenAI SDK

PythonJavaScriptGoApps Script

หากใช้ Python 3.9 ขึ้นไป ให้ติดตั้งแพ็กเกจ google-genai โดยใช้คำสั่ง pip ต่อไปนี้

pip install -q -U google-genai

เมื่อใช้ Node.js v18 ขึ้นไป ให้ติดตั้ง Google Gen AI SDK สําหรับ TypeScript และ JavaScript โดยใช้คําสั่ง npm ต่อไปนี้

npm install @google/genai

ติดตั้ง google.golang.org/genai ในไดเรกทอรีโมดูลโดยใช้คำสั่ง go get

go get google.golang.org/genai
  1. หากต้องการสร้างโปรเจ็กต์ Apps Script ใหม่ ให้ไปที่ script.new
  2. คลิกโปรเจ็กต์ที่ไม่มีชื่อ
  3. เปลี่ยนชื่อโปรเจ็กต์ Apps Script เป็น AI Studio แล้วคลิกเปลี่ยนชื่อ
  4. ตั้งค่าคีย์ API
    1. คลิกการตั้งค่าโปรเจ็กต์ ไอคอนการตั้งค่าโปรเจ็กต์ ทางด้านซ้าย
    2. ในส่วนพร็อพเพอร์ตี้สคริปต์ ให้คลิกเพิ่มพร็อพเพอร์ตี้สคริปต์
    3. ในส่วน Property ให้ป้อนชื่อคีย์: GEMINI_API_KEY
    4. สําหรับค่า ให้ป้อนค่าสําหรับคีย์ API
    5. คลิกบันทึกพร็อพเพอร์ตี้ของสคริปต์
  5. แทนที่เนื้อหาไฟล์ Code.gs ด้วยโค้ดต่อไปนี้

ส่งคำขอแรก

ใช้เมธอด generateContent เพื่อส่งคําขอไปยัง Gemini API

PythonJavaScriptGoApps ScriptREST
from google import genai

client = genai.Client(api_key="YOUR_API_KEY")

response = client.models.generate_content(
    model="gemini-2.0-flash", contents="Explain how AI works in a few words"
)
print(response.text)
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });

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();
package main

import (
    "context"
    "fmt"
    "log"

    "google.golang.org/genai"
)

func main() {
    ctx := context.Background()
    client, err := genai.NewClient(ctx, &genai.ClientConfig{
        APIKey:  "YOUR_API_KEY",
        Backend: genai.BackendGeminiAPI,
    })
    if err != nil {
        log.Fatal(err)
    }

    result, err := client.Models.GenerateContent(
        ctx,
        "gemini-2.0-flash",
        genai.Text("Explain how AI works in a few words"),
        nil,
    )
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(result.Text())
}
// See https://developers.google.com/apps-script/guides/properties
// for instructions on how to set the API key.
const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');
function main() {
  const payload = {
    contents: [
      {
        parts: [
          { text: 'Explain how AI works in a few words' },
        ],
      },
    ],
  };

  const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${apiKey}`;
  const options = {
    method: 'POST',
    contentType: 'application/json',
    payload: JSON.stringify(payload)
  };

  const response = UrlFetchApp.fetch(url, options);
  const data = JSON.parse(response);
  const content = data['candidates'][0]['content']['parts'][0]['text'];
  console.log(content);
}
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$YOUR_API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [
      {
        "parts": [
          {
            "text": "Explain how AI works in a few words"
          }
        ]
      }
    ]
  }'

ขั้นตอนถัดไป

เมื่อส่งคำขอ API รายการแรกแล้ว คุณอาจต้องการดูคำแนะนำต่อไปนี้ซึ่งแสดงการทำงานของ Gemini