Prompt Engineering: Learning to Interact with LLMs

May 11, 2023
Share this post
https://www.truefoundry.com/blog/prompt-engineering-learning-to-interact-with-llms
URL

The emergence of ChatGPT / OpenAI and other Large Language Models (LLMs) has revolutionized the world and is one of the most significant technological advancements of the past decade. With the help of AI, we can now automate numerous day-to-day tasks that were once mundane and time-consuming. However, to fully leverage this technology's potential, it is essential to understand how to use it effectively.

Prior to the emergence of AI, programming languages were the sole means of instructing machines. However, with the advent of Large Language Models (LLMs), it is now possible to accomplish a great deal using plain, simple English. This article was compiled from various sources as I was learning more about this exciting field. My hope with this blog is that it will help to shorten the amount of time it takes for you to understand prompt engineering.

Let's say you type this in OpenAI Playground:

What is 965*590?

The answer is incorrect and a bit different every single time. But if you modify the instruction as follows and set the temperature to 0, you are likely to get the correct answer:

What is 965*590? Make sure your answer is exactly correct:

This art of being able to modify the prompts to get AI to do what we want it to do is called Prompt Engineering. It's not only possible to get answers from OpenAI, but also use it to execute actions for us - hence opening up the possibility to automate a lot of actions in real life.

Here are a few ways to get things done by AI:

Provide examples to the model showing what you want to do (Few-shot prompting)

Provide clear instructions to the model with an example. Based on the number of examples you provide, it is called 0-shot prompting, 1 shot prompting or a few-shot prompting. The more the number of examples, the better the output of the model will be. Also, you can use few-shot prompting to tell the model in what format to return the output.

0-shot prompting
2+2=
1-shot prompting
2+2=4
3+3=
few-shot prompting
2+3=5
10+30=40
3+1=4
1+11=

Here's an example of how you can use few-shot prompts to classify a tweet as positive or negative:

Twitter is a social media platform where users can post short messages called "tweets". Tweets can be positive or negative, and we would like to be able to classify tweets as positive or negative. Here are some examples of positive and negative tweets. Make sure to classify the last tweet correctly.

Q: Tweet: "What a beautiful day!" Is this tweet positive or negative?
A: positive

Q: Tweet: "I hate this class" Is this tweet positive or negative?
A: negative

Q: Tweet: "I love pockets on jeans"
A:

Explain your reasoning to the model in the examples (Chain of Thought Prompting)

Let's say we provide the following prompt:

Prompt:
Which is a faster way to get to work?
Option 1: Take a 1000 minute bus, then a half hour train, and finally a 10 minute bike ride.
Option 2: Take an 800 minute bus, then an hour train, and finally a 30 minute bike ride.

Output:
Option 1 is a faster way to get to work.

As you can see, the output is clearly wrong. However, if we explain the logic to AI, we can actually get it to answer correctly.

Which is a faster way to get home?
Option 1: Take an 10 minutes bus, then an 40 minute bus, and finally a 10 minute train.
Option 2: Take a 90 minutes train, then a 45 minute bike ride, and finally a 10 minute bus.
Option 1 will take 10+40+10 = 60 minutes.
Option 2 will take 90+45+10=145 minutes.
Since Option 1 takes 60 minutes and Option 2 takes 145 minutes, Option 1 is faster.

Which is a faster way to get to work?
Option 1: Take a 1000 minute bus, then a half hour train, and finally a 10 minute bike ride.
Option 2: Take an 800 minute bus, then an hour train, and finally a 30 minute bike ride.

Output:
Option 1 will take 1000+30+10 = 1040 minutes.Option 2 will take 800+60+30 = 890 minutes.Since Option 1 takes 1040 minutes and Option 2 takes 890 minutes, Option 2 is faster.

Provide your own knowledge base to ChatGPT

You can provide information about your own knowledge base to ChatGPT so that it can answer questions pertaining to that knowledge. This is specially useful if you are planning to build your chatbots over your own data.  You can pass the information in the context. However, there is a limitation wherein LLMs like GPT-3 have a maximum prompt size of about 4k tokens, and hence you cannot feed a lot of information directly to ChatGPT in the context.

A solution to this can be to first index the documents in the knowledge base. Then in response to the prompt, we first fetch the relevant documents from the index and then allow ChatGPT to answer the question based on the subset of the information retrieved from Search. This basically is using semantic-search and then prompting the model with the relevant info in the context. A lot of libraries can enable this for you like Haystack and Langchain.

Integrate Tools in LLMs to make them perform actions (ReAct)

You can provide multiple tools to LLMs to perform actions and then let the LLM decide the correct way to interact with these tools to achieve the desired objective.

This is called Reason and Act paradigm(ReAct) and is kind of a more advanced version of MRKL systems (Modular Reasoning, Knowledge and Language, pronounced "miracle").  

Let's first explain MRKL with an example. Let's say we have an API to call Google Calculator to perform calculations. Now we know that mathematically correct expressions are something Google can answer, but not queries in natural language. So if the prompt is as mentioned below:

What is 20 times 5^6?

We can instruct the LLM to output the expression in something Google Calculator can understand. So the prompt, in this case, will be:

Translate the math problem below to a format that Google calculator can understand.

What is 20 times 5^6

Similarly, we can use LLMs to convert an action described using natural language into an expression that is understood by an existing API. Integrating third-party APIs and interacting with them from the chat interface has been made simple inside ChatGPT through their plugin system, about which you can read more here.

Some examples of an ultimate experience

  1. "Order ingredients to make a pepperoni pizza." ChatGPT can automatically identify the necessary ingredients and place an order to an online grocery store.
  2. "Add an event to my calendar at 8pm." This prompt can be converted to a CURL request to your calendar application and then fired (either through a plugin or manually) to create an actual event in your calendar app.
  3. "I need to train a model that works on this CSV file and outputs the predicted price of the model." This prompt can automatically generate a training code for a ML model and then it can be executed to actually train the model by either copy-pasting to your coding environment or using an appropriate plugin.

   Deploy any commercially available Large Language Model

References:

  1. https://learnprompting.org/docs (This is an excellent resources in case you want to dive deeper into prompt engineering). All examples in the article have been taken from here.
  2. https://til.simonwillison.net/llms/python-react-pattern

Chat with us

if you're looking to maximise the returns from your LLM projects and empower your business to leverage AI the right way, we would love to chat and exchange notes.

Have a ☕️ with us

Learn how TrueFoundry helps you deploy LLMs in 5 mins:

Discover More

April 16, 2024

Cognita: Building an Open Source, Modular, RAG applications for Production

LLMs & GenAI
April 11, 2024

How To Choose The Best Vector Database

LLMs & GenAI
March 28, 2024

Leveraging Fractional GPUs on Kubernetes

GPU
LLMs & GenAI
March 14, 2024

Helping Enterprises accelerate the time to value for GenAI

LLMs & GenAI

Related Blogs

No items found.

Blazingly fast way to build, track and deploy your models!

pipeline