Monday, August 21, 2023

Prompt Engineering: A Step-by-Step Guide to Optimizing Your Work Processes

Prompt engineering is a technique used to optimize and fine-tune language models and other generative AI models for specific tasks and desired outputs. It refers to the process of carefully crafting prompts or inputs for AI models to enhance their performance and accuracy.

A prompt can be as simple as a few words or as complex as an entire paragraph, and it serves as the starting point for an AI model to generate a response. For example, a prompt for a text-to-text model can be a query such as “what is Fermat’s little theorem?”, a command such as “write a poem about leaves falling”, or a short statement of feedback such as “too verbose” or “rephrase again”. A prompt for a text-to-image model can be a description of a desired output such as “a high-quality photo of an astronaut riding a horse”.

Prompt engineering is enabled by in-context learning, which is the ability of large language models(LLM) to temporarily learn from prompts. This means that the model can adapt to different tasks and domains without requiring additional training or fine-tuning. Prompt engineering is also an emergent property of model scale, meaning that larger models are more capable of in-context learning than smaller models.

Prompt engineering is important because it can help improve the quality, relevance, and creativity of the AI model’s responses. It can also help reduce the amount of data and computational resources needed to train and fine-tune the model for each specific task. Prompt engineering can be applied to various domains and tasks, such as language translation, question answering, chatbots, text summarization, text generation, image generation, and more.

How prompt engineering works  

Due to the way the instruction-following models are trained or the data they are trained on, there are specific prompt formats that work particularly well and align better with the tasks at hand. Below we present a number of prompt formats we find work reliably well, but feel free to explore different formats, which may fit your task best.

1. Use the latest model

For best results, we generally recommend using the latest, most capable models. As of November 2022, the best options are the “text-davinci-003” model for text generation, and the “code-davinci-002” model for code generation.

2. Put instructions at the beginning of the prompt and use ### or """ to separate the instruction and context 

Less effective ❌: 

Summarize the text below as a bullet point list of the most important points.

{text input here}

Better ✅:

Summarize the text below as a bullet point list of the most important points.

Text: """ 

{text input here} 

"""

3. Be specific, descriptive and as detailed as possible about the desired context, outcome, length, format, style, etc 

Be specific about the context, outcome, length, format, style, etc 

           Less effective ❌:

            Write a poem about OpenAI. 

Better ✅:

Write a short inspiring poem about OpenAI, focusing on the recent DALL-E product launch (DALL-E is a text to image ML model) in the style of a {famous poet}

 4. Articulate the desired output format through examples (example 1, example 2). 

Less effective ❌: 

Extract the entities mentioned in the text below. Extract the following 4 entity types: company names, people names, specific topics and themes.

Text: {text}

Show, and tell - the models respond better when shown specific format requirements. This also makes it easier to programmatically parse out multiple outputs reliably.

          Better ✅:

Extract the important entities mentioned in the text below. First extract all company names, then extract all people names, then extract specific topics which fit the content and finally extract general overarching themes

Desired format:

Company names: <comma_separated_list_of_company_names> 

People names: -||- 

Specific topics: -||- 

General themes: -||-

          Text: {text}


5. Start with zero-shot, then few-shot (example), neither of them worked, then fine-tune 

✅ Zero-shot  

Extract keywords from the below text.

          Text: {text}

Keywords:

✅ Few-shot - provide a couple of examples 

Extract keywords from the corresponding texts below.

Text 1: Stripe provides APIs that web developers can use to integrate payment processing into their websites and mobile applications.

Keywords 1: Stripe, payment processing, APIs, web developers, websites, mobile applications## 

Text 2: OpenAI has trained cutting-edge language models that are very good at understanding and generating text. Our API provides access to these models and can be used to solve virtually any task that involves processing language. 

Keywords 2: OpenAI, language models, text processing, API. 

## 

Text 3: {text}

           Keywords 3:
          ✅Fine-tune: see fine-tune best practices here.

6. Reduce “fluffy” and imprecise descriptions

Less effective ❌:

          The description for this product should be fairly short, a few sentences only, and not too much more.

Better ✅:

Use a 3 to 5 sentence paragraph to describe this product.  

 7. Instead of just saying what not to do, say what to do instead

Less effective ❌: 

The following is a conversation between an Agent and a Customer. DO NOT ASK USERNAME OR PASSWORD. DO NOT REPEAT.

