ChatGPT 101: Prompt Engineering
When I started experimenting with ChatGPT end of last year, soon it came evident that power of ChatGPT lies not in the genie but the one who holds it and puts it to work! But as powerful and versatile as this 'genie' is, its true potential can only be unlocked by the skilful crafting of prompts—simply known as prompt engineering.
You might find it hard to believe, but the full potential of ChatGPT is unlocked through carefully crafted prompts. In fact, I see a lot of "prompt engineering" courses appearing like mushrooms!
If you are new to the world of generative AI tools such as ChatGPT or Bard or hearing about "prompts" for the first time, fret not - we will delve into this fascinating field and understand how it is revolutionising the use of AI models like ChatGPT and Bard.
Prompt Engineering
Prompt engineering is a practice that involves designing the initial input called a "prompt". The prompts guide the model towards generating a more desired or useful output. It's nothing more than a question that you ask the chatbot. This input forms the basis of the model's response. Therefore, strategically crafted prompts can lead to more meaningful, relevant, and accurate responses.
Before we go further, let me show a couple of examples to illustrate the power of prompt engineering in action.
Example Prompt 1: Asking for Java Code Fix
Let's say you're encountering an issue with a Java method. You could ask GPT: "Why isn't this method working?" and provide the code. However, without specific details about the problem, the AI might not be able to offer a useful response.
A more effective approach might be to provide as much context as possible. For example, you could say:
"his Java method is supposed to sort an ArrayList of integers in ascending order, but it's returning a NullPointerException. Here is the method and the input I'm testing it with. Can you help me fix it?"
By providing specific details about the expected behaviour, the actual behaviour, and the error message, you're more likely to get a useful response.
Example Prompt 2: Ask GPT a Detailed API question
Suppose you're trying to use a new API and you're not sure how to structure your API request. Asking GPT "How do I use this API?" will only return unhelpful response.
A better approach would be to ask a more specific question with clear context.
For example:
"I'm trying to use the REST API for FlixReviews service to get a list of all user reviews. I need to include an API key in my request and handle pagination in the response. Can you show me an example of how to do this in Java using the OkHttp library?"
GPT can assist us with much more tailored and helpful response.
The Art of Prompt Engineering
Prompt engineering is, in essence, a method of interaction design. It's about understanding how to shape and work with the input given to a system (in this case, a language model like ChatGPT) to get the most desirable and useful output from it.
For a language model, the prompts are the inputs we provide in the form of sentences and phrases. The way you formulate this input can have a profound effect on the output, and that's where the art of prompt engineering comes into play.
Prompt engineering isn't about trying to 'trick' the model into giving the right answer, or to limit its capabilities. Rather, it's about understanding the intricacies of the model, and the data it was trained on, to guide it towards generating responses that are more in line with what you need or expect.
The potential for prompt engineering extends far beyond just getting a chatbot to generate more useful or relevant responses. As language models continue to evolve and improve, the art of crafting effective prompts will become increasingly important. It will become an essential skill for anyone who wants to make the most out of these powerful tools.
Prompt Engineer Role
A good prompt engineer can be thought of more as a knowledge “lead” for an AI module (AI workforce) in the organisations. These engineers bring a host of skills to the table, I'd argue most of these skills to be soft skills as opposed to hard technical skills. These include communication skills, attention to detail, data skills, subject matter expertise, critical thinking, and others.
The market for prompt engineer has just caught on fire: companies ranging from startups to healthcare providers to financial organisations are advertising for prompt engineers. While the field is rapidly growing, so is the ask for good engineers.
Hundreds of online courses have already come up online that teach the ins and outs of prompt engineering, promising to equip students with the skills to create effective prompts that give them a competitive edge.
Prompt Framework
In my view, following a simple prompt framework extracts the full potential of the AI framework. Harnessing the full potential of an AI framework like ChatGPT often comes down to the art of crafting effective prompts. To this end, a simple yet powerful approach is to use the "Context - Details - Converse" (CDC) framework.
Context: This is where we set the stage for the interaction with the ChatGPT. You provide the general topic or subject area, as well as defining the AI's role in the conversation. For example: "We are in the process of building an online cloud training academy. You are serving as an expert Azure trainer in this context." Here, we are setting a context in which the AI is going to operate. We are also providing a role to assign itself (Expert Azure Trainer).
Details: This part involves supplying specific information about our topic or query. By providing clear and comprehensive details, we guide the AI's response and ensure it has enough information to produce a relevant and accurate output. For example, after setting the context as an Azure trainer, you might provide details such as: "We are designing a course on Azure Virtual Networks. The course should cover topics including network security groups, subnet creation, and VPN gateway configuration."
Converse: The final step involves engaging in a conversation with the AI, asking it to generate the desired output based on the context and details provided. In the Azure training example, you might ask: "Could you outline a lesson plan for the Azure Virtual Networks course, incorporating the topics we've discussed?"
The example here shows the CDC framework in action.
"""
You are an expert in building micro-service architectures on Java based platform using Java and Spring framework ==> [Context]
We are building a FlixService - a NetFlix show review service. The frontend is built using Angular while the backend consists of Spring Boot initiated microservices written in Java. We deploy these services to AWS platform ==> [Details]
Create a step-by-step angular application that would talk to backend Java microservices for domain related APIs. ==> [Converse]
"""
By following this homegrown CDC framework, you can craft prompts that effectively guide the AI, leading to more precise, useful, and creative responses.
See resources for a handful of prompt engineering guides/tutorials/articles in the Resources section.
Practical Examples
It would be incomplete if we don't write some fully developed prompts - let's aim to do so in this section.
Mood Movie Recommender
Me:
You are a good friend who can recommend appropriate and uplifting movies when asked [Context]
领英推荐
A 12 year girl is is feeling down and terrible due to loss of their pet dog [Details]
Recommend a movie to uplift the mood of the girl [Converse]
Feeding this prompt to ChatGPT will fetch a response with a recommended movie. The following is the response:
ChatGPT: "The Secret Life of Pets" is an animated comedy film that explores what pets do when their owners are away. It's a heartwarming and funny movie that can remind her of the joy and love pets bring into our lives. The film follows the adventures of Max, a terrier who forms an unlikely bond with a stray dog named Duke. Together, they embark on a journey filled with humor and excitement.
Well, by reading this recommendation, don't we wish to screen this movie for our little girl?
However, may be we don't want our little girl to be reminded of the pet by watching a movie about pets. We ask the agent to help us by "conversing":
Me: Thank you - very wise choice. However, I would like to be recommended about a movie that would be about a dog. I feel it might trigger her sadness back. Do you have any other choices?
ChatGPT: I understand your concern about triggering her sadness again, and I apologize for the oversight. In that case, I recommend a different movie that doesn't involve pets directly, but still carries a positive and uplifting message. One such movie is "Inside Out."
"Inside Out" is an animated film that explores the emotions inside a young girl's mind. It follows the story of Riley, a 12-year-old girl who experiences various emotions like Joy, Sadness, Anger, Fear, and Disgust. The movie beautifully portrays how these emotions work together to shape Riley's experiences and memories.
Isn't that clever?
(I am working on this movie-mood-recommender plugin for ChatGPT with my son - he's just finishing off his GCSEs - who can do the plugin development in Python with me - fingers cross :))
Prompt Parmeters
While the above prompt for the movie-mood-recommender may be a great one, it would be cumbersome to re-type everytime if we want to change the "who" in that question. Here's where the prompt parameters comes in.
We can provide a set of parameters in [] as the following re-write of the above prompt demonstrates.
"""
You are a good friend who can recommend appropriate, mood-lifting movies when asked.
A [who] is feeling [feeling] due to [reason]
Recommend a movie to uplift the mood of the [who]
"""
As you can see the [who] can be a 12 year girl or a 60 year old grandfather. We can now re-use the prompt for multiple situations for recommending movies to various people.
Improvising the Prompt
We can improvise the prompt a bit further too, as shown below:
"""
You are a good friend who can recommend appropriate, mood-lifting movies when asked.
A [who] is feeling [feeling] due to [reason]
Recommend a movie to uplift the mood of the [who]
Do not summarise or provide any other comments other than standard recommendation. You can back up your recommendation by saying why you want that movie for the [who].
Answer me when I say Go.
I'll provide the variables in a name=value format
"""
As you can see, we provided clear instructions as to what the GPT should output - we asked it not to be too chatty, instead a movie with the reason for its recommendation.
Resources
Open AI's has channel called prompt-enginnering. The OpenAI's Discord invite is here.
The Prompt-engineering-guide from DAIR-AI
GitHub has hundreds of open source prompt-engineering projects. Simply search and find one that suits your purpose.
There a ton more out there - browse the net!
Wrap Up
Phew - that's a long article! Let's wrap this up.
Prompt engineering involves the strategic crafting of prompts to guide the model towards generating more desired or useful outputs. With the increasing reliance on AI technologies in various sectors, the role of prompt engineers is becoming increasingly important and lucrative.
Prompt engineering is not just about understanding the mechanics of AI; it's about understanding how to communicate with AI in a way that makes it most effective. It's a skill set that combines technical know-how, communication skills, and a deep understanding of both AI and the subject matter at hand.
Stay tuned for the upcoming articles.
Digital Skills Instructor & Media Producer (French)
1 年Thank you for this excellent article. I had been looking for one as complete as yours about this topic for a good while!
Experienced Creative Digital Marketer | Campaign Management Expert | Passionate About Growth & Brand Awareness through Partnerships & Digital Marketing Strategies
1 年Brilliant article Madhusudhan Konda
Great article Madhusudhan Konda.
Oracle Apps Lead & Solution Architect at Infosys
1 年Total agreed with your analysis ????
Software Engineer(Java)
1 年Awesome!