0Pricing
AI Prompt Engineering · 课时

提示中的 Markdown 格式

了解如何指定标题、粗体和代码块等丰富格式

提示中的 Markdown 格式 是 CoddyKit 上的免费 AI Prompt Engineering 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Prompt Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Prompt Engineering 课程共包含 4 节课。

AI 输出中的 Markdown

Markdown 是一种轻量级文本格式语法,AI 模型原生理解这种语法。当您要求输出采用 Markdown 格式时,模型会生成能够在兼容环境中呈现为丰富格式的文本。

准确了解如何请求每种 Markdown 元素,可以让您精确控制每份 AI 生成文档的结构。

请求标题

Markdown 标题使用井号:# 表示 H1,## 表示 H2,### 表示 H3。

请明确提出请求:“使用 H2 作为各节标题”、“使用 ## 表示主要部分,使用 ### 表示子部分”,或“在顶部包含一个 # H1 标题”。

标题可以在 Notion、GitHub、Obsidian 以及大多数文档工具中创建可导航的结构。

import anthropic

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

response = client.messages.create(
    model='claude-opus-4-5',
    max_tokens=400,
    messages=[{
        'role': 'user',
        'content': (
            'Write a technical guide outline for "Getting Started with FastAPI". '
            'Structure: one # H1 title at the top, then 4 ## H2 section headers, '
            'each with 2 ### H3 subsection headers beneath it. '
            'Add one sentence of placeholder content under each H3.'
        )
    }]
)
print(response.content[0].text)

粗体和斜体强调

Markdown 中的粗体和斜体强调:

  • **bold text** → 粗体文本
  • *italic text* → 斜体文本
  • ***bold and italic*** → 粗体和斜体

请求示例:“首次出现时将所有关键术语设为粗体”、“使用斜体表示产品名称”或“将每个步骤中的行动项目设为粗体”。

import openai

client = openai.OpenAI(api_key='sk-your-key-here')

response = client.chat.completions.create(
    model='gpt-4o',
    messages=[{
        'role': 'user',
        'content': (
            'Explain the concept of idempotency in REST APIs. '
            'Rules:\n'
            '- Bold every technical term on its first occurrence only\n'
            '- Italicize all HTTP method names (GET, POST, PUT, DELETE, PATCH)\n'
            '- 150 words max, flowing prose — no bullets or headers'
        )
    }]
)
print(response.choices[0].message.content)

代码块

Markdown 中的代码块使用三个反引号,后面可以选择性添加语言提示以启用语法高亮:

```python
print('hello')
```

请求示例:“将所有代码放入 Python 代码块中”、“将每条命令放入 Bash 代码块中”或“在 JSON 代码块中展示该 JSON 示例”。

语言提示可以在 GitHub、VS 代码编辑器和文档网站中启用语法高亮。

import anthropic

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

response = client.messages.create(
    model='claude-opus-4-5',
    max_tokens=400,
    messages=[{
        'role': 'user',
        'content': (
            'Show me how to connect to PostgreSQL from Python using psycopg3.\n'
            'Structure:\n'
            '1. Install command in a bash code block.\n'
            '2. Connection example in a python code block with type hints.\n'
            '3. A sample SELECT query in a python code block.\n'
            'Keep each code block under 10 lines. Brief one-sentence intro before each block.'
        )
    }]
)
print(response.content[0].text)

行内代码

行内代码使用一对反引号:`variable_name`。它会在句子中以等宽文本呈现,非常适合用于:

  • 变量名称:user_id
  • 函数名称:calculate_tax()
  • 命令名称:git commit
  • 文件路径:/etc/nginx/nginx.conf
  • HTTP 端点:/api/v1/users

请求:“为所有变量和函数名称使用行内代码格式。”

import openai

client = openai.OpenAI(api_key='sk-your-key-here')

response = client.chat.completions.create(
    model='gpt-4o',
    messages=[{
        'role': 'user',
        'content': (
            'Explain the difference between Python list .append() and .extend(). '
            'Rules:\n'
            '- Use inline code for all method names, parameter names, and variable examples\n'
            '- Use a python code block for each demonstration example\n'
            '- Prose sections: max 2 sentences\n'
            '- Do NOT use headers or bullets — flowing prose with code blocks only'
        )
    }]
)
print(response.choices[0].message.content)

