AI Prompt Engineering · บทเรียน

รูปแบบพรอมต์ TTS สำหรับเสียงพูดที่เป็นธรรมชาติ

โครงสร้างประโยค เครื่องหมายวรรคตอน และสัญญาณกำหนดจังหวะที่ช่วยปรับปรุงข้อมูลส่งออกของ TTS

บทเรียน 1 จาก 413 ขั้นตอน

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

เหตุใดการเขียนพรอมต์สำหรับ TTS จึงแตกต่างกัน

ระบบแปลงข้อความเป็นเสียงจะแปลงข้อความของคุณเป็นเสียงตามตัวอักษร ต่างจากข้อความที่มองเห็นซึ่งผู้อ่านสามารถย้อนกลับไปอ่านส่วนที่สับสนได้ ผู้ฟังจะรับเสียงตามลำดับและไม่สามารถหยุดเพื่อถอดความประโยคที่กำกวมได้

การเขียนสำหรับ TTS หมายถึงการคำนึงถึงวิธีที่คำ ฟังดู ทั้งจังหวะ ความยาวประโยค ความกำกวมในการออกเสียง และการไม่มีรูปแบบที่มองเห็นได้ เช่น ตัวหนาหรือรายการหัวข้อย่อย

ประโยคสั้นย่อมดีกว่า

ประโยคยาวที่มีโครงสร้าง complex และมีอนุประโยคหลายส่วนทำให้ติดตามเนื้อหาในเสียงได้ยาก ระบบ TTS มักตัดจังหวะในจุดที่ถูกต้องตามไวยากรณ์แต่ฟังดูไม่เป็นธรรมชาติ ควรตั้งเป้าให้ประโยคมีความยาว 10-20 คำเพื่อให้การพูดฟังเป็นธรรมชาติ

# Prompt a model to generate TTS-optimized text
TTS_SYSTEM_PROMPT = (
    'You are writing text that will be read aloud by a text-to-speech system.\n\n'
    'Rules:\n'
    '- Use short, clear sentences (10-20 words each).\n'
    '- Avoid complex nested clauses.\n'
    '- End each sentence with a period for clear pause signals.\n'
    '- Avoid parenthetical asides in the middle of sentences.\n'
    '- Use conversational vocabulary — write as you would speak.\n'
    '- Never use bullet points, headers, or markdown formatting.'
)

# Bad (long, nested):
BAD = (
    'The transformer architecture, which was introduced in the landmark 2017 paper '
    '"Attention is All You Need" by Vaswani et al. at Google, fundamentally changed '
    'natural language processing by replacing recurrence with self-attention.'
)

# Good (TTS-friendly):
GOOD = (
    'The transformer architecture changed natural language processing. '
    'It was introduced in 2017 by researchers at Google. '
    'Their key innovation was replacing recurrence with self-attention.'
)

เครื่องหมายวรรคตอนเป็นสัญญาณเสียง

โปรแกรม TTS ใช้เครื่องหมายวรรคตอนเพื่อควบคุมจังหวะ:

  • จุด (.): จบประโยคและหยุดนานขึ้น
  • จุลภาค (,): หยุดชั่วครู่
  • เครื่องหมายคำถาม (?): ระดับเสียงสูงขึ้น
  • เครื่องหมายอัศเจรีย์ (!): การเน้นและพลังเสียง
  • อัฒภาค (;): การทำงานแตกต่างกันตามระบบ มักถูกละเลย
  • ขีดคั่นยาว (—): มักทำให้เกิดการหยุดที่ฟังไม่เป็นธรรมชาติ ควรหลีกเลี่ยง

ใช้จุดอย่างชัดเจนแทนขีดคั่นยาวหรืออัฒภาค เพื่อควบคุมจังหวะได้อย่างสม่ำเสมอ

TTS_PUNCTUATION_PROMPT = (
    'Write the following content for text-to-speech narration. '
    'Use only periods, commas, and question marks for punctuation. '
    'Avoid em dashes, semicolons, colons, and parentheses. '
    'Break complex thoughts into multiple short sentences.\n\n'
    'Content to rewrite: {content}'
)

# Example transformation
original = (
    'There are three main factors to consider: cost, quality, and speed — '
    'and often, you can only optimize for two of them (this is sometimes '
    'called the project management triangle).'
)

for_tts = (
    'There are three main factors to consider. The first is cost. '
    'The second is quality. The third is speed. '
    'Usually, you can only optimize for two of these at once. '
    'This is sometimes called the project management triangle.'
)
print(for_tts)

