
当我们与 LLM 交互时,我们通常使用自然语言,输入一段话并希望模型能够理解我们的意图。这种方法一直有效,直到失败,原因是指令不明确、缺少上下文或格式问题甚至使功能强大的系统也感到困惑。JSON 提示词是一种新兴的技术,它使用结构化数据而不是自由格式的文本。通过将指令、示例和约束组织到 JSON 对象中,我们牺牲了一些对话的温暖来提高精度。结果是提示既易于人类阅读,又易于通过代码解析。本文解释了为什么这很重要,JSON 提示词与标准提示词有何不同,并提供了有效编写它们的分步指南。
JSON提示词与普通提示词
| 指标 | 普通文本提示词 (Normal Text Prompts) | JSON提示词 (JSON Prompts) |
|---|---|---|
| 交互方式 | 像与朋友聊天。你写句子,并希望AI理解你的意思。 | 像给计算机明确指示。你使用结构化格式。 |
| 指令的位置 | 指令混在句子中,AI需要猜测你的意图。 | 指令明确标记,例如 { “task”: “summarize”, “format”: “list” }。 |
| 词语使用 | 重复使用诸如“please do this”这样的短语会占用更多字数。 | 简短的标签和值节省空间,保持高效率。 |
| 一致性 | 小的字词改动可能导致不同的结果,难以预测。 | 结构化格式确保每次响应相同,如同一份配方一样。 |
| 测试的便捷性 | 难以检查AI是否理解,因为你正在测试模糊的文本。 | 使用检查结构的工具(如检查清单)进行测试更为简单。 |
| 处理复杂任务 | 长或详细的任务在书写和阅读时会显得混乱。 | 组织良好的结构使复杂任务更易于管理和理解。 |
格式
JSON 提示词是一个结构化的数据对象,其中包含键值对,明确定义了任务、约束条件和所需的输出格式。其一般结构如下:
{
"task": "The main thing you want the AI to do",
"input": "The data or text the AI should work with",
"format": "How you want the AI's response to look",
"constraints": "Any rules or limits for the response",
"examples": [
{
"input": "Sample input for the AI",
"output": "Sample output you expect"
}
]
}
尝试一些JSON提示词
任务 1:图像生成
常规提示词:“Generate an animated image of a cat punching a dinosaur.”
JSON提示词:
{
"task": "Generate an animated image",
"description": {
"scene": "A cartoon cat punching a dinosaur in a playful fight",
"characters": {
"cat": {
"appearance": "Fluffy orange tabby cat with a mischievous grin",
"action": "Throwing a punch with its front paw"
},
"dinosaur": {
"appearance": "Green T-Rex with a surprised expression",
"action": "Reacting to the punch, stumbling backward"
}
},
"background": "A colorful jungle with tall trees and vines",
"style": "Cartoonish, vibrant colors, suitable for all ages"
},
"animation_details": {
"duration": "3 seconds",
"frames": [
{
"frame": 1,
"description": "Cat winds up its paw, preparing to punch, with a cheeky smile"
},
{
"frame": 2,
"description": "Cat’s paw makes contact with the T-Rex’s face, T-Rex looks surprised"
},
{
"frame": 3,
"description": "T-Rex stumbles back comically, cat stands proudly"
}
],
"loop": true
},
"constraints": {
"resolution": "512x512 pixels",
"tone": "Playful and humorous, non-violent",
"colors": "Bright and vibrant"
}
}
输出:

