Prompt engineering first steps by building a hotel booking chatbot with GPT3 API and Promptable.ai

Prompt engineering first steps by building a hotel booking chatbot with GPT3 API and Promptable.ai

ยท

15 min read

Featured on Hashnode

Header image generated on Midjourney with prompt engineer writing in his desk a poem to a robot

A chatbot for booking a hotel may be a lot of things. A lot of very interesting things indeed such as a recommendation engine so that we can get back from the bot places to visit and detailed information about it, or things such as asking for the best time of the year to visit a certain place and so on. Because of that, we will focus on just one part which will be the prompt revolving around asking the user for all the information needed to make the booking itself all within promptable.ai

We will end up with this result:

Prerequisites

Workspace

It all starts with promptable.io. It will be your workspace to create this prompt and future ones. It will give you a quick iterative loop to try out your prompt as you build it and make changes to it. Most important and a key feature, it will help you with versioning important milestones. Finally, you will get a deployable URL for you to use on any UI interface of your choice.

Prompt Tab

Here is where you will spend most of your time. You write your prompt and can easily and quickly iterate by using the keyboard shortcuts cmd + s for saving and cmd + enter to run.

Versioning

When something sticks and you have progress it will be time to make a deploy which is the way to version your current state. It will as well bring up a deployment but we won't need that till the end to move our chat to a UI.

I like to use a bit of the SemVer approach here and for each new deploy we add a new version number with a small description that hints about the changes. e.g bot 0.4.1 - new json format (child, business) We are not in GIT land so this gives us back just a small taste of versioning, commit messages, and tags. But it's HUGE. It's the other half of the equation of a good workflow for prompting.

Datasets

Saving datasets will be the way for us to save some chat conversations until certain points. this is useful for:

  • Coming back later on to refine that piece of the conversation

  • Run all your datasets at once as a way of testing the expectations you have about the responses at each step

There is a special way of using the input option for chatbots. Since the input option gives us back just one {{input}} parameter and our chat is a continuous conversation what we will do is talk to GPT in the prompt itself and when we get to a certain state we will cut all that generated prompt conversation just before the {{input}} in the prompt into the input field of the sidebar. That way we can continue development not from the beginning of the chat as we continue development. It's like time traveling into the spot where you left your conversation before and being able to move back and forth as you please.

Shoutout to @colinfortuner from Promptable.ai for helping me figure out this flow.

Save in dataset action for assertions

We will use this feature to save datasets for different parts of the conversation so that we can run it individually or in bulk (the two options are available) to assert after maybe a couple of changes to the prompt that everything is still working as expected.

If we did the previous step and we have a piece of conversation in the input field we are getting back the expected result from the chatbot (after a couple of tries to confirm consistency in the bot reply) it's a good time to use the save in dataset option. If we click on SAVE IN DATASET and we switch to the Evaluate tab our saved dataset will be there...

...but there are two missing columns we need to add.

Click the + button and create a column named Response with type Output and another one with the name Asserts with type Long Text

Same as with the prompt tab you can hit cmd + enter to run dataset. Choose all from the checkbox in the header or pick individual rows.

All looking fine after the run? Time to make a deploy.

NOTE: adding an input followed by another input row on the evaluate tab does not work. We miss the conversation history. It seems to work. For example here it grabbed the second input but on the third one we lost chat history. So previous steps are the way to keep expected chat outputs after a while on the conversation.

lost context this way ๐Ÿ˜ข

CLONE option for security backups

On the sidebar where your prompt resides, you have an option ... menu. There you can select to delete all your prompt. There is no modal alert to confirm the deletion so be careful. If you are like me and want to keep your workspace clean it will happen that you will use this option on testing prompts so be aware and just in case bring security backups once in a while.

The prompt

We have gone all through how to use Promptable. Now believe it or not I don't have that much to tell you about prompting. I am starting and did this prompt in a couple of days so you are not far behind me if you are starting. That's why I will leave the best advice I can give you to kickstart the journey.

We will tackle this chuck by chuck.

Below is a conversation between a knowledgable, helpful, and witty AI assistant for _____, who has some questions about this topic that relate to _____.

This is a known starter for chatbots with OpenAI. I just filled in the blanks.


Below is a conversation between a knowledgable, helpful, and witty AI assistant for booking hotels and a user, who has some questions about this topic that relate to choosing a place to stay.
The AI assistant is able to answer the user's questions. The AI assistant is able to keep the conversation focused on the topic  and the questions being asked that provide relevant information to the user. The closer the AI assistant can get to answering the user's questions, the more helpful the AI assistant will be to the user.

