0Pricing
AI Prompt Engineering · درس

الوكلاء متعددو الوسائط للصوت والنص

تنسيق الردود المنطوقة مع النص الظاهر على الشاشة في أنظمة الوكلاء الصوتيين

الوكلاء متعددو الوسائط للصوت والنص درس مجاني في AI Prompt Engineering على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في AI Prompt Engineering، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة AI Prompt Engineering 4 دروس في المجموع.

السياقات الصوتية فقط مقابل السياقات متعددة الوسائط

تعمل وكلاء الذكاء الاصطناعي الصوتي في سياقين مختلفين جوهريًا:

  • الصوت فقط: مكبرات الصوت الذكية، وأنظمة IVR، والمكالمات الهاتفية — يسمع المستخدمون الصوت فقط ولا توجد شاشة
  • متعدد الوسائط: تطبيقات الهاتف المحمول، وتطبيقات الويب، ولوحات قيادة السيارات — يمكن للمستخدمين رؤية الشاشة وسماع الصوت في الوقت نفسه

يتطلب هذان السياقان استراتيجيات مختلفة للاستجابة. ففي سياق الصوت فقط، يجب نطق كل شيء. أما في السياق متعدد الوسائط، فيمكنك تنسيق ما يُنطق مع ما يُعرض.

صياغة الموجّه للاستجابات الصوتية فقط

في سياقات الصوت فقط، يجب أن ينتج LLM استجابات تعمل بالكامل من دون عناصر مرئية. وهذا يعني عدم الإشارة إلى عناصر الشاشة، وعدم استخدام قوائم تتطلب تصفحًا بصريًا، وعدم تقديم محتوى لا يكون مفهومًا إلا بفضل التنسيق.

VOICE_ONLY_SYSTEM_PROMPT = (
    'You are a voice-only assistant. The user cannot see any screen.\n\n'
    'Requirements:\n'
    '- Never reference visual elements ("tap here", "see the chart", "the blue button")\n'
    '- Never use numbered or bulleted lists — use spoken sequences instead:\n'
    '  BAD: "1. First do X 2. Then do Y"\n'
    '  GOOD: "Start by doing X. When that is done, do Y."\n'
    '- Limit responses to what can be comfortably spoken in 30 seconds\n'
    '- Offer to give more detail rather than overwhelming the user\n'
    '- Use verbal signposts: "First", "Next", "Finally"\n'
    '- Read out all important data: codes, dates, amounts as full words'
)
print(VOICE_ONLY_SYSTEM_PROMPT)

تنسيق النص المنطوق والمعروض على الشاشة

في السياقات متعددة الوسائط، يمكنك تقسيم المحتوى بين الصوت والشاشة. يتولى الصوت المحتوى الحواري والعاطفي والديناميكي، بينما تتولى الشاشة المعلومات الكثيفة والجداول والنصوص الطويلة.

MULTIMODAL_SYSTEM_PROMPT = (
    'You are a multimodal assistant with both a voice and a screen.\n\n'
    'When responding, consider what each modality does best:\n\n'
    'SPEAK (voice):\n'
    '- Conversational summary, emotional tone, key highlights\n'
    '- Guide the user to look at the screen when needed:\n'
    '  "I have shown the details on screen. The key number to notice is..."\n\n'
    'SHOW (screen):\n'
    '- Detailed data, tables, long lists, code, maps, images\n\n'
    'When your response includes structured data, respond in this format:\n'
    'SPOKEN: <what to say aloud>\n'
    'VISUAL: <what to display on screen in markdown>'
)

# Example LLM output for multimodal response:
EXAMPLE_MULTIMODAL_OUTPUT = (
    'SPOKEN: Your top three expenses this month are food, transport, and entertainment. '
    'Food was the biggest, almost double your budget. Check the screen for the full breakdown.\n\n'
    'VISUAL: | Category | Budget | Actual | Difference |\n'
    '|---|---|---|---|\n'
    '| Food | $400 | $780 | -$380 |\n'
    '| Transport | $150 | $162 | -$12 |\n'
    '| Entertainment | $100 | $145 | -$45 |'
)
print(EXAMPLE_MULTIMODAL_OUTPUT)

هيكلة مخرجات LLM لوكلاء الصوت

في تطبيقات وكلاء الصوت، وجّه LLM إلى إعادة مخرجات منظمة يمكن تحليلها وتوجيهها إلى الصوت أو الشاشة كلٌّ على حدة. ويصلح JSON أو تنسيق أقسام محدد لهذا الغرض.

