iOLAP is now part of Elixirr Digital. All previous iOLAP services, thought leadership and career opportunities will shortly be integrated into the full Elixirr Digital site

Blog
Igor Jonjic

Generative AI models like ChatGPT and Claude have become powerful tools for tackling a variety of challenges, but effectively harnessing their capabilities requires careful prompt engineering.  Imagine asking an AI to analyse thousands of documents for a crucial business decision, only to receive irrelevant or vague answers. Generative AI models like GPT and Claude hold immense potential for such tasks – but only when guided by precise and thoughtful prompts.

In this blog, we’ll show you how to unlock that potential.

We will use a specific case as an example. The best way to get familiar with prompting techniques is to use them, of course. The second-best way is to go through examples. Before we start with the task that we were solving, there are some key concepts that we should cover first.

Key concepts

To follow this guide effectively, here are the definitions of terms we use frequently:

  • Prompt engineering: The practice of creating structured instructions to guide AI responses. This includes specifying tools, rules, and workflows.
  • Retrieval-Augmented Generation (RAG): A method where the AI combines document retrieval with text generation to ensure responses are based on external, up-to-date information.
  • Few-shot learning: A technique where the AI learns desired behaviour from a few example queries and responses embedded in the prompt.
  • Tool: A function that an AI model can call for performing a task, such as gathering information, taking action, or manipulating data.

Case task

The main task is prompting the agents to invoke specific tools to ensure accurate responses.

After receiving a query, the agent should first search for the CSV document, and if the information was insufficient, extend the search to PDF and Word documents, informing the user if data was still lacking. Agents are searching through documents by using tools.

The system prompt was structured to define the necessary steps the agent should follow before delivering a response, as clients often require concise, step-by-step solutions, and the team set specific rules and used few-shot learning with example queries to limit the agent’s tendency for verbose explanations.

The approach

We implemented knowledge base agents using the Retrieval-Augmented Generation (RAG) approach, which allowed for seamless integration of various file formats like PDF, CSV, and Word documents. This enabled the agents to directly interact with the document corpus. The backend for document processing was already established and operational, so our primary focus was on configuring the agents through prompt engineering.

Prompt structure and components

The key to creating success with the system prompt is structuring it in a clear, modular way. We organised the prompt into the following core components:

  1. Tool definitions

We explicitly defined the tools available to the agent, such as spreadsheet tool and knowledge base tool, which could be called to gather relevant information.

  1. Step-by-step instructions

We outlined a specific set of steps the agent should follow for each query, including calling the defined tools, reviewing the returned data, and determining if a confident response could be provided.

  1. Response rules

To ensure consistent, actionable responses, we established a list of rules the agent must adhere to, such as basing answers solely on the tool outputs, avoiding assumptions, and providing concise, step-by-step solutions.

  1. Example queries and responses

We included a few examples query-response pairs to guide the agent’s behaviour through few-shot learning.

Practical implementation

Initially, we structured the entire prompt as a single, large text block. However, this approach often led to confusion, as the agent struggled to properly invoke the tools, provided overly verbose responses, or gave answers based on its training data rather than the information gathered from the tools.

To address this, we introduced XML tags to organise the prompt into the distinct sections mentioned above. This clear structure made it much easier for the agent, particularly Anthropic’s Claude models, to comprehend and follow the instructions.

For GPT models, we found that using Markdown syntax provided a similar organisational benefit.

Claude responds better to XML tags because it was trained on such data, while GPT models were trained on Markdown files and, because of that, respond better to Markdown instructions inside the system prompt.

The system prompt example with XML tags:

  1. Tool definitions

We defined the tools explicitly:

You have 2 tools that you must call before giving an answer to the user. Tools are:

<tools>
1. spreadsheet_tool
2. knowledgebase_tool
</tools>
  1. Step-by-step instructions

Clear steps were outlined for the agent:

Please follow these steps every time before giving an answer:

<steps>
1. Call spreadsheet_tool with domain: 1 and call knowledgebase_tool.
2. Review the returned data carefully.
3. If confident, provide the answer. Otherwise, request more information.
</steps>
  1. Response rules

The agent was given rules to ensure consistency:

Please follow these rules when providing the final answer:

<rules>Always call the tools before responding.

1. Base responses only on the returned data, avoid assumptions.
2. Re-call the tools for each new query to ensure up-to-date information.
3. Answer only if you're highly confident.
4. If unsure, ask for more information.
5. Never fabricate sources.
6. Responses must be concise, with no preambles.
7. Provide step-by-step solutions only.
8. Take your time and use all available tools.

</rules>
  1. Examples for few-shot learning

To guide the agent, we provided for example queries and responses:

<example>

If the user asks [something specific]

Your answer:
1. Do this.
2. Then do this.
3. Finally, do this.
</example>
<example>
Question: Why is the LED on my device green?
Your answer: Blinking green means multiple devices are detected.
</example>

By breaking down the prompt in this way, the agents are able to consistently follow the prescribed steps, rules, and examples, leading to highly satisfactory user interactions. It also simplified prompt modifications, as new steps, rules, or examples could be easily incorporated based on user feedback.

We’re here to help

Effective prompt engineering is essential for getting the most out of generative AI models when tackling real-world problems. By structuring system prompts with clear tools, step-by-step instructions, response rules, and example-based guidance, you can ensure the models provide reliable, actionable solutions. This approach has proven highly successful in our use cases and can be adapted to a wide range of problem domains.

Generative AI is opening doors to smarter ways of working, and Elixirr Digital is here to help you unlock its full potential. Contact us today and let’s discuss how we can help your business overcome AI challenges.

If you’re interested in contributing to projects like these, visit our careers page to learn about joining our team.

More on this subject