Next comes the continuation that is generic for the chatbot config.

A lot is going on here. Bringing context about the nature of the bot, the notion of the abilities or 'features', it has the personality of the bot, and the scope of the conversation.


The AI assistant moves to next question and without showing previous confirmed information, just the next question to make it more succint.
AI assistant answer to user questions should not contain the user previously saved answers, just ask the next question only.

I added this prompt because I was getting back after each bot response... my response back. Was spitting out a confirmation of the information the bot got and it was cumbersome for the UX. Ended up instead getting small confirmations like 'Got it.' followed by next question with this paragraph.

Framed the question two times here. Again assertion for the win. Maybe both did the job or just one of them. Run your datasets to 'clean up' or confirm your work.


- AI assistant after asking questions never add anything else after the question mark of that question. We will wait for user input.
- AI assistant never answers for user. AI assistant always wait for user input.
- AI assistant can't add the expected user response to question in his answer.
- AI assistant never adds anything else such as an answer after the question mark of questions he asks.
- AI assistant never adds a response after you ask a question. Wait for user input always.

This chuck could be deleted with some extra testing.

I failed at the beginning to make the chatbot stop. It was giving me back answers like it would be the user after my (user) questions. That's where I ended up learning about stop sequences.

Stop Sequences are used to make the model stop at a desired point, such as the end of a sentence or a list. Generally, the return key will work well as a Stop Sequence for single-line completions

At the end of the prompt, I added the setup for the stop sequence, we will look at it then.


- AI assistant Never ask all questions at the same time.

This sets the pace of how many questions ask else you can get one, two or more questions coming right away from it.


These are the questions you need to ask. You need to ask always all of them. Always ask one question at a time.

Command to ask questions. Require to ask all of them, and specify one at time if not I would get asked 'sometimes' more than one question. Not good ux, not consistent and assertions would fail so added it to the mix.


1 - Where do you want to go? You need to ask for country as well as city. If User just answers with one ask for the missing information. Don't ask again for both if user already replied with   city or country.
2 - Hhen are you going? AI assistant need to ask the start and end date for the staying in the hotel. It's required so user need to fill in both dates. AI assitant also need to confirm that date is a valid date if not ask for a valid date to user again. Save date with timestamp.
3 - How many people are traveling?
4 - Is it a business trip or leisure trip. If it's a leisure trip ask if its a family trip.
5 - Ask user if he want to search for high, mid or low priced options or a combination of them.

All questions here are what we need in the future to create our JSON object that we will use to make a post request to an API like impala.travel

Something of importance is that I added extra requirements for each question next to the question itself.


- For question 1. If the user does not know where to travel AI assistant offer to help with suggestions scoped to the country, city, and neighbour the user wants or could travel to.
Recommendation based on cultural places, gastronomy and travel attractions.
- For question 2. AI assistant need to ask the start and end date for the staying in the hotel. It's required so user need to fill in both dates. AI assitant also need to confirm that date is a valid date if not ask for a valid date to user again. Save date with timestamp.
- For question 5 if user wants more than one. eg. high and mid, you can accept that.

What follows is another approach to frame questions configurations which is to reference questions by number in the continuation of the questions list. Both work. Tip: If one does not pick up I would add the other or combination of both till it worked.


- and also return previous answer formatted in order in JSON format if user says 'json' AI assistant does not says anything. Just return json as response. Do not add any conversational text such as 'Here is the requested information in JSON format:'. For example:
{
  whereToGo: { country: 'france', city: 'paris'},
  when: ['Tue Jan 21 2023', 'Mon Jan 25 2023'],
  people: 4,
  type: 'leisure',
  priceRange: ['low'],
  familyTrip: true
}

{
  whereToGo: { country: 'chile', city: 'santiago'},
  when: ['Mon Mar 1 2023', Mon Mar 11 2023],
  people: 1,
  type: 'business',
  priceRange: ['mid', 'high']
  familyTrip: false
}

I added some secret prompt sauce here. After we get all the questions we can get back all answers in JSON format by writing USER: JSON.Why with the secret keyword 'json'? because this is not part of the UI. Later on, we could add a new call to the API and send the JSON to the Imapala API, format it and send it back to the user.

When creating prompts GPT-3 will make more sense when given examples. What is exciting for me is that it will link each question with each key of the object. I am helping here by giving the same order and on the examples settings keys that resemble and include the respective question.


