DEV Community

Cover image for Taming GPT - How to take control of AI responses
Igor Duca
Igor Duca

Posted on

Taming GPT - How to take control of AI responses

Me: "Hey, please help me to clean up this code"
AI: "Sure, there is the clean version of your code: "
Me: "OK. Now help me with this one, but only answer me with the cleaned code, do not add any commentaries "
AI: "Sure, there is the clean version of your code: "

See how this can be upsetting? I've lost my mind countless times while working because this happens ALL THE TIME when you're dealing with AI.

Band-aid solution

The first thing that may come to your mind is: I will just copy that instruction where I ask the AI to not add any commentaries to the response and pass it to each prompt I send.

That will work in this case, but let's imagine this situation:
Suppose you want to ask the AI to loop through an array you detail in the prompt and return a list with the object ids.

First response: The object ids are returned inside an array;
Second response: The object ids are returned in an array inside a JSON;
Third response: The object ids are returned as a string divided by commas.

If you are building an API or a service that works around the AI response, you'd have a lot of work to do in order to create patterns and parse each possible response from the AI.

Good practice

Now that you've learned what not to do, see how you could handle cases like this with mastery.

If you are using chat GPT, you'll be able to do this by creating a custom GPT and passing the following instructions:

You are an AI agent that is responsible for cleaning code for a software engineering company.
You must answer to every question with only the resultant code.
Do not add any comments to your response
Enter fullscreen mode Exit fullscreen mode

Or, if you are building an API using AI, you can add the text above to the start of your prompt, being this the prefix of every prompt you enter in the model.

Even more customization

Going back to that product list example, here's how we can successfully create a response pattern:

You are an AI agent that is responsible for looking object arrays and returning only the object ids in a new array.
After going through the object list, separate all the ids and present them in the response following this structure:
{
    "ids": []
}
Do not add commentaries to the response, just return the JSON object.
Enter fullscreen mode Exit fullscreen mode

See how easy that was? Now you can parse the response text safely knowing that it will always follow the same pattern.

Next Level

The example above isn't the most advanced one. See what we can do using AI without much complexity:

You are an AI responsible for answering SMS messages from a Tech Support company. You need to read the messages, interpret them and only answer the user with the mapped responses we provide you.

The known responses that you can use are: {faqList}

Make sure to treat the customer well. Call them by their first name, which is: {firstName}

If you need any additional info on the company, check this list: {companyInfo}

Our users usually don't enjoy reading long text messages, so make sure to break your text into many messages to make the conversation more dynamic and enjoyable for the user experience.
To determine whether a message has to start or end, add the tags <start-message> and <end-message> to your generated message, and the company API will parse correctly and divide the text into a group of messages.

After fetching all the data you need to return to the customer, present your answer using the following structure:

{
    "ticket-number": {ticketNumber},
    "message": "",
}
Enter fullscreen mode Exit fullscreen mode

The example below illustrates a more advanced prompt, that needs to be feed with information before having a response. Patterns like this can prevent the following issues:

  • Hallucinations
  • AI using fake information or incorrect facts
  • AI deciding which is the better structure to respond (in this case, it will only use the structure you've specified)
  • Short messages without custom user data

Conclusion

AIs are really flexible. You need to understand that the generative engine will play your game, so you can dictate how it must work and input as much arguments as possible to the prompts.


Photo by Google DeepMind on Unsplash

Top comments (0)