หลีกเลี่ยงคำย่อที่กำกวม

คำย่อที่ผู้อ่านถอดความจากภาพได้ อาจกลายเป็นกับดักที่ทำให้ TTS ออกเสียงผิด ระบบ TTS แต่ละระบบจัดการกับคำย่อไม่เหมือนกัน

  • Dr. → อาจอ่านว่า ดอกเตอร์ หรือ ไดรฟ์
  • St. → เซนต์ หรือ ถนน
  • vs. → ตรงข้ามกับ หรือ วีเอส
  • etc. → และอื่น ๆ หรือ เอ็ตซี

เขียนคำย่อเป็นคำเต็มในข้อความ TTS เพื่อให้แน่ใจว่าออกเสียงได้ถูกต้อง

TTS_ABBREVIATION_RULES = (
    'When writing for text-to-speech:\n'
    '- Write "Doctor" not "Dr."\n'
    '- Write "Street" or "Saint" not "St."\n'
    '- Write "versus" not "vs."\n'
    '- Write "et cetera" not "etc."\n'
    '- Write "for example" not "e.g."\n'
    '- Write "that is" not "i.e."\n'
    '- Write out years: "twenty twenty-five" not "2025" when narrated in prose\n'
    '- Write out numbers under 10: "three" not "3" in conversational text\n'
    '- Spell out acronyms on first use: '
    '"Application Programming Interface, or API"\n'
)
print(TTS_ABBREVIATION_RULES)

คำที่ทำให้การออกเสียงผิด

คำหรือรูปแบบบางอย่างทำให้ระบบ TTS สับสนซ้ำ ๆ โปรดทำความรู้จักและใช้ทางเลือกอื่น:

  • คำพ้องรูป: read (ปัจจุบันเทียบกับอดีต), ลีด (โลหะเทียบกับนำทาง), วินด์ ระบบ TTS จะเลือกการออกเสียงแบบใดแบบหนึ่ง
  • ชื่อเฉพาะที่ไม่คุ้น: คำศัพท์เทคนิค ชื่อแบรนด์ และคำต่างประเทศ
  • ตัวเลขในบริบทที่ไม่ทั่วไป: รุ่นของ API รูปแบบวันที่ และหน่วยวัด
# Prompting LLM to detect and fix TTS pronunciation issues
TTS_REVIEW_PROMPT = (
    'Review the following text for text-to-speech pronunciation issues.\n'
    'Identify words that might be mispronounced by a TTS engine because they:\n'
    '1. Are homographs with multiple pronunciations\n'
    '2. Are technical terms, brand names, or foreign words\n'
    '3. Are abbreviations or acronyms\n'
    '4. Are numbers in unusual formats\n\n'
    'For each issue, provide the original text and a TTS-safe replacement.\n\n'
    'Text to review: {text}'
)

# Example issues and fixes
ISSUES = {
    'She read the docs': 'She read (past tense - ambiguous) -> She finished reading the docs',
    'Lead developer': 'Lead (metal or guide?) -> Lead (rhymes with feed) developer',
    'API v2.3.1': 'v2.3.1 -> version 2 point 3 point 1',
    'The .env file': 'dot E N V file (may say .env) -> the environment config file',
    'Re-init': 'may say Ray-in-it -> reinitialize',
}
for problem, fix in ISSUES.items():
    print(f'Issue: {problem}\nFix: {fix}\n')

ตัวเลขและวันที่สำหรับ TTS

ตัวเลขเป็นสาเหตุสำคัญที่ทำให้เสียงจาก TTS ฟังไม่เป็นธรรมชาติ ควรเขียนตัวเลขในรูปแบบที่ไม่ทำให้เกิดความกำกวมเกี่ยวกับวิธีอ่าน

NUMBER_TTS_RULES = (
    'When writing numbers for TTS narration:\n'
    '- Dates: write "January fifteenth, 2025" not "01/15/2025"\n'
    '- Time: write "3 in the afternoon" not "15:00" in casual speech\n'
    '- Percentages: write "forty-five percent" not "45%" in narration\n'
    '- Large numbers: write "one point two million" not "1.2M"\n'
    '- Phone numbers: spell with hyphens "555-123-4567" (TTS reads each digit)\n'
    '- Currency: write "twelve dollars and fifty cents" not "$12.50" in speech\n'
    '- Fractions: write "three quarters" not "3/4"\n'
    '- Ordinals: write "first" not "1st" (TTS may say "one S T")\n'
)