引用块

引用块在行首使用 >。在标记语法中:

> This is a blockquote.

适用场景包括:提示框、重要说明、示例对话、引用的源材料和警告。

请求:“将最重要的警告放入引用块中”,或“为示例场景使用引用块。”

import anthropic

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

response = client.messages.create(
    model='claude-opus-4-5',
    max_tokens=300,
    messages=[{
        'role': 'user',
        'content': (
            'Write a security guide section about SQL injection prevention. '
            'Structure:\n'
            '- 2-sentence explanation of the risk\n'
            '- One blockquote containing a real example of vulnerable code (as a note/warning)\n'
            '- 3 bullet points on how to prevent it\n'
            '- One blockquote containing the safe alternative code pattern'
        )
    }]
)
print(response.content[0].text)

标记语法中的嵌套列表

嵌套的标记语法列表(lists)通过缩进(两个或四个空格)来创建层级关系:

- Main item
  - Sub-item
  - Sub-item
    - Sub-sub-item

请求:“创建一个两级嵌套列表,其中包含 X 个主项目,每个主项目包含 Y 个子项目”,或“使用嵌套项目符号展示类别与示例之间的关系。”

import openai

client = openai.OpenAI(api_key='sk-your-key-here')

response = client.chat.completions.create(
    model='gpt-4o',
    messages=[{
        'role': 'user',
        'content': (
            'Create a 2-level nested markdown list of AWS services for a web startup. '
            'Level 1: 4 service categories (Compute, Storage, Database, Networking). '
            'Level 2: 3 specific services under each category with a 5-word description. '
            'Format: markdown nested bullets with proper indentation.'
        )
    }]
)
print(response.choices[0].message.content)

链接与图像

标记语法链接:[link text](URL)
标记语法图像:![alt text](image-URL)

