DEV Community

Cover image for Why XML Prompting?
Sanat Kulkarni
Sanat Kulkarni

Posted on

Why XML Prompting?

LLMs act in weird ways for unexplainable reasons sometimes, especially SLMs (Small Language Models). Yet, we have to work with them for various reasons; be it available resources, policy restraints, etc.

XML (Extensible Markup Language) Prompting helps you with this exact problem. It gives your LLM or SLM the exact right type of context that is needed for it to act unambiguously. It is a way of structuring prompts that mix instructions, context, examples, and variable inputs.

How does an XML look? To those who have used HTML before, it might look similar. A snippet looks like this:

<message>
    <text>Hello World!</text>
</message>
Enter fullscreen mode Exit fullscreen mode

Use case:
SLMs usually struggle with maintaining context, have less memory capacity and are either heavily quantized or having less training data as compared to LLMs.

They are the exact playground when you need speed yet you want the job to be done in specified constraints (Assuming usage of local AI to prevent any kind of confidential data leakage).

Let's take an example of a prompt with and without XML prompting:

Without XML Prompting:

You are a document summarization assistant.

Task:
Summarize the document provided by the user.

Instructions:
- Identify the main topic.
- Extract the most important points.
- Keep the summary under 150 words.
- Do not invent information.
- Use simple language.

Example:
Input: "The company reported a 20% increase in revenue..."
Output: "The company experienced significant revenue growth..."

Document:
{{DOCUMENT}}

Additional user instructions:
{{USER_INSTRUCTIONS}}
Enter fullscreen mode Exit fullscreen mode

With XML Prompting:

<task>
  <name>Document Summarization</name>

  <instructions>
    <instruction>Identify the main topic.</instruction>
    <instruction>Extract the most important points.</instruction>
    <instruction>Keep the summary under 150 words.</instruction>
    <instruction>Do not invent information.</instruction>
    <instruction>Use simple language.</instruction>
  </instructions>

  <example>
    <input>
      The company reported a 20% increase in revenue...
    </input>
    <output>
      The company experienced significant revenue growth...
    </output>
  </example>

  <context>
    <audience>Business executives</audience>
    <tone>Concise and professional</tone>
  </context>

  <document>
    {{DOCUMENT}}
  </document>

  <user_instructions>
    {{USER_INSTRUCTIONS}}
  </user_instructions>
</task>
Enter fullscreen mode Exit fullscreen mode

With XML prompting, the model does not have to infer the purpose of the specific parts of the prompt, leading to desirable and structured answers. The model refers to the specific way of input and output and provides the result in a similar way (where formatting practice constraints are necessary)

This can help speed up a lagging process in a sustainable and structured way.

Hope you enjoyed reading! :)

Top comments (0)