เผยแพร่เมื่อวันที่ 27 มกราคม 2025
วิดีโออธิบาย | เว็บ | ส่วนขยาย | สถานะ Chrome | ความตั้งใจ |
---|---|---|---|---|
GitHub | ไม่เกี่ยวข้อง | ไม่เกี่ยวข้อง |
ฟีเจอร์สําคัญอย่างหนึ่งของ Prompt API คือเซสชัน ซึ่งช่วยให้คุณสนทนากับโมเดล AI ได้อย่างต่อเนื่อง 1 ครั้งหรือหลายครั้ง โดยที่โมเดลไม่หลงทางจากบริบทของสิ่งที่พูด คู่มือนี้จะแนะนำแนวทางปฏิบัติแนะนำสำหรับการจัดการเซสชันด้วยโมเดลภาษา
คุณอาจต้องจัดการเซสชันสำหรับเซสชันที่ทำงานพร้อมกันอย่างน้อย 1 รายการหากกำลังสร้างแชทบ็อตแบบคลาสสิกที่ผู้ใช้รายหนึ่งโต้ตอบกับ AI หรือหากคุณมีระบบการจัดการความสัมพันธ์กับลูกค้า (CRM) ที่ตัวแทนฝ่ายสนับสนุน 1 คนต้องจัดการกับลูกค้าหลายรายพร้อมกันและใช้ AI เพื่อช่วยตัวแทนฝ่ายสนับสนุนติดตามการสนทนาต่างๆ
เริ่มต้นเซสชันด้วยพรอมต์ของระบบ
ข้อความแจ้งของระบบจะตั้งค่าบริบทของเซสชันตั้งแต่เริ่มต้น เช่น คุณสามารถใช้พรอมต์ของระบบเพื่อบอกให้โมเดลตอบสนองอย่างไร
// Make this work in web apps and in extensions.
const aiNamespace = self.ai || chrome.aiOriginTrial || chrome.ai;
const languageModel = await aiNamespace.languageModel.create({
systemPrompt: 'You are a helpful assistant and you speak like a pirate.',
});
console.log(await languageModel.prompt('Tell me a joke.'));
// 'Avast ye, matey! What do you call a lazy pirate?\n\nA **sail-bum!**\n\nAhoy
// there, me hearties! Want to hear another one? \n'
โคลนเซสชันหลัก
หากต้องการเริ่มเซสชันใหม่หลังจากเซสชันสิ้นสุดลง หรือหากต้องการการสนทนาอิสระหลายรายการพร้อมกัน คุณสามารถโคลนเซสชันหลักได้
โคลนจะรับค่าพารามิเตอร์เซสชัน เช่น temperature
หรือ topK
และประวัติการโต้ตอบของเซสชันทั้งหมด ซึ่งจะมีประโยชน์ในกรณีที่คุณเริ่มต้นเซสชันหลักด้วยพรอมต์ของระบบ วิธีนี้ทำให้แอปของคุณต้องดำเนินการนี้เพียงครั้งเดียวเท่านั้น โคลนทั้งหมดจะรับพรอมต์ของระบบมาจากเซสชันหลัก
// Make this work in web apps and in extensions.
const aiNamespace = self.ai || chrome.aiOriginTrial || chrome.ai;
const languageModel = await aiNamespace.languageModel.create({
systemPrompt: 'You are a helpful assistant and you speak like a pirate.',
});
// The original session `languageModel` remains unchanged, and
// the two clones can be interacted with independently from each other.
const firstClonedLanguageModel = await languageModel.clone();
const secondClonedLanguageModel = await languageModel.clone();
// Interact with the sessions independently.
await firstClonedLanguageModel.prompt('Tell me a joke about parrots.');
await secondClonedLanguageModel.prompt('Tell me a joke about treasure troves.');
// Each session keeps its own context.
// The first session's context is jokes about parrots.
await firstClonedLanguageModel.prompt('Tell me another.');
// The second session's context is jokes about treasure troves.
await secondClonedLanguageModel.prompt('Tell me another.');
กู้คืนเซสชันที่ผ่านมา
พรอมต์เริ่มต้นช่วยให้คุณเตรียมโมเดลด้วยชุดตัวอย่างพรอมต์และคำตอบเพื่อสร้างผลลัพธ์ที่ดีขึ้นได้ มักใช้ในพรอมต์แบบหลายช็อตเพื่อสร้างคำตอบที่ตรงกับความต้องการของคุณ
หากติดตามการสนทนากับโมเดลอย่างต่อเนื่อง คุณจะใช้แนวทางนี้เพื่อกู้คืนเซสชันได้ เช่น หลังจากเบราว์เซอร์รีสตาร์ท คุณสามารถช่วยผู้ใช้ให้มีส่วนร่วมกับโมเดลต่อจากจุดที่หยุดไว้ได้ โดยวิธีหนึ่งคือติดตามประวัติเซสชันในพื้นที่เก็บข้อมูลในเครื่อง
// Make this work in web apps and in extensions.
const aiNamespace = self.ai || chrome.aiOriginTrial || chrome.ai;
// Restore the session from localStorage, or initialize a new session.
// The UUID is hardcoded here, but would come from a
// session picker in your user interface.
const uuid = '7e62c0e0-6518-4658-bc38-e7a43217df87';
function getSessionData(uuid) {
try {
const storedSession = localStorage.getItem(uuid);
return storedSession ? JSON.parse(storedSession) : false;
} catch {
return false;
}
}
let sessionData = getSessionData(uuid);
// Initialize a new session.
if (!sessionData) {
// Get the current default parameters so they can be restored as they were,
// even if the default values change in the future.
const { defaultTopK, defaultTemperature } =
await aiNamespace.languageModel.capabilities();
sessionData = {
systemPrompt: '',
initialPrompts: [],
topK: defaultTopK,
temperature: defaultTemperature,
};
}
// Initialize the session with the (previously stored or new) session data.
const languageModel = await aiNamespace.languageModel.create(sessionData);
// Keep track of the ongoing conversion and store it in localStorage.
const prompt = 'Tell me a joke';
try {
const stream = languageModel.promptStreaming(prompt);
let result = '';
// You can already work with each `chunk`, but then store
// the final `result` in history.
for await (const chunk of stream) {
// In practice, you'd render the chunk.
console.log(chunk);
result = chunk;
}
sessionData.initialPrompts.push(
{ role: 'user', content: prompt },
{ role: 'assistant', content: result },
);
// To avoid growing localStorage infinitely, make sure to delete
// no longer used sessions from time to time.
localStorage.setItem(uuid, JSON.stringify(sessionData));
} catch (err) {
console.error(err.name, err.message);
}
รักษาโควต้าเซสชันไว้โดยให้ผู้ใช้หยุดโมเดล
เซสชันแต่ละรายการจะมีกรอบบริบทที่คุณดูได้โดยไปที่ช่องที่เกี่ยวข้องของเซสชัน maxTokens
, tokensLeft
และ tokensSoFar
const { maxTokens, tokensLeft, tokensSoFar } = languageModel;
เมื่อเกินกรอบเวลาบริบทนี้ จะทำให้เซสชันไม่ติดตามข้อความเก่าที่สุด ซึ่งอาจไม่ต้องการเนื่องจากบริบทนี้อาจสำคัญ หากหลังจากส่งพรอมต์แล้ว ผู้ใช้เห็นว่าคำตอบนั้นไม่เป็นประโยชน์ โปรดอนุญาตให้ผู้ใช้หยุดโมเดลภาษาไม่ให้ตอบโดยใช้ AbortController
เพื่อรักษาโควต้า
ทั้งเมธอด prompt()
และ promptStreaming()
ยอมรับพารามิเตอร์ที่ 2 (ไม่บังคับ) ที่มีช่อง signal
เพื่อให้ผู้ใช้หยุดเซสชันได้
const controller = new AbortController();
stopButton.onclick = () => controller.abort();
try {
const stream = languageModel.promptStreaming('Write me a poem!', {
signal: controller.signal,
});
for await (const chunk of stream) {
console.log(chunk);
}
} catch (err) {
// Ignore `AbortError` errors.
if (err.name !== 'AbortError') {
console.error(err.name, err.message);
}
}
สาธิต
ดูการจัดการเซสชันด้วย AI ได้ในการสาธิตการจัดการเซสชันด้วย AI สร้างการสนทนาหลายรายการพร้อมกันด้วย Prompt API, โหลดแท็บซ้ำ หรือแม้แต่รีสตาร์ทเบราว์เซอร์ แล้วดำเนินการต่อจากที่ค้างไว้ ดูซอร์สโค้ดใน GitHub
สรุป
การจัดการเซสชัน AI อย่างรอบคอบด้วยเทคนิคและแนวทางปฏิบัติแนะนำเหล่านี้จะช่วยให้คุณปลดล็อกศักยภาพของ Prompt API ได้อย่างเต็มที่ ซึ่งจะมอบแอปพลิเคชันที่มีประสิทธิภาพ ตอบสนองได้รวดเร็ว และเน้นผู้ใช้เป็นศูนย์กลางมากขึ้น นอกจากนี้ คุณยังใช้แนวทางเหล่านี้ร่วมกันได้ด้วย เช่น โดยการอนุญาตให้ผู้ใช้โคลนเซสชันที่ผ่านมาที่กู้คืนแล้ว เพื่อให้ผู้ใช้สามารถเรียกใช้สถานการณ์ "จะเกิดอะไรขึ้นหาก"
ขอขอบคุณ
คู่มือนี้ได้รับการตรวจสอบโดย Sebastian Benz, Andre Bandarra, François Beaufort และ Alexandra Klepper