การเริ่มต้นอย่างรวดเร็วนี้จะแสดงวิธีเริ่มต้นใช้งาน Gemini API โดยใช้ SDK ที่คุณเลือก
ข้อกำหนดเบื้องต้น
Python
ดูใน AI ของ Google | เรียกใช้ใน Google Colab | ดูสมุดบันทึกใน GitHub |
โปรดตรวจสอบว่าสภาพแวดล้อมในการพัฒนาซอฟต์แวร์ของคุณเป็นไปตามข้อกำหนดต่อไปนี้ เพื่อให้การเริ่มต้นอย่างรวดเร็วนี้เสร็จสมบูรณ์ภายในเครื่อง
- Python 3.9 ขึ้นไป
- การติดตั้ง
jupyter
เพื่อเรียกใช้สมุดบันทึก
Go
การเริ่มต้นอย่างรวดเร็วนี้จะถือว่าคุณคุ้นเคยกับการสร้างแอปพลิเคชันด้วย Go
ในการเริ่มต้นอย่างรวดเร็วนี้ โปรดตรวจสอบว่าสภาพแวดล้อมในการพัฒนาซอฟต์แวร์ของคุณเป็นไปตามข้อกำหนดต่อไปนี้
- เริ่มเวอร์ชัน 1.20 ขึ้นไป
Node.js
การเริ่มต้นอย่างรวดเร็วนี้จะถือว่าคุณคุ้นเคยกับการสร้างแอปพลิเคชันด้วย Node.js
ในการเริ่มต้นอย่างรวดเร็วนี้ โปรดตรวจสอบว่าสภาพแวดล้อมในการพัฒนาซอฟต์แวร์ของคุณเป็นไปตามข้อกำหนดต่อไปนี้
- Node.js v18 ขึ้นไป
- npm
เว็บไซต์
การเริ่มต้นอย่างรวดเร็วนี้จะถือว่าคุณคุ้นเคยกับการใช้ JavaScript เพื่อพัฒนาเว็บแอป คู่มือนี้ไม่เกี่ยวข้องกับเฟรมเวิร์ก
ในการเริ่มต้นอย่างรวดเร็วนี้ โปรดตรวจสอบว่าสภาพแวดล้อมในการพัฒนาซอฟต์แวร์ของคุณเป็นไปตามข้อกำหนดต่อไปนี้
- (ไม่บังคับ) Node.js
- เว็บเบราว์เซอร์สมัยใหม่
ลูกดอก (Flutter)
การเริ่มต้นอย่างรวดเร็วนี้จะถือว่าคุณคุ้นเคยกับการสร้างแอปพลิเคชันด้วย Dart
ในการเริ่มต้นอย่างรวดเร็วนี้ โปรดตรวจสอบว่าสภาพแวดล้อมในการพัฒนาซอฟต์แวร์ของคุณเป็นไปตามข้อกำหนดต่อไปนี้
- Dart 3.2.0 ขึ้นไป
Swift
การเริ่มต้นอย่างรวดเร็วนี้จะถือว่าคุณคุ้นเคยกับการใช้ Xcode เพื่อพัฒนาแอป Swft
ในการเริ่มต้นอย่างรวดเร็วนี้ โปรดตรวจสอบว่าสภาพแวดล้อมการพัฒนาและแอป Swft เป็นไปตามข้อกำหนดต่อไปนี้
- Xcode 15.0 ขึ้นไป
- แอป Swift ต้องกำหนดเป้าหมายไปที่ iOS 15 ขึ้นไปหรือ macOS 12 ขึ้นไป
Android
การเริ่มต้นอย่างรวดเร็วนี้จะถือว่าคุณคุ้นเคยกับการใช้ Android Studio เพื่อพัฒนาแอป Android
ในการเริ่มต้นอย่างรวดเร็วนี้ โปรดตรวจสอบว่าสภาพแวดล้อมการพัฒนาและแอป Android ของคุณเป็นไปตามข้อกำหนดต่อไปนี้
- Android Studio (เวอร์ชันล่าสุด)
- แอป Android ต้องกำหนดเป้าหมายเป็น API ระดับ 21 ขึ้นไป
ตั้งค่าคีย์ API
หากต้องการใช้ Gemini API คุณจะต้องมีคีย์ API หากยังไม่มี ให้ สร้างคีย์ใน Google AI Studio
จากนั้นกำหนดค่าคีย์
Python
ขอแนะนำว่าอย่าตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน การเริ่มต้นอย่างรวดเร็วนี้จะถือว่าคุณเข้าถึง คีย์ API เป็นตัวแปรสภาพแวดล้อม
กำหนดคีย์ API ให้กับตัวแปรสภาพแวดล้อม
export API_KEY=<YOUR_API_KEY>
Go
ขอแนะนำว่าอย่าตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน การเริ่มต้นอย่างรวดเร็วนี้จะถือว่าคุณเข้าถึง คีย์ API เป็นตัวแปรสภาพแวดล้อม
กำหนดคีย์ API ให้กับตัวแปรสภาพแวดล้อม
export API_KEY=<YOUR_API_KEY>
Node.js
ขอแนะนำว่าอย่าตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน การเริ่มต้นอย่างรวดเร็วนี้จะถือว่าคุณเข้าถึง คีย์ API เป็นตัวแปรสภาพแวดล้อม
กำหนดคีย์ API ให้กับตัวแปรสภาพแวดล้อม
export API_KEY=<YOUR_API_KEY>
เว็บไซต์
ขอแนะนำว่าอย่าตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน ดังนั้น คุณจึงควรส่งคีย์ API ไปยังแอปก่อนที่จะเริ่มต้นโมเดล
การเริ่มต้นอย่างรวดเร็วนี้จะถือว่าคุณเข้าถึงคีย์ API เป็นค่าคงที่ทั่วโลก
ลูกดอก (Flutter)
ขอแนะนำว่าอย่าตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน การเริ่มต้นอย่างรวดเร็วนี้จะถือว่าคุณเข้าถึง
คีย์ API เป็นตัวแปรสภาพแวดล้อมการประมวลผล หากกำลังพัฒนาแอป Flutter คุณสามารถใช้ String.fromEnvironment
และส่ง --dart-define=API_KEY=$API_KEY
ไปยัง flutter build
หรือ flutter run
เพื่อคอมไพล์ด้วยคีย์ API เนื่องจากสภาพแวดล้อมของกระบวนการจะแตกต่างออกไปเมื่อเรียกใช้แอป
Swift
ขอแนะนำว่าอย่าตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน อีกตัวเลือกหนึ่งคือการจัดเก็บไว้ในไฟล์ GenerativeAI-Info.plist
แล้วอ่านคีย์ API จากไฟล์ .plist
อย่าลืมใส่ไฟล์ .plist
นี้ในโฟลเดอร์รากของแอปและยกเว้นไฟล์ดังกล่าวจากการควบคุมเวอร์ชัน
enum APIKey {
// Fetch the API key from `GenerativeAI-Info.plist`
static var `default`: String {
guard let filePath = Bundle.main.path(forResource: "GenerativeAI-Info", ofType: "plist")
else {
fatalError("Couldn't find file 'GenerativeAI-Info.plist'.")
}
let plist = NSDictionary(contentsOfFile: filePath)
guard let value = plist?.object(forKey: "API_KEY") as? String else {
fatalError("Couldn't find key 'API_KEY' in 'GenerativeAI-Info.plist'.")
}
if value.starts(with: "_") {
fatalError(
"Follow the instructions at https://ai.google.dev/tutorials/setup to get an API key."
)
}
return value
}
}
Android
ขอแนะนำว่าอย่าตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน แต่ควรจัดเก็บไว้ในไฟล์ local.properties
(ซึ่งอยู่ในไดเรกทอรีรูทของโปรเจ็กต์ แต่ไม่รวมอยู่ในการควบคุมเวอร์ชัน) แล้วใช้ปลั๊กอิน Secret Gradle สำหรับ Android เพื่ออ่านคีย์ API เป็นตัวแปรการกําหนดค่าบิลด์
Kotlin:
// Access your API key as a Build Configuration variable
val apiKey = BuildConfig.apiKey
Java:
// Access your API key as a Build Configuration variable
String apiKey = BuildConfig.apiKey;
หากต้องการดูการติดตั้งใช้งานปลั๊กอิน Secrets Gradle โปรดดู
แอปตัวอย่าง
สำหรับ SDK นี้ หรือใช้ตัวอย่างล่าสุดของ Android Studio Iguana ที่มี
เทมเพลต Gemini API Starter
(ซึ่งมีไฟล์ local.properties
ให้คุณเริ่มต้นใช้งาน)
ติดตั้ง SDK
Python
Python SDK สำหรับ Gemini API อยู่ในแพ็กเกจ google-generativeai
ติดตั้งทรัพยากร Dependency โดยใช้ PIP:
pip install -q -U google-generativeai
Go
หากต้องการใช้ Gemini API ในแอปพลิเคชันของคุณเอง คุณต้องget
แพ็กเกจ Go SDK ในไดเรกทอรีโมดูลดังนี้
go get github.com/google/generative-ai-go
Node.js
หากต้องการใช้ Gemini API ในแอปพลิเคชันของคุณเอง คุณต้องติดตั้งแพ็กเกจ GoogleGenerativeAI
สำหรับ Node.js โดยทำดังนี้
npm install @google/generative-ai
เว็บไซต์
หากต้องการใช้ Gemini API ในเว็บแอปของคุณเอง ให้นำเข้า @google/generative-ai
ดังนี้
<script type="importmap">
{
"imports": {
"@google/generative-ai": "https://esm.run/@google/generative-ai"
}
}
</script>
ลูกดอก (Flutter)
หากต้องการใช้ Gemini API ในแอปพลิเคชันของคุณเอง คุณต้องadd
แพ็กเกจ google_generative_ai
ลงในแอป Dart หรือ Flutter ดังนี้
ลูกดอก:
dart pub add google_generative_ai
แขนระบาย:
flutter pub add google_generative_ai
Swift
หากต้องการใช้ Gemini API ในแอป Swift ของคุณเอง ให้เพิ่มแพ็กเกจ GoogleGenerativeAI
ลงในแอปโดยทำดังนี้
ใน Xcode ให้คลิกขวาที่โปรเจ็กต์ในตัวนำทางโปรเจ็กต์
เลือกเพิ่มแพ็กเกจจากเมนูตามบริบท
ในกล่องโต้ตอบเพิ่มแพ็กเกจ ให้วาง URL แพ็กเกจในแถบค้นหา
none https://github.com/google/generative-ai-swift
คลิกเพิ่มแพ็กเกจ Xcode จะเพิ่มแพ็กเกจ
GoogleGenerativeAI
ลงในโปรเจ็กต์ของคุณ
Android
ในโมดูล (ระดับแอป) ไฟล์การกำหนดค่า Gradle (เช่น
<project>/<app-module>/build.gradle.kts
) ให้เพิ่มทรัพยากร Dependency สำหรับ Google AI SDK สำหรับ Android ดังนี้Kotlin:
dependencies { // add the dependency for the Google AI client SDK for Android implementation("com.google.ai.client.generativeai:generativeai:0.3.0") }
Java:
สำหรับ Java คุณต้องเพิ่มไลบรารีอีก 2 รายการ
dependencies { // add the dependency for the Google AI client SDK for Android implementation("com.google.ai.client.generativeai:generativeai:0.3.0") // Required for one-shot operations (to use `ListenableFuture` from Guava Android) implementation("com.google.guava:guava:31.0.1-android") // Required for streaming operations (to use `Publisher` from Reactive Streams) implementation("org.reactivestreams:reactive-streams:1.0.4") }
ซิงค์โปรเจ็กต์ Android กับไฟล์ Gradle
เริ่มต้นโมเดล Generative
Python
คุณต้องนำเข้าและเริ่มต้นโมเดล Generative ก่อนจึงจะทำการเรียก API ได้
import google.generativeai as genai
genai.configure(api_key=os.environ["API_KEY"])
model = genai.GenerativeModel('gemini-pro')
Go
คุณต้องนำเข้าและเริ่มต้นโมเดล Generative ก่อนจึงจะทำการเรียก API ได้
import "github.com/google/generative-ai-go/genai"
import "google.golang.org/api/option"
ctx := context.Background()
// Access your API key as an environment variable (see "Set up your API key" above)
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
// For text-only input, use the gemini-pro model
model := client.GenerativeModel("gemini-pro")
Node.js
คุณต้องนำเข้าและเริ่มต้นโมเดล Generative ก่อนจึงจะทำการเรียก API ได้
const { GoogleGenerativeAI } = require("@google/generative-ai");
// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
// ...
// For text-only input, use the gemini-pro model
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
// ...
เว็บไซต์
คุณต้องนำเข้าและเริ่มต้นโมเดล Generative ก่อนจึงจะทำการเรียก API ได้
<html>
<body>
<!-- ... Your HTML and CSS -->
<!-- Import @google/generative-ai, as shown above. -->
<script type="module">
import { GoogleGenerativeAI } from "@google/generative-ai";
// Fetch your API_KEY
const API_KEY = "...";
// Access your API key (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI(API_KEY);
// ...
// For text-only input, use the gemini-pro model
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
// ...
</script>
</body>
</html>
ลูกดอก (Flutter)
คุณต้องนำเข้าและเริ่มต้นโมเดล Generative ก่อนจึงจะทำการเรียก API ได้
import 'package:google_generative_ai/google_generative_ai.dart';
// Access your API key as an environment variable (see "Set up your API key" above)
final apiKey = Platform.environment['API_KEY'];
if (apiKey == null) {
print('No \$API_KEY environment variable');
exit(1);
}
// For text-only input, use the gemini-pro model
final model = GenerativeModel(model: 'gemini-pro', apiKey: apiKey);
Swift
คุณต้องเริ่มต้นโมเดล Generative ก่อนจึงจะทำการเรียก API ได้
นำเข้าโมดูล
GoogleAI
:import GoogleGenerativeAI
เริ่มต้นโมเดล Generative ดังนี้
// For text-only input, use the gemini-pro model // Access your API key from your on-demand resource .plist file (see "Set up your API key" above) let model = GenerativeModel(name: "gemini-pro", apiKey: APIKey.default)
Android
ก่อนที่จะเรียก API คุณต้องเริ่มต้นออบเจ็กต์ GenerativeModel
โดยทำดังนี้
Kotlin:
val generativeModel = GenerativeModel(
// For text-only input, use the gemini-pro model
modelName = "gemini-pro",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
apiKey = BuildConfig.apiKey
)
Java:
สำหรับ Java คุณต้องเริ่มต้นออบเจ็กต์ GenerativeModelFutures
ด้วย
// For text-only input, use the gemini-pro model
GenerativeModel gm = new GenerativeModel(/* modelName */ "gemini-pro",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
/* apiKey */ BuildConfig.apiKey);
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
สร้างข้อความ
Python
response = model.generate_content("Write a story about a magic backpack.")
print(response.text)
Go
resp, err := model.GenerateContent(ctx, genai.Text("Write a story about a magic backpack."))
if err != nil {
log.Fatal(err)
}
Node.js
async function run() {
const prompt = "Write a story about a magic backpack."
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
console.log(text);
}
run();
เว็บไซต์
async function run() {
const prompt = "Write a story about a magic backpack."
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
console.log(text);
}
run();
ลูกดอก (Flutter)
void main() async {
// Access your API key as an environment variable (see "Set up your API key" above)
final apiKey = Platform.environment['API_KEY'];
if (apiKey == null) {
print('No \$API_KEY environment variable');
exit(1);
}
// For text-only input, use the gemini-pro model
final model = GenerativeModel(model: 'gemini-pro', apiKey: apiKey);
final content = [Content.text('Write a story about a magic backpack.')];
final response = await model.generateContent(content);
print(response.text);
}
Swift
let prompt = "Write a story about a magic backpack."
let response = try await model.generateContent(prompt)
if let text = response.text {
print(text)
}
Android
Kotlin:
โปรดทราบว่า generateContent()
เป็นฟังก์ชันระงับและต้องเรียกใช้จากขอบเขต Coroutine หากคุณไม่คุ้นเคยกับ Coroutines โปรดอ่าน
Kotlin Coroutines บน Android
val prompt = "Write a story about a magic backpack."
val response = generativeModel.generateContent(prompt)
print(response.text)
Java:
โปรดทราบว่า generateContent()
แสดงผล ListenableFuture
หากคุณไม่คุ้นเคยกับ API นี้ โปรดดูเอกสารประกอบของ Android เกี่ยวกับการใช้ ListenableFuture
Content content = new Content.Builder()
.addText("Write a story about a magic backpack.")
.build();
Executor executor = // ...
ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
ขั้นตอนถัดไป
หากต้องการดูข้อมูลเพิ่มเติมเกี่ยวกับการทำงานกับ Gemini API โปรดดูบทแนะนำสำหรับภาษาที่คุณต้องการ
คุณยังใช้คำสั่ง curl
เพื่อลองใช้ Gemini API ได้ด้วย โดยทำดังนี้
หากคุณเพิ่งเคยใช้โมเดล Generative AI ให้อ่านคู่มือแนวคิดและภาพรวมของ Gemini API ก่อนที่จะลองเริ่มใช้งานอย่างรวดเร็ว