SDK AI tạo sinh mới của Google cung cấp một giao diện hợp nhất cho Gemini 2.0 thông qua cả API dành cho nhà phát triển Gemini và Vertex AI (API Gemini Enterprise). Ngoại trừ một số trường hợp, mã chạy trên một nền tảng sẽ chạy trên cả hai nền tảng. SDK AI tạo sinh cũng hỗ trợ các mô hình Gemini 1.5.
Python
SDK AI tạo sinh của Google dành cho Python có trên PyPI và GitHub.
Để tìm hiểu thêm, hãy xem tài liệu tham khảo về SDK Python.
Bắt đầu nhanh
1. Cài đặt SDK
pip install google-genai
2. Nhập thư viện
from google import genai
3. Tạo ứng dụng
client = genai.Client(api_key='GEMINI_API_KEY')
4. Tạo nội dung
response = client.models.generate_content(
model='gemini-2.0-flash', contents='How does RLHF work?'
)
print(response.text)
Go
SDK AI tạo sinh của Google dành cho Go có trên go.dev và GitHub.
Bắt đầu nhanh
1. Nhập thư viện
import "google.golang.org/genai"
2. Tạo ứng dụng
client, err := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: apiKey,
Backend: genai.BackendGeminiAPI,
})
3. Tạo nội dung
// Call the GenerateContent method
result, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", genai.Text("How does RLHF work?"), nil)
Java
Bạn có thể sử dụng SDK AI thế hệ mới của Google cho Java thông qua Maven và GitHub.
Bắt đầu nhanh
1. Nhập thư viện
Nếu bạn đang sử dụng Maven, hãy thêm các phần phụ thuộc sau vào:
<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>
2. Tạo ứng dụng
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. Tạo nội dung
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());
}
}