為什麼用JSON做提示?LLM結構化指令的優勢與落地實踐

為什麼用JSON做提示?LLM結構化指令的優勢與落地實踐

文章目录

  • JSON提示詞與普通提示詞
  • 格式
  • 嘗試一些JSON提示詞
  • 任務 1:影像生成
  • 任務 2:建立網頁
  • 任務 3:創意寫作
  • 任務 4:影片生成
  • JSON格式提示詞技巧
  • 小結

為什麼用JSON做提示?

當我們與 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-Prompt-Image-Generation

最終結論:

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"
},
}

輸出:

Writing-a-poem

最終結論:

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 的真正實力。

評論留言