SDK สำหรับ Gen AI ของ Google

Google Gen AI SDK เวอร์ชันใหม่มีอินเทอร์เฟซแบบรวมสำหรับ Gemini 2.0 ทั้งผ่าน Gemini Developer API และ Vertex AI (Gemini Enterprise API) โค้ดที่ทำงานในแพลตฟอร์มหนึ่งจะทำงานในทั้ง 2 แพลตฟอร์มได้ โดยมีข้อยกเว้นบางประการ Gen AI SDK ยังรองรับโมเดล Gemini 1.5 ด้วย

Python

Google Gen AI SDK สําหรับ Python พร้อมให้บริการใน PyPI และ GitHub

ดูข้อมูลเพิ่มเติมได้ที่ข้อมูลอ้างอิง Python SDK

คู่มือเริ่มใช้งานฉบับย่อ

1. ติดตั้ง SDK

pip install google-genai

2. นำเข้าคลัง

from google import genai

3. สร้างไคลเอ็นต์

client = genai.Client(api_key='GEMINI_API_KEY')

4. สร้างเนื้อหา

response = client.models.generate_content(
    model='gemini-2.0-flash', contents='How does RLHF work?'
)
print(response.text)

JavaScript (เวอร์ชันตัวอย่าง)

ตัวอย่าง Google Gen AI SDK สำหรับ TypeScript และ JavaScript พร้อมให้บริการแล้วใน GitHub

ดูข้อมูลเพิ่มเติมได้ที่ข้อมูลอ้างอิงเกี่ยวกับ JavaScript SDK

คู่มือเริ่มใช้งานฉบับย่อ

1. ติดตั้ง SDK

npm install @google/genai

2. นำเข้าคลัง

import {GoogleGenAI} from '@google/genai';

3. สร้างไคลเอ็นต์

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

4. สร้างเนื้อหา

async function main() {
  const response = await ai.models.generateContent({
    model: 'gemini-2.0-flash-001',
    contents: 'How does RLHF work?',
  });
  console.log(response.text);
}

Go

Gen AI SDK ของ Google สำหรับ Go พร้อมให้บริการใน go.dev และ GitHub

คู่มือเริ่มใช้งานฉบับย่อ

1. นำเข้าคลัง

import "google.golang.org/genai"

2. สร้างไคลเอ็นต์

client, err := genai.NewClient(ctx, &genai.ClientConfig{
    APIKey:   apiKey,
    Backend:  genai.BackendGeminiAPI,
})

3. สร้างเนื้อหา

// Call the GenerateContent method
  result, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", genai.Text("How does RLHF work?"), nil)

Java

Google Gen AI SDK สําหรับ Java พร้อมใช้งานผ่าน Maven และ GitHub

คู่มือเริ่มใช้งานฉบับย่อ

1. นำเข้าคลัง

หากคุณใช้ Maven ให้เพิ่มรายการต่อไปนี้ลงใน Dependency

<dependencies>
  <dependency>
    <groupId>com.google.genai</groupId>
    <artifactId>google-genai</artifactId>
    <version>0.1.0</version>
  </dependency>
</dependencies>

2. สร้างไคลเอ็นต์

import com.google.genai.Client;

// The client gets the API key from the environment variable `GOOGLE_API_KEY`
Client client = new Client();

// Use the builder class for instantiation.
Client client = Client.builder().apiKey("your-api-key").build();

3. สร้างเนื้อหา

package <your.pack.name>;

import com.google.genai.Client;
import com.google.genai.types.GenerateContentResponse;
import java.io.IOException;
import org.apache.http.HttpException;

public class GenerateContentWithTextInput {
  public static void main(String[] args) throws IOException, HttpException {
    Client client = new Client();

    GenerateContentResponse response =
        client.models.generateContent("gemini-2.0-flash-001", "How does RLHF work?", null);
    System.out.println("Unary response: " + response.text());
  }
}