Customer: I can’t log in to my account.

Agent:

          Better ✅:

The following is a conversation between an Agent and a Customer. The agent will attempt to diagnose the problem and suggest a solution, whilst refraining from asking any questions related to PII. Instead of asking for PII, such as username or password, refer the user to the help article www.samplewebsite.com/help/faq

Customer: I can’t log in to my account.

Agent:

8. Code Generation Specific - Use “leading words” to nudge the model toward a particular pattern

Less effective ❌: 

# Write a simple python function that

# 1. Ask me for a number in mile 

# 2. It converts miles to kilometers

In this code example below, adding “import” hints to the model that it should start writing in Python. (Similarly “SELECT” is a good hint for the start of a SQL statement.) 

Better ✅:

# Write a simple python function that

# 1. Ask me for a number in mile

# 2. It converts miles to kilometers

 import

Tips to enhance your prompt-engineering abilities 

Tip #1: Know the model’s strengths and weaknesses:

As AI models evolve and become more complex, it is essential for developers to comprehend their capabilities and limitations. Understanding these strengths and weaknesses can help you, as a developer, avoid making mistakes and create safer, more reliable applications.

Tip #2: Be as specific as possible:

AI models have the ability to comprehend a variety of prompts. For instance Google’s PaLM 2 can understand natural language prompts, multilingual text, and even programming codes like Python and JavaScript. Although AI models can be very knowledgeable, they are still imperfect, and have the ability to misinterpret prompts that are not specific enough. In order for AI models to navigate ambiguity, it is important to tailor your prompts specifically to your desired outcome. 

Tip #3: Utilize contextual prompts:

Utilize contextual information in your prompts to help the model gain an in-depth understanding of your requests. Contextual prompts can include the specific task you want the model to perform, a replica of the output you're looking for, or a persona to emulate, from a marketer or engineer to a high school teacher. Defining a tone and perspective for an AI model gives it a blueprint of the tone, style, and focused expertise you’re looking for to improve the quality, relevance, and effectiveness of your output.

Tip #4: Provide AI models with examples :

When creating prompts for AI models, it is helpful to provide examples. This is because prompts act as instructions for the model, and examples can help the model to understand what you are asking for. Providing a prompt with an example looks something like this: “here are several recipes I like – create a new recipe based on the ones I provided.” The model can now understand the your ability and needs in order to make this pastry,

Tip #5: Experiment with prompts and personas:

The way you construct your prompt impacts the model’s output. By creatively exploring different requests, you will soon have an understanding of how the model weighs its answers, and what happens when you interfuse your domain knowledge, expertise, and lived experience with the power of a multi-billion parameter large language model. 

Try experimenting with different keywords, sentence structures, and prompt lengths to discover the perfect formula. Allow yourself to step into the shoes of various personas, from work personas such as “product engineer” or “customer service representatives,” to parental figures or celebrities such as your grandmother, a celebrity chef, and explore everything from cooking to coding!

By crafting unique, and innovative, requests replete with your expertise and experience, you can learn which prompts provide you with your ideal output. Further refining your prompts, known as ‘tuning,’ allows the model to have a greater understanding and framework for your next output.

Tip #6: Try chain-of-thought prompting:

Chain of thought prompting is a technique for improving the reasoning capabilities of large language models (LLMs). It works by breaking down a complex problem into smaller steps, and then prompting the LLM to provide intermediate reasoning for each step. This helps the LLM to understand the problem more deeply, and to generate more accurate and informative answers. This will help you to understand the answer better and to make sure that the LLM is actually understanding the problem.

Conclusion
Prompt engineering is a skill that all workers, across industries and organizations, will need as AI-powered tools are becoming more prevalent. Remember to incorporate these five essential tips the next time you communicate with an AI model, so you can generate the accurate outputs that you desire. AI will forever continue to develop, constantly refining itself as we use it, so I encourage you to remember that learning, for mind and machine, is a never ending journey. Happy Prompting!

Thank you

Venkata kishore T(Intern)
Shield Warriors,
Data Shield Team,
Enterprise Minds.

No comments:

Post a Comment

EM-Tirupati Codeathon Series #08

[Question] ROBOTIC CRICKET MATCH you should write a program that simulate an automatic cricket match between India and Sri Lanka. The fo...