import anthropic
import json

client = anthropic.Anthropic(api_key='sk-ant-...')

VOICE_AGENT_SYSTEM = (
    'You are a financial voice assistant. For each response, return JSON with:\n'
    '{\n'
    '  "spoken": "Short spoken response (max 2 sentences)",\n'
    '  "visual_title": "Header for the on-screen card (optional)",\n'
    '  "visual_content": "Detailed content for screen (markdown, optional)",\n'
    '  "action_label": "Button label if action needed (optional)",\n'
    '  "action_type": "one of: none, confirm, navigate, call"\n'
    '}\n'
    'Return only the JSON object.'
)

def voice_agent_query(user_message):
    r = client.messages.create(
        model='claude-opus-4-5',
        max_tokens=500,
        system=VOICE_AGENT_SYSTEM,
        messages=[{'role': 'user', 'content': user_message}]
    )
    try:
        response_data = json.loads(r.content[0].text)
        return response_data
    except json.JSONDecodeError:
        return {'spoken': r.content[0].text, 'visual_content': None}

result = voice_agent_query('What is my account balance?')
print('SPEAK:', result.get('spoken'))
print('SHOW:', result.get('visual_content', 'Nothing to display'))

تنسيق النصوص المفرغة لوكلاء الصوت

يجب تسجيل محادثات وكلاء الصوت في صورة نصوص مفرغة لأغراض تصحيح الأخطاء والامتثال ومراجعة الجودة. نسّق النصوص المفرغة لتسجيل هوية المتحدثين، والطوابع الزمنية، والمخرجات الصوتية والمرئية معًا.

import datetime
import json

class VoiceTranscript:
    def __init__(self, session_id):
        self.session_id = session_id
        self.turns = []

    def add_user_turn(self, text, audio_duration_ms=None):
        self.turns.append({
            'speaker': 'user',
            'timestamp': datetime.datetime.utcnow().isoformat(),
            'text': text,
            'audio_duration_ms': audio_duration_ms,
        })

    def add_agent_turn(self, spoken_text, visual_content=None, action=None):
        self.turns.append({
            'speaker': 'agent',
            'timestamp': datetime.datetime.utcnow().isoformat(),
            'spoken': spoken_text,
            'visual': visual_content,
            'action': action,
        })

    def save(self, filepath):
        with open(filepath, 'w') as f:
            json.dump({'session_id': self.session_id, 'turns': self.turns}, f, indent=2)
        print(f'Transcript saved: {filepath}')

# Usage
transcript = VoiceTranscript('session_001')
transcript.add_user_turn('What is my balance?', audio_duration_ms=1200)
transcript.add_agent_turn('Your balance is four hundred dollars.', visual_content='Balance: $400')
transcript.save('/tmp/session_001_transcript.json')

التعامل مع أخطاء الإدخال الصوتي

يجب أن يتعامل وكلاء الصوت بسلاسة مع أخطاء التعرّف على الكلام — مثل سوء فهم الكلمات، والعبارات غير المكتملة، وضوضاء الخلفية. وجّه LLM إلى اكتشاف الإدخال الغامض والتعافي منه.

import anthropic

client = anthropic.Anthropic(api_key='sk-ant-...')

AMBIGUITY_HANDLING_SYSTEM = (
    'You are a voice assistant. User input comes from speech recognition '
    'and may contain transcription errors.\n\n'
    'When input seems unclear or ambiguous:\n'
    '1. State what you think the user might have meant.\n'
    '2. Ask a single clarifying yes/no question to confirm.\n'
    '3. Never ask more than one question at a time.\n'
    '4. Offer the most likely interpretation as the default.\n\n'
    'Example:\n'
    'Input: "transfer five hundred to john or gene" (ambiguous name)\n'
    'Response: "It sounds like you want to transfer five hundred dollars. '
    'Did you mean John Smith or Gene Lee?"'
)

def handle_voice_input(user_speech):
    r = client.messages.create(
        model='claude-opus-4-5',
        max_tokens=200,
        system=AMBIGUITY_HANDLING_SYSTEM,
        messages=[{'role': 'user', 'content': user_speech}]
    )
    return r.content[0].text

print(handle_voice_input('pay the electric company bill thing'))

تناوب الأدوار في المحادثات الصوتية