人工智能模型可以生成带有明确含义文本的占位链接:“加入指向相关文档的标记语法链接——使用类似 [官方文档](https://example.com) 的占位网址。”

对于包含图表占位内容的文档:“加入带有明确含义替代文本的图像占位内容。”

import anthropic

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

response = client.messages.create(
    model='claude-opus-4-5',
    max_tokens=300,
    messages=[{
        'role': 'user',
        'content': (
            'Write a README section for a Python open-source project called "sqlens". '
            'Include:\n'
            '- An image placeholder for a demo screenshot: ![Demo screenshot](docs/demo.png)\n'
            '- At least 2 markdown links: one to the PyPI page, one to the documentation\n'
            '- A badge placeholder using an image link\n'
            '- 3 bullet points of key features\n'
            'Use realistic placeholder URLs (pypi.org/project/sqlens etc).'
        )
    }]
)
print(response.content[0].text)

水平线与分隔线

水平线可以使用三个连字符(---)、星号(***)或下划线(___)。

使用它们可以在视觉上分隔文档的主要部分。请求:“在每个主要部分之间加入一条 --- 水平线”,或“使用标记语法分隔线分隔这三个部分。”

水平线可以在大多数标记语法环境中呈现,并帮助读者浏览较长的文档。

import openai

client = openai.OpenAI(api_key='sk-your-key-here')

response = client.chat.completions.create(
    model='gpt-4o',
    messages=[{
        'role': 'user',
        'content': (
            'Write a mini technical specification document for a user authentication API. '
            'Include exactly 3 sections: Overview, Endpoints, Security Requirements. '
            'Separate each section with a --- horizontal rule. '
            'Each section: ## H2 header + 3-5 bullet points of content. '
            'Under Endpoints: use inline code for all route paths and HTTP methods.'
        )
    }]
)
print(response.choices[0].message.content)

标记语法无法呈现的情况

只有在输出环境能够呈现标记语法时,标记语法才有帮助。标记语法在以下环境中不会呈现(NOT):

  • 纯文本电子邮件客户端(会显示原始星号)
  • SMS 消息
  • 大多数 CRM 备注字段
  • 语音输出(文本转语音)
  • 要求使用纯文本的旧版系统

对于这些场景,请明确请求纯文本输出。下一课将介绍这一点。

import anthropic

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

# Check if environment renders markdown before requesting it
rendering_environments = {
    'GitHub':     True,
    'Notion':     True,
    'Obsidian':   True,
    'VS Code':    True,
    'Gmail body': False,  # some markdown, not all
    'Outlook':    False,
    'SMS':        False,
    'Plain text file': False,
}

print('Markdown rendering support:')
for env, renders in rendering_environments.items():
    status = 'RENDERS' if renders else 'DOES NOT RENDER'
    print(f'  {env:<20} {status}')

# Decision: use markdown only when you know it renders
use_markdown = True  # set based on your environment

format_instruction = (
    'Use markdown headers, bold, and code blocks.' if use_markdown
    else 'Plain text only — no markdown symbols.'
)
print('\nFormat instruction:', format_instruction)

组合使用标记语法元素

生产级人工智能生成文档会组合使用多种标记语法元素。一份结构良好的技术文档可能会使用:

  • # H1 标题加上 ## H2 部分
  • 首次出现的关键术语使用 **bold**
  • 所有代码都使用带语言提示的代码块
  • 所有变量和函数名称都使用行内代码
  • 要求使用项目符号列表;步骤使用编号列表
  • 警告和重要说明使用引用块
  • 主要部分之间使用 --- 分隔线
import openai

client = openai.OpenAI(api_key='sk-your-key-here')

response = client.chat.completions.create(
    model='gpt-4o',
    messages=[{
        'role': 'user',
        'content': (
            'Write a mini developer guide for the requests Python library. '
            'Use all of the following markdown elements:\n'
            '- # H1 title at the top\n'
            '- ## H2 sections: Installation, Basic Usage, Error Handling\n'
            '- Bold all key terms on first use\n'
            '- Code blocks with python/bash language hints\n'
            '- Inline code for all function names\n'
            '- One blockquote warning about timeout best practice\n'
            '- --- between each section\n'
            'Max 300 words total.'
        )
    }]
)
print(response.choices[0].message.content)

知识检查

一名开发人员正在构建一个人工智能助手,该助手使用 print() 将内容输出到终端中——没有网页界面,也没有标记语法渲染器。他们要求人工智能解释某项功能,却 get 到了充满星号和井号的输出。要解决这个问题,他们应在系统消息中添加什么内容?

提示中的标记语法——回顾

标记语法格式可以为人工智能生成的文档提供专业的结构。需要请求的关键元素包括:

  • 标题:# 一级标题、## 二级标题、### 三级标题——用于构建便于浏览的文档结构
  • 强调:关键术语使用 **粗体**,特殊名称使用 *斜体*
  • 代码块:使用带语言提示的三个反引号,以实现语法高亮
  • 行内代码:使用单个反引号表示变量名称、命令和路径
  • 引用块:使用 > 前缀表示警告、提示框和引用内容
  • 嵌套列表:使用缩进的项目符号表示层级信息

只有在确定输出环境能够呈现标记语法时,才使用标记语法。

常见问题解答

「提示中的 Markdown 格式」课时是免费的吗?

是的 — 「提示中的 Markdown 格式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Prompt Engineering 课程的其余内容,请升级到 CoddyKit PRO。 AI Prompt Engineering 课程共包含 4 节课。

「提示中的 Markdown 格式」这节课中我会学到什么?

了解如何指定标题、粗体和代码块等丰富格式 你通过在浏览器中直接运行的动手代码来练习 AI Prompt Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 AI Prompt Engineering 需要有经验吗?

无需任何先前经验。CoddyKit 上的 AI Prompt Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「提示中的 Markdown 格式」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 AI Prompt Engineering 课中编写并运行代码吗?

能。每节 AI Prompt Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 请求列表与项目符号
  2. 请求表格与结构化数据
  3. 提示中的 Markdown 格式
  4. 纯文本与格式化输出
← 返回 AI Prompt Engineering