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

Google Gen AI SDK เวอร์ชันใหม่มีอินเทอร์เฟซแบบรวมสำหรับ Gemini 2.0 ทั้งผ่าน Gemini Developer API และ Vertex AI (Gemini Enterprise API) โค้ดที่ทำงานในแพลตฟอร์มหนึ่งจะทำงานในอีกแพลตฟอร์มหนึ่งด้วย ยกเว้นบางกรณี 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)

Go

Google Gen AI SDK สําหรับ 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());
  }
}