0Pricing
AI Prompt Engineering · บทเรียน

ตัวแทนเสียงและข้อความแบบหลายรูปแบบ

ประสานคำตอบที่พูดออกมากับข้อความบนหน้าจอในระบบตัวแทนเสียง

ตัวแทนเสียงและข้อความแบบหลายรูปแบบ เป็นบทเรียน AI Prompt Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 ส่งคืนผลลัพธ์ที่มีโครงสร้าง ซึ่งคุณสามารถแยกวิเคราะห์และส่งต่อไปยังเสียงหรือหน้าจอแยกกันได้ รูปแบบเจสันหรือรูปแบบส่วนที่กำหนดไว้ใช้ได้ดี

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 ของคุณ จากนั้นตรวจสอบคุณภาพโดยอัตโนมัติ ได้แก่ ความยาวประโยค การออกเสียงคำย่อ การไม่มีส่วนเกินจากมาร์กดาวน์ และสัญญาณการผลัดกันพูด

ตรวจสอบความรู้: ข้อจำกัดของบริบทเสียงเท่านั้น

ในบริบทเสียงเท่านั้น (ลำโพงอัจฉริยะที่ไม่มีหน้าจอ) คำตอบประเภทใดของเอเจนต์จึงเหมาะสมที่สุด (MOST)

สรุป: เอเจนต์เสียงและข้อความหลายรูปแบบ

เอเจนต์เสียงทำงานได้สองโหมด ได้แก่ เสียงเท่านั้น (ไม่มีหน้าจอ) และหลายรูปแบบ (เสียง + หน้าจอ) คำตอบแบบเสียงเท่านั้นต้องหลีกเลี่ยงการอ้างอิงภาพและใช้งานได้ทั้งหมดในรูปเสียงพูด พร้อมใช้คำบอกลำดับด้วยวาจา คำตอบแบบหลายรูปแบบแบ่งเนื้อหาออกเป็นสองส่วน ได้แก่ เสียงสำหรับสรุปเชิงสนทนาและน้ำเสียงทางอารมณ์ และหน้าจอสำหรับข้อมูลโดยละเอียดกับข้อความขนาดยาว กำหนดพรอมต์ให้ LLM ส่งคืนผลลัพธ์ที่มีโครงสร้าง (เจสันพร้อมเขตข้อมูลเสียงพูด/ภาพ) เพื่อให้ส่งต่อได้ง่าย ออกแบบการผลัดกันพูดด้วยการจบคำตอบที่ชัดเจน การจัดการการขัดจังหวะอย่างราบรื่น และการรองรับการพูดซ้ำอย่างชัดเจน คำนึงถึงการเข้าถึงเสมอ — เสียงมักเป็นส่วนติดต่อหลักสำหรับผู้ใช้ที่ต้องพึ่งพาเสียงมากที่สุด

คำถามที่พบบ่อย

บทเรียน “ตัวแทนเสียงและข้อความแบบหลายรูปแบบ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ตัวแทนเสียงและข้อความแบบหลายรูปแบบ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Prompt Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Prompt Engineering มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ตัวแทนเสียงและข้อความแบบหลายรูปแบบ”

ประสานคำตอบที่พูดออกมากับข้อความบนหน้าจอในระบบตัวแทนเสียง คุณปฏิบัติ AI Prompt Engineering ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Prompt Engineering หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Prompt Engineering บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ตัวแทนเสียงและข้อความแบบหลายรูปแบบ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน AI Prompt Engineering นี้ได้ไหม

ได้ บทเรียน AI Prompt Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. รูปแบบพรอมต์ TTS สำหรับเสียงพูดที่เป็นธรรมชาติ
  2. การควบคุม SSML และท่วงทำนองเสียง
  3. การออกแบบบุคลิกของปัญญาประดิษฐ์เสียงพูด
  4. ตัวแทนเสียงและข้อความแบบหลายรูปแบบ
← กลับไปที่ AI Prompt Engineering