# Apply when prompting the LLM to generate TTS scripts
TTS_SCRIPT_PROMPT = (
    'Write a 30-second product announcement for text-to-speech.\n'
    'Product: TaskFlow Pro, $49/month, 10,000 users worldwide, '
    'launched January 2025.\n\n'
    + NUMBER_TTS_RULES
)

นำการจัดรูปแบบที่มองเห็นออก

การจัดรูปแบบมาร์กดาวน์และ HTML ไม่ส่งเสียงเมื่อมองเห็น แต่ระบบ TTS บางระบบจะอ่านออกเสียง เครื่องหมายดอกจัน เครื่องหมายแฮช และวงเล็บแหลมต้องถูกนำออกหรือแทนที่ด้วยรูปแบบที่อ่านออกเสียงได้

TTS_FORMAT_CONVERSION_PROMPT = (
    'Convert the following markdown text into plain prose suitable '
    'for text-to-speech narration.\n\n'
    'Rules:\n'
    '- Remove all markdown formatting (**, *, #, -, etc.)\n'
    '- Convert bullet lists into spoken sequences '
    '("First... Second... Third...")\n'
    '- Convert headers into topic introduction sentences\n'
    '- Remove any hyperlinks or URLs\n'
    '- Convert code snippets into descriptions '
    '("a Python function that...")\n\n'
    'Markdown text:\n'
    '{markdown_text}'
)

EXAMPLE_MARKDOWN = (
    '## Getting Started\n'
    '- Install the package with 'pip install mylib'\n'
    '- Set your **API key** in '.env'\n'
    '- Run 'python main.py' to start\n'
)

EXAMPLE_TTS = (
    'To get started, install the package using pip. '
    'Next, set your API key in your environment configuration file. '
    'Finally, run the main Python script to start.'
)
print(EXAMPLE_TTS)

ควบคุมจังหวะด้วยวลีเชื่อมโยง

ในข้อความที่เขียน โครงสร้างที่มองเห็นได้ เช่น ย่อหน้าและหัวข้อ จะช่วยนำทางผู้อ่าน ในเสียง TTS คุณต้องใช้วลีเชื่อมโยงด้วยคำพูดเพื่อช่วยให้ผู้ฟังติดตามโครงสร้างได้

TRANSITION_PHRASES = {
    'starting_new_topic':    ['Let us now talk about', 'Moving on to', 'Next,'],
    'adding_information':    ['Additionally,', 'Also worth noting,', 'Furthermore,'],
    'contrasting':           ['However,', 'On the other hand,', 'That said,'],
    'concluding':            ['To summarize,', 'In short,', 'To wrap up,'],
    'sequencing':            ['First,', 'Second,', 'Then,', 'Finally,'],
    'emphasizing':           ['Importantly,', 'Keep in mind that', 'Note that'],
}

TTS_STRUCTURE_PROMPT = (
    'Write a two-minute explainer on {topic} for text-to-speech narration.\n'
    'Use verbal transition phrases to guide listeners through each point. '
    'Avoid headers or bullet points. '
    'Structure the content with clear spoken signposts '
    '("First...", "Moving on...", "To summarize...").'
)

print('Available transitions:')
for category, phrases in TRANSITION_PHRASES.items():
    print(f'  {category}: {phrases[0]}')

การทำ test ข้อความ TTS

test เดียวที่เชื่อถือได้สำหรับคุณภาพ TTS คือการฟัง สร้างวงจร test และฟัง: สร้างข้อความ → ส่งไปยัง API ของ TTS → ฟัง → ทำซ้ำ อย่าพึ่งพาการอ่านข้อความด้วยสายตา

import openai
import pathlib

client = openai.OpenAI(api_key='sk-...')

def generate_and_test_tts(text, output_file='test_audio.mp3'):
    """Generate TTS audio from text for auditory review."""
    response = client.audio.speech.create(
        model='tts-1-hd',
        voice='alloy',   # alloy, echo, fable, onyx, nova, shimmer
        input=text,
        speed=1.0        # 0.25 to 4.0
    )

    audio_path = pathlib.Path(output_file)
    response.stream_to_file(audio_path)
    print(f'Audio saved to {audio_path}')
    print(f'Text length: {len(text)} chars, {len(text.split())} words')
    return audio_path

# Generate and listen before shipping
tts_text = (
    'Welcome to our weekly update. '
    'This week, the engineering team shipped three major features. '
    'First, we improved search speed by forty percent. '
    'Second, we added support for twelve new languages. '
    'Third, we launched our new mobile application.'
)
path = generate_and_test_tts(tts_text)