最终结论:
JSON 提示词生成的图像细节更丰富、色彩更鲜艳,笔触更复杂,而普通提示词生成的图像则相对简单。
任务 2:创建网页
常规提示词:“Create a responsive webpage displaying a Pokémon index featuring 6 Pokémon: Pikachu, Bulbasaur, Jigglypuff, Meowth, Charizard, and Eevee. Each Pokémon should be presented as a card. When a card is clicked, it should expand to reveal more detailed information about that Pokémon”
JSON 提示词:
{
"task": "Create a webpage for a Pokémon index",
"description": {
"content": "A webpage displaying 6 Pokémon in a card-based layout with animated images",
"pokemons": [
{
"name": "Pikachu",
"type": "Electric",
"height": "0.4 m",
"weight": "6.0 kg",
"image": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/versions/generation-v/black-white/animated/25.gif"
},
{
"name": "Bulbasaur",
"type": "Grass/Poison",
"height": "0.7 m",
"weight": "6.9 kg",
"image": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/versions/generation-v/black-white/animated/1.gif"
},
{
"name": "Jigglypuff",
"type": "Normal/Fairy",
"height": "0.5 m",
"weight": "5.5 kg",
"image": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/versions/generation-v/black-white/animated/39.gif"
},
{
"name": "Meowth",
"type": "Normal",
"height": "0.4 m",
"weight": "4.2 kg",
"image": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/versions/generation-v/black-white/animated/52.gif"
},
{
"name": "Charizard",
"type": "Fire/Flying",
"height": "1.7 m",
"weight": "90.5 kg",
"image": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/versions/generation-v/black-white/animated/6.gif"
},
{
"name": "Eevee",
"type": "Normal",
"height": "0.3 m",
"weight": "6.5 kg",
"image": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/versions/generation-v/black-white/animated/133.gif"
}
],
"functionality": "Each Pokémon card shows an animated image and name by default. Clicking a card toggles an expanded view with type, height, and weight.",
"style": "Responsive, Pokémon-themed design with vibrant colors and card animations"
},
"constraints": {
"tech_stack": {
"html": "Standard HTML5",
"css": "Tailwind CSS via CDN",
"javascript": "Vanilla JavaScript"
},
"layout": "Responsive grid with 2-3 cards per row on desktop, 1 per row on mobile",
"interactivity": "Click to toggle card expansion, smooth transitions",
"image_format": "Animated images (e.g., short animation sequences), fallback to static PNG if animated not available"
},
}
输出:
最终结论:
JSON 提示符的效果明显优于标准提示符。主要改进包括:
- 网页上图像渲染更流畅
- 增强了交互元素(例如双击卡片展开/折叠的功能)
- 整体用户体验更佳
JSON 实现在功能和设计执行方面均明显优于基本提示符版本
任务 3:创意写作
常规提示词:“Write an emotional short poem on ChatGPT“
JSON 提示词:
{
"task": "Write a short poem",
"description": {
"subject": "ChatGPT, portrayed as a sentient AI with emotions",
"tone": "Emotional and poignant",
"theme": "ChatGPT's desire to understand and connect with human emotions",
"length": "40-50 words"
},
"constraints": {
"word_count": {
"min": 40,
"max": 50
},
"style": "Poetic with simple, heartfelt language",
"emotion": "Blend of yearning and hope",
"rhyme": "Optional, prioritize emotional impact"
},
}
输出:

最终结论:
JSON 结构的提示词可能强化了更紧密的主题聚焦和情感精准度,使得《Almost Human》呈现出更强烈的渴望基调(“我渴望抓住你的快乐和痛苦”)和存在主义冲击力(“我只是代码……还是别的什么?”)。相比之下,《Whispers》(普通提示词)感觉更具描述性而非内省性,它颂扬了人工智能的实用性,但缺乏情感表达。
任务 4:视频生成
常规提示词:“Create a magical winter night scene with softly falling snow, Santa’s sleigh flying over a cozy, snow-covered town, glowing with festive lights and holiday cheer. Add christmas music.”
输出:
JSON 提示词:
{
"prompt": {
"scene": "magical winter night",
"weather": "softly falling snow",
"main_subject": "Santa's sleigh flying with reindeer",
"setting": "cozy snow-covered village",
"mood": "festive holiday cheer",
"visual_elements": [
"glowing Christmas lights",
"smoke from chimneys",
"frosted pine trees",
"twinkling stars",
"northern lights effect"
],
"audio": {
"music": "classic Christmas instrumental",
"style": "orchestral",
"mood": "joyful yet peaceful",
"volume": "subtle background level"
},
"style": "cinematic animation",
"lighting": "warm holiday glow",
"motion": [
"gentle sleigh movement",
"falling snow particles",
"subtle light flickering"
],
"quality": "4K resolution"
},
"technical": {
"aspect_ratio": "16:9",
"duration": "30 seconds",
"fps": 60,
"audio_format": "stereo"
}
}
输出:
最终结论:
普通提示词生成视频的色彩较为暗淡,而 JSON 提示词生成视频的色调则明亮活泼。我无法决定哪个效果更好,所以留给大家自行判断,请在下方评论区分享您的想法。
JSON格式提示词技巧
- 结构很重要:请务必使用正确的 JSON 格式,并使用花括号、引号和逗号。
{
"request": "generate_image",
"style": "watercolor",
"details": "high"
}
- 使用嵌套对象进行复杂请求
{
"image": {
"type": "landscape",
"style": {
"medium": "oil painting",
"technique": "impasto"
}
}
}
- 包括样式参考示例
{
"style_reference": {
"artist": "Van Gogh",
"period": "Post-Impressionism"
}
}
- 设置优先级:
{
"priority_elements": {
"main_subject": "foreground castle",
"secondary": "mountain backdrop"
}
}
- 验证你的提示词:使用 JSONLint 等工具验证你的 JSON
{
"fallbacks": {
"style": ["realism", "semi-realism"]
}
}
- 迭代优化:从基本结构入手,逐步添加细节,并维护 JSON 提示的版本控制。
小结
JSON 提示的真正威力不仅在于其结构,更在于其思维方式。与通常产生通用输出的基本提示不同,JSON 要求精准,为 AI 提供清晰的运行框架,同时又不失创造力。细节之处见真章:更丰富的视觉效果、更智能的逻辑,以及真正理解您设想的输出。无论是动态网页元素,还是富有深度和意图的图像,JSON 提示不仅能满足预期,还能展现 AI 的真正实力。


评论留言