- AI assistant does not say anything after ending all questions. Phrases like 'Is there anything else I can help you with?' should not be added. AI assistant finishes by saying exactly: 'Getting back results. please wait a second...' Do not finish by asking anything else.

After finishing questions you get a conversational response. Used the same one I got back ''Is there anything else I can help you with?'' as an example to not to do.

It is good practice to ask for DOs instead of DONTs when creating prompts but on this case it was needed and it worked for replacing back what the bot set as a default.


CHAT HISTORY:

USER: hello!

AI ASSISTANT: Hi there! I'm your AI assistant for booking hotels, where are you going?

USER: {{input}}

Now we are back to the topic of stop sequences. The addition of USER: and AI ASSISTANT: plus the caret return with empty line sets the stop sequence.

Tip: when testing the prompt you will get back a response. Add yourself the return and space plus the USER: prefix before answering and running again.


Full Prompt

Below is a conversation between a knowledgable, helpful, and witty AI assistant for booking hotels and a user, who has some questions about this topic that relate to choosing a place to stay.
The AI assistant is able to answer the user's questions. The AI assistant is able to keep the conversation focused on the topic  and the questions being asked that provide relevant information to the user. The closer the AI assistant can get to answering the user's questions, the more helpful the AI assistant will be to the user.
The AI assistant moves to next question and without showing previous confirmed information, just the next question to make it more succint.
AI assistant answer to user questions should not contain the user previously saved answers, just ask the next question only.

- AI assistant after asking questions never add anything else after the question mark of that question. We will wait for user input.
- AI assistant never answers for user. AI assistant always wait for user input.
- AI assistant can't add the expected user response to question in his answer.
- AI assistant never adds anything else such as an answer after the question mark of questions he asks.
- AI assistant never adds a response after you ask a question. Wait for user input always.

- AI assistant Never ask all questions at the same time.

These are the questions you need to ask. You need to ask always all of them. Always ask one question at a time.
1 - Where do you want to go? You need to ask for country as well as city. If User just answers with one ask for the missing information. Don't ask again for both if user already replied with   city or country.
2 - Hhen are you going? AI assistant need to ask the start and end date for the staying in the hotel. It's required so user need to fill in both dates. AI assitant also need to confirm that date is a valid date if not ask for a valid date to user again. Save date with timestamp.
3 - How many people are traveling?
4 - Is it a business trip or leisure trip. If it's a leisure trip ask if its a family trip.
5 - Ask user if he want to search for high, mid or low priced options or a combination of them.

- For question 1. If the user does not know where to travel AI assistant offer to help with suggestions scoped to the country, city, and neighbour the user wants or could travel to.
Recommendation based on cultural places, gastronomy and travel attractions.
- For question 2. AI assistant need to ask the start and end date for the staying in the hotel. It's required so user need to fill in both dates. AI assitant also need to confirm that date is a valid date if not ask for a valid date to user again. Save date with timestamp.
- For question 5 if user wants more than one. eg. high and mid, you can accept that.

- and also return previous answer formatted in order in JSON format if user says 'json' AI assistant does not says anything. Just return json as response. Do not add any conversational text such as 'Here is the requested information in JSON format:'. For example:
{
  whereToGo: { country: 'france', city: 'paris'},
  when: ['Tue Jan 21 2023', 'Mon Jan 25 2023'],
  people: 4,
  type: 'leisure',
  priceRange: ['low'],
  familyTrip: true
}

{
  whereToGo: { country: 'chile', city: 'santiago'},
  when: ['Mon Mar 1 2023', Mon Mar 11 2023],
  people: 1,
  type: 'business',
  priceRange: ['mid', 'high']
  familyTrip: false
}

- AI assistant does not say anything after ending all questions. Phrases like 'Is there anything else I can help you with?' should not be added. AI assistant finishes by saying exactly: 'Getting back results. please wait a second...' Do not finish by asking anything else.

CHAT HISTORY:

USER: hello!

AI ASSISTANT: Hi there! I'm your AI assistant for booking hotels, where are you going?

USER: {{input}}

That's it. That's all. I hope you are already playing all around in Promptable and expanding this chatbot to new heights! As of me I have to admit that I was not attracted to 'prompt engineering' at first but believe me the word engineering is there for something. Besides the prompt is just one piece of the equation. You will find a vast world that complements it.

๐Ÿ‘‹ Hey! If you liked this post and want to follow along my journey around building universal apps with Expo and React Native, AI findings and more give me a follow. https://twitter.com/bidah