การทดลองใช้ Gemini Nano ออกแบบมาสำหรับนักพัฒนาแอปที่ต้องการทดสอบ การเพิ่มประสิทธิภาพแอปของตนด้วยความสามารถของ AI ในอุปกรณ์ที่ล้ำสมัย คำแนะนำนี้ให้รายละเอียดเกี่ยวกับวิธีทดสอบ Gemini Nano โดยใช้ Google AI Edge SDK ในแอปของคุณเอง
ดาวน์โหลดแอปตัวอย่าง
หากต้องการดูการสาธิตที่เตรียมไว้ โปรดดูแอปตัวอย่างใน GitHub
สิ่งที่ต้องมีก่อน
หากต้องการทดลองใช้ Gemini Nano คุณจะต้องมีอุปกรณ์ซีรีส์ Pixel 9 โปรดเตรียมอุปกรณ์ดังกล่าวไว้ก่อนดำเนินการต่อ และตรวจสอบว่าคุณได้ลงชื่อเข้าใช้ด้วยบัญชีที่ต้องการใช้สำหรับการทดสอบเท่านั้น
- เข้าร่วมกลุ่ม Google aicore-experimental
- เลือกเข้าร่วมโปรแกรมทดสอบ Android AICore
หลังจากทำตามขั้นตอนเหล่านี้เสร็จแล้ว ชื่อแอปใน Play Store (ในส่วนจัดการแอปและอุปกรณ์) ควรเปลี่ยนจาก "Android AICore" เป็น "Android AICore (เบต้า)"
อัปเดต APK และดาวน์โหลดไบนารี
- อัปเดต APK ของ AICore ดังนี้
- แตะไอคอนโปรไฟล์ที่ด้านขวาบน
- แตะจัดการแอปและอุปกรณ์ > จัดการ
- แตะ Android AICore
- แตะอัปเดตหากมีการอัปเดตพร้อมใช้งาน
- อัปเดต APK ของบริการประมวลผลแบบส่วนตัว ดังนี้
- แตะไอคอนโปรไฟล์ที่ด้านขวาบน
- แตะจัดการแอปและอุปกรณ์ > จัดการ
- แตะPrivate Compute Services
- แตะอัปเดตหากมีการอัปเดตพร้อมใช้งาน
- ตรวจสอบเวอร์ชันในแท็บเกี่ยวกับแอปนี้ และยืนยันว่าแอป เป็นเวอร์ชัน 1.0.release.658389993 ขึ้นไป
- รีสตาร์ทอุปกรณ์แล้วรอสักครู่เพื่อให้การลงทะเบียนทดสอบมีผล
- ตรวจสอบเวอร์ชัน APK ของ AICore ใน Play Store (ในแท็บ "เกี่ยวกับแอปนี้") เพื่อยืนยันว่าขึ้นต้นด้วย 0.thirdpartyeap
กำหนดค่า Gradle
เพิ่มข้อมูลต่อไปนี้ลงในบล็อกการขึ้นต่อกันในการกำหนดค่า build.gradle
implementation("com.google.ai.edge.aicore:aicore:0.0.1-exp01")
ในการbuild.gradle
กำหนดค่า ให้ตั้งค่าเป้าหมาย SDK ขั้นต่ำเป็น 31
defaultConfig {
...
minSdk = 31
...
}
รับ AICore และเรียกใช้การอนุมาน
สร้างออบเจ็กต์ GenerationConfig
ซึ่งมีพารามิเตอร์เพื่อปรับแต่งพร็อพเพอร์ตี้
สําหรับวิธีที่โมเดลควรเรียกใช้การอนุมาน
พารามิเตอร์ ได้แก่
- อุณหภูมิ: ควบคุมความสุ่ม ค่าที่สูงขึ้นจะเพิ่มความหลากหลาย
- Top K: จำนวนโทเค็นจากโทเค็นที่มีอันดับสูงสุดที่จะนำมาพิจารณา
- จำนวนผู้สมัคร: คำตอบสูงสุดที่จะแสดง
- โทเค็นเอาต์พุตสูงสุด: ความยาวของคำตอบ
- Worker Executor:
ExecutorService
ที่ควรเรียกใช้งานงานในเบื้องหลัง - Callback Executor:
Executor
ที่ควรเรียกใช้การเรียกกลับ
Kotlin
val generationConfig = generationConfig { context = ApplicationProvider.getApplicationContext() // required temperature = 0.2f topK = 16 maxOutputTokens = 256 }
Java
GenerationConfig.Builder configBuilder = GenerationConfig.Companion.builder(); configBuilder.setContext(context); configBuilder.setTemperature(0.2f); configBuilder.setTopK(16); configBuilder.setMaxOutputTokens(256);
สร้าง downloadCallback
(ไม่บังคับ) ฟังก์ชัน Callback นี้ใช้สำหรับ
การดาวน์โหลดโมเดล ข้อความที่ส่งกลับมามีไว้เพื่อการแก้ไขข้อบกพร่อง
สร้างออบเจ็กต์ GenerativeModel
ด้วยการกำหนดค่าการสร้างและการดาวน์โหลดที่ไม่บังคับ
ที่คุณสร้างไว้ก่อนหน้านี้
Kotlin
val downloadConfig = DownloadConfig(downloadCallback) val generativeModel = GenerativeModel( generationConfig = generationConfig, downloadConfig = downloadConfig // optional )
Java
GenerativeModel generativeModel = new GenerativeModel( generationConfig, downloadConfig = DownloadConfig(downloadCallback) // optional );
เรียกใช้การอนุมานด้วยโมเดลและส่งพรอมต์ เนื่องจาก
GenerativeModel.generateContent()
เป็นฟังก์ชันระงับ เราจึงต้องตรวจสอบว่า
ฟังก์ชันนี้อยู่ในขอบเขตของโครูทีนที่ถูกต้องเพื่อเปิดใช้
Kotlin
scope.launch { // Single string input prompt val input = "I want you to act as an English proofreader. I will provide you texts, and I would like you to review them for any spelling, grammar, or punctuation errors. Once you have finished reviewing the text, provide me with any necessary corrections or suggestions for improving the text: These arent the droids your looking for." val response = generativeModel.generateContent(input) print(response.text) // Or multiple strings as input val response = generativeModel.generateContent( content { text("I want you to act as an English proofreader. I will provide you texts and I would like you to review them for any spelling, grammar, or punctuation errors.") text("Once you have finished reviewing the text, provide me with any necessary corrections or suggestions for improving the text:") text("These arent the droids your looking for.") } ) print(response.text) }
Java
Futures.addCallback( String input = "I want you to act as an English proofreader. I will provide you texts, and I would like you to review them for any spelling, grammar, or punctuation errors. Once you have finished reviewing the text, provide me with any necessary corrections or suggestions for improving the text: These aren't the droids you're looking for." generativeModelFutures.generateContent(input), new FutureCallback<GenerateContentResponse>() { @Override public void onSuccess(GenerateContentResponse result) { // generation successful } @Override public void onFailure(Throwable t) { // generation failed } }, ContextCompat.getMainExecutor(this));
หากมีข้อเสนอแนะเกี่ยวกับ Google AI Edge SDK หรือข้อเสนอแนะอื่นๆ สำหรับ ทีมของเรา โปรดส่งคำขอแจ้งปัญหา
เคล็ดลับเกี่ยวกับพรอมต์
การออกแบบพรอมต์คือกระบวนการสร้างพรอมต์ที่ดึงคำตอบที่เหมาะสมที่สุดจากโมเดลภาษา การเขียนพรอมต์ที่มีโครงสร้างดีเป็นส่วนสำคัญ ในการรับประกันว่าโมเดลภาษาจะให้คำตอบที่ถูกต้องและมีคุณภาพสูง เราได้ รวมตัวอย่างบางส่วนไว้เพื่อช่วยให้คุณเริ่มต้นใช้งานกรณีการใช้งานทั่วไปสำหรับ Gemini Nano ดูข้อมูลเพิ่มเติมได้ที่กลยุทธ์การเขียนพรอมต์สำหรับ Gemini
สำหรับการเขียนใหม่
I want you to act as an English proofreader. I will provide you texts, and I
would like you to review them for any spelling, grammar, or punctuation errors.
Once you have finished reviewing the text, provide me with any necessary
corrections or suggestions for improving the text: These arent the droids your
looking for
สำหรับกรณีการใช้งานฟีเจอร์ช่วยตอบ
Prompt: Predict up to 5 emojis as a response to a text chat message. The output
should only include emojis.
input: The new visual design is blowing my mind 🤯
output: ➕,💘, ❤🔥
input: Well that looks great regardless
output: 💗,🪄
input: Unfortunately this won't work
output: 💔,😔
input: sounds good, I'll look into that
output: 🙏,👍
input: 10hr cut of jeff goldblum laughing URL
output: 😂,💀,⚰️
input: Woo! Launch time!
Output:
สำหรับการสรุป
Summarize this text as bullet points of key information.
Text: A quantum computer exploits quantum mechanical phenomena to perform
calculations exponentially faster than any modern traditional computer. At
very tiny scales, physical matter acts as both particles and as waves, and
quantum computing uses specialized hardware to leverage this behavior. The
operating principles of quantum devices are beyond the scope of classical
physics. When deployed at scale, quantum computers could be used in a wide
variety of applications such as: in cybersecurity to break existing encryption
methods while helping researchers create new ones, in meteorology to develop
better weather forecasting etc. However, the current state-of-the-art quantum
computers are still largely experimental and impractical.