การเลือกเสียงและการจับคู่พรอมต์

เสียง TTS แต่ละแบบมีจุดแข็งแตกต่างกัน จับคู่เสียงให้เหมาะกับโทนของเนื้อหา:

  • อบอุ่น/เชิงบรรยาย: การเล่าเรื่องและเนื้อหาเพื่อการศึกษา
  • เป็นมืออาชีพ/เป็นกลาง: รายงานธุรกิจและเอกสารทางเทคนิค
  • กระฉับกระเฉง/สดใส: การตลาด การสาธิตผลิตภัณฑ์ และการแจ้งเตือน

กำหนดพรอมต์ให้ LLM เขียนเนื้อหาที่ตรงกับระดับภาษาตามธรรมชาติของเสียง

VOICE_SPECIFIC_PROMPTS = {
    'professional': (
        'Write in a clear, neutral, professional tone. '
        'Use complete sentences. Avoid contractions. '
        'Suitable for business reports and documentation.'
    ),
    'warm_narrative': (
        'Write in a warm, engaging, conversational tone. '
        'Use contractions naturally. '
        'Speak directly to the listener using "you" and "we". '
        'Suitable for educational content and storytelling.'
    ),
    'energetic': (
        'Write in an upbeat, enthusiastic tone. '
        'Use shorter sentences for impact. '
        'Include emphasis words like "amazing", "incredible", "now". '
        'Suitable for marketing and product announcements.'
    ),
}

# Select based on use case
use_case = 'warm_narrative'
print(VOICE_SPECIFIC_PROMPTS[use_case])

การออกแบบกระบวนการจาก LLM ไปยัง TTS

ในกระบวนการใช้งานจริง LLM จะสร้างข้อความ แล้วส่งต่อไปยังระบบ TTS ทั้งสองส่วนต้องทำงานสอดคล้องกัน LLM ต้องรู้ว่ากำลังสร้างข้อความสำหรับ TTS ไม่ใช่สำหรับการอ่าน และพรอมต์ระบบหรือการประมวลผลภายหลังต้องบังคับใช้กฎที่เหมาะกับ TTS ก่อนที่ข้อความจะไปถึง API ของ TTS

เพิ่มขั้นตอนการประมวลผลภายหลังแบบเบาระหว่างเอาต์พุตของ LLM กับอินพุตของ TTS: ใช้ strip กับมาร์กดาวน์ที่หลุดเข้ามา ขยายคำย่อ และตรวจสอบความยาวประโยค วิธีนี้จะดักจับข้อผิดพลาดด้านการจัดรูปแบบของ LLM ก่อนที่จะกลายเป็นความผิดเพี้ยนของเสียง

ตรวจสอบความรู้: โครงสร้างประโยคสำหรับ TTS

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

สรุป: รูปแบบพรอมต์ TTS สำหรับเสียงพูดที่เป็นธรรมชาติ

ข้อความ TTS ต้องเขียนสำหรับการฟัง ไม่ใช่สำหรับการมอง กฎสำคัญคือ จำกัดความยาวประโยคไว้ที่ 10-20 คำ ใช้เฉพาะจุดและจุลภาคเพื่อควบคุมจังหวะ เขียนคำย่อและตัวเลขทั้งหมดเป็นคำเต็ม นำมาร์กดาวน์และการจัดรูปแบบที่มองเห็นได้ออกทั้งหมด และใช้วลีเชื่อมโยงด้วยคำพูดแทนโครงสร้างที่มองเห็น คำพ้องรูป คำศัพท์เทคนิค และรูปแบบตัวเลขที่ไม่ทั่วไปอาจทำให้เกิดการออกเสียงผิด ให้ตรวจหาและแทนที่คำเหล่านี้เสมอ ทดสอบด้วยการฟังเสียงที่สร้างขึ้น ไม่ใช่การอ่านข้อความ

เริ่มต้นได้ฟรี

เรียนรู้ AI Prompt Engineering ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
53
บทเรียน
199

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

บทเรียน “รูปแบบพรอมต์ TTS สำหรับเสียงพูดที่เป็นธรรมชาติ” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “รูปแบบพรอมต์ TTS สำหรับเสียงพูดที่เป็นธรรมชาติ”

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

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

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

บทเรียน “รูปแบบพรอมต์ TTS สำหรับเสียงพูดที่เป็นธรรมชาติ” ใช้เวลานานแค่ไหน

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

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

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

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

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