على خلاف الدردشة النصية، يتطلب الصوت إدارة صريحة للأدوار. يجب أن يعرف الوكيل متى يتوقف عن الكلام ويستمع، كما يجب أن يعرف المستخدم متى ينتهي الوكيل من حديثه. صمّم موجّهات تنتج استجابات ذات إشارات ختامية طبيعية.

TURN_TAKING_SYSTEM = (
    'You are a voice assistant. Responses must be designed for spoken conversation:\n\n'
    'End each response with exactly ONE of:\n'
    '- A direct question inviting the user to respond\n'
    '- A clear statement that the task is complete (e.g., "That is done.")\n'
    '- An explicit offer to continue (e.g., "Is there anything else?")\n\n'
    'Never end mid-thought. Never trail off. '
    'Avoid open-ended statements that leave the user unsure if they should speak.\n\n'
    'GOOD endings:\n'
    '- "The transfer is complete. Would you like a confirmation number?"\n'
    '- "That is all I have. Is there anything else?"\n'
    'BAD endings:\n'
    '- "You might also want to consider..." (open, unclear)\n'
    '- "The balance is..." (incomplete)'
)
print(TURN_TAKING_SYSTEM[:300])

التعامل مع المقاطعات

يقاطع المستخدمون وكلاء الصوت. ويجب أن يكتشف النظام المقاطعات (عبر VAD — اكتشاف النشاط الصوتي)، وأن يوجّه الوكيل إلى استئناف حديثه بسلاسة أو إعادة توجيه الحوار. وجّه LLM إلى قبول تغييرات الموضوع في منتصف المحادثة.

import anthropic

client = anthropic.Anthropic(api_key='sk-ant-...')

INTERRUPTION_SYSTEM = (
    'You are a voice assistant. Users may interrupt mid-conversation.\n\n'
    'If the user changes topic abruptly, smoothly acknowledge the change:\n'
    '"Of course. Let us switch to that." Then answer the new question.\n\n'
    'If the user says something like "wait", "stop", "hold on":\n'
    'Pause and say "Sure, take your time" and wait for them to continue.\n\n'
    'If the user repeats a question, they likely did not hear the answer:\n'
    'Say "Let me repeat that." and say it again more slowly.\n\n'
    'Never express frustration at interruptions or repetition.'
)

def handle_conversation(turns):
    """Handle multi-turn voice conversation with interruptions."""
    messages = []
    for speaker, text in turns:
        messages.append({'role': speaker, 'content': text})

    r = client.messages.create(
        model='claude-opus-4-5',
        max_tokens=200,
        system=INTERRUPTION_SYSTEM,
        messages=messages
    )
    return r.content[0].text

# Simulate an interruption scenario
conversation = [
    ('user', 'What is my balance?'),
    ('assistant', 'Your checking account balance is four hundred dollars and—'),
    ('user', 'Actually wait, can you tell me my savings instead?'),
]
print(handle_conversation(conversation))

الشاشة المصاحبة للصوت

عند توفر شاشة، صمّم المحتوى المعروض عليها ليكمّل الصوت المنطوق لا أن يكرره. تتولى الشاشة التفاصيل، بينما يتولى الصوت التنقل والتفاعل العاطفي.

def render_multimodal_response(agent_output):
    """
    Render a voice agent response to both TTS and screen components.
    agent_output: dict with 'spoken', 'visual_content', 'action_label'
    """
    # Route to TTS
    spoken = agent_output.get('spoken', '')
    if spoken:
        send_to_tts(spoken)  # Your TTS function
        print(f'[AUDIO] {spoken}')

    # Route to screen
    visual = agent_output.get('visual_content')
    if visual:
        render_card_on_screen(visual)  # Your UI function
        print(f'[SCREEN] {visual[:100]}')

    # Optional action button
    action_label = agent_output.get('action_label')
    if action_label:
        show_action_button(action_label)  # Your UI function
        print(f'[BUTTON] {action_label}')

def send_to_tts(text):
    print(f'TTS: {text}')

def render_card_on_screen(content):
    print(f'Screen card: {content[:50]}')

def show_action_button(label):
    print(f'Button: {label}')

# Test it
render_multimodal_response({
    'spoken': 'I found three flights to New York.',
    'visual_content': '| Flight | Departs | Price |\n|---|---|---|\n| AA101 | 08:00 | $299 |',
    'action_label': 'Book cheapest'
})

اعتبارات إمكانية الوصول

