what is a good way to give context to LLMs? Like saying "this is an example", "this is the resulting output of the example", "this is the specific input you should process" and so on ?
Would it be ok to use sort of xml tags?
Like text here </example input> ?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Providing context to large language models (LLMs) is crucial for ensuring they understand the task, interpret inputs correctly, and generate meaningful outputs. Your idea of using structured formats, such as XML-like tags, is a valid and effective approach, but there are several considerations and best practices to keep in mind. Below, I’ll outline some good ways to provide context, including the use of tags, and discuss their pros and cons.
1. Use Structured Formats (e.g., XML-like Tags)
Using XML-like tags or other structured formats (e.g., JSON, Markdown) is a great way to provide context, especially when you want to clearly delineate different parts of your input, such as examples, instructions, or specific data to process.
Example Using XML-like Tags:
plaintext
<instruction>
You are a code reviewer. Your task is to review the following code snippet and provide feedback on its readability, efficiency, and potential bugs.
</instruction>
<example_input>
def calculate_sum(a, b):
return a + b
</example_input>
<example_output>
The code is readable and efficient for simple addition. However, consider adding type hints to improve maintainability, e.g., `def calculate_sum(a: int, b: int) -> int`.
</example_output>
<input_to_process>
def multiply_numbers(x, y):
result = x * y
return result
</input_to_process>
Pros of Using XML-like Tags:
Clarity: Tags make it explicit which part of the input serves what purpose (e.g., instructions, examples, or data to process).
Consistency: Structured formats help maintain a consistent way of providing context, especially in automated pipelines or when working with multiple examples.
Machine Parsability: If you’re building a system that preprocesses inputs for an LLM, XML-like tags can be easily parsed programmatically.
Flexibility: You can define custom tags for different use cases (e.g., <context>, <constraints>, <desired_output_format>).
Cons of Using XML-like Tags:
Verbosity: Tags can make the input longer and harder to read for humans, especially if overused.
Model Familiarity: Some LLMs may not inherently understand XML-like tags unless they’ve been fine-tuned or prompted to interpret them. You may need to include instructions on how to interpret the tags.
Overhead: For simple tasks, tags might add unnecessary complexity.
Tips for Using XML-like Tags:
Use clear, descriptive tag names (e.g., <example_input> instead of <ex_in>).
Include an instruction at the beginning of your prompt explaining how the model should interpret the tags, if necessary.
Avoid excessive nesting of tags, as it can confuse both humans and models.
Test whether the model performs better with or without tags for your specific use case—some models may not need such explicit structure if the context is clear.
2. Use Natural Language with Clear Labels
If XML-like tags feel too formal or verbose, you can achieve similar clarity using natural language with labeled sections. This approach is often more human-readable and works well for most LLMs, especially if they’ve been trained on diverse text formats.
Example Using Natural Language Labels:
plaintext
Instructions: You are a code reviewer. Your task is to review the following code snippet and provide feedback on its readability, efficiency, and potential bugs.
Example Input:
def calculate_sum(a, b):
return a + b
code
Example Output:
The code is readable and efficient for simple addition. However, consider adding type hints to improve maintainability, e.g., `def calculate_sum(a: int, b: int) -> int`.
Input to Process:
def multiply_numbers(x, y):
result = x * y
return result
code
Pros of Using Natural Language Labels:
Readability: This format is more human-friendly and easier to write, especially for non-technical users.
Model Compatibility: Most LLMs are well-trained on natural language and can easily interpret labeled sections without additional instructions.
Simplicity: It avoids the overhead of formal tags while still providing structure.
Cons of Using Natural Language Labels:
Less Machine-Parsable: Unlike XML, natural language labels are harder to parse programmatically, which might be a drawback if you’re building an automated system.
Ambiguity: If labels are not consistently formatted (e.g., “Example Input” vs. “Input Example”), the model might misinterpret the structure.
Tips for Using Natural Language Labels:
Use consistent and clear labels (e.g., always use “Example Input” instead of switching between synonyms).
Use formatting (e.g., indentation, line breaks, or Markdown) to visually separate sections.
If the task is complex, consider adding a brief instruction at the beginning to explain how the model should use the labeled sections.
3. Use JSON for Structured Data
If your use case involves structured data or you’re working in a programmatic context, JSON is an excellent alternative to XML-like tags. JSON is widely supported, machine-readable, and familiar to most developers.
Example Using JSON:
json
{
"instruction": "You are a code reviewer. Your task is to review the following code snippet and provide feedback on its readability, efficiency, and potential bugs.",
"example": {
"input": "def calculate_sum(a, b):\n return a + b",
"output": "The code is readable and efficient for simple addition. However, consider adding type hints to improve maintainability, e.g., `def calculate_sum(a: int, b: int) -> int`."
},
"input_to_process": "def multiply_numbers(x, y):\n result = x * y\n return result"
}
Pros of Using JSON:
Machine-Readability: JSON is ideal for programmatic workflows, as it can be easily parsed and validated.
Standardization: JSON is a widely recognized format, making it easier to integrate with APIs or other systems.
Model Familiarity: Many LLMs are trained on JSON data and can interpret it effectively, especially if instructed to do so.
Cons of Using JSON:
Verbosity: JSON can be verbose, especially for simple tasks, due to the need for quotes, braces, and commas.
Human Readability: JSON is less human-friendly compared to natural language or even XML-like tags, especially for non-technical users.
Error-Prone: JSON requires strict syntax (e.g., no trailing commas), which can lead to errors if not carefully formatted.
Tips for Using JSON:
Use JSON for tasks that involve structured data or integration with other systems.
Include an instruction in the prompt explaining how the model should interpret the JSON structure, if necessary.
Use tools to validate JSON syntax before passing it to the model.
4. Use Markdown for Formatting
Markdown is a lightweight, human-readable format that works well for providing context to LLMs, especially in documentation-heavy or text-based tasks. It’s particularly useful when you want to combine natural language with some structure.
Example Using Markdown:
markdown
# Instructions
You are a code reviewer. Your task is to review the following code snippet and provide feedback on its readability, efficiency, and potential bugs.
## Example
### Input
def calculate_sum(a, b):
return a + b
code
### Output
The code is readable and efficient for simple addition. However, consider adding type hints to improve maintainability, e.g., `def calculate_sum(a: int, b: int) -> int`.
## Input to Process
def multiply_numbers(x, y):
result = x * y
return result
code
Pros of Using Markdown:
Readability: Markdown is highly human-readable and widely used in documentation.
Formatting Support: Markdown supports headers, code blocks, lists, and other formatting options, making it easy to structure your input.
Model Familiarity: Many LLMs are trained on Markdown-formatted text (e.g., GitHub READMEs, forums), so they can interpret it well.
Cons of Using Markdown:
Limited Machine Parsability: Markdown is less structured than XML or JSON, making it harder to parse programmatically.
Inconsistency: Markdown syntax can vary slightly across platforms, which might lead to rendering issues.
Tips for Using Markdown:
Use headers (#, ##, etc.) to clearly separate sections.
Use code blocks (`````) for code snippets or other preformatted text.
Keep the structure consistent across prompts to help the model understand the context.
5. General Best Practices for Providing Context
Regardless of the format you choose, here are some general best practices for providing context to LLMs:
Be Explicit: Clearly state the task, the role of each section, and what you expect from the model. For example, “Your task is to process the input in the <input_to_process> section and provide feedback similar to the <example_output>.”
Provide Examples: Including examples (input-output pairs) is one of the most effective ways to help the model understand the task. This is especially important for complex or domain-specific tasks.
Use Consistent Formatting: Whether you use tags, labels, JSON, or Markdown, maintain a consistent structure across prompts to reduce ambiguity.
Test and Iterate: Experiment with different formats to see what works best for your specific use case and model. Some models may perform better with natural language, while others may benefit from structured formats.
Limit Complexity: Avoid overcomplicating the prompt with too many tags, sections, or nested structures, as this can confuse the model or lead to errors.
Include Instructions for Interpretation: If you use a structured format like XML or JSON, consider adding a brief instruction explaining how the model should interpret it, especially if the format is unconventional.
6. When to Use XML-like Tags vs. Other Formats
Your idea of using XML-like tags (<example_input>, etc.) is perfectly valid, but whether it’s the best choice depends on your use case:
Use XML-like Tags When:
You need a machine-parsable format but don’t want to use JSON.
You’re working in a context where XML is already a standard (e.g., certain enterprise systems).
You want a balance between human-readability and structure.
Use Natural Language Labels When:
You’re working with non-technical users or in a context where simplicity is key.
The task is straightforward and doesn’t require machine parsing.
Use JSON When:
You’re building an automated system or integrating with APIs.
You need a highly structured, machine-readable format.
Use Markdown When:
You’re working in a documentation-heavy context (e.g., GitHub, technical writing).
You want a human-readable format with some structure but without the formality of XML or JSON.
7. Example of Combining Approaches
In some cases, you might want to combine approaches for maximum clarity. For example, you could use natural language labels for instructions and examples, but use XML-like tags or JSON for the input to process.
Combined Example:
plaintext
Instructions: You are a code reviewer. Your task is to review the following code snippet and provide feedback on its readability, efficiency, and potential bugs.
Example Input:
def calculate_sum(a, b):
return a + b
code
Example Output:
The code is readable and efficient for simple addition. However, consider adding type hints to improve maintainability, e.g., `def calculate_sum(a: int, b: int) -> int`.
<input_to_process>
def multiply_numbers(x, y):
result = x * y
return result
</input_to_process>
This approach combines the readability of natural language with the explicit structure of tags for the input to process.
8. Final Recommendation
Your idea of using XML-like tags (<example_input>, etc.) is a good one, especially if you’re working in a context where structure and machine parsability are important. However, for most general-purpose tasks, natural language labels or Markdown might be simpler and equally effective, especially if you’re not building an automated system. If you’re unsure, start with natural language labels and test whether adding tags or switching to JSON improves the model’s performance for your specific use case.
Ultimately, the best approach depends on your task, audience, and workflow—so don’t hesitate to experiment!