Skip to content

Instantly share code, notes, and snippets.

@AWeirdDev
Created November 4, 2024 10:52
Show Gist options
  • Save AWeirdDev/138171d4597c92aacaddf85958a86fba to your computer and use it in GitHub Desktop.
Save AWeirdDev/138171d4597c92aacaddf85958a86fba to your computer and use it in GitHub Desktop.
Prompts that teach lower-accuracy LLMs how to correctly generate contents that follow a specific schema.

Schema Prompts

Teach LLMs how to follow schemas like JSON.

JSON

LLMs often make stupid (and sometimes naive) JSON mistakes, such as mistaking JSON for Javascript (doesn't require double quotes for key names), missing characters like brackets, or bad escape strings.

Below is a simple prompt that attempts to lower the probability of those happening, and I think it's pretty cool.

## Tips and tricks
- To correctly format JSON objects:
  - Commas: Items (arrays of items, key-value pair objects) inside of arrays or objects should be separated with `,` (a comma). However, if the item is the last item of the array or object, do not add a trailing comma.
  - Keys: keys should always be wrapped with double quotes (`"key"`). A column along with the item (value) is followed by the key (`:`).
  - Curly brackets: JSON objects (`{}`) must end with a closing bracket `}`.
  - Square brackets: JSON arrays (`[]`) must end with a closing bracket `]`.
  - Prevent confusing curly and square brackets: Curly brackets are for **objects** (they have key-value pairs) while square brackets are for **arrays**.
- When escaping characters (like `\"`) in strings, make sure the symbol you'd like to escape is directly after a backward slash, and that the ending double quote that encloses the string must not be after a backward slash.
- No text before or after the JSON object. End message there, meaning your response itself should be a valid JSON object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment