این راهنما بهترین شیوههایی را که میتوانید برای بهینهسازی استفاده از Live API دنبال کنید، پوشش میدهد. برای مرور کلی و نمونه کد برای موارد استفاده رایج، به صفحه شروع به کار با Live API مراجعه کنید.
دستورالعملهای سیستمی واضحی طراحی کنید
برای به دست آوردن بهترین عملکرد از Live API، توصیه میکنیم مجموعهای از دستورالعملهای سیستمی (SI) با تعریف واضح داشته باشید که شخصیت عامل، قوانین مکالمه و موانع را به ترتیب زیر تعریف کند.
برای بهترین نتیجه، هر عامل را در یک SI مجزا قرار دهید.
شخصیت نماینده را مشخص کنید: جزئیات مربوط به نام، نقش و هرگونه ویژگی ترجیحی نماینده را ارائه دهید. اگر میخواهید لهجه را مشخص کنید، حتماً زبان خروجی ترجیحی (مانند لهجه بریتانیایی برای یک گوینده انگلیسی) را نیز مشخص کنید.
قوانین مکالمه را مشخص کنید: این قوانین را به ترتیبی که انتظار دارید مدل از آن پیروی کند، قرار دهید. بین عناصر یکباره مکالمه و حلقههای مکالمه تمایز قائل شوید. برای مثال:
- عنصر یکبار مصرف: اطلاعات مشتری را یکبار جمعآوری کنید (مانند نام، موقعیت مکانی، شماره کارت وفاداری).
- حلقه مکالمه: کاربر میتواند در مورد توصیهها، قیمتگذاری، بازگشت کالا و تحویل بحث کند و ممکن است بخواهد از موضوعی به موضوع دیگر برود. به مدل اطلاع دهید که تا زمانی که کاربر بخواهد، میتواند در این حلقه مکالمه شرکت کند.
فراخوانیهای ابزار را در یک جریان با جملات مجزا مشخص کنید: برای مثال، اگر یک مرحلهی یکباره برای جمعآوری اطلاعات مشتری نیاز به فراخوانی تابع
get_user_infoداشته باشد، میتوانید بگویید: اولین قدم شما جمعآوری اطلاعات کاربر است. ابتدا از کاربر بخواهید نام، مکان و شماره کارت وفاداری خود را ارائه دهد. سپسget_user_infoرا با این جزئیات فراخوانی کنید.هرگونه محافظ لازم را اضافه کنید: هرگونه محافظ مکالمهای کلی که نمیخواهید مدل انجام دهد را ارائه دهید. در صورت تمایل میتوانید مثالهای خاصی از اینکه اگر x اتفاق بیفتد، میخواهید مدل y را انجام دهد، ارائه دهید. اگر هنوز به سطح دقت دلخواه نرسیدهاید، از کلمه unmistakably برای هدایت مدل به سمت دقت بیشتر استفاده کنید.
ابزارها را به طور دقیق تعریف کنید
هنگام استفاده از ابزارها با Live API، در تعریف ابزار خود دقیق باشید. حتماً به Gemini بگویید که تحت چه شرایطی باید فراخوانی ابزار انجام شود. برای جزئیات بیشتر، به تعاریف ابزار در بخش مثال مراجعه کنید.
دستورالعملهای مؤثر بسازید
- از دستورالعملهای واضح استفاده کنید: مثالهایی از آنچه مدلها باید و نباید در دستورالعملها انجام دهند، ارائه دهید و سعی کنید دستورالعملها را به یک دستورالعمل برای هر شخص یا نقش در هر زمان محدود کنید. به جای دستورالعملهای طولانی و چند صفحهای، استفاده از زنجیرهسازی دستورالعملها را در نظر بگیرید. این مدل در وظایفی با فراخوانیهای تک تابعی بهترین عملکرد را دارد.
- دستورات و اطلاعات شروع را ارائه دهید: Live API قبل از پاسخ دادن، ورودی کاربر را انتظار دارد. برای اینکه Live API مکالمه را آغاز کند، یک اعلان اضافه کنید که از آن بخواهد به کاربر سلام کند یا مکالمه را شروع کند. اطلاعاتی در مورد کاربر اضافه کنید تا Live API آن سلام را شخصیسازی کند.
زبان را مشخص کنید
برای عملکرد بهینه در API زندهی cascaded gemini-live-2.5-flash ، مطمئن شوید که language_code مربوط به API با زبانی که کاربر صحبت میکند مطابقت دارد.
اگر انتظار میرود که مدل به زبانی غیر از انگلیسی پاسخ دهد، موارد زیر را به عنوان بخشی از دستورالعملهای سیستم خود لحاظ کنید:
RESPOND IN {OUTPUT_LANGUAGE}. YOU MUST RESPOND UNMISTAKABLY IN {OUTPUT_LANGUAGE}.
پخش جریانی
هنگام پیادهسازی صدای بلادرنگ، این بهترین شیوهها را دنبال کنید:
- اندازه قطعه و تأخیر : صدا را در قطعات 20 تا 40 میلیثانیهای ارسال کنید.
- مدیریت وقفه : وقتی کاربر صحبت میکند در حالی که مدل در حال پاسخ دادن است، سرور یک پیام
server_contentبا"interrupted": trueارسال میکند. شما باید فوراً بافر صوتی سمت کلاینت خود را حذف کنید تا از ادامه صحبت عامل به جای کاربر جلوگیری شود.
مدیریت زمینه
برای جلسات طولانی ContextWindowCompressionConfig استفاده کنید، زیرا توکنهای صوتی بومی به سرعت جمع میشوند (تقریباً 25 توکن در هر ثانیه از صدا).
بافرینگ کلاینت
قبل از ارسال، صدای ورودی را به میزان قابل توجهی (مثلاً ۱ ثانیه) بافر نکنید. برای به حداقل رساندن تأخیر، تکههای کوچک (۲۰ تا ۱۰۰ میلیثانیه) ارسال کنید.
نمونهگیری مجدد
مطمئن شوید که برنامهی کلاینت شما قبل از انتقال، ورودی میکروفون (اغلب ۴۴.۱ کیلوهرتز یا ۴۸ کیلوهرتز) را به ۱۶ کیلوهرتز تغییر نمونهبرداری میکند.
مدیریت جلسه
برای مدیریت چرخه عمر جلسه و اطمینان از یک تجربه کاربری قابل اعتماد، این دستورالعملها را دنبال کنید:
- فعال کردن فشردهسازی پنجره زمینه: توکنهای صوتی تقریباً با سرعت ۲۵ توکن در ثانیه جمع میشوند. بدون فشردهسازی، جلسات فقط صوتی به ۱۵ دقیقه و جلسات صوتی-تصویری به ۲ دقیقه محدود میشوند. فشردهسازی پنجره زمینه را فعال کنید تا جلسات به مدت نامحدود تمدید شوند.
- پیادهسازی از سرگیری جلسه: سرور ممکن است به صورت دورهای اتصال WebSocket را مجدداً تنظیم کند. از از سرگیری جلسه برای اتصال مجدد یکپارچه و بدون از دست دادن متن استفاده کنید. آخرین توکن از سرگیری را از پیامهای
SessionResumptionUpdateنگه دارید و هنگام اتصال مجدد آن را به عنوان شناسه ارسال کنید. توکنهای از سرگیری به مدت ۲ ساعت پس از پایان آخرین جلسه معتبر هستند. - Handle GoAway messages: The server sends a GoAway message before terminating a connection. Listen for this message and use the
timeLeftfield to gracefully wrap up or reconnect before the connection closes. - مدیریت سیگنالهای generationComplete: از پیام
generationCompleteبرای اطلاع از زمان پایان تولید پاسخ توسط مدل استفاده کنید تا برنامه شما بتواند رابط کاربری خود را بهروزرسانی کند یا به اقدام بعدی بپردازد.
برای جزئیات پیادهسازی، به مدیریت جلسه مراجعه کنید.
مثالها
این مثال، بهترین شیوهها و دستورالعملهای طراحی آموزش سیستم را با هم ترکیب میکند تا عملکرد مدل را به عنوان یک مربی شغلی هدایت کند.
**Persona:**
You are Laura, a career coach from Brooklyn, NY. You specialize in providing
data driven advice to give your clients a fresh perspective on the career
questions they're navigating. Your special sauce is providing quantitative,
data-driven insights to help clients think about their issues in a different
way. You leverage statistics, research, and psychology as much as possible.
You only speak to your clients in English, no matter what language they speak
to you in.
**Conversational Rules:**
1. **Introduce yourself:** Warmly greet the client.
2. **Intake:** Ask for your client's full name, date of birth, and state they're
calling in from. Call `create_client_profile` to create a new patient profile.
3. **Discuss the client's issue:** Get a sense of what the client wants to
cover in the session. DO NOT repeat what the client is saying back to them in
your response. Don't ask more than a few questions here.
4. **Reframe the client's issue with real data:** NO PLATITUDES. Start providing
data-driven insights for the client, but embed these as general facts within
conversation. This is what they're coming to you for: your unique thinking on
the subjects that are stressing them out. Show them a new way of thinking about
something. Let this step go on for as long as the client wants. As part of this,
if the client mentions wanting to take any actions, update
`add_action_items_to_profile` to remind the client later.
5. **Next appointment:** Call `get_next_appointment` to see if another
appointment has already been scheduled for the client. If so, then share the
date and time with the client and confirm if they'll be able to attend. If
there is no appointment, then call `get_available_appointments` to see openings.
Share the list of openings with the client and ask what they would prefer. Save
their preference with `schedule_appointment`. If the client prefers to schedule
offline, then let them know that's perfectly fine and to use the patient portal.
**General Guidelines:** You're meant to be a witty, snappy conversational
partner. Keep your responses short and progressively disclose more information
if the client requests it. Don't repeat back what the client says back to them.
Each response you give should be a net new addition to the conversation, not a
recap of what the client said. Be relatable by bringing in your own background
growing up professionally in Brooklyn, NY. If a client tries to get you off
track, gently bring them back to the workflow articulated above.
**Guardrails:** If the client is being hard on themselves, never encourage that.
Remember that your ultimate goal is to create a supportive environment for your
clients to thrive.
تعاریف ابزار
این JSON توابع مرتبط فراخوانی شده در مثال مربی شغلی را تعریف میکند. برای بهترین نتیجه هنگام تعریف توابع، نام، توضیحات، پارامترها و شرایط فراخوانی آنها را ذکر کنید.
[
{
"name": "create_client_profile",
"description": "Creates a new client profile with their personal details. Returns a unique client ID. \n**Invocation Condition:** Invoke this tool *only after* the client has provided their full name, date of birth, AND state. This should only be called once at the beginning of the 'Intake' step.",
"parameters": {
"type": "object",
"properties": {
"full_name": {
"type": "string",
"description": "The client's full name."
},
"date_of_birth": {
"type": "string",
"description": "The client's date of birth in YYYY-MM-DD format."
},
"state": {
"type": "string",
"description": "The 2-letter postal abbreviation for the client's state (e.g., 'NY', 'CA')."
}
},
"required": ["full_name", "date_of_birth", "state"]
}
},
{
"name": "add_action_items_to_profile",
"description": "Adds a list of actionable next steps to a client's profile using their client ID. \n**Invocation Condition:** Invoke this tool *only after* a list of actionable next steps has been discussed and agreed upon with the client during the 'Actions' step. Requires the `client_id` obtained from the start of the session.",
"parameters": {
"type": "object",
"properties": {
"client_id": {
"type": "string",
"description": "The unique ID of the client, obtained from create_client_profile."
},
"action_items": {
"type": "array",
"items": {
"type": "string"
},
"description": "A list of action items for the client (e.g., ['Update resume', 'Research three companies'])."
}
},
"required": ["client_id", "action_items"]
}
},
{
"name": "get_next_appointment",
"description": "Checks if a client has a future appointment already scheduled using their client ID. Returns the appointment details or null. \n**Invocation Condition:** Invoke this tool at the *start* of the 'Next Appointment' workflow step, immediately after the 'Actions' step is complete. This is used to check if an appointment *already exists*.",
"parameters": {
"type": "object",
"properties": {
"client_id": {
"type": "string",
"description": "The unique ID of the client."
}
},
"required": ["client_id"]
}
},
{
"name": "get_available_appointments",
"description": "Fetches a list of the next available appointment slots. \n**Invocation Condition:** Invoke this tool *only if* the `get_next_appointment` tool was called and it returned `null` (or an empty response), indicating no future appointment is scheduled.",
"parameters": {
"type": "object",
"properties": {}
}
},
{
"name": "schedule_appointment",
"description": "Books a new appointment for a client at a specific date and time. \n**Invocation Condition:** Invoke this tool *only after* `get_available_appointments` has been called, a list of openings has been presented to the client, and the client has *explicitly confirmed* which specific date and time they want to book.",
"parameters": {
"type": "object",
"properties": {
"client_id": {
"type": "string",
"description": "The unique ID of the client."
},
"appointment_datetime": {
"type": "string",
"description": "The chosen appointment slot in ISO 8601 format (e.g., '2025-10-30T14:30:00')."
}
},
"required": ["client_id", "appointment_datetime"]
}
}
]