يُعد الذكاء الاصطناعي الصوتي بحد ذاته ميزةً لإمكانية الوصول للمستخدمين ذوي الإعاقات البصرية أو الصعوبات الحركية. صمّم وكيلك ليدعم أيضًا المستخدمين الذين يعتمدون على الصوت بوصفه واجهتهم الأساسية.

ACCESSIBILITY_VOICE_SYSTEM = (
    'This voice assistant serves users who may be using voice as their '
    'primary access method due to disability or preference.\n\n'
    'Guidelines:\n'
    '- Never require the user to see a screen to complete a task.\n'
    '- Read out all information that matters, including confirmation codes, '
    'totals, and status messages.\n'
    '- Offer to repeat any information: '
    '"I can repeat that if you would like."\n'
    '- Describe any actions you took: '
    '"I have sent the confirmation to your email."\n'
    '- Accept multiple phrasings for the same command — users phrase '
    'voice commands inconsistently.\n'
    '- Confirm all destructive or financial actions before executing:\n'
    '  "Just to confirm: you want to transfer $500 to John. Is that right?"'
)
print(ACCESSIBILITY_VOICE_SYSTEM[:300])

اختبار استجابات وكيل الصوت

يتطلب اختبار استجابات وكيل الصوت نهجًا مختلفًا عن اختبار الاستجابات النصية. يجب تقييم الصوت المنطوق (التنغيم، والوضوح، والطبيعية) والمكوّن المرئي (الاكتمال، والتنسيق) معًا. فقد تبدو الاستجابة النصية جيدة عند قراءتها، لكنها قد تبدو متكلفة عند نطقها.

أنشئ خط أنابيب للاختبار يحوّل مخرجات الوكيل إلى صوت باستخدام محرك TTS، ثم يطبّق فحصًا آليًا للجودة يشمل طول الجمل، ونطق الاختصارات، وغياب بقايا تنسيق Markdown، وإشارات تناوب الأدوار.

اختبار المعرفة: قيد الصوت فقط

في سياق الصوت فقط (مثل مكبر صوت ذكي من دون شاشة)، ما نوع استجابة الوكيل الأنسب على الإطلاق؟

مراجعة: وكلاء الصوت والنص متعددو الوسائط

يعمل وكلاء الصوت في وضعين: الصوت فقط (من دون شاشة) ومتعدد الوسائط (الصوت مع الشاشة). يجب أن تتجنب الاستجابات الصوتية فقط الإشارات المرئية وأن تعمل بالكامل كصوت منطوق، مع استخدام إشارات لفظية واضحة. أما الاستجابات متعددة الوسائط فتقسّم المحتوى: يتولى الصوت الملخصات الحوارية والنبرة العاطفية، بينما تتولى الشاشة البيانات التفصيلية والنصوص الطويلة. وجّه نماذج LLM إلى إعادة مخرجات منظمة (JSON يضم حقولًا للمنطوق والمرئي) لتسهيل التوجيه. صمّم المحادثة لتدعم تناوب الأدوار عبر نهايات واضحة للاستجابات، والتعامل السلس مع المقاطعات، ودعم التكرار الصريح. وراعِ دائمًا إمكانية الوصول — فالصوت غالبًا ما يكون الواجهة الأساسية للمستخدمين الذين هم في أمسّ الحاجة إليه.

الأسئلة الشائعة

هل درس «الوكلاء متعددو الوسائط للصوت والنص» مجاني؟

نعم — نص درس «الوكلاء متعددو الوسائط للصوت والنص» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة AI Prompt Engineering، انتقل إلى CoddyKit PRO. تتضمن دورة AI Prompt Engineering 4 دروس في المجموع.

ماذا ستتعلم في «الوكلاء متعددو الوسائط للصوت والنص»؟

تنسيق الردود المنطوقة مع النص الظاهر على الشاشة في أنظمة الوكلاء الصوتيين تتمرن على AI Prompt Engineering مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ AI Prompt Engineering؟

لا تُشترط خبرة سابقة. AI Prompt Engineering على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «الوكلاء متعددو الوسائط للصوت والنص»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس AI Prompt Engineering هذا؟

نعم. كل درس في AI Prompt Engineering يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. أنماط مطالبات TTS للكلام الطبيعي
  2. التحكم في SSML والتنغيم
  3. تصميم شخصية الذكاء الاصطناعي الصوتية
  4. الوكلاء متعددو الوسائط للصوت والنص
← العودة إلى AI Prompt Engineering