Table of Contents

Prompt Engineering for GPT-3

Cesar Banchio Updated by Cesar Banchio

Prompt engineering is the delicate and subtle art of crafting specialized instructions for AI models to perform specific tasks with high accuracy and efficiency. In the case of Large Language Models such as GPT-3 we can improve the quality of the responses generated by providing effective prompts that guide the model to produce more relevant and accurate output. By carefully constructing prompts that provide relevant context and guidance, we can enhance the performance of language models and even leverage their capabilities to perform other Natural Language Processing (NLP) tasks such as text classification or entity recognition.

By the end of this post, you will have a comprehensive understanding of how to build effective prompts that can significantly improve the performance of language models in various NLP applications.

Intent detection

A really common NLP task is to classify text according to certain predefined categories or labels. It is a subdomain known as Natural Language Understanding (NLU). Imagine you own a restaurant and you receive questions from your customers by your awesome Landbot bot and you want to understand the intent of the questions. Is the customer asking where the restaurant is? Or is she asking when is it open? We can accomplish this with LLM and a proper prompt.

Prompt:

Classify questions based on whether they are related to the following labels. Use "General" label when the question is off-topic. Could be questions with more than one label: [Location, Open hours, Reservation, Menu, General].

  • When does the restaurant close?
  • What color is the skye?
  • Do you serve vegan options?
  • Where can I find the restaurant?
  • Can I make a reservation for today?
  • Is the restaurant open on Mondays? I want to book a table for two people.

Did you know that you can accomplish exactly that with a native Landbot feature? We are developing a NLU component (in closed beta version) with which you will be able to set up everything inside Landbot! Register to become a beta tester here!

Named Entity Recognition

Named entity recognition is the task of identifying and categorizing key information (entities) in free text. By providing a good prompt we can guide GPT-3 to perform this task with no extra efforts. In case we are using their API we can even specify the format in which we want the entities to be extracted so we get the response in the format we needed for our Landbot bots.

Prompt:

Extract the following entities mentioned in the text below. Use the JSON format: { "reservation_date": "reservation_time": "n_guests": "n_vegan_menus": }

Text: Hello, I want to make a reservation for this saturday for 5 people at 9:30 PM. One of us wants the vegan option please

Generate JSONL for fine-tuning a model

One of LLM limitation is the lack of specific context of your business information which can make the model hallucinate and provide inaccurate responses to customer queries. Fortunately, there is a solution: fine-tuning.

Fine-tuning an LLM involves providing it with a database of specific information from which it can learn. For example, if you want any GPT-3 model to provide personalized responses to customer queries, you'll need a set of training examples that each consist of a single input ("prompt") and its associated output ("completion"). Your data must be a JSONL document, where each line is a prompt-completion pair corresponding to a training example. The model will then use this information to generate its own responses, tailored to your business documentation.

If you don’t have this set of prompt-completion don’t worry, you can use any LLM to generate them! In this case the AI model will read your documentation and will output a set of relevant questions and their corresponding answers. You can even request the AI to generate them in JSONL format, so it’s ready to be used for fine-tuning.

Prompt:

Generate 5 questions and their respective answers based on the following text. Only output using JSONL format {"prompt": "", "completion": ""}, filling the "prompt" with the question and "completion" with the answer:

It's not possible to leave a variable without values.

It is not possible to delete a variable once you've created it.

However, it is possible to manually delete the variables for a specific user in the Chats section. Only the variables you’ve created (Custom Variables) will be deleted, not the Landbot System Variables. Click here for more information.

It is not possible to change the type of variable eg. string to number once it is set.

If a variable has a value set and you want to overwrite it, you can use a set variable block.

If you find a blank variable list it's because the user didn't answer the question that had that variable.

Arrays have a limit of 1MB anything more will cause errors in the Dynamic Data block.

Handling hallucinations

In case the AI cannot understand or cannot perform the task specified we can construct a prompt to guide the model. For example, you can force it to give a specific response.

Reducing the temperature of the request will also help with the hallucination issue. However, this is a current problem of LLM which is not easily solved.

Reducing the temperature of the request will help this also.

Prompt:

Answer the following question based on the following information provided. If a question cannot be answered with the information, answer "I cannot answer that":

It's not possible to leave a variable without values.

It is not possible to delete a variable once you've created it.

However, it is possible to manually delete the variables for a specific user in the Chats section. Only the variables you’ve created (Custom Variables) will be deleted, not the Landbot System Variables. Click here for more information.

It is not possible to change the type of variable eg. string to number once it is set.

If a variable has a value set and you want to overwrite it, you can use a set variable block.

If you find a blank variable list it's because the user didn't answer the question that had that variable.

Arrays have a limit of 1MB anything more will cause errors in the Dynamic Data block.

Question: What is an operating system?

Configure the Webhook Block

Once we have tested the prompt and it's responding the way we want, we can copy the prompt

and paste it in the Webhook block:

How did we do?

OpenAI

Build a Customer Service Bot with ChatGPT and